Yoast_Notification

Implements individual notification.

Defined (1)

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

/admin/class-yoast-notification.php  
  1. class Yoast_Notification { 
  2.  
  3. const MATCH_ALL = 'all'; 
  4. const MATCH_ANY = 'any'; 
  5.  
  6. const ERROR = 'error'; 
  7. const WARNING = 'warning'; 
  8. const UPDATED = 'updated'; 
  9.  
  10. /** 
  11. * Contains optional arguments: 
  12. * - type: The notification type, i.e. 'updated' or 'error' 
  13. * - id: The ID of the notification 
  14. * - nonce: Security nonce to use in case of dismissible notice. 
  15. * - priority: From 0 to 1, determines the order of Notifications. 
  16. * - dismissal_key: Option name to save dismissal information in, ID will be used if not supplied. 
  17. * - capabilities: Capabilities that a user must have for this Notification to show. 
  18. * - capability_check: How to check capability pass: all or any. 
  19. * - wpseo_page_only: Only display on wpseo page or on every page. 
  20. * @var array Options of this Notification. 
  21. */ 
  22. private $options = array(); 
  23.  
  24. /** @var array Contains default values for the optional arguments */ 
  25. private $defaults = array( 
  26. 'type' => self::UPDATED,  
  27. 'id' => '',  
  28. 'nonce' => null,  
  29. 'priority' => 0.5,  
  30. 'data_json' => array(),  
  31. 'dismissal_key' => null,  
  32. 'capabilities' => array(),  
  33. 'capability_check' => self::MATCH_ALL,  
  34. ); 
  35.  
  36. /** 
  37. * Notification class constructor. 
  38. * @param string $message Message string. 
  39. * @param array $options Set of options. 
  40. */ 
  41. public function __construct( $message, $options = array() ) { 
  42. $this->message = $message; 
  43. $this->options = $this->normalize_options( $options ); 
  44.  
  45. /** 
  46. * Retrieve notification ID string. 
  47. * @return string 
  48. */ 
  49. public function get_id() { 
  50. return $this->options['id']; 
  51.  
  52. /** 
  53. * Retrieve nonce identifier. 
  54. * @return null|string Nonce for this Notification. 
  55. */ 
  56. public function get_nonce() { 
  57. if ( $this->options['id'] && empty( $this->options['nonce'] ) ) { 
  58. $this->options['nonce'] = wp_create_nonce( $this->options['id'] ); 
  59.  
  60. return $this->options['nonce']; 
  61.  
  62. /** 
  63. * Make sure the nonce is up to date 
  64. */ 
  65. public function refresh_nonce() { 
  66. if ( $this->options['id'] ) { 
  67. $this->options['nonce'] = wp_create_nonce( $this->options['id'] ); 
  68.  
  69. /** 
  70. * Get the type of the notification 
  71. * @return string 
  72. */ 
  73. public function get_type() { 
  74. return $this->options['type']; 
  75.  
  76. /** 
  77. * Priority of the notification 
  78. * Relative to the type. 
  79. * @return float Returns the priority between 0 and 1. 
  80. */ 
  81. public function get_priority() { 
  82. return $this->options['priority']; 
  83.  
  84. /** 
  85. * Get the User Meta key to check for dismissal of notification 
  86. * @return string User Meta Option key that registers dismissal. 
  87. */ 
  88. public function get_dismissal_key() { 
  89. if ( empty( $this->options['dismissal_key'] ) ) { 
  90. return $this->options['id']; 
  91.  
  92. return $this->options['dismissal_key']; 
  93.  
  94. /** 
  95. * Is this Notification persistent 
  96. * @return bool True if persistent, False if fire and forget. 
  97. */ 
  98. public function is_persistent() { 
  99. $id = $this->get_id(); 
  100.  
  101. return ! empty( $id ); 
  102.  
  103. /** 
  104. * Check if the notification is relevant for the current user 
  105. * @return bool True if a user needs to see this Notification, False if not. 
  106. */ 
  107. public function display_for_current_user() { 
  108. // If the notification is for the current page only, always show. 
  109. if ( ! $this->is_persistent() ) { 
  110. return true; 
  111.  
  112. // If the current user doesn't match capabilities. 
  113. return $this->match_capabilities(); 
  114.  
  115. /** 
  116. * Does the current user match required capabilities 
  117. * @return bool 
  118. */ 
  119. public function match_capabilities() { 
  120. // Super Admin can do anything. 
  121. if ( is_multisite() && is_super_admin() ) { 
  122. return true; 
  123.  
  124. /** 
  125. * Filter capabilities that enable the displaying of this notification. 
  126. * @since 3.2 
  127. * @param array $capabilities The capabilities that must be present for this Notification. 
  128. * @param Yoast_Notification $notification The notification object. 
  129. * @return array of capabilities or empty for no restrictions. 
  130. */ 
  131. $capabilities = apply_filters( 'wpseo_notification_capabilities', $this->options['capabilities'], $this ); 
  132.  
  133. // Should be an array. 
  134. if ( ! is_array( $capabilities ) ) { 
  135. $capabilities = (array) $capabilities; 
  136.  
  137. /** 
  138. * Filter capability check to enable all or any capabilities. 
  139. * @since 3.2 
  140. * @param string $capability_check The type of check that will be used to determine if an capability is present. 
  141. * @param Yoast_Notification $notification The notification object. 
  142. * @return string self::MATCH_ALL or self::MATCH_ANY. 
  143. */ 
  144. $capability_check = apply_filters( 'wpseo_notification_capability_check', $this->options['capability_check'], $this ); 
  145.  
  146. if ( ! in_array( $capability_check, array( self::MATCH_ALL, self::MATCH_ANY ), true ) ) { 
  147. $capability_check = self::MATCH_ALL; 
  148.  
  149. if ( ! empty( $capabilities ) ) { 
  150.  
  151. $has_capabilities = array_filter( $capabilities, array( $this, 'has_capability' ) ); 
  152.  
  153. switch ( $capability_check ) { 
  154. case self::MATCH_ALL: 
  155. return $has_capabilities === $capabilities; 
  156. case self::MATCH_ANY: 
  157. return ! empty( $has_capabilities ); 
  158.  
  159. return true; 
  160.  
  161. /** 
  162. * Array filter function to find matched capabilities 
  163. * @param string $capability Capability to test. 
  164. * @return bool 
  165. */ 
  166. private function has_capability( $capability ) { 
  167. return current_user_can( $capability ); 
  168.  
  169. /** 
  170. * Return the object properties as an array 
  171. * @return array 
  172. */ 
  173. public function to_array() { 
  174. return array( 
  175. 'message' => $this->message,  
  176. 'options' => $this->options,  
  177. ); 
  178.  
  179. /** 
  180. * Adds string (view) behaviour to the Notification 
  181. * @return string 
  182. */ 
  183. public function __toString() { 
  184. $attributes = array(); 
  185.  
  186. // Default notification classes. 
  187. $classes = array( 
  188. 'yoast-alert',  
  189. ); 
  190.  
  191. // Maintain WordPress visualisation of alerts when they are not persistent. 
  192. if ( ! $this->is_persistent() ) { 
  193. $classes[] = 'notice'; 
  194. $classes[] = $this->get_type(); 
  195.  
  196. if ( ! empty( $classes ) ) { 
  197. $attributes['class'] = implode( ' ', $classes ); 
  198.  
  199. // Combined attribute key and value into a string. 
  200. array_walk( $attributes, array( $this, 'parse_attributes' ) ); 
  201.  
  202. // Build the output DIV. 
  203. return '<div ' . implode( ' ', $attributes ) . '>' . wpautop( $this->message ) . '</div>' . PHP_EOL; 
  204.  
  205. /** 
  206. * Get the JSON if provided 
  207. * @return false|string 
  208. */ 
  209. public function get_json() { 
  210. if ( empty( $this->options['data_json'] ) ) { 
  211. return ''; 
  212.  
  213. return wp_json_encode( $this->options['data_json'] ); 
  214.  
  215. /** 
  216. * Make sure we only have values that we can work with 
  217. * @param array $options Options to normalize. 
  218. * @return array 
  219. */ 
  220. private function normalize_options( $options ) { 
  221. $options = wp_parse_args( $options, $this->defaults ); 
  222.  
  223. // Should not exceed 0 or 1. 
  224. $options['priority'] = min( 1, max( 0, $options['priority'] ) ); 
  225.  
  226. // Set default capabilities when not supplied. 
  227. if ( empty( $options['capabilities'] ) || array() === $options['capabilities'] ) { 
  228. $options['capabilities'] = array( 'manage_options' ); 
  229.  
  230. return $options; 
  231.  
  232. /** 
  233. * Format HTML element attributes 
  234. * @param string $value Attribute value. 
  235. * @param string $key Attribute name. 
  236. */ 
  237. private function parse_attributes( & $value, $key ) { 
  238. $value = sprintf( '%s="%s"', $key, esc_attr( $value ) );