*/ class Notifications { use Date; public $manager; public $conflict_days = 5; public $slug; /** * Settings page slug. * * @var string */ public $settings_page = 'jlt_admin_bar_editor-settings'; /** * Constructor method */ public function __construct() { $this->manager = new Manager(); $this->slug = Helper::jlt_admin_bar_editor_slug_cleanup(); add_action( 'admin_notices', array( $this, 'setup_notifications' ) ); add_action( 'jlt_admin_bar_editor_display_notice', array( $this, 'display_notice' ), 10, 2 ); add_action( 'wp_ajax_jlt_admin_bar_editor_notification_action', array( $this, 'notification_action' ) ); } /** * Notification Action * * @author Jewel Theme */ public function notification_action() { check_ajax_referer( 'jlt_admin_bar_editor_notification_nonce' ); $action_type = ! empty( $_REQUEST['action_type'] ) ? sanitize_key( $_REQUEST['action_type'] ) : ''; $notification_type = ! empty( $_REQUEST['notification_type'] ) ? sanitize_key( $_REQUEST['notification_type'] ) : ''; $trigger_time = ! empty( $_REQUEST['trigger_time'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['trigger_time'] ) ) : ''; $exec_notifications = $this->manager->get_exec_notifications( $trigger_time, $notification_type ); // No Executable Notifications found . if ( empty( $exec_notifications ) ) { die( 0 ); } $count = 0; foreach ( $exec_notifications as $index => $notification ) { if ( 0 === $index ) { if ( 'disable' === $action_type ) { $notification->is_active = false; } $notification->fire( $trigger_time, $notification_type )->save(); } else { $count++; $notification->maybe_delay( $this->date_increment( $trigger_time, $this->conflict_days * $count ) )->save(); } } die( 0 ); } /** * Notification Setup * * @param [type] $type . * * @author Jewel Theme */ public function setup_notifications_by_type( $type ) { // $trigger_time should be today . $trigger_time = $this->current_time(); // Block if necessary . $notification_last_fired = get_option( "jlt_admin_bar_editor_{$type}_last_interact" ); if ( $notification_last_fired ) { $notification_enable_date = $this->date_increment( $notification_last_fired, $this->conflict_days ); if ( $this->date_is_prev( $trigger_time, $notification_enable_date ) ) { return; } } // Get Executable Notifications . $exec_notifications = $this->manager->get_exec_notifications( $trigger_time, $type ); // No Executable Notifications found . if ( empty( $exec_notifications ) ) { return; } $notification = $exec_notifications[0]; do_action( "jlt_admin_bar_editor_display_{$type}", $notification, $trigger_time ); } /** * Whether notices are allowed on the current screen. * * Notices are limited to the plugin's own settings page, to avoid * cluttering unrelated screens. * * @return bool * @author Jewel Theme */ public function is_allowed_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $allowed = ( $screen && false !== strpos( $screen->id, $this->settings_page ) ); return (bool) apply_filters( 'jlt_admin_bar_editor_notice_allowed_screen', $allowed ); } /** * Notification setup * * @author Jewel Theme */ public function setup_notifications() { if ( ! $this->is_allowed_screen() ) { return; } $this->setup_notifications_by_type( 'notice' ); } /** * Display notice * * @param [type] $notice . * @param [type] $trigger_time . * * @return void * @author Jewel Theme */ public function display_notice( $notice, $trigger_time ) { $notice->notice_header(); $notice->notice_content(); $notice->notice_footer(); $notice->core_script( $trigger_time ); } }