PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/Admin/Notices.php

https://bitbucket.org/djmdigital/total-auto-care-wordpress
PHP | 273 lines | 182 code | 51 blank | 40 comment | 67 complexity | 7f674ee1afa0d80bcd09e04aeb8f6206 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, 0BSD
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * NF_Notices Class
  4. *
  5. * Can be simply used be adding another line into the nf_admin_notices() function under notices.php
  6. *
  7. * Can be extended to create more advanced notices to include triggered events
  8. *
  9. * @since 2.9
  10. */
  11. class NF_Admin_Notices
  12. {
  13. // Highlander the instance
  14. static $instance;
  15. public static function instance()
  16. {
  17. if ( ! isset( self::$instance ) ) {
  18. self::$instance = new NF_Notices();
  19. }
  20. return self::$instance;
  21. }
  22. public $notice_spam = 0;
  23. public $notice_spam_max = 1;
  24. // Basic actions to run
  25. public function __construct(){
  26. // Runs the admin notice ignore function incase a dismiss button has been clicked
  27. add_action( 'admin_init', array( $this, 'admin_notice_ignore' ) );
  28. // Runs the admin notice temp ignore function incase a temp dismiss link has been clicked
  29. add_action( 'admin_init', array( $this, 'admin_notice_temp_ignore' ) );
  30. }
  31. // Checks to ensure notices aren't disabled and the user has the correct permissions.
  32. public function nf_admin_notice() {
  33. $nf_settings = get_option( 'ninja_forms_settings' );
  34. if ( ! isset( $nf_settings[ 'disable_admin_notices' ] ) || ( isset( $nf_settings[ 'disable_admin_notices' ] ) && $nf_settings[ 'disable_admin_notices' ] == 0 ) ){
  35. if ( current_user_can( apply_filters( 'ninja_forms_admin_parent_menu_capabilities', 'manage_options' ) ) ) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. // Primary notice function that can be called from an outside function sending necessary variables
  42. public function admin_notice( $admin_notices ) {
  43. // Check options
  44. if ( ! $this->nf_admin_notice() ) {
  45. return false;
  46. }
  47. foreach ( $admin_notices as $slug => $admin_notice ) {
  48. if ( isset ( $admin_notice[ 'ignore_spam' ] ) && true == $admin_notice[ 'ignore_spam' ] ) {
  49. $ignore_spam = true;
  50. } else {
  51. $ignore_spam = false;
  52. }
  53. // Call for spam protection
  54. if ( ! $ignore_spam && $this->anti_notice_spam() ) {
  55. continue;
  56. }
  57. // Check for proper page to display on
  58. if ( isset( $admin_notices[ $slug ][ 'pages' ] ) && is_array( $admin_notices[ $slug ][ 'pages' ] )
  59. || isset( $admin_notices[ $slug ][ 'blacklist' ] ) && is_array( $admin_notices[ $slug ][ 'blacklist' ] )
  60. ) {
  61. if( ( isset( $admin_notices[ $slug ][ 'blacklist' ] ) && $this->admin_notice_pages_blacklist( $admin_notices[ $slug ][ 'blacklist' ] ) )
  62. || ( isset( $admin_notices[ $slug ][ 'pages' ] ) && ! $this->admin_notice_pages( $admin_notices[ $slug ][ 'pages' ] ) )
  63. ) {
  64. continue;
  65. }
  66. }
  67. // Check for required fields
  68. if ( ! $this->required_fields( $admin_notices[ $slug ] ) ) {
  69. // Get the current date then set start date to either passed value or current date value and add interval
  70. $current_date = current_time( "n/j/Y" );
  71. $start = ( isset( $admin_notices[ $slug ][ 'start' ] ) ? $admin_notices[ $slug ][ 'start' ] : $current_date );
  72. $start = date( "n/j/Y", strtotime( $start ) );
  73. $date_array = explode( '/', $start );
  74. $interval = ( isset( $admin_notices[ $slug ][ 'int' ] ) ? $admin_notices[ $slug ][ 'int' ] : 0 );
  75. $date_array[1] += $interval;
  76. $start = date( "n/j/Y", mktime( 0, 0, 0, $date_array[0], $date_array[1], $date_array[2] ) );
  77. // This is the main notices storage option
  78. $admin_notices_option = get_option( 'nf_admin_notice', array() );
  79. // Check if the message is already stored and if so just grab the key otherwise store the message and its associated date information
  80. if ( ! array_key_exists( $slug, $admin_notices_option ) ) {
  81. $admin_notices_option[ $slug ][ 'start' ] = $start;
  82. $admin_notices_option[ $slug ][ 'int' ] = $interval;
  83. update_option( 'nf_admin_notice', $admin_notices_option );
  84. }
  85. // Sanity check to ensure we have accurate information
  86. // New date information will not overwrite old date information
  87. $admin_display_check = ( isset( $admin_notices_option[ $slug ][ 'dismissed' ] ) ? $admin_notices_option[ $slug ][ 'dismissed'] : 0 );
  88. $admin_display_start = ( isset( $admin_notices_option[ $slug ][ 'start' ] ) ? $admin_notices_option[ $slug ][ 'start'] : $start );
  89. $admin_display_interval = ( isset( $admin_notices_option[ $slug ][ 'int' ] ) ? $admin_notices_option[ $slug ][ 'int'] : $interval );
  90. $admin_display_msg = ( isset( $admin_notices[ $slug ][ 'msg' ] ) ? $admin_notices[ $slug ][ 'msg'] : '' );
  91. $admin_display_title = ( isset( $admin_notices[ $slug ][ 'title' ] ) ? $admin_notices[ $slug ][ 'title'] : '' );
  92. $admin_display_link = ( isset( $admin_notices[ $slug ][ 'link' ] ) ? $admin_notices[ $slug ][ 'link' ] : '' );
  93. $admin_can_dismiss = ( isset( $admin_notices[ $slug ][ 'dismiss' ] ) ? $admin_notices[ $slug ][ 'dismiss' ] : 1 );
  94. $output_css = false;
  95. // Ensure the notice hasn't been hidden and that the current date is after the start date
  96. if ( $admin_display_check == 0 && strtotime( $admin_display_start ) <= strtotime( $current_date ) ) {
  97. // Get remaining query string
  98. $query_str = esc_url( add_query_arg( 'nf_admin_notice_ignore', $slug ) );
  99. // Admin notice display output
  100. echo '<div class="update-nag nf-admin-notice">';
  101. echo '<div class="nf-notice-logo"></div>';
  102. echo ' <p class="nf-notice-title">';
  103. echo esc_html( $admin_display_title );
  104. echo ' </p>';
  105. echo ' <p class="nf-notice-body">';
  106. echo $admin_display_msg;
  107. echo ' </p>';
  108. echo '<ul class="nf-notice-body nf-red">
  109. ' . $admin_display_link . '
  110. </ul>';
  111. if ( $admin_can_dismiss ) {
  112. echo '<a href="' . wp_nonce_url( esc_attr( $query_str ) ) . '" class="dashicons dashicons-dismiss"></a>';
  113. }
  114. echo '</div>';
  115. $this->notice_spam += 1;
  116. $output_css = true;
  117. }
  118. if ( $output_css ) {
  119. wp_enqueue_style( 'nf-admin-notices', Ninja_Forms::$url .'assets/css/admin-notices.css?nf_ver=' . Ninja_Forms::VERSION );
  120. }
  121. }
  122. }
  123. // die( 'done looping' );
  124. }
  125. // Spam protection check
  126. public function anti_notice_spam() {
  127. if ( $this->notice_spam >= $this->notice_spam_max ) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. // Ignore function that gets ran at admin init to ensure any messages that were dismissed get marked
  133. public function admin_notice_ignore() {
  134. // If user clicks to ignore the notice, update the option to not show it again
  135. if ( isset($_GET['nf_admin_notice_ignore']) && current_user_can( apply_filters( 'ninja_forms_admin_parent_menu_capabilities', 'manage_options' ) ) ) {
  136. if ( ! check_admin_referer() ) {
  137. $query_str = remove_query_arg( array( 'nf_admin_notice_ignore', '_wpnonce' ) );
  138. wp_safe_redirect( $query_str );
  139. exit;
  140. }
  141. $admin_notices_option = get_option( 'nf_admin_notice', array() );
  142. $admin_notices_option[ WPN_Helper::sanitize_text_field($_GET[ 'nf_admin_notice_ignore' ]) ][ 'dismissed' ] = 1;
  143. update_option( 'nf_admin_notice', $admin_notices_option );
  144. $query_str = remove_query_arg( array( 'nf_admin_notice_ignore', '_wpnonce' ) );
  145. wp_safe_redirect( $query_str );
  146. exit;
  147. }
  148. }
  149. // Temp Ignore function that gets ran at admin init to ensure any messages that were temp dismissed get their start date changed
  150. public function admin_notice_temp_ignore() {
  151. // If user clicks to temp ignore the notice, update the option to change the start date - default interval of 14 days
  152. if ( isset($_GET['nf_admin_notice_temp_ignore']) && current_user_can( apply_filters( 'ninja_forms_admin_parent_menu_capabilities', 'manage_options' ) ) ) {
  153. $admin_notices_option = get_option( 'nf_admin_notice', array() );
  154. $current_date = current_time( "n/j/Y" );
  155. $date_array = explode( '/', $current_date );
  156. $interval = ( isset( $_GET[ 'nf_int' ] ) ? intval($_GET[ 'nf_int' ]) : 14 );
  157. $date_array[1] += $interval;
  158. $new_start = date( "n/j/Y", mktime( 0, 0, 0, $date_array[0], $date_array[1], $date_array[2] ) );
  159. $admin_notices_option[ WPN_Helper::sanitize_text_field($_GET[ 'nf_admin_notice_temp_ignore' ]) ][ 'start' ] = $new_start;
  160. $admin_notices_option[ WPN_Helper::sanitize_text_field($_GET[ 'nf_admin_notice_temp_ignore' ]) ][ 'dismissed' ] = 0;
  161. update_option( 'nf_admin_notice', $admin_notices_option );
  162. $query_str = remove_query_arg( array( 'nf_admin_notice_temp_ignore', 'nf_int' ) );
  163. wp_redirect( $query_str );
  164. exit;
  165. }
  166. }
  167. public function admin_notice_pages_blacklist( $pages ) {
  168. foreach( $pages as $key => $page ) {
  169. if ( is_array( $page ) ) {
  170. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page[0] && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == $page[1] ) {
  171. return true;
  172. }
  173. } else {
  174. if ( get_current_screen()->id === $page ) {
  175. return true;
  176. }
  177. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page ) {
  178. return true;
  179. }
  180. }
  181. }
  182. return false;
  183. }
  184. // Page check function - This should be called from class extensions if the notice should only show on specific admin pages
  185. // Expects an array in the form of IE: array( 'dashboard', 'ninja-forms', array( 'ninja-forms', 'builder' ) )
  186. // Function accepts dashboard as a special check and also whatever is passed to page or tab as parameters
  187. // The above example will display on dashboard and all of the pages that have page=ninja-forms and any page=ninja-forms&tab=builder which is redundant in the example
  188. public function admin_notice_pages( $pages ) {
  189. foreach( $pages as $key => $page ) {
  190. if ( is_array( $page ) ) {
  191. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page[0] && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == $page[1] ) {
  192. return true;
  193. }
  194. } else {
  195. if ( $page == 'all' ) {
  196. return true;
  197. }
  198. if ( get_current_screen()->id === $page ) {
  199. return true;
  200. }
  201. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page ) {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. // Required fields check
  209. public function required_fields( $fields ) {
  210. if ( ! isset( $fields[ 'msg' ] ) || ( isset( $fields[ 'msg' ] ) && empty( $fields[ 'msg' ] ) ) ) {
  211. return true;
  212. }
  213. if ( ! isset( $fields[ 'title' ] ) || ( isset( $fields[ 'title' ] ) && empty( $fields[ 'title' ] ) ) ) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. // Special parameters function that is to be used in any extension of this class
  219. public function special_parameters( $admin_notices ) {
  220. // Intentionally left blank
  221. }
  222. }