settings = wp_parse_args( $settings, $defaults ); if( !empty($content) ) $this->settings['content'] = $content; if( empty($this->settings['content']) ) $this->settings['id'] = crc32($this->settings['content']); // Not ideal, but with no ID we need some unique value } public function get_notification_as_string() { $html = '
'; $html .= wpautop( $this->settings['content'] ); if( version_compare($GLOBALS['wp_version'], 4.2, '<' ) ) { $html .= '

  '. __('Dismiss', 'powerpress') .'

'; } $html .= '
' . PHP_EOL; return $html; } }; class PowerPress_Notification_Manager { private $notifications = array(); private $dismissedNotifications = array(); public function __construct() { $this->dismissedNotifications = get_option('powerpress_dismissed_notices'); add_action( 'all_admin_notices', array( $this, 'all_admin_notices' ) ); add_action('wp_ajax_powerpress_notice_dismiss', array($this, 'wp_ajax_powerpress_notice_dismiss') ); add_action('admin_head', array($this, 'admin_head') ); } public function all_admin_notices() { foreach( $this->notifications as $key => $notification ) { echo $notification->get_notification_as_string(); } } public function wp_ajax_powerpress_notice_dismiss() { $dismiss_notice_id = $_POST['dismiss_notice_id']; preg_match('/^powerpress-notice-(.*)$/i', $dismiss_notice_id, $match ); if( empty($match[1]) ) die('-1'); $DismissedNotifications = get_option('powerpress_dismissed_notices'); if( !is_array($DismissedNotifications) ) $DismissedNotifications = array(); $DismissedNotifications[ $match[1] ] = 1; update_option('powerpress_dismissed_notices', $DismissedNotifications); die('1'); } function admin_head() { if( count($this->notifications) > 0 ) // If there are notices to print, then lets also put in the ajax to clear them { if( version_compare($GLOBALS['wp_version'], 4.2, '>=' ) ) { ?> dismissedNotifications[ $notification_id ]) ) return true; return false; } public function add($notification_id, $notification_content) { if( !$this->dismissed_status($notification_id) ) { $this->notifications[$notification_id] = new PowerPress_Notification($notification_content, array('id'=>$notification_id) ); } } }; function powerpressadmin_notifications_appropriate() { // Any powerpress page if( preg_match('/wp-admin\/.*powerpress/', $_SERVER['REQUEST_URI']) ) return true; // Dashboard is acceptable: if( preg_match('/wp-admin\/(index\.php)?$/', $_SERVER['REQUEST_URI']) ) return true; // edit posts (pages, post types, etc...) if( preg_match('/wp-admin\/edit\.php/', $_SERVER['REQUEST_URI']) ) return true; // managing plugins if( preg_match('/wp-admin\/plugins\.php/', $_SERVER['REQUEST_URI']) ) return true; // Required so we can dismiss notices if( preg_match('/wp-admin\/admin-ajax\.php/', $_SERVER['REQUEST_URI']) ) return true; return false; } // eof