SimpleCalendarAdminNotice

Admin notice.

Defined (1)

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

/includes/admin/notice.php  
  1. class Notice { 
  2.  
  3. /** 
  4. * Notice id. 
  5. * Will be the notice key in saved notices option. 
  6. * @access public 
  7. * @var string|array 
  8. */ 
  9. public $id = ''; 
  10.  
  11. /** 
  12. * Notice type 
  13. * Gives the notice a CSS class. 
  14. * @access public 
  15. * @var string notice|error|updated|update-nag 
  16. */ 
  17. public $type = ''; 
  18.  
  19. /** 
  20. * Additional classes. 
  21. * @access public 
  22. * @var string 
  23. */ 
  24. public $class = ''; 
  25.  
  26. /** 
  27. * To which users the notice should be shown. 
  28. * If not set, will be visible to all users. 
  29. * @access public 
  30. * @var string 
  31. */ 
  32. public $capability = ''; 
  33.  
  34. /** 
  35. * In which screen the notice should appear. 
  36. * If not set, will appear in every dashboard page/screen. 
  37. * @access public 
  38. * @var array 
  39. */ 
  40. public $screen = array(); 
  41.  
  42. /** 
  43. * For which posts edit screens the notice should appear. 
  44. * If not set, will fallback on $screen rule only. 
  45. * @access public 
  46. * @var array 
  47. */ 
  48. public $post = array(); 
  49.  
  50. /** 
  51. * Can the notice be dismissed by the user? 
  52. * If false, you need to set up a dismissal event. 
  53. * @access public 
  54. * @var bool 
  55. */ 
  56. public $dismissible = true; 
  57.  
  58. /** 
  59. * Whether to hide notice while keeping it stored. 
  60. * If false, will keep the notice in option without showing it. 
  61. * @access public 
  62. * @var bool 
  63. */ 
  64. public $visible = true; 
  65.  
  66. /** 
  67. * The notice content. 
  68. * Supports html. You would normally wrap this in paragraph tags. 
  69. * @access public 
  70. * @var string 
  71. */ 
  72. public $content = ''; 
  73.  
  74. /** 
  75. * Make a notice. 
  76. * @since 3.0.0 
  77. * @param array $notice 
  78. */ 
  79. public function __construct( $notice ) { 
  80.  
  81. if ( ! empty( $notice['id'] ) && ! empty( $notice['content'] ) ) { 
  82.  
  83. // Content. 
  84. $this->id = is_array( $notice['id'] ) ? array_map( 'sanitize_key', $notice['id'] ) : sanitize_key( $notice['id'] ); 
  85. $this->content = wp_kses_post( $notice['content'] ); 
  86. if ( ! empty( $notice['class'] ) ) { 
  87. $this->class = is_array( $notice['class'] ) ? join( ' ', array_map( 'esc_attr', $notice['class'] ) ) : esc_attr( $notice['class'] ); 
  88.  
  89. // Type. 
  90. $default = 'notice'; 
  91. $type = isset( $notice['type'] ) ? esc_attr( $notice['type'] ) : $default; 
  92. $types = array( 
  93. 'error',  
  94. 'notice',  
  95. 'updated',  
  96. 'update-nag',  
  97. ); 
  98. $this->type = in_array( $type, $types ) ? $type : $default; 
  99.  
  100. // Visibility. 
  101. if ( ! empty( $notice['capability'] ) ) { 
  102. $this->capability = esc_attr( $notice['capability'] ); 
  103. if ( ! empty( $notice['screen'] ) ) { 
  104. $this->screen = is_array( $notice['screen'] ) ? array_map( 'esc_attr', $notice['screens'] ) : array( esc_attr( $notice['screen'] ) ); 
  105. if ( ! empty( $notice['post'] ) ) { 
  106. $this->post = is_array( $notice['post'] ) ? array_map( 'intval', $notice['post'] ) : array( intval( $notice['post'] ) ); 
  107. if ( ! empty( $notice['dismissible'] ) ) { 
  108. $this->dismissible = $notice['dismissible'] === false ? false: true; 
  109. if ( ! empty( $notice['visible'] ) ) { 
  110. $this->visible = $notice['visible'] === false ? false: true; 
  111.  
  112.  
  113. /** 
  114. * Add the notice. 
  115. * @since 3.0.0 
  116. */ 
  117. public function add() { 
  118. if ( ! empty( $this->id ) && ! empty( $this->content ) ) { 
  119. $notices = get_option( 'simple-calendar_admin_notices', array() ); 
  120. if ( is_array( $this->id ) ) { 
  121. foreach ( $this->id as $k => $v ) { 
  122. $notices[ $k ][ $v ] = $this; 
  123. } else { 
  124. $notices[ $this->id ][] = $this; 
  125. update_option( 'simple-calendar_admin_notices', $notices ); 
  126.  
  127. /** 
  128. * Remove the notice. 
  129. * @since 3.0.0 
  130. */ 
  131. public function remove() { 
  132. if ( ! empty( $this->id ) && ! empty( $this->content ) ) { 
  133. $notices = get_option( 'simple-calendar_admin_notices', array() ); 
  134. if ( is_array( $this->id ) ) { 
  135. foreach ( $this->id as $k => $v ) { 
  136. unset( $notices[ $k ] ); 
  137. } else { 
  138. unset( $notices[ $this->id ] ); 
  139. update_option( 'simple-calendar_admin_notices', $notices ); 
  140.