Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. // class found here: https://www.alexgeorgiou.gr/persistently-dismissible-notices-wordpress/ edited slightly due to issues
  2. // 1. The code was just doing zerobscrm_dismiss_1 (rather than zerobscrm_dismiss_name) which meant dismissing one notice dismissed them all
  3. // 2. Added transients for temporary dismissible notice (hard coded for just one variable (for now) can expand if need be
  4.  
  5. if ( ! class_exists( 'zeroBSCRM_Admin_Notices' ) ) {
  6.  
  7. class zeroBSCRM_Admin_Notices {
  8.  
  9. private static $_instance;
  10. private $admin_notices;
  11. const TYPES = 'error,warning,info,success';
  12.  
  13. private function __construct() {
  14. $this->admin_notices = new stdClass();
  15. foreach ( explode( ',', self::TYPES ) as $type ) {
  16. $this->admin_notices->{$type} = array();
  17. }
  18. add_action( 'admin_init', array( &$this, 'action_admin_init' ) );
  19. add_action( 'admin_notices', array( &$this, 'action_admin_notices' ) );
  20. add_action( 'admin_enqueue_scripts', array( &$this, 'action_admin_enqueue_scripts' ) );
  21. }
  22.  
  23. public static function get_instance() {
  24. if ( ! ( self::$_instance instanceof self ) ) {
  25. self::$_instance = new self();
  26. }
  27. return self::$_instance;
  28. }
  29.  
  30. public function action_admin_init() {
  31. $dismiss_name = filter_input( INPUT_GET, 'zerobscrm_dismiss', FILTER_SANITIZE_STRING );
  32. if ( is_string( $dismiss_name ) ) {
  33. if($dismiss_name == 'one-or-more'){
  34. //then we are in our plugin update check. Let them dismiss it. But also want to return it every month
  35. set_transient( "zerobscrm_dismissed_$dismiss_name", true, 30 * 24 * HOUR_IN_SECONDS);
  36. }else{
  37. update_option( "zerobscrm_dismissed_$dismiss_name", true );
  38. }
  39.  
  40. wp_die();
  41. }
  42. }
  43.  
  44. public function action_admin_enqueue_scripts() {
  45. wp_enqueue_script( 'jquery' );
  46. wp_enqueue_script(
  47. 'zerobscrm-notify',
  48. plugins_url( 'js/ZeroBSCRM.admin.notify.js', __FILE__ ),
  49. array( 'jquery' )
  50. );
  51. }
  52.  
  53. public function action_admin_notices() {
  54. foreach ( explode( ',', self::TYPES ) as $type ) {
  55. foreach ( $this->admin_notices->{$type} as $admin_notice ) {
  56.  
  57. $zbs_option = sanitize_title($admin_notice->dismiss_name); //using the name passed but turned from Sales Dashboard to sales-dashboard
  58.  
  59. $dismiss_url = add_query_arg( array(
  60. 'zerobscrm_dismiss' => sanitize_title($admin_notice->dismiss_name)
  61. ), admin_url() );
  62.  
  63. //edited to check transient too
  64. if ( ! get_option( "zerobscrm_dismissed_{$zbs_option}" ) || !get_transient("zerobscrm_dismissed_{$zbs_option}") ) {
  65. ?><div
  66. class="ui message <?php echo $type; ?> notice zerobscrm-notice notice-<?php echo $type;
  67.  
  68. if ( $admin_notice->dismiss_option ) {
  69. echo ' is-dismissible" data-dismiss-url="' . esc_url( $dismiss_url );
  70. } ?>">
  71. <p><?php echo $admin_notice->message; ?></p>
  72.  
  73. </div><?php
  74. }
  75. }
  76. }
  77. }
  78.  
  79. public function error( $message, $dismiss_option = false, $dismiss_name) {
  80. $this->notice( 'error', $message, $dismiss_option, $dismiss_name );
  81. }
  82.  
  83. public function warning( $message, $dismiss_option = false, $dismiss_name) {
  84. $this->notice( 'warning', $message, $dismiss_option, $dismiss_name );
  85. }
  86.  
  87. public function success( $message, $dismiss_option = false, $dismiss_name) {
  88. $this->notice( 'success', $message, $dismiss_option, $dismiss_name );
  89. }
  90.  
  91. public function info( $message, $dismiss_option = false, $dismiss_name) {
  92. $this->notice( 'info', $message, $dismiss_option, $dismiss_name );
  93. }
  94.  
  95. private function notice( $type, $message, $dismiss_option, $dismiss_name ) {
  96. $notice = new stdClass();
  97. $notice->message = $message;
  98. $notice->dismiss_option = $dismiss_option;
  99. $notice->dismiss_name = $dismiss_name;
  100.  
  101. $this->admin_notices->{$type}[] = $notice;
  102. }
  103.  
  104. public static function error_handler( $errno, $errstr, $errfile, $errline, $errcontext ) {
  105. if ( ! ( error_reporting() & $errno ) ) {
  106. // This error code is not included in error_reporting
  107. return;
  108. }
  109.  
  110. $message = "errstr: $errstr, errfile: $errfile, errline: $errline, PHP: " . PHP_VERSION . " OS: " . PHP_OS;
  111.  
  112. $self = self::get_instance();
  113.  
  114. switch ($errno) {
  115. case E_USER_ERROR:
  116. $self->error( $message );
  117. break;
  118.  
  119. case E_USER_WARNING:
  120. $self->warning( $message );
  121. break;
  122.  
  123. case E_USER_NOTICE:
  124. default:
  125. $self->notice( $message );
  126. break;
  127. }
  128.  
  129. // write to wp-content/debug.log if logging enabled
  130. error_log( $message );
  131.  
  132. // Don't execute PHP internal error handler
  133. return true;
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement