/library/admin/redux-framework/inc/class.redux_admin_notices.php

  1. <?php 
  2.  
  3. /** 
  4. * Redux Framework Admin Notice Class 
  5. * Makes instantiating a Redux object an absolute piece of cake. 
  6. * 
  7. * @package Redux_Framework 
  8. * @author Kevin Provance 
  9. * @author Dovy Paukstys 
  10. * @subpackage Core 
  11. */ 
  12.  
  13. // Exit if accessed directly 
  14. if ( ! defined( 'ABSPATH' ) ) { 
  15. exit; 
  16.  
  17. // Don't duplicate me! 
  18. if ( ! class_exists( 'Redux_Admin_Notices' ) ) { 
  19.  
  20. /** 
  21. * Redux API Class 
  22. * Simple API for Redux Framework 
  23. * 
  24. * @since 1.0.0 
  25. */ 
  26. class Redux_Admin_Notices { 
  27.  
  28. static public $_parent; 
  29.  
  30. public static function load() { 
  31. add_action( 'wp_ajax_redux_hide_admin_notice', array( 
  32. 'Redux_Admin_Notices',  
  33. 'dismissAdminNoticeAJAX' 
  34. ) ); 
  35.  
  36. /** 
  37. * adminNotices - Evaluates user dismiss option for displaying admin notices 
  38. * 
  39. * @since 3.2.0 
  40. * @access public 
  41. * @return void 
  42. */ 
  43. public static function adminNotices( $notices = array() ) { 
  44. global $current_user, $pagenow; 
  45.  
  46. // Check for an active admin notice array 
  47. if ( ! empty( $notices ) ) { 
  48.  
  49. // Enum admin notices 
  50. foreach ( $notices as $notice ) { 
  51.  
  52. $add_style = ''; 
  53. if ( strpos( $notice['type'], 'redux-message' ) != false ) { 
  54. $add_style = 'style="border-left: 4px solid ' . esc_attr( $notice['color'] ) . '!important;"'; 
  55.  
  56. if ( true == $notice['dismiss'] ) { 
  57.  
  58. // Get user ID 
  59. $userid = $current_user->ID; 
  60.  
  61. if ( ! get_user_meta( $userid, 'ignore_' . $notice['id'] ) ) { 
  62.  
  63. // Check if we are on admin.php. If we are, we have 
  64. // to get the current page slug and tab, so we can 
  65. // feed it back to WordPress. Why> admin.php cannot 
  66. // be accessed without the page parameter. We add the 
  67. // tab to return the user to the last panel they were 
  68. // on. 
  69. $pageName = ''; 
  70. $curTab = ''; 
  71. if ( $pagenow == 'admin.php' || $pagenow == 'themes.php' ) { 
  72.  
  73. // Get the current page. To avoid errors, we'll set 
  74. // the redux page slug if the GET is empty. 
  75. $pageName = empty( $_GET['page'] ) ? '&page=' . self::$_parent->args['page_slug'] : '&page=' . esc_attr( $_GET['page'] ); 
  76.  
  77. // Ditto for the current tab. 
  78. $curTab = empty( $_GET['tab'] ) ? '&tab=0' : '&tab=' . esc_attr( $_GET['tab'] ); 
  79.  
  80. global $wp_version; 
  81. // Print the notice with the dismiss link 
  82. if ( version_compare( $wp_version, '4.2', '>' ) ) { 
  83. $output = ""; 
  84. $css_id = esc_attr( $notice['id'] ) . $pageName . $curTab; 
  85. $css_class = esc_attr( $notice['type'] ) . ' redux-notice notice is-dismissible redux-notice'; 
  86. $output .= "<div {$add_style} id='$css_id' class='$css_class'> \n"; 
  87. $nonce = wp_create_nonce( $notice['id'] . $userid . 'nonce' ); 
  88. $output .= "<input type='hidden' class='dismiss_data' id='" . esc_attr( $notice['id'] ) . $pageName . $curTab . "' value='{$nonce}'> \n"; 
  89. $output .= '<p>' . wp_kses_post( $notice['msg'] ) . '</p>'; 
  90. $output .= "</div> \n"; 
  91. echo $output; 
  92. } else { 
  93. echo '<div ' . $add_style . ' class="' . esc_attr( $notice['type'] ) . ' notice is-dismissable"><p>' . wp_kses_post( $notice['msg'] ) . '  <a href="?dismiss=true&id=' . esc_attr( $notice['id'] ) . $pageName . $curTab . '">' . esc_html__( 'Dismiss', 'evolve' ) . '</a>.</p></div>'; 
  94. } else { 
  95. // Standard notice 
  96. echo '<div ' . $add_style . ' class="' . esc_attr( $notice['type'] ) . ' notice"><p>' . wp_kses_post( $notice['msg'] ) . '</a>.</p></div>'; 
  97. ?> 
  98. <script> 
  99. jQuery( document ).ready( 
  100. function( $ ) { 
  101. $( 'body' ).on( 
  102. 'click', '.redux-notice.is-dismissible .notice-dismiss', function( event ) { 
  103. var $data = $( this ).parent().find( '.dismiss_data' ); 
  104. $.post( 
  105. ajaxurl, { 
  106. action: 'redux_hide_admin_notice',  
  107. id: $data.attr( 'id' ),  
  108. nonce: $data.val() 
  109. ); 
  110. ); 
  111. ); 
  112. </script> 
  113. <?php 
  114. /** 
  115.   
  116. */ 
  117.  
  118.  
  119.  
  120. // Clear the admin notice array 
  121. self::$_parent->admin_notices = array(); 
  122.  
  123. /** 
  124. * dismissAdminNotice - Updates user meta to store dismiss notice preference 
  125. * 
  126. * @since 3.2.0 
  127. * @access public 
  128. * @return void 
  129. */ 
  130. public static function dismissAdminNotice() { 
  131. global $current_user; 
  132.  
  133. // Verify the dismiss and id parameters are present. 
  134. if ( isset( $_GET['dismiss'] ) && isset( $_GET['id'] ) ) { 
  135. if ( 'true' == $_GET['dismiss'] || 'false' == $_GET['dismiss'] ) { 
  136.  
  137. // Get the user id 
  138. $userid = $current_user->ID; 
  139.  
  140. // Get the notice id 
  141. $id = esc_attr( $_GET['id'] ); 
  142. $val = esc_attr( $_GET['dismiss'] ); 
  143.  
  144. // Add the dismiss request to the user meta. 
  145. update_user_meta( $userid, 'ignore_' . $id, $val ); 
  146.  
  147. /** 
  148. * dismissAdminNotice - Updates user meta to store dismiss notice preference 
  149. * 
  150. * @since 3.2.0 
  151. * @access public 
  152. * @return void 
  153. */ 
  154. public static function dismissAdminNoticeAJAX() { 
  155. global $current_user; 
  156.  
  157. // Get the notice id 
  158. $id = explode( '&', $_POST['id'] ); 
  159. $id = $id[0]; 
  160. // Get the user id 
  161. $userid = $current_user->ID; 
  162.  
  163. if ( ! wp_verify_nonce( $_POST['nonce'], $id . $userid . 'nonce' ) ) { 
  164. die( 0 ); 
  165. } else { 
  166. // Add the dismiss request to the user meta. 
  167. update_user_meta( $userid, 'ignore_' . $id, true ); 
  168.  
  169. Redux_Admin_Notices::load(); 
.