'Popups',
'singular_name' => 'Popup',
'add_new' => 'Add New Popup',
'add_new_item' => 'Add New Popup',
'edit_item' => 'Edit Popup',
'new_item' => 'New Popup',
'view_item' => 'View Popup',
'search_items' => 'Search Popups',
'not_found' => 'No Popups found',
'not_found_in_trash' => 'No Popups found in Trash',
];
$args = [
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 55,
'supports' => ['title'],
'menu_icon' => 'dashicons-format-chat',
];
register_post_type('mp_popup', $args);
});
/* ============================= META BOX ============================= */
add_action('add_meta_boxes', function() {
add_meta_box(
'mp_popup_settings',
'Popup Settings',
'mp_popup_meta_box_html',
'mp_popup',
'normal',
'high'
);
});
function mp_popup_meta_box_html($post){
$defaults = [
'template' => 'default',
'default_width' => 400,
'image_url' => '',
'image_link' => '',
'image_width_vh' => 60,
'content' => '',
'btn_text' => '',
'btn_link' => '',
'color' => '#000000',
'delay' => 3,
'animation' => 'fade',
'target' => 'all',
'selected_page' => '',
'padding' => 25,
'cookie' => 0,
'start_datetime' => '',
'end_datetime' => '',
'devices' => ['desktop','tablet','mobile'],
];
$meta = get_post_meta($post->ID, '_mp_popup_meta', true);
$o = wp_parse_args($meta, $defaults);
wp_nonce_field('mp_save_popup', 'mp_popup_nonce');
?>
- Template
- Appearance
- Schedule
- Visibility
in_array($data['template'] ?? 'default',['default','image']) ? $data['template'] : 'default',
'default_width' => intval($data['default_width'] ?? 400),
'image_url' => esc_url_raw($data['image_url'] ?? ''),
'image_link' => esc_url_raw($data['image_link'] ?? ''),
'image_width_vh' => intval($data['image_width_vh'] ?? 60),
'content' => sanitize_textarea_field($data['content'] ?? ''),
'btn_text' => sanitize_text_field($data['btn_text'] ?? ''),
'btn_link' => esc_url_raw($data['btn_link'] ?? ''),
'color' => sanitize_hex_color($data['color'] ?? '#000000'),
'delay' => intval($data['delay'] ?? 3),
'animation' => in_array($data['animation'] ?? 'fade',['fade','slide','zoom']) ? $data['animation'] : 'fade',
'target' => in_array($data['target'] ?? 'all',['all','pages','woocommerce']) ? $data['target'] : 'all',
'selected_page' => intval($data['selected_page'] ?? 0),
'padding' => intval($data['padding'] ?? 25),
'cookie' => !empty($data['cookie']) ? 1 : 0,
'start_datetime' => sanitize_text_field($data['start_datetime'] ?? ''),
'end_datetime' => sanitize_text_field($data['end_datetime'] ?? ''),
'devices' => $devices,
];
update_post_meta($post_id,'_mp_popup_meta',$save);
}
});
/* ============================= FRONTEND DISPLAY ============================= */
add_action('wp_footer', function(){
$args = [
'post_type'=>'mp_popup',
'post_status'=>'publish',
'posts_per_page'=>-1,
];
$popups = get_posts($args);
if(!$popups) return;
$current_time = current_time('timestamp');
foreach($popups as $popup_post){
$o = get_post_meta($popup_post->ID,'_mp_popup_meta',true);
if(empty($o) || empty($o['template'])) continue;
$start = !empty($o['start_datetime']) ? strtotime(sanitize_text_field($o['start_datetime'])) : false;
$end = !empty($o['end_datetime']) ? strtotime(sanitize_text_field($o['end_datetime'])) : false;
if($start && $current_time < $start) continue;
if($end && $current_time > $end) continue;
if($o['target']==='pages' && (!isset($o['selected_page']) || !is_page($o['selected_page']))) continue;
if($o['target']==='woocommerce' && function_exists('is_shop') && !(is_shop()||is_product()||is_cart())) continue;
$page_id = get_the_ID();
$settings_version = get_option('lp_settings_version', time());
$cookie_name = ($o['target']==='pages')
? "mp_hide_page_{$page_id}_v{$settings_version}_{$popup_post->ID}"
: "mp_hide_global_v{$settings_version}_{$popup_post->ID}";
?>