self::translate( 'Popup' ),
'singular_name' => self::translate( 'Popup' ),
'add_new' => self::translate( 'Add New' ),
'add_new_item' => self::translate( 'Add New Popup' ),
'edit_item' => self::translate( 'Edit Popup' ),
'new_item' => self::translate( 'New Popup' ),
'view_item' => self::translate( 'View Popup' ),
'view_items' => self::translate( 'View Popups' ),
'search_items' => self::translate( 'Search Popups' ),
'not_found' => self::translate( 'No Popups found.' ),
'not_found_in_trash' => self::translate( 'No Popups found in Trash.' ),
'all_items' => self::translate( 'All Popups' ),
'archives' => self::translate( 'Popup Archives' ),
'attributes' => self::translate( 'Popup Attributes' ),
'insert_into_item' => self::translate( /** @lang text */ 'Insert into Popup' ),
'uploaded_to_this_item' => self::translate( 'Uploaded to this Popup' ),
'featured_image' => self::translate( 'Featured Image' ),
'set_featured_image' => self::translate( 'Set featured image' ),
'remove_featured_image' => self::translate( 'Remove featured image' ),
'use_featured_image' => self::translate( 'Use as featured image' ),
'filter_items_list' => self::translate( 'Filter Popups list' ),
'items_list_navigation' => self::translate( 'Popups list navigation' ),
'items_list' => self::translate( 'Popups list' ),
];
register_post_type( self::POST_TYPE, [
'labels' => $labels,
'public' => false,
'hierarchical' => false,
'show_ui' => true,
'menu_icon' => 'dashicons-media-document',
'capability_type' => 'post',
'supports' => [ 'title', 'excerpt', 'editor', 'thumbnail' ],
] );
}
public static function load_textdomain() {
$plugin_rel_path = basename( dirname( __FILE__ ) ) . '/languages/';
load_plugin_textdomain( self::TEXT_DOMAIN, false, $plugin_rel_path );
}
public static function admin_enqueue_scripts() {
global $pagenow, $typenow;
if ( in_array( $pagenow, [ 'post-new.php', 'post.php' ] ) && self::POST_TYPE == $typenow ) {
$suffix = self::get_script_suffix();
wp_enqueue_style( self::POST_TYPE . '_admin', plugins_url( "css/admin{$suffix}.css", __FILE__ ) );
wp_enqueue_script( self::POST_TYPE . '_admin', plugins_url( "js/edit-form{$suffix}.js", __FILE__ ), [ 'jquery' ] );
}
}
public static function edit_form_after_title( $post ) {
if ( self::POST_TYPE != $post->post_type ) {
return;
}
$subtitle = get_post_meta( $post->ID, self::get_meta_key( 'subtitle' ), true );
$subtitle_label_class = ($subtitle) ? 'screen-reader-text' : '';
?>
ID}" );
wp_nonce_field($action, $name);
}
public static function save_post( $post_ID ) {
$nonce_name = self::get_meta_key( 'wpnonce' );
$nonce_action = self::get_meta_key( "save_{$post_ID}" );
if ( ! isset( $_POST[$nonce_name] )
|| ! wp_verify_nonce( $_POST[$nonce_name], $nonce_action )
) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_ID ) ) {
return;
}
if ( isset( $_POST['post_type'] ) && self::POST_TYPE === $_POST['post_type'] ) {
foreach ( self::$meta_fields as $field ) {
$value = @$_POST[$field];
if ( is_callable( [ self::class, "sanitize_{$field}" ] ) ) {
$value = call_user_func( [ self::class, "sanitize_{$field}" ], $value );
}
update_post_meta( $post_ID, self::get_meta_key( $field ), $value );
}
}
if ( ! empty( self::$errors ) ) {
$error = new WP_Error();
foreach ( self::$errors as $code => $message ) {
$error->add( $code, $message );
}
set_transient( self::get_transient_key( 'save_post_error', $post_ID ), $error, 45 );
remove_action( 'save_post_' . self::POST_TYPE, [ self::class, __FUNCTION__ ] ); // unhook this function to prevent indefinite loop
wp_update_post( [ 'ID' => $post_ID, 'post_status' => 'draft' ] ); // update the post to change post status
remove_action( 'save_post_' . self::POST_TYPE, [ self::class, __FUNCTION__ ] ); // re-hook this function again
}
}
public static function admin_notices() {
$transient = self::get_transient_key( 'save_post_error', @$_GET['post'] );
/** @var WP_Error $error */
$error = get_transient( $transient );
if ( $error ) {
foreach ( $error->get_error_codes() as $code ) {
?>
get_error_message( $code ); ?>
ID, self::get_meta_key( 'subtitle' ), true );
echo ( empty( $subtitle ) ) ? '-' : $subtitle;
}
public static function the_posts_custom_column_shortcode( $post) {
echo '[' . self::SHORTCODE . ' id=' . $post->ID . ']';
}
public static function shortcode( $atts )
{
$atts = shortcode_atts( [
'id' => null,
], $atts, self::SHORTCODE );
$output = '';
if ( null !== $atts['id'] ) {
$post = get_post( $atts['id'] );
if ( $post ) {
$subtitle = get_post_meta( $post->ID, self::get_meta_key( 'subtitle' ), true);
ob_start();
?>
ID;
}
add_action( 'wp_footer', [ self::class, 'wp_footer'] ); // Output popup
}
return $output;
}
public static function enqueue_scripts() {
$suffix = self::get_script_suffix();
wp_enqueue_style( 'magnific_popup', plugins_url( 'css/magnific-popup.css', __FILE__ ) );
wp_enqueue_script( 'magnific_popup', plugins_url( 'js/jquery.magnific-popup.js', __FILE__ ), [ 'jquery' ] );
wp_enqueue_style( self::POST_TYPE . '_user', plugins_url( "css/user{$suffix}.css", __FILE__ ), [ 'magnific_popup' ] );
wp_enqueue_script( self::POST_TYPE . '_user', plugins_url( "js/user{$suffix}.js", __FILE__ ), [ 'magnific_popup', 'jquery-effects-core' ] );
}
public static function wp_footer() {
if ( ! empty( self::$presented_popups ) ) {
$query = new WP_Query( [
'post_type' => self::POST_TYPE,
'post__in' => self::$presented_popups,
] );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>