class-admin-notices.php 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class Booked_WC_Notice {
  3. protected $notice_text = '';
  4. protected $notice_type = 'updated'; // updated || error || notice-warning
  5. private function __construct() {
  6. }
  7. public static function add_notice($notice_text='', $type='updated') {
  8. $notice = new self();
  9. $notice->notice_text = $notice_text;
  10. $notice->notice_type = $type;
  11. $notice->_add_notice();
  12. }
  13. protected function _add_notice() {
  14. $this->check_notice_type();
  15. if ( $this->notice_text ) {
  16. add_action('admin_notices', array($this, 'print_notice'));
  17. }
  18. }
  19. protected function check_notice_type() {
  20. if ( !in_array($this->notice_type, array('updated', 'error', 'notice-warning')) ) {
  21. $this->notice_type = 'updated';
  22. }
  23. return $this;
  24. }
  25. public function print_notice() {
  26. $class = $this->notice_type;
  27. if ( $class==='notice-warning' ) {
  28. $class .= ' notice';
  29. }
  30. echo '<div class="' . esc_attr($class) . '"><p>' . $this->notice_text . '</p></div>';
  31. }
  32. }