GFPDFHelperHelper_Notices

The Gravity PDF GFPDF Helper Notices class.

Defined (1)

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

/src/helper/Helper_Notices.php  
  1. class Helper_Notices implements Helper_Interface_Actions { 
  2.  
  3. /** 
  4. * Holds any notices that we've triggered 
  5. * @var array 
  6. * @since 4.0 
  7. */ 
  8. private $notices = []; 
  9.  
  10. /** 
  11. * Holds any errors that we've triggered 
  12. * @var array 
  13. * @since 4.0 
  14. */ 
  15. private $errors = []; 
  16.  
  17. /** 
  18. * Initialise our class defaults 
  19. * @since 4.0 
  20. * @return void 
  21. */ 
  22. public function init() { 
  23. $this->add_actions(); 
  24.  
  25. /** 
  26. * Apply any actions needed to implement notices 
  27. * @since 4.0 
  28. * @return void 
  29. */ 
  30. public function add_actions() { 
  31. add_action( $this->get_notice_type(), [ $this, 'process' ] ); 
  32.  
  33. /** 
  34. * Determine which notice should be triggered 
  35. * @return string 
  36. * @since 4.0 
  37. */ 
  38. private function get_notice_type() { 
  39. if ( is_multisite() && is_network_admin() ) { 
  40. return 'network_admin_notices'; 
  41.  
  42. return 'admin_notices'; 
  43.  
  44. /** 
  45. * Public endpoint for adding a new notice 
  46. * @param string $notice The message to be queued 
  47. * @param string $class The class that should be included with the notice box 
  48. * @return void 
  49. * @since 4.0 
  50. */ 
  51. public function add_notice( $notice, $class = '' ) { 
  52.  
  53. if ( empty( $class ) ) { 
  54. $this->notices[] = $notice; 
  55. } else { 
  56. $this->notices[ $class ] = $notice; 
  57.  
  58. /** 
  59. * Public endpoint for adding a new notice 
  60. * @param string $error The error message that should be added 
  61. * @param string $class Any class names that should apply to the error 
  62. * @internal param string $notice The message to be queued 
  63. * @since 4.0 
  64. */ 
  65. public function add_error( $error, $class = '' ) { 
  66.  
  67. if ( empty( $class ) ) { 
  68. $this->errors[] = $error; 
  69. } else { 
  70. $this->errors[ $class ] = $error; 
  71.  
  72. /** 
  73. * Check if we currently have a notice 
  74. * @return boolean 
  75. * @since 4.0 
  76. */ 
  77. public function has_notice() { 
  78. if ( sizeof( $this->notices ) > 0 ) { 
  79. return true; 
  80.  
  81. return false; 
  82.  
  83. /** 
  84. * Check if we currently have an error 
  85. * @return boolean 
  86. * @since 4.0 
  87. */ 
  88. public function has_error() { 
  89. if ( sizeof( $this->errors ) > 0 ) { 
  90. return true; 
  91.  
  92. return false; 
  93.  
  94. /** 
  95. * Remove all notices / errors 
  96. * @param string $type Switch to remove all messages, errors or just notices. Valid arguments are 'all', 'notices', 'errors' 
  97. * @since 4.0 
  98. */ 
  99. public function clear( $type = 'all' ) { 
  100.  
  101. if ( 'errors' === $type || 'all' === $type ) { 
  102. $this->errors = []; 
  103.  
  104. if ( 'notices' === $type || 'all' === $type ) { 
  105. $this->notices = []; 
  106.  
  107. /** 
  108. * Process our admin notice and error messages 
  109. * @return void 
  110. * @since 4.0 
  111. */ 
  112. public function process() { 
  113. foreach ( $this->notices as $class => $notice ) { 
  114. $include_class = ( ! is_int( $class ) ) ? $class : ''; 
  115. $this->html( $notice, 'updated ' . $include_class ); 
  116.  
  117. foreach ( $this->errors as $class => $error ) { 
  118. $include_class = ( ! is_int( $class ) ) ? $class : ''; 
  119. $this->html( $error, 'error ' . $include_class ); 
  120.  
  121. /** 
  122. * Generate the HTML used to display the notice / error 
  123. * @param string $text The message to be displayed 
  124. * @param string $class The class name (updated / error) 
  125. * @return void 
  126. * @since 4.0 
  127. */ 
  128. private function html( $text, $class = 'updated' ) { 
  129. ?> 
  130. <div class="<?php echo $class; ?> notice"> 
  131. <p><?php echo $text; ?></p> 
  132. </div> 
  133. <?php