diff --git a/admin/class-yoast-notification-center.php b/admin/class-yoast-notification-center.php new file mode 100644 index 00000000000..cb7b6876c24 --- /dev/null +++ b/admin/class-yoast-notification-center.php @@ -0,0 +1,78 @@ + 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 ); + } + +} \ No newline at end of file diff --git a/admin/class-yoast-notification.php b/admin/class-yoast-notification.php new file mode 100644 index 00000000000..143bf9ec58f --- /dev/null +++ b/admin/class-yoast-notification.php @@ -0,0 +1,66 @@ +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 '
' . $this->get_message() . '