Whip_MessagesManager

Manages messages using a global to prevent duplicate messages.

Defined (1)

The class is defined in the following location(s).

/vendor/yoast/whip/src/Whip_MessagesManager.php  
  1. class Whip_MessagesManager { 
  2.  
  3. /** 
  4. * Whip_MessagesManager constructor. 
  5. */ 
  6. public function __construct() 
  7. if ( ! array_key_exists( 'whip_messages', $GLOBALS ) ) { 
  8. $GLOBALS['whip_messages'] = array(); 
  9.  
  10. /** 
  11. * Adds a message to the Messages Manager. 
  12. * @param Whip_Message $message The message to add. 
  13. */ 
  14. public function addMessage( Whip_Message $message ) { 
  15. $whipVersion = require dirname( __FILE__ ) . '/configs/version.php'; 
  16.  
  17. $GLOBALS[ 'whip_messages' ][$whipVersion] = $message; 
  18.  
  19. /** 
  20. * Determines whether or not there are messages available. 
  21. * @return bool Whether or not there are messages available. 
  22. */ 
  23. public function hasMessages() { 
  24. return isset( $GLOBALS['whip_messages'] ) && count( $GLOBALS['whip_messages'] ) > 0; 
  25.  
  26. /** 
  27. * Lists the messages that are currently available. 
  28. * @return array The messages that are currently set. 
  29. */ 
  30. public function listMessages() { 
  31. return $GLOBALS[ 'whip_messages' ]; 
  32.  
  33. /** 
  34. * Deletes all messages. 
  35. */ 
  36. public function deleteMessages() { 
  37. unset( $GLOBALS[ 'whip_messages' ] ); 
  38.  
  39. /** 
  40. * Gets the latest message. 
  41. * @return Whip_Message The message. Returns a NullMessage if none is found. 
  42. */ 
  43. public function getLatestMessage() { 
  44. if ( ! $this->hasMessages() ) { 
  45. return new Whip_NullMessage(); 
  46.  
  47. $messages = $this->sortByVersion( $this->listMessages() ); 
  48.  
  49. $this->deleteMessages(); 
  50.  
  51. return array_pop( $messages ); 
  52.  
  53. /** 
  54. * Sorts the list of messages based on the version number. 
  55. * @param array $messages The list of messages to sort. 
  56. * @return array The sorted list of messages. 
  57. */ 
  58. private function sortByVersion( array $messages ) { 
  59. uksort( $messages, 'version_compare' ); 
  60.  
  61. return $messages;