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. public function get_page_title() 
  17. return __( 'Settings', 'ninja-forms' ); 
  18.  
  19. public function get_capability() 
  20. return apply_filters( 'ninja_forms_admin_settings_capabilities', $this->capability ); 
  21.  
  22. public function display() 
  23. $tabs = apply_filters( 'ninja_forms_settings_tabs', array( 
  24. 'settings' => __( 'Settings', 'ninja-forms' ),  
  25. 'licenses' => __( 'Licenses', 'ninja-forms' ) 
  26. ); 
  27.  
  28. $tab_keys = array_keys( $tabs ); 
  29. $active_tab = ( isset( $_GET[ 'tab' ] ) ) ? $_GET[ 'tab' ] : reset( $tab_keys ); 
  30.  
  31. wp_enqueue_style( 'nf-admin-settings', Ninja_Forms::$url . 'assets/css/admin-settings.css' ); 
  32.  
  33. $groups = Ninja_Forms()->config( 'PluginSettingsGroups' ); 
  34.  
  35. $grouped_settings = $this->get_settings(); 
  36.  
  37. $save_button_text = __( 'Save Settings', 'ninja-forms' ); 
  38.  
  39. $setting_defaults = Ninja_Forms()->get_settings(); 
  40.  
  41. $errors = array(); 
  42.  
  43. foreach( $grouped_settings as $group => $settings ) { 
  44.  
  45. foreach( $settings as $id => $setting ) { 
  46.  
  47. $value = ( isset( $setting_defaults[ $id ] ) ) ? $setting_defaults[$id] : ''; 
  48.  
  49. $grouped_settings[$group][$id]['id'] = $this->prefix( $grouped_settings[$group][$id]['id'] ); 
  50. $grouped_settings[$group][$id]['value'] = $value; 
  51.  
  52. $grouped_settings[$group][$id] = apply_filters( 'ninja_forms_check_setting_' . $id, $grouped_settings[$group][$id] ); 
  53.  
  54. if( ! isset( $grouped_settings[$group][$id][ 'errors' ] ) || ! $grouped_settings[$group][$id][ 'errors' ] ) continue; 
  55.  
  56. if( ! is_array( $grouped_settings[$group][$id][ 'errors' ] ) ) $grouped_settings[$group][$id][ 'errors' ] = array( $grouped_settings[$group][$id][ 'errors' ] ); 
  57.  
  58. foreach( $grouped_settings[$group][$id][ 'errors' ] as $old_key => $error ) { 
  59. $new_key = $grouped_settings[$group][$id][ 'id' ] . "[" . $old_key . "]"; 
  60. $errors[ $new_key ] = $error; 
  61. $grouped_settings[$group][$id][ 'errors'][ $new_key ] = $error; 
  62. unset( $grouped_settings[$group][$id][ 'errors' ][ $old_key ] ); 
  63.  
  64. $grouped_settings[ 'general' ][ 'version' ][ 'value' ] = Ninja_Forms::VERSION; 
  65.  
  66. $saved_fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1 ) ); 
  67.  
  68. foreach( $saved_fields as $saved_field ) { 
  69.  
  70. $saved_field_id = $saved_field->get_id(); 
  71.  
  72. $grouped_settings[ 'saved_fields'][] = array( 
  73. 'id' => '',  
  74. 'type' => 'html',  
  75. 'html' => '<a class="js-delete-saved-field button button-secondary" data-id="' . $saved_field_id . '">' . __( 'Delete' ) . '</a>',  
  76. 'label' => $saved_field->get_setting( 'label' ),  
  77.  
  78. ); 
  79.  
  80. if( $saved_fields ) { 
  81. wp_register_script( 'ninja_forms_admin_menu_settings', Ninja_Forms::$url . 'assets/js/admin-settings.js', array( 'jquery' ), FALSE, TRUE ); 
  82. wp_localize_script( 'ninja_forms_admin_menu_settings', 'nf_settings', array( 
  83. 'ajax_url' => admin_url( 'admin-ajax.php' ),  
  84. 'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ) 
  85. )); 
  86. wp_enqueue_script( 'ninja_forms_admin_menu_settings' ); 
  87.  
  88. Ninja_Forms::template( 'admin-menu-settings.html.php', compact( 'tabs', 'active_tab', 'groups', 'grouped_settings', 'save_button_text', 'errors' ) ); 
  89.  
  90.  
  91. public function update_settings() 
  92. if( ! current_user_can( apply_filters( 'ninja_forms_admin_form_settings_capabilities', 'manage_options' ) ) ) return; 
  93.  
  94. if( ! isset( $_POST[ $this->_prefix ] ) ) return; 
  95.  
  96. $settings = $_POST[ 'ninja_forms' ]; 
  97.  
  98. if( isset( $settings[ 'currency' ] ) ) { 
  99. $currency = sanitize_text_field( $settings[ 'currency' ] ); 
  100. $currency_symbols = Ninja_Forms::config( 'CurrencySymbol' ); 
  101. $settings[ 'currency_symbol' ] = ( isset( $currency_symbols[ $currency ] ) ) ? $currency_symbols[ $currency ] : ''; 
  102.  
  103. foreach( $settings as $id => $value ) { 
  104. $value = sanitize_text_field( $value ); 
  105. $value = apply_filters( 'ninja_forms_update_setting_' . $id, $value ); 
  106. Ninja_Forms()->update_setting( $id, $value ); 
  107. do_action( 'ninja_forms_save_setting_' . $id, $value ); 
  108.  
  109. private function get_settings() 
  110. return apply_filters( 'ninja_forms_plugin_settings', array( 
  111. 'general' => Ninja_Forms()->config( 'PluginSettingsGeneral' ),  
  112. 'recaptcha' => Ninja_Forms()->config( 'PluginSettingsReCaptcha' ),  
  113. 'advanced' => Ninja_Forms()->config( 'PluginSettingsAdvanced' ),  
  114. )); 
  115.  
  116. private function prefix( $value ) { 
  117. return "{$this->_prefix}[$value]"; 
  118.  
  119. } // End Class NF_Admin_Settings