forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8eb38c9
commit caf10a6
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters