notices-class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_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. // Call for spam protection
  49. if ( $this->anti_notice_spam() ) {
  50. return false;
  51. }
  52. // Check for proper page to display on
  53. if ( isset( $admin_notices[ $slug ][ 'pages' ] ) && is_array( $admin_notices[ $slug ][ 'pages' ] )
  54. || isset( $admin_notices[ $slug ][ 'blacklist' ] ) && is_array( $admin_notices[ $slug ][ 'blacklist' ] )
  55. ) {
  56. if( ( isset( $admin_notices[ $slug ][ 'blacklist' ] ) && $this->admin_notice_pages_blacklist( $admin_notices[ $slug ][ 'blacklist' ] ) )
  57. || ( isset( $admin_notices[ $slug ][ 'pages' ] ) && ! $this->admin_notice_pages( $admin_notices[ $slug ][ 'pages' ] ) )
  58. ) {
  59. return false;
  60. }
  61. }
  62. // Check for required fields
  63. if ( ! $this->required_fields( $admin_notices[ $slug ] ) ) {
  64. // Get the current date then set start date to either passed value or current date value and add interval
  65. $current_date = current_time( "n/j/Y" );
  66. $start = ( isset( $admin_notices[ $slug ][ 'start' ] ) ? $admin_notices[ $slug ][ 'start' ] : $current_date );
  67. $start = date( "n/j/Y", strtotime( $start ) );
  68. $date_array = explode( '/', $start );
  69. $interval = ( isset( $admin_notices[ $slug ][ 'int' ] ) ? $admin_notices[ $slug ][ 'int' ] : 0 );
  70. $date_array[1] += $interval;
  71. $start = date( "n/j/Y", mktime( 0, 0, 0, $date_array[0], $date_array[1], $date_array[2] ) );
  72. // This is the main notices storage option
  73. $admin_notices_option = get_option( 'nf_admin_notice', array() );
  74. // Check if the message is already stored and if so just grab the key otherwise store the message and its associated date information
  75. if ( ! array_key_exists( $slug, $admin_notices_option ) ) {
  76. $admin_notices_option[ $slug ][ 'start' ] = $start;
  77. $admin_notices_option[ $slug ][ 'int' ] = $interval;
  78. update_option( 'nf_admin_notice', $admin_notices_option );
  79. }
  80. // Sanity check to ensure we have accurate information
  81. // New date information will not overwrite old date information
  82. $admin_display_check = ( isset( $admin_notices_option[ $slug ][ 'dismissed' ] ) ? $admin_notices_option[ $slug ][ 'dismissed'] : 0 );
  83. $admin_display_start = ( isset( $admin_notices_option[ $slug ][ 'start' ] ) ? $admin_notices_option[ $slug ][ 'start'] : $start );
  84. $admin_display_interval = ( isset( $admin_notices_option[ $slug ][ 'int' ] ) ? $admin_notices_option[ $slug ][ 'int'] : $interval );
  85. $admin_display_msg = ( isset( $admin_notices[ $slug ][ 'msg' ] ) ? $admin_notices[ $slug ][ 'msg'] : '' );
  86. $admin_display_title = ( isset( $admin_notices[ $slug ][ 'title' ] ) ? $admin_notices[ $slug ][ 'title'] : '' );
  87. $admin_display_link = ( isset( $admin_notices[ $slug ][ 'link' ] ) ? $admin_notices[ $slug ][ 'link' ] : '' );
  88. $output_css = false;
  89. // Ensure the notice hasn't been hidden and that the current date is after the start date
  90. if ( $admin_display_check == 0 && strtotime( $admin_display_start ) <= strtotime( $current_date ) ) {
  91. // Get remaining query string
  92. $query_str = esc_url( add_query_arg( 'nf_admin_notice_ignore', $slug ) );
  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. $this->notice_spam += 1;
  108. $output_css = true;
  109. }
  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. }
  114. }
  115. }
  116. // Spam protection check
  117. public function anti_notice_spam() {
  118. if ( $this->notice_spam >= $this->notice_spam_max ) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. // Ignore function that gets ran at admin init to ensure any messages that were dismissed get marked
  124. public function admin_notice_ignore() {
  125. // If user clicks to ignore the notice, update the option to not show it again
  126. if ( isset($_GET['nf_admin_notice_ignore']) && current_user_can( apply_filters( 'ninja_forms_admin_parent_menu_capabilities', 'manage_options' ) ) ) {
  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. }
  135. // Temp Ignore function that gets ran at admin init to ensure any messages that were temp dismissed get their start date changed
  136. public function admin_notice_temp_ignore() {
  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. $admin_notices_option = get_option( 'nf_admin_notice', array() );
  140. $current_date = current_time( "n/j/Y" );
  141. $date_array = explode( '/', $current_date );
  142. $interval = ( isset( $_GET[ 'nf_int' ] ) ? $_GET[ 'nf_int' ] : 14 );
  143. $date_array[1] += $interval;
  144. $new_start = date( "n/j/Y", mktime( 0, 0, 0, $date_array[0], $date_array[1], $date_array[2] ) );
  145. $admin_notices_option[ $_GET[ 'nf_admin_notice_temp_ignore' ] ][ 'start' ] = $new_start;
  146. $admin_notices_option[ $_GET[ 'nf_admin_notice_temp_ignore' ] ][ 'dismissed' ] = 0;
  147. update_option( 'nf_admin_notice', $admin_notices_option );
  148. $query_str = remove_query_arg( array( 'nf_admin_notice_temp_ignore', 'nf_int' ) );
  149. wp_redirect( $query_str );
  150. exit;
  151. }
  152. }
  153. public function admin_notice_pages_blacklist( $pages ) {
  154. foreach( $pages as $key => $page ) {
  155. if ( is_array( $page ) ) {
  156. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page[0] && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == $page[1] ) {
  157. return true;
  158. }
  159. } else {
  160. if ( get_current_screen()->id === $page ) {
  161. return true;
  162. }
  163. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page ) {
  164. return true;
  165. }
  166. }
  167. }
  168. return false;
  169. }
  170. // Page check function - This should be called from class extensions if the notice should only show on specific admin pages
  171. // Expects an array in the form of IE: array( 'dashboard', 'ninja-forms', array( 'ninja-forms', 'builder' ) )
  172. // Function accepts dashboard as a special check and also whatever is passed to page or tab as parameters
  173. // 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
  174. public function admin_notice_pages( $pages ) {
  175. foreach( $pages as $key => $page ) {
  176. if ( is_array( $page ) ) {
  177. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page[0] && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == $page[1] ) {
  178. return true;
  179. }
  180. } else {
  181. if ( $page == 'all' ) {
  182. return true;
  183. }
  184. if ( get_current_screen()->id === $page ) {
  185. return true;
  186. }
  187. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page'] == $page ) {
  188. return true;
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. // Required fields check
  195. public function required_fields( $fields ) {
  196. if ( ! isset( $fields[ 'msg' ] ) || ( isset( $fields[ 'msg' ] ) && empty( $fields[ 'msg' ] ) ) ) {
  197. return true;
  198. }
  199. if ( ! isset( $fields[ 'title' ] ) || ( isset( $fields[ 'title' ] ) && empty( $fields[ 'title' ] ) ) ) {
  200. return true;
  201. }
  202. return false;
  203. }
  204. // Special parameters function that is to be used in any extension of this class
  205. public function special_parameters( $admin_notices ) {
  206. // Intentionally left blank
  207. }
  208. }