NF_Notices

NF_Notices Class.

Defined (1)

The class is defined in the following location(s).

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