*/
class NotificationX_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* All builder args
*
* @var array
*/
private $builder_args;
/**
* Builder Metabox ID
*
* @var string
*/
private $metabox_id;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The type.
*
* @since 1.0.0
* @access public
* @var string the post type of notificationx.
*/
public $type = 'notificationx';
public $metabox;
public static $prefix = 'nx_meta_';
public static $settings;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public static $counts;
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
self::$settings = NotificationX_DB::get_settings();
}
/**
* Get all active items.
*
* @return void
*/
public static function get_active_items() {
// WP Query arguments.
$source_types = NotificationX_Helper::source_types();
$args = array(
'post_type' => 'notificationx',
'posts_per_page' => '-1',
'post_status' => 'publish',
);
$active = [];
// Get the notification posts.
$posts = get_posts( $args );
if ( count( $posts ) ) {
foreach ( $posts as $post ) {
$settings = NotificationX_MetaBox::get_metabox_settings( $post->ID );
$type = '';
if( array_key_exists( $settings->display_type, $source_types ) ) {
$type = $settings->{ $source_types[ $settings->display_type ] };
}
$active[ $type ][] = $post->ID;
}
}
return $active;
}
public static function get_enabled_types() {
// WP Query arguments.
$source_types = NotificationX_Helper::source_types();
$args = array(
'post_type' => 'notificationx',
'posts_per_page' => '-1',
'post_status' => 'publish',
'meta_key' => '_nx_meta_active_check',
'meta_value' => 1
);
$active = [];
// Get the notification posts.
$posts = get_posts( $args );
if ( count( $posts ) ) {
foreach ( $posts as $post ) {
$settings = NotificationX_MetaBox::get_metabox_settings( $post->ID );
$type = '';
if( array_key_exists( $settings->display_type, $source_types ) ) {
$type = $settings->{ $source_types[ $settings->display_type ] };
}
$active[ $type ][] = $post->ID;
}
}
return $active;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles( $hook ) {
global $post_type;
$page_status = false;
wp_enqueue_style(
$this->plugin_name . '-admin-global',
NOTIFICATIONX_ADMIN_URL . 'assets/css/nx-admin-global.min.css',
array(), $this->version, 'all'
);
if( $hook == 'notificationx_page_nx-builder' || $hook == 'notificationx_page_nx-settings' ) {
$page_status = true;
}
if( $post_type != $this->type && ! $page_status ) {
return;
}
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_style(
$this->plugin_name . '-select2',
NOTIFICATIONX_ADMIN_URL . 'assets/css/select2.min.css',
array(), $this->version, 'all'
);
wp_enqueue_style(
$this->plugin_name,
NOTIFICATIONX_ADMIN_URL . 'assets/css/nx-admin.min.css',
array(), $this->version, 'all'
);
}
/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts( $hook ) {
global $post_type;
$page_status = false;
if( $hook == 'notificationx_page_nx-builder' || $hook == 'notificationx_page_nx-settings' ) {
$page_status = true;
}
if( $hook === 'toplevel_page_nx-admin' ) {
wp_enqueue_script(
$this->plugin_name . '-sweetalert',
NOTIFICATIONX_ADMIN_URL . 'assets/js/sweetalert.min.js',
array( 'jquery' ), $this->version, true
);
wp_enqueue_script(
$this->plugin_name,
NOTIFICATIONX_ADMIN_URL . 'assets/js/nx-admin.min.js',
array( 'jquery' ), $this->version, true
);
}
if( $post_type != $this->type && ! $page_status ) {
return;
}
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_media();
wp_enqueue_script(
$this->plugin_name . '-sweetalert',
NOTIFICATIONX_ADMIN_URL . 'assets/js/sweetalert.min.js',
array( 'jquery' ), $this->version, true
);
wp_enqueue_script(
$this->plugin_name . '-select2',
NOTIFICATIONX_ADMIN_URL . 'assets/js/select2.min.js',
array( 'jquery' ), $this->version, true
);
wp_enqueue_script(
$this->plugin_name,
NOTIFICATIONX_ADMIN_URL . 'assets/js/nx-admin.min.js',
array( 'jquery' ), $this->version, true
);
wp_localize_script( $this->plugin_name, 'notificationx', self::toggleFields() );
}
public function toggleFields( $builder = false ){
$args = NotificationX_MetaBox::get_args();
if( $builder ) {
$args = NotificationX_MetaBox::get_builder_args();
}
$toggleFields = $hideFields = $conditions = array();
$tabs = $args[ 'tabs' ];
if( ! empty( $tabs ) ) {
foreach( $tabs as $tab_id => $tab ) {
$sections = isset( $tab['sections'] ) ? $tab[ 'sections' ] : [];
if( ! empty( $sections ) ) {
foreach( $sections as $section_id => $section ) {
$fields = isset( $section['fields'] ) ? $section[ 'fields' ] : [];
if( ! empty( $fields ) ) {
foreach( $fields as $field_key => $field ) {
if( isset( $field['fields'] ) ) {
foreach( $field['fields'] as $inner_field_key => $inner_field ) {
if( isset( $inner_field['hide'] ) && ! empty( $inner_field['hide'] ) && is_array( $inner_field['hide'] ) ) {
foreach( $inner_field['hide'] as $key => $hide ) {
$hideFields[ $inner_field_key ][ $key ] = $hide;
}
}
if( isset( $inner_field['dependency'] ) && ! empty( $inner_field['dependency'] ) && is_array( $inner_field['dependency'] ) ) {
foreach( $inner_field['dependency'] as $key => $dependency ) {
$conditions[ $inner_field_key ][ $key ] = $dependency;
}
}
}
}
if( isset( $field['hide'] ) && ! empty( $field['hide'] ) && is_array( $field['hide'] ) ) {
foreach( $field['hide'] as $key => $hide ) {
$hideFields[ $field_key ][ $key ] = $hide;
}
}
if( isset( $field['dependency'] ) && ! empty( $field['dependency'] ) && is_array( $field['dependency'] ) ) {
foreach( $field['dependency'] as $key => $dependency ) {
$conditions[ $field_key ][ $key ] = $dependency;
}
}
}
}
}
}
}
}
$template = apply_filters( 'nx_template_name', array() );
$template_settings = apply_filters( 'nx_template_settings_by_theme', array() );
return array(
'toggleFields' => $conditions, // TODO: toggling system has to be more optimized!
'hideFields' => $hideFields,
'template' => $template,
'template_settings' => $template_settings,
'source_types' => NotificationX_Helper::source_types(),
'theme_sources' => NotificationX_Helper::theme_sources(),
'template_keys' => NotificationX_Helper::template_keys(),
);
}
public function custom_columns( $columns ) {
$title_column = $columns['title'];
$date_column = $columns['date'];
unset( $columns['title'] );
unset( $columns['date'] );
$columns['notification_status'] = __('Enable / Disable', 'notificationx');
$columns['title'] = $title_column;
$columns['notification_type'] = __('Type', 'notificationx');
$columns['date'] = $date_column;
return apply_filters('nx_post_columns', $columns );
}
public function manage_custom_columns( $column, $post_id ){
switch ( $column ) {
case 'notification_type':
$type = get_post_meta( $post_id, '_nx_meta_display_type', true );
if ( $type ) {
$type = NotificationX_Helper::notification_types( $type );
if( $type !== 'Conversions' ) {
echo $type;
} else {
$from = get_post_meta( $post_id, '_nx_meta_conversion_from', true );
echo $type . ' - ' . NotificationX_Helper::conversion_from( $from );
}
}
break;
case 'notification_status':
$status = get_post_meta( $post_id, '_nx_meta_active_check', true );
self::notification_toggle( $status, $post_id );
break;
}
do_action( 'nx_post_columns_content', $column, $post_id );
}
public static function notification_toggle( $status = '1', $post_id ){
$text = __('Active', 'notificationx');
$img_active = NOTIFICATIONX_ADMIN_URL . 'assets/img/active1.png';
$img_inactive = NOTIFICATIONX_ADMIN_URL . 'assets/img/active0.png';
$active = 'true';
$img = $img_active;
if ( ! $status ) {
$text = __('Inactive', 'notificationx');
$img = $img_inactive;
$active = 'false';
}
?>
'NotificationX',
'singular_name' => 'NotificationX',
'add_new' => esc_html__( 'Add New', 'notificationx' ) ,
'add_new_item' => esc_html__( 'Add New', 'notificationx' ),
'edit_item' => esc_html__( 'Edit', 'notificationx' ),
'new_item' => esc_html__( 'New', 'notificationx' ),
'view_item' => esc_html__( 'View', 'notificationx' ),
'search_items' => esc_html__( 'Search', 'notificationx' ),
'not_found' => esc_html__( 'No notification x is found', 'notificationx' ),
'not_found_in_trash' => esc_html__( 'No notification x is found in Trash', 'notificationx' ),
'menu_name' => 'NotificationX',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => '',
'taxonomies' => array( '' ),
'public' => false,
'show_ui' => true,
'show_in_menu' => 'notificationx',
'show_in_admin_bar' => true,
'show_in_rest' => false,
'menu_position' => 80,
'menu_icon' => NOTIFICATIONX_ADMIN_URL . 'assets/img/nx-menu-icon-colored.png',
'show_in_nav_menus' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => '',
'capability_type' => 'post',
'supports' => array( 'title' ),
);
register_post_type( $this->type, $args );
add_image_size( "_nx_notification_thumb", 100, 100, true );
}
/**
* Admin Menu Page
*
* @return void
*/
public function menu_page(){
if( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings_class = new NotificationX_Settings();
$settings = apply_filters( 'notificationx_admin_menu', array(
'nx-settings' => array(
'title' => __('Settings', 'notificationx'),
'capability' => 'delete_users',
'callback' => array( $settings_class, 'settings_page' )
),
'nx-builder' => array(
'title' => __('Quick Builder', 'notificationx'),
'capability' => 'delete_users',
'callback' => array( $this, 'quick_builder' )
),
) );
$this->builder_args = NotificationX_MetaBox::get_builder_args();
$this->metabox_id = $this->builder_args['id'];
$flag = true;
// Enable Disable Redirect for - nx-admin.php in notificationx();
$post_status = self::count_posts();
$get_enabled_post = $post_status->enabled;
$get_disabled_post = $post_status->disabled;
$trash_notificationx = $post_status->trash;
$current_url = admin_url('admin.php?page=nx-admin');
if( isset( $_GET['status'], $_GET['page'] ) && $_GET['page'] == 'nx-admin' ) {
if( ( $_GET['status'] == 'disabled' && $get_disabled_post == 0 ) || ( $_GET['status'] == 'trash' && $trash_notificationx == 0 ) || ( $_GET['status'] == 'enabled' && $get_enabled_post == 0 )) {
wp_safe_redirect( $current_url, 200 );
}
}
// Duplicating NotificationX
if( isset( $_GET['action'], $_GET['page'], $_GET['post'], $_GET['nx_duplicate_nonce'] ) && $_GET['action'] === 'nxduplicate' && $_GET['page'] === 'nx-admin' ) {
if( wp_verify_nonce( $_GET['nx_duplicate_nonce'], 'nx_duplicate_nonce' ) ) {
$nx_post_id = intval( $_GET['post'] );
$get_post = get_post( $nx_post_id );
$post_data = json_decode( json_encode( $get_post ), true );
unset( $post_data['ID'] );
$post_data['post_title'] = $post_data['post_title'] . ' - Copy';
$duplicate_post_id = wp_insert_post( $post_data );
$get_post_meta = get_metadata( 'post', $nx_post_id );
if( ! empty( $get_post_meta ) ) {
foreach( $get_post_meta as $key => $value ){
if( in_array( $key, array( '_edit_lock', '_edit_last' ) ) ) {
continue;
}
add_post_meta( intval( $duplicate_post_id ), $key, $value[0], true );
}
}
wp_safe_redirect( $current_url, 200 );
}
}
if( isset( $_GET['delete_all'], $_GET['page'] ) && $_GET['delete_all'] == true && $_GET['page'] == 'nx-admin' ) {
$notificationx = new WP_Query(array(
'post_type' => 'notificationx',
'post_status' => array('trash'),
'numberposts' => -1,
));
if( $notificationx->have_posts() ) {
while( $notificationx->have_posts() ) : $notificationx->the_post();
$iddd = get_the_ID();
wp_delete_post( $iddd );
endwhile;
wp_safe_redirect( $current_url, 200 );
}
}
/**
* Add Submit
*/
if( isset( $_POST[ 'nx_builder_add_submit' ] ) ) :
if ( ! isset( $_POST[$this->metabox_id . '_nonce'] ) || ! wp_verify_nonce( $_POST[$this->metabox_id . '_nonce'], $this->metabox_id ) ) {
$flag = false;
}
if( $flag ) {
if( $_POST['nx_meta_display_type'] == 'press_bar' ) {
$title = __('NotificationX - Notification Bar', 'notificationx');
} elseif( $_POST['nx_meta_display_type'] == 'comments' ) {
$title = __('NotificationX - WP Comments', 'notificationx');
} elseif( $_POST['nx_meta_display_type'] == 'conversions' ) {
$conversions = NotificationX_Helper::conversion_from();
$title = 'NotificationX - ' . $conversions[$_POST['nx_meta_conversion_from']];
}
$_POST['post_type'] = 'notificationx';
$postdata = array(
'post_type' => 'notificationx',
'post_title' => $title . ' - ' . date( get_option( 'date_format' ), current_time( 'timestamp' ) ),
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$p_id = wp_insert_post($postdata);
if( $p_id || ! is_wp_error( $p_id ) ) {
do_action( 'nx_before_builder_submit', $_POST );
// saving builder meta data with post
NotificationX_MetaBox::save_data( $this->builder_data( $_POST ), $p_id );
/**
* Safely Redirect to NotificationX Page
*/
wp_safe_redirect( add_query_arg( array(
'page' => 'nx-admin',
), admin_url( 'admin.php' ) ) );
}
}
endif;
add_menu_page( 'NotificationX', 'NotificationX', 'delete_users', 'nx-admin', array( $this, 'notificationx' ), NOTIFICATIONX_ADMIN_URL . 'assets/img/nx-menu-icon-colored.png', 80 );
foreach( $settings as $slug => $setting ) {
$cap = isset( $setting['capability'] ) ? $setting['capability'] : 'delete_users';
$hook = add_submenu_page( 'nx-admin', $setting['title'], $setting['title'], $cap, $slug, $setting['callback'] );
}
}
public function highlight_admin_menu( $parent_file ){
if( $parent_file === 'notificationx' ) {
return 'nx-admin';
}
return $parent_file;
}
public function highlight_admin_submenu( $submenu_file, $parent_file ){
if( $parent_file == 'nx-admin' && $submenu_file == 'edit.php?post_type=notificationx' ) {
return "nx-admin";
}
return $submenu_file;
}
public static function count_posts( $type = 'notificationx', $perm = '' ) {
global $wpdb;
if ( ! post_type_exists( $type ) ) {
return new stdClass;
}
$cache_key = 'nx_counts_cache';
self::$counts = wp_cache_get( $cache_key, 'counts' );
if ( false !== self::$counts ) {
return self::$counts;
}
$query = "SELECT ID, post_status, meta_key, meta_value FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ID = post_id WHERE post_type = %s AND meta_key = '_nx_meta_active_check'";
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
$counts = array_fill_keys( array( 'enabled', 'disabled', 'trash', 'publish' ), 0 );
$disable = 0;
$enable = 0;
foreach ( $results as $row ) {
$counts[ 'publish' ] = $counts['publish'] + ( $row['post_status'] === 'publish' ? 1 : 0 );
$counts[ 'trash' ] = $counts['trash'] + ( $row['post_status'] === 'trash' ? 1 : 0 );
if( $row[ 'meta_value' ] == 0 ) {
$disable = 1;
$enable = 0;
}
if( $row[ 'meta_value' ] == 1 ) {
$disable = 0;
$enable = 1;
}
if( $disable == 1 && $row['post_status'] == 'trash' ) {
$disable = 0;
}
if( $enable == 1 && $row['post_status'] == 'trash' ) {
$enable = 0;
}
$counts[ 'disabled' ] = $counts[ 'disabled' ] + $disable;
$counts[ 'enabled' ] = $counts[ 'enabled' ] + $enable;
}
self::$counts = (object) $counts;
wp_cache_set( $cache_key, self::$counts, 'counts' );
return self::$counts;
}
public function notificationx(){
$notificationx = new WP_Query(array(
'post_type' => 'notificationx',
'post_status' => array('publish', 'trash', 'draft'),
'numberposts' => -1,
));
include_once NOTIFICATIONX_ADMIN_DIR_PATH . 'partials/nx-admin.php';
}
public function quick_builder(){
$builder_args = $this->builder_args;
$tabs = $this->builder_args['tabs'];
$prefix = self::$prefix;
$metabox_id = $this->metabox_id;
/**
* This lines of code is for editing a notification in simple|quick builder
*
* @var [type]
*/
$idd = null;
if( isset( $_GET['post_id'] ) && ! empty( $_GET['post_id'] )) {
$idd = intval( $_GET['post_id'] );
}
include_once NOTIFICATIONX_ADMIN_DIR_PATH . 'partials/nx-quick-builder-display.php';
}
/**
* Generate the builder data acording to default meta data
*
* @param array $data
* @return array
*/
protected function builder_data( $data ) {
$post_data = [];
$prefix = self::$prefix;
$meta_fields = NotificationX_MetaBox::get_metabox_fields( $prefix );
foreach( $meta_fields as $meta_key => $meta_field ) {
if( in_array( $meta_key, array_keys($data) ) ) {
$post_data[ $meta_key ] = $data[ $meta_key ];
} else {
$post_data[ $meta_key ] = '';
if( isset( $meta_field['defaults'] ) ) {
$post_data[ $meta_key ] = $meta_field['defaults'];
}
if( isset( $meta_field['default'] ) ) {
$post_data[ $meta_key ] = $meta_field['default'];
}
}
}
return array_merge( $post_data, $data );
}
public static function get_form_action( $query_var = '', $builder_form = false ) {
$page = '/admin.php?page=nx-settings';
if( $builder_form ) {
$page = '/admin.php?page=nx-builder';
}
if ( is_network_admin() ) {
return network_admin_url( $page . $query_var );
} else {
return admin_url( $page . $query_var );
}
}
public function notification_preview(){
global $pagenow, $post_type, $post;
if ( ! in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) {
return false;
}
if ( $this->type != $post_type ) {
return false;
}
$display_type = get_post_meta( $post->ID, '_nx_meta_display_type', true );
include NOTIFICATIONX_ADMIN_DIR_PATH . 'partials/nx-admin-preview.php';
}
//TODO: Notification Preview Not Visible for now.
public function preview_html( $settings, $type = 'conversion' ){
$data = array(
'comment' => array(
'link' => '#',
'post_title' => 'Hello world!',
'post_link' => '#',
'timestamp' => '1550986787',
'user_id' => get_current_user_id(),
'name' => 'John D',
),
'conversion' => array(
'link' => '#',
'title' => 'Hello world!',
'timestamp' => '1550986787',
'user_id' => get_current_user_id(),
'name' => 'John D',
)
);
$unique_id = uniqid( 'notificationx-' );
$output = '
';
$output .= '