Skip to content

Commit

Permalink
Complete new setup, now using transients and the shutdown hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
barrykooij committed May 7, 2014
1 parent f812f05 commit 3a81021
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 29 deletions.
129 changes: 102 additions & 27 deletions admin/class-yoast-notification-center.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,66 @@

class Yoast_Notification_Center {

const COOKIE_KEY = 'yoast_notices';
const TRANSIENT_KEY = 'yoast_notifications';

private static $instance = null;

private $notifications = array();

/**
* Construct
*/
private function __construct() {

// Load the notifications from cookie
$this->notifications = $this->get_notifications_from_transient();

// Clear the cookie
if ( count( $this->notifications ) > 0 ) {
$this->remove_transient();
}

// Display the notifications in all_admin_notices
add_action( 'all_admin_notices', array( $this, 'display_notifications' ) );

// Write the cookie on shutdown
add_action( 'shutdown', array( $this, 'set_transient' ) );

// AJAX
add_action( 'wp_ajax_yoast_get_notifications', array( $this, 'ajax_get_notifications' ) );
}

/**
* Singleton getter
*
* @return Yoast_Notification_Center
*/
public static function get() {

if ( null == self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Get the notifications from cookie
*
* @return array
*/
private static function get_notifications_from_cookie() {
private function get_notifications_from_transient() {

// The notifications array
$notifications = array();

$transient_notifications = get_transient( self::TRANSIENT_KEY );

// Check if cookie is set
if ( isset( $_COOKIE[ self::COOKIE_KEY ] ) ) {
if ( false !== $transient_notifications ) {

// Get json notifications from cookie
$json_notifications = json_decode( $_COOKIE[ self::COOKIE_KEY ], true );
$json_notifications = json_decode( $transient_notifications, true );

// Create Yoast_Notification objects
if ( count( $json_notifications ) > 0 ) {
Expand All @@ -32,47 +75,79 @@ private static function get_notifications_from_cookie() {
}

/**
* Display the message
* Clear the cookie
*/
public static function display_notices() {
private function remove_transient() {
error_log('remove_transient');
delete_transient( self::TRANSIENT_KEY );
}

// Get the messages
$notifications = self::get_notifications_from_cookie();
/**
* Clear local stored notifications
*/
private function clear_notifications() {
$this->notifications = array();
}

// Display notifications
if ( count( $notifications ) > 0 ) {
foreach ( $notifications as $notification ) {
add_action( 'all_admin_notices', array( $notification, 'output' ) );
/**
* Write the notifications to cookie
*/
public function set_transient() {

// Count local stored notifications
if ( count( $this->notifications ) > 0 ) {

// Create array with all notifications
$arr_notifications = array();

// Add each notification as array to $arr_notifications
foreach ( $this->notifications as $notification ) {
$arr_notifications[] = $notification->to_array();
}

// Set the cookie with notifications
set_transient( self::TRANSIENT_KEY, json_encode( $arr_notifications ), 60 * 10 );

}

// Remove the cookie
setcookie( self::COOKIE_KEY, null, -1 );
}

/**
* Add notification to the cookie
*
* @param Yoast_Notification $notification
*/
public static function add_notice( Yoast_Notification $notification ) {
public function add_notification( Yoast_Notification $notification ) {
$this->notifications[] = $notification;
}

/**
* Display the notifications
*/
public function display_notifications() {

// Get the messages
$notifications = self::get_notifications_from_cookie();
// Display notifications
if ( count( $this->notifications ) > 0 ) {
foreach ( $this->notifications as $notification ) {
$notification->output();
}
}

// Add the message
$notifications[] = $notification;
// Clear the local stored notifications
$this->clear_notifications();

// Create array with all notifications
$arr_notifications = array();
}

// Add each notification as array to $arr_notifications
foreach($notifications as $notification ) {
$arr_notifications[] = $notification->to_array();
}
/**
* AJAX display notifications
*/
public function ajax_get_notifications() {

// Display the notices
$this->display_notifications();

// Set the cookie with notifications
setcookie( self::COOKIE_KEY, json_encode( $arr_notifications ), time() + 60 * 10 );
// AJAX die
exit;
}

}
8 changes: 6 additions & 2 deletions wp-seo-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ function wpseo_admin_init() {
$GLOBALS['wpseo_sitemaps_admin'] = new WPSEO_Sitemaps_Admin;
}

// Display Yoast notifications
Yoast_Notification_Center::display_notices();
}

add_action( 'plugins_loaded', 'wpseo_init', 14 );
Expand All @@ -284,6 +282,12 @@ function wpseo_admin_init() {
add_action( 'plugins_loaded', 'wpseo_frontend_init', 15 );
}

function load_yoast_notifications() {
// Init Yoast_Notification_Center class
Yoast_Notification_Center::get();
}
add_action( 'admin_init', 'load_yoast_notifications' );

// Activation and deactivation hook
register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );

0 comments on commit 3a81021

Please sign in to comment.