_x( 'All Notifications', 'post type general name', 'notipulse' ), 'singular_name' => _x( 'Notification', 'post type singular name', 'notipulse' ), 'add_new' => _x( 'Add New', 'Notification', 'notipulse' ), 'add_new_item' => __( 'Add New Notification', 'notipulse' ), 'edit_item' => __( 'Edit Notification', 'notipulse' ), 'new_item' => __( 'New Notification', 'notipulse' ), 'all_items' => __( 'All Notifications', 'notipulse' ), 'view_item' => __( 'View Notification', 'notipulse' ), 'search_items' => __( 'Search Notifications', 'notipulse' ), 'not_found' => __( 'No Notifications found', 'notipulse' ), 'not_found_in_trash' => __( 'No Notifications found in the Trash', 'notipulse' ), 'menu_name' => __( 'NotiPulse', 'notipulse' ), ); $args = array( 'labels' => $labels, 'supports' => array( 'title' ), 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-bell', 'has_archive' => false, 'rewrite' => false, 'capability_type' => 'post', 'show_in_rest' => false, ); register_post_type( 'notipulse', $args ); } /** * Create Submenu for Overview Page. */ function notipulse_add_submenu_in_cpt() { add_submenu_page( 'edit.php?post_type=notipulse', __( 'Overview', 'notipulse' ), __( 'Overview', 'notipulse' ), 'manage_options', 'overview-page', 'notipulse_overview_page' ); } /** * Render Overview Page callback. */ function notipulse_overview_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $file = NOTIPULSE_DIR . 'functions/overview-page.php'; if ( file_exists( $file ) ) { include $file; } } /** * Meta box class for NotiPulse. */ abstract class NotiPulse_Meta_Box { /** * Set up and add the meta box. */ public static function add_meta_box_for_plugin_options() { add_meta_box( 'notipulse_settings', __( 'Notification Settings', 'notipulse' ), array( __CLASS__, 'notipulse_admin_screen' ), 'notipulse', 'normal', 'high' ); } /** * Save the meta box selections. * * @param int $post_id The post ID. */ public static function save_data_for_plugin_options( $post_id ) { // Nonce verification. if ( ! isset( $_POST['notipulse_meta_box_nonce'] ) ) { return; } $nonce = sanitize_key( wp_unslash( $_POST['notipulse_meta_box_nonce'] ) ); if ( ! wp_verify_nonce( $nonce, 'notipulse_save_meta_box_data' ) ) { return; } // Prevent autosave from overwriting data. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check user permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Check post type. if ( isset( $_POST['post_type'] ) && 'notipulse' !== $_POST['post_type'] ) { return; } // Avoid updating revisions. if ( wp_is_post_revision( $post_id ) ) { return; } $display_in = ''; if ( ! empty( $_POST['display_in'] ) && is_array( $_POST['display_in'] ) ) { $clean_display = array_map( 'sanitize_text_field', wp_unslash( $_POST['display_in'] ) ); $display_in = implode( ',', $clean_display ); } $notipulse_options = array( 'notipulse_type' => isset( $_POST['notipulse_type'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_type'] ) ) : 'Simple', 'bg_color' => isset( $_POST['bg_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['bg_color'] ) ) : '', 'bg_color_opacity' => isset( $_POST['bg_color_opacity'] ) ? intval( $_POST['bg_color_opacity'] ) : 6, 'notipulse_position' => isset( $_POST['notipulse_position'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_position'] ) ) : 'Top', 'notipulse_head' => isset( $_POST['notipulse_head'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_head'] ) ) : '', 'head_tags' => isset( $_POST['head_tags'] ) ? sanitize_text_field( wp_unslash( $_POST['head_tags'] ) ) : 'h2', 'headline_color' => isset( $_POST['headline_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['headline_color'] ) ) : '', 'head_top_mar' => isset( $_POST['head_top_mar'] ) ? intval( $_POST['head_top_mar'] ) : 0, 'head_btm_mar' => isset( $_POST['head_btm_mar'] ) ? intval( $_POST['head_btm_mar'] ) : 0, 'head_lft_mar' => isset( $_POST['head_lft_mar'] ) ? intval( $_POST['head_lft_mar'] ) : 0, 'head_ryt_mar' => isset( $_POST['head_ryt_mar'] ) ? intval( $_POST['head_ryt_mar'] ) : 0, 'head_top_pad' => isset( $_POST['head_top_pad'] ) ? intval( $_POST['head_top_pad'] ) : 0, 'head_btm_pad' => isset( $_POST['head_btm_pad'] ) ? intval( $_POST['head_btm_pad'] ) : 0, 'head_lft_pad' => isset( $_POST['head_lft_pad'] ) ? intval( $_POST['head_lft_pad'] ) : 0, 'head_ryt_pad' => isset( $_POST['head_ryt_pad'] ) ? intval( $_POST['head_ryt_pad'] ) : 0, 'notipulse_content' => isset( $_POST['notipulse_content'] ) ? wp_kses_post( wp_unslash( $_POST['notipulse_content'] ) ) : '', 'text_color' => isset( $_POST['text_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['text_color'] ) ) : '', 'font_size' => isset( $_POST['font_size'] ) ? intval( $_POST['font_size'] ) : 18, 'line_height' => isset( $_POST['line_height'] ) ? intval( $_POST['line_height'] ) : 28, 'content_top_mar' => isset( $_POST['content_top_mar'] ) ? intval( $_POST['content_top_mar'] ) : 0, 'content_btm_mar' => isset( $_POST['content_btm_mar'] ) ? intval( $_POST['content_btm_mar'] ) : 0, 'content_lft_mar' => isset( $_POST['content_lft_mar'] ) ? intval( $_POST['content_lft_mar'] ) : 0, 'content_ryt_mar' => isset( $_POST['content_ryt_mar'] ) ? intval( $_POST['content_ryt_mar'] ) : 0, 'content_top_pad' => isset( $_POST['content_top_pad'] ) ? intval( $_POST['content_top_pad'] ) : 0, 'content_btm_pad' => isset( $_POST['content_btm_pad'] ) ? intval( $_POST['content_btm_pad'] ) : 0, 'content_lft_pad' => isset( $_POST['content_lft_pad'] ) ? intval( $_POST['content_lft_pad'] ) : 0, 'content_ryt_pad' => isset( $_POST['content_ryt_pad'] ) ? intval( $_POST['content_ryt_pad'] ) : 0, 'notipulse_link' => isset( $_POST['notipulse_link'] ) ? esc_url_raw( wp_unslash( $_POST['notipulse_link'] ) ) : '', 'notipulse_btn_txt' => isset( $_POST['notipulse_btn_txt'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_btn_txt'] ) ) : '', 'open_in' => isset( $_POST['open_in'] ) ? sanitize_text_field( wp_unslash( $_POST['open_in'] ) ) : 'Same Window', 'notipulse_btn_bg_clr' => isset( $_POST['notipulse_btn_bg_clr'] ) ? sanitize_hex_color( wp_unslash( $_POST['notipulse_btn_bg_clr'] ) ) : '', 'notipulse_btn_txt_clr' => isset( $_POST['notipulse_btn_txt_clr'] ) ? sanitize_hex_color( wp_unslash( $_POST['notipulse_btn_txt_clr'] ) ) : '', 'notipulse_btn_size' => isset( $_POST['notipulse_btn_size'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_btn_size'] ) ) : 'Medium', 'countdown_on_off' => isset( $_POST['countdown_on_off'] ) ? sanitize_text_field( wp_unslash( $_POST['countdown_on_off'] ) ) : 'Off', 'countdown_date_time' => isset( $_POST['countdown_date_time'] ) ? sanitize_text_field( wp_unslash( $_POST['countdown_date_time'] ) ) : '', 'countdown_top_mar' => isset( $_POST['countdown_top_mar'] ) ? intval( $_POST['countdown_top_mar'] ) : 0, 'countdown_btm_mar' => isset( $_POST['countdown_btm_mar'] ) ? intval( $_POST['countdown_btm_mar'] ) : 0, 'countdown_lft_mar' => isset( $_POST['countdown_lft_mar'] ) ? intval( $_POST['countdown_lft_mar'] ) : 0, 'countdown_ryt_mar' => isset( $_POST['countdown_ryt_mar'] ) ? intval( $_POST['countdown_ryt_mar'] ) : 0, 'countdown_top_pad' => isset( $_POST['countdown_top_pad'] ) ? intval( $_POST['countdown_top_pad'] ) : 0, 'countdown_btm_pad' => isset( $_POST['countdown_btm_pad'] ) ? intval( $_POST['countdown_btm_pad'] ) : 0, 'countdown_lft_pad' => isset( $_POST['countdown_lft_pad'] ) ? intval( $_POST['countdown_lft_pad'] ) : 0, 'countdown_ryt_pad' => isset( $_POST['countdown_ryt_pad'] ) ? intval( $_POST['countdown_ryt_pad'] ) : 0, 'countdown_font_size' => isset( $_POST['countdown_font_size'] ) ? intval( $_POST['countdown_font_size'] ) : 16, 'countdown_bg_clr' => isset( $_POST['countdown_bg_clr'] ) ? sanitize_hex_color( wp_unslash( $_POST['countdown_bg_clr'] ) ) : '', 'countdown_txt_clr' => isset( $_POST['countdown_txt_clr'] ) ? sanitize_hex_color( wp_unslash( $_POST['countdown_txt_clr'] ) ) : '', 'display_in' => sanitize_text_field( $display_in ), 'user_roles' => isset( $_POST['user_roles'] ) ? sanitize_text_field( wp_unslash( $_POST['user_roles'] ) ) : '', 'notipulse_time' => isset( $_POST['notipulse_time'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_time'] ) ) : 'Every Time On Page Load', 'notipulse_close_when' => isset( $_POST['notipulse_close_when'] ) ? sanitize_text_field( wp_unslash( $_POST['notipulse_close_when'] ) ) : 'Do not close - visitor will have to manually close.', 'notipulse_how_much_time' => isset( $_POST['notipulse_how_much_time'] ) ? intval( $_POST['notipulse_how_much_time'] ) : 0, 'last_updated' => current_time( 'mysql' ), ); update_post_meta( $post_id, 'notipulse_meta_key', $notipulse_options ); } /** * Display the meta box HTML to the user. * * @param WP_Post $post Post object. */ public static function notipulse_admin_screen( $post ) { $file = NOTIPULSE_DIR . 'functions/admin-ui.php'; if ( file_exists( $file ) ) { include $file; } } } /** * Enqueue scripts & styles for admin screens. * * @param string $hook Current admin page hook. */ function notipulse_backend_enqueue( $hook ) { global $post_type; $is_notipulse_screen = ( 'notipulse' === $post_type ) || ( isset( $_GET['page'] ) && 'overview-page' === $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! $is_notipulse_screen ) { return; } // Enqueue color picker for NotiPulse admin screens. wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'notipulse-admin', NOTIPULSE_URL . 'assets/css/style.css', array( 'wp-color-picker' ), NOTIPULSE_VERSION ); wp_enqueue_style( 'notipulse-responsive', NOTIPULSE_URL . 'assets/css/responsive.css', array( 'notipulse-admin' ), NOTIPULSE_VERSION ); wp_enqueue_script( 'notipulse-admin-js', NOTIPULSE_URL . 'assets/js/main.js', array( 'jquery', 'wp-color-picker' ), NOTIPULSE_VERSION, true ); } /** * Add custom body class on the frontend. * * @param array $classes Current body classes. * @return array */ function notipulse_body_class( $classes ) { $classes[] = 'activated_notipulse_plugin'; $classes[] = 'activated_np_plugin'; return $classes; } /** * Add custom column headers in CPT list. * * @param array $cols Columns array. * @return array */ function notipulse_columns_key( $cols ) { $cols['notipulse_type'] = __( 'Notification Type', 'notipulse' ); $cols['notipulse_position'] = __( 'Notification Position', 'notipulse' ); return $cols; } /** * Render custom column content in CPT list. * * @param string $col Column key. * @param int $post_id Post ID. */ function notipulse_columns_value( $col, $post_id ) { $meta = get_post_meta( $post_id, 'notipulse_meta_key', true ); if ( ! is_array( $meta ) ) { $meta = maybe_unserialize( $meta ); } if ( ! is_array( $meta ) ) { return; } switch ( $col ) { case 'notipulse_type': if ( ! empty( $meta['notipulse_type'] ) ) { echo esc_html( $meta['notipulse_type'] ); } break; case 'notipulse_position': if ( ! empty( $meta['notipulse_position'] ) ) { echo esc_html( $meta['notipulse_position'] ); } break; } } /** * Reorder custom columns in CPT list. * * @param array $defaults Default columns. * @return array */ function notipulse_columns_head( $defaults ) { global $post_type; if ( 'notipulse' !== $post_type ) { return $defaults; } $new = array(); $type_col = isset( $defaults['notipulse_type'] ) ? $defaults['notipulse_type'] : ''; $pos_col = isset( $defaults['notipulse_position'] ) ? $defaults['notipulse_position'] : ''; foreach ( $defaults as $key => $value ) { if ( 'date' === $key ) { if ( $type_col ) { $new['notipulse_type'] = $type_col; } if ( $pos_col ) { $new['notipulse_position'] = $pos_col; } } $new[ $key ] = $value; } return $new; }