NF_Admin_Menus_Settings

The Ninja Forms NF Admin Menus Settings class.

Defined (1)

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

/includes/Admin/Menus/Settings.php  
  1. final class NF_Admin_Menus_Settings extends NF_Abstracts_Submenu 
  2. public $parent_slug = 'ninja-forms'; 
  3.  
  4. public $menu_slug = 'nf-settings'; 
  5.  
  6. public $priority = 11; 
  7.  
  8. protected $_prefix = 'ninja_forms'; 
  9.  
  10. public function __construct() 
  11. parent::__construct(); 
  12.  
  13. if( isset( $_POST[ 'update_ninja_forms_settings' ] ) ) { 
  14. add_action( 'admin_init', array( $this, 'update_settings' ) ); 
  15.  
  16. // Catch Contact Form 7 reCAPTCHA conflict. 
  17. add_action( 'admin_init', array( $this, 'nf_cf7_notice_dismissed' ) ); 
  18. add_action( 'admin_notices', array( $this, 'ninja_forms_cf7_notice' ) ); 
  19. add_action( 'admin_body_class', array( $this, 'body_class' ) ); 
  20.  
  21. public function body_class( $classes ) 
  22. // Add class for the builder. 
  23. if( isset( $_GET['page'] ) && $_GET['page'] == $this->menu_slug ) { 
  24. $classes = "$classes ninja-forms-settings"; 
  25.  
  26. return $classes; 
  27.  
  28. /** 
  29. * Function to notify users of CF7 conflict 
  30. *  
  31. * Since 3.0 
  32. *  
  33. * @return (bool) false on exit 
  34. */ 
  35. public function ninja_forms_cf7_notice() 
  36. // If we don't have recaptcha keys, bail. 
  37. $recaptcha_site_key = Ninja_Forms()->get_settings(); 
  38. if ( $recaptcha_site_key[ 'recaptcha_site_key' ] === '' ) { 
  39. return false; 
  40. // If we can detect Contact Form 7... 
  41. include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  42. if ( is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ) ) { 
  43. $user_id = get_current_user_id(); 
  44. // And if the user has not dimsmissed our notice... 
  45. if ( ! get_user_meta( $user_id, 'nf_cf7_notice_dismissed', true ) ) { 
  46. ?> 
  47. <div class="nf-admin-notice notice"> 
  48. <div class="nf-notice-logo"></div> 
  49. <p class="nf-notice-title"><?php _e( 'Contact Form 7 is currently activated.', 'ninja-forms' ); ?></p> 
  50. <p class="nf-notice-body"><?php _e( 'Please be aware that there is an issue with Contact Form 7 that breaks reCAPTCHA in other plugins.<br />If you need to use reCAPTCHA on any of your Ninja Forms, you will need to disable Contact Form 7.', 'ninja-forms' ); ?></p> 
  51. <a href="<?php print(add_query_arg('nf-cf7-notice-dismissed', 'true')); ?>">Dismiss</a> 
  52. </div> 
  53. <?php 
  54.  
  55. wp_enqueue_style( 'nf-admin-notices', Ninja_Forms::$url .'assets/css/admin-notices.css?nf_ver=' . Ninja_Forms::VERSION ); 
  56.  
  57. /** 
  58. * Function to hide our CF7 conflict notice, once dismissed 
  59. *  
  60. * Since 3.0 
  61. */ 
  62. public function nf_cf7_notice_dismissed() 
  63. $user_id = get_current_user_id(); 
  64. if ( isset( $_GET['nf-cf7-notice-dismissed'] ) ) 
  65. add_user_meta( $user_id, 'nf_cf7_notice_dismissed', 'true', true ); 
  66.  
  67. public function get_page_title() 
  68. return __( 'Settings', 'ninja-forms' ); 
  69.  
  70. public function get_capability() 
  71. return apply_filters( 'ninja_forms_admin_settings_capabilities', $this->capability ); 
  72.  
  73. public function display() 
  74. $tabs = apply_filters( 'ninja_forms_settings_tabs', array( 
  75. 'settings' => __( 'Settings', 'ninja-forms' ),  
  76. 'licenses' => __( 'Licenses', 'ninja-forms' ) 
  77. ); 
  78.  
  79. $tab_keys = array_keys( $tabs ); 
  80. $active_tab = ( isset( $_GET[ 'tab' ] ) ) ? $_GET[ 'tab' ] : reset( $tab_keys ); 
  81.  
  82. wp_enqueue_style( 'nf-admin-settings', Ninja_Forms::$url . 'assets/css/admin-settings.css' ); 
  83.  
  84. $groups = Ninja_Forms()->config( 'PluginSettingsGroups' ); 
  85.  
  86. $grouped_settings = $this->get_settings(); 
  87.  
  88. $save_button_text = __( 'Save Settings', 'ninja-forms' ); 
  89.  
  90. $setting_defaults = Ninja_Forms()->get_settings(); 
  91.  
  92. $errors = array(); 
  93.  
  94. foreach( $grouped_settings as $group => $settings ) { 
  95.  
  96. foreach( $settings as $id => $setting ) { 
  97.  
  98. $value = ( isset( $setting_defaults[ $id ] ) ) ? $setting_defaults[$id] : ''; 
  99.  
  100. $grouped_settings[$group][$id]['id'] = $this->prefix( $grouped_settings[$group][$id]['id'] ); 
  101. $grouped_settings[$group][$id]['value'] = $value; 
  102.  
  103. $grouped_settings[$group][$id] = apply_filters( 'ninja_forms_check_setting_' . $id, $grouped_settings[$group][$id] ); 
  104.  
  105. if( ! isset( $grouped_settings[$group][$id][ 'errors' ] ) || ! $grouped_settings[$group][$id][ 'errors' ] ) continue; 
  106.  
  107. if( ! is_array( $grouped_settings[$group][$id][ 'errors' ] ) ) $grouped_settings[$group][$id][ 'errors' ] = array( $grouped_settings[$group][$id][ 'errors' ] ); 
  108.  
  109. foreach( $grouped_settings[$group][$id][ 'errors' ] as $old_key => $error ) { 
  110. $new_key = $grouped_settings[$group][$id][ 'id' ] . "[" . $old_key . "]"; 
  111. $errors[ $new_key ] = $error; 
  112. $grouped_settings[$group][$id][ 'errors'][ $new_key ] = $error; 
  113. unset( $grouped_settings[$group][$id][ 'errors' ][ $old_key ] ); 
  114.  
  115. $grouped_settings[ 'general' ][ 'version' ][ 'value' ] = Ninja_Forms::VERSION; 
  116.  
  117. $saved_fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1 ) ); 
  118.  
  119. foreach( $saved_fields as $saved_field ) { 
  120.  
  121. $saved_field_id = $saved_field->get_id(); 
  122.  
  123. $grouped_settings[ 'saved_fields'][] = array( 
  124. 'id' => '',  
  125. 'type' => 'html',  
  126. 'html' => '<a class="js-delete-saved-field button button-secondary" data-id="' . $saved_field_id . '">' . __( 'Delete' ) . '</a>',  
  127. 'label' => $saved_field->get_setting( 'label' ),  
  128.  
  129. ); 
  130.  
  131. if( $saved_fields ) { 
  132. wp_register_script( 'ninja_forms_admin_menu_settings', Ninja_Forms::$url . 'assets/js/admin-settings.js', array( 'jquery' ), FALSE, TRUE ); 
  133. wp_localize_script( 'ninja_forms_admin_menu_settings', 'nf_settings', array( 
  134. 'ajax_url' => admin_url( 'admin-ajax.php' ),  
  135. 'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ) 
  136. )); 
  137. wp_enqueue_script( 'ninja_forms_admin_menu_settings' ); 
  138.  
  139. Ninja_Forms::template( 'admin-menu-settings.html.php', compact( 'tabs', 'active_tab', 'groups', 'grouped_settings', 'save_button_text', 'errors' ) ); 
  140.  
  141.  
  142. public function update_settings() 
  143. if( ! current_user_can( apply_filters( 'ninja_forms_admin_settings_capabilities', 'manage_options' ) ) ) return; 
  144.  
  145. if( ! isset( $_POST[ $this->_prefix ] ) ) return; 
  146.  
  147. $settings = $_POST[ 'ninja_forms' ]; 
  148.  
  149. if( isset( $settings[ 'currency' ] ) ) { 
  150. $currency = sanitize_text_field( $settings[ 'currency' ] ); 
  151. $currency_symbols = Ninja_Forms::config( 'CurrencySymbol' ); 
  152. $settings[ 'currency_symbol' ] = ( isset( $currency_symbols[ $currency ] ) ) ? $currency_symbols[ $currency ] : ''; 
  153.  
  154. foreach( $settings as $id => $value ) { 
  155. $value = sanitize_text_field( $value ); 
  156. $value = apply_filters( 'ninja_forms_update_setting_' . $id, $value ); 
  157. Ninja_Forms()->update_setting( $id, $value ); 
  158. do_action( 'ninja_forms_save_setting_' . $id, $value ); 
  159.  
  160. private function get_settings() 
  161. return apply_filters( 'ninja_forms_plugin_settings', array( 
  162. 'general' => Ninja_Forms()->config( 'PluginSettingsGeneral' ),  
  163. 'recaptcha' => Ninja_Forms()->config( 'PluginSettingsReCaptcha' ),  
  164. 'advanced' => Ninja_Forms()->config( 'PluginSettingsAdvanced' ),  
  165. )); 
  166.  
  167. private function prefix( $value ) { 
  168. return "{$this->_prefix}[$value]"; 
  169.  
  170. } // End Class NF_Admin_Settings