Skip to content

Commit

Permalink
Yoast Notification Center setup
Browse files Browse the repository at this point in the history
  • Loading branch information
barrykooij committed May 6, 2014
1 parent 8eb38c9 commit caf10a6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
78 changes: 78 additions & 0 deletions admin/class-yoast-notification-center.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

class Yoast_Notification_Center {

const COOKIE_KEY = 'yoast_notices';

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

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

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

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

// Create Yoast_Notification objects
if ( count( $json_notifications ) > 0 ) {
foreach ( $json_notifications as $json_notification ) {
$notifications[] = new Yoast_Notification( $json_notification['message'], $json_notification['type'] );
}
}
}

return $notifications;
}

/**
* Display the message
*/
public static function display_notices() {

// Get the messages
$notifications = self::get_notifications_from_cookie();

// Display notifications
if ( count( $notifications ) > 0 ) {
foreach ( $notifications as $notification ) {
add_action( 'all_admin_notices', array( $notification, 'output' ) );
}
}

// 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 ) {

// Get the messages
$notifications = self::get_notifications_from_cookie();

// Add the message
$notifications[] = $notification;

// 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();
}

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

}
66 changes: 66 additions & 0 deletions admin/class-yoast-notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

class Yoast_Notification {

private $message;
private $type;

/**
* The Constructor
*
* @param String $message
* @param String $type
*/
public function __construct( $message, $type = 'updated' ) {
$this->message = $message;
$this->type = $type;
}

/**
* @return String
*/
public function get_message() {
return $this->message;
}

/**
* @param String $message
*/
public function set_message( $message ) {
$this->message = $message;
}

/**
* @return String
*/
public function get_type() {
return $this->type;
}

/**
* @param String $type
*/
public function set_type( $type ) {
$this->type = $type;
}

/**
* Return the object properties as an array
*
* @return array
*/
public function to_array() {
return array(
'message' => $this->get_message(),
'type' => $this->get_type()
);
}

/**
* Output the message
*/
public function output() {
echo '<div class="' . $this->get_type() . '"><p>' . $this->get_message() . '</p></div>' . PHP_EOL;
}

}
6 changes: 6 additions & 0 deletions wp-seo-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ function wpseo_auto_load( $class ) {
'yoast_plugin_license_manager' => WPSEO_PATH . 'admin/license-manager/class-plugin-license-manager.php',
'yoast_product' => WPSEO_PATH . 'admin/license-manager/class-product.php',

'yoast_notification_center' => WPSEO_PATH . 'admin/class-yoast-notification-center.php',
'yoast_notification' => WPSEO_PATH . 'admin/class-yoast-notification.php',

'wp_list_table' => ABSPATH . 'wp-admin/includes/class-wp-list-table.php',
'walker_category' => ABSPATH . 'wp-includes/category-template.php',
'pclzip' => ABSPATH . 'wp-admin/includes/class-pclzip.php',
Expand Down Expand Up @@ -262,6 +265,9 @@ function wpseo_admin_init() {
if ( $options['enablexmlsitemap'] === true ) {
$GLOBALS['wpseo_sitemaps_admin'] = new WPSEO_Sitemaps_Admin;
}

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

add_action( 'plugins_loaded', 'wpseo_init', 14 );
Expand Down

0 comments on commit caf10a6

Please sign in to comment.