/deprecated/classes/notices-class.php

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