PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ReduxCore/inc/class.redux_admin_notices.php

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