Settings.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Admin_Menus_Settings extends NF_Abstracts_Submenu
  3. {
  4. public $parent_slug = 'ninja-forms';
  5. public $menu_slug = 'nf-settings';
  6. public $priority = 11;
  7. protected $_prefix = 'ninja_forms';
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. if( isset( $_POST[ 'update_ninja_forms_settings' ] ) ) {
  12. add_action( 'admin_init', array( $this, 'update_settings' ) );
  13. }
  14. add_action( 'admin_body_class', array( $this, 'body_class' ) );
  15. // Catch Contact Form 7 reCAPTCHA conflict.
  16. add_filter( 'nf_admin_notices', array( $this, 'ninja_forms_cf7_notice' ) );
  17. }
  18. public function body_class( $classes )
  19. {
  20. // Add class for the builder.
  21. if( isset( $_GET['page'] ) && $_GET['page'] == $this->menu_slug ) {
  22. $classes = "$classes ninja-forms-settings";
  23. }
  24. return $classes;
  25. }
  26. /**
  27. * Function to notify users of CF7 conflict
  28. *
  29. * Since 3.0
  30. *
  31. * @param (array) $notices
  32. * @return (array) $notices
  33. */
  34. public function ninja_forms_cf7_notice( $notices )
  35. {
  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 $notices;
  40. }
  41. // If we can detect Contact Form 7...
  42. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  43. if ( is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ) ) {
  44. $notices[ 'cf7' ] = array(
  45. 'title' => __( 'Contact Form 7 is currently activated.', 'ninja-forms' ),
  46. 'msg' => sprintf( __( 'Please be aware that there is an issue with Contact Form 7 that breaks reCAPTCHA in other plugins.%sIf you need to use reCAPTCHA on any of your Ninja Forms, you will need to disable Contact Form 7.', 'ninja-forms' ), '<br />' ),
  47. 'int' => 0
  48. );
  49. }
  50. return $notices;
  51. }
  52. public function get_page_title()
  53. {
  54. return __( 'Settings', 'ninja-forms' );
  55. }
  56. public function get_capability()
  57. {
  58. return apply_filters( 'ninja_forms_admin_settings_capabilities', $this->capability );
  59. }
  60. public function display()
  61. {
  62. $tabs = apply_filters( 'ninja_forms_settings_tabs', array(
  63. 'settings' => __( 'Settings', 'ninja-forms' ),
  64. 'licenses' => __( 'Licenses', 'ninja-forms' )
  65. )
  66. );
  67. $tab_keys = array_keys( $tabs );
  68. $active_tab = ( isset( $_GET[ 'tab' ] ) ) ? $_GET[ 'tab' ] : reset( $tab_keys );
  69. wp_enqueue_style( 'nf-admin-settings', Ninja_Forms::$url . 'assets/css/admin-settings.css' );
  70. $groups = Ninja_Forms()->config( 'PluginSettingsGroups' );
  71. $grouped_settings = $this->get_settings();
  72. $save_button_text = __( 'Save Settings', 'ninja-forms' );
  73. $setting_defaults = Ninja_Forms()->get_settings();
  74. $errors = array();
  75. foreach( $grouped_settings as $group => $settings ){
  76. foreach( $settings as $id => $setting ){
  77. $value = ( isset( $setting_defaults[ $id ] ) ) ? $setting_defaults[$id] : '';
  78. $grouped_settings[$group][$id]['id'] = $this->prefix( $grouped_settings[$group][$id]['id'] );
  79. $grouped_settings[$group][$id]['value'] = $value;
  80. $grouped_settings[$group][$id] = apply_filters( 'ninja_forms_check_setting_' . $id, $grouped_settings[$group][$id] );
  81. if( ! isset( $grouped_settings[$group][$id][ 'errors' ] ) || ! $grouped_settings[$group][$id][ 'errors' ] ) continue;
  82. if( ! is_array( $grouped_settings[$group][$id][ 'errors' ] ) ) $grouped_settings[$group][$id][ 'errors' ] = array( $grouped_settings[$group][$id][ 'errors' ] );
  83. foreach( $grouped_settings[$group][$id][ 'errors' ] as $old_key => $error ){
  84. $new_key = $grouped_settings[$group][$id][ 'id' ] . "[" . $old_key . "]";
  85. $errors[ $new_key ] = $error;
  86. $grouped_settings[$group][$id][ 'errors'][ $new_key ] = $error;
  87. unset( $grouped_settings[$group][$id][ 'errors' ][ $old_key ] );
  88. }
  89. }
  90. }
  91. $grouped_settings[ 'general' ][ 'version' ][ 'value' ] = Ninja_Forms::VERSION;
  92. $saved_fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1 ) );
  93. foreach( $saved_fields as $saved_field ){
  94. $saved_field_id = $saved_field->get_id();
  95. $grouped_settings[ 'saved_fields'][] = array(
  96. 'id' => '',
  97. 'type' => 'html',
  98. 'html' => '<a class="js-delete-saved-field button button-secondary" data-id="' . $saved_field_id . '">' . __( 'Delete', 'ninja-forms' ) . '</a>',
  99. 'label' => $saved_field->get_setting( 'label' ),
  100. );
  101. }
  102. $forms = Ninja_Forms()->form()->get_forms();
  103. $form_options = array();
  104. foreach( $forms as $form ){
  105. $form_options[] = array( 'id' => $form->get_id(),
  106. 'title' => $form->get_setting( 'title' ) );
  107. }
  108. $form_options = apply_filters( 'ninja_forms_submission_filter_form_options', $form_options );
  109. asort($form_options);
  110. if ( get_option( 'ninja_forms_allow_tracking' ) && '1' == get_option( 'ninja_forms_allow_tracking' ) ) {
  111. $allow_tel = 1;
  112. } else {
  113. $allow_tel = 0;
  114. }
  115. wp_enqueue_script( 'jBox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js', array( 'jquery' ) );
  116. wp_enqueue_style( 'nf-combobox', Ninja_Forms::$url . 'assets/css/combobox.css' );
  117. wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
  118. wp_register_script( 'ninja_forms_admin_menu_settings', Ninja_Forms::$url . 'assets/js/admin-settings.js', array( 'jquery' ), FALSE, TRUE );
  119. wp_localize_script( 'ninja_forms_admin_menu_settings', 'nf_settings', array(
  120. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  121. 'forms' => $form_options,
  122. 'nf_nuke_title' => __( 'Remove ALL Ninja Forms data and uninstall?', 'ninja-forms' ),
  123. 'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ),
  124. 'batch_nonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
  125. 'i18n' => array(
  126. 'downgradeMessage' => __( 'Are you sure you want to downgrade?', 'ninja-forms' ),
  127. 'downgradeWarningMessage' => __( 'You WILL lose any forms or submissions created on this version of Ninja Forms.', 'ninja-forms' ),
  128. 'downgradeConfirmMessage' => __( 'Type ', 'ninja-forms' ) . '<span style="color: red";>' . 'DOWNGRADE' . "</span>" . __( ' to confirm.', 'ninja-forms' ),
  129. 'downgradeButtonPrimary' => __( 'Downgrade', 'ninja-forms'),
  130. 'downgradeButtonSecondary' => __( 'Cancel', 'ninja-forms' ),
  131. 'trashExpiredSubsMessage' => __( 'Are you sure you want to trash all expired submissions?', 'ninja-forms' ),
  132. 'trashExpiredSubsButtonPrimary' => __( 'Trash', 'ninja-forms' ),
  133. 'trashExpiredSubsButtonSecondary' => __( 'Cancel', 'ninja-forms' ),
  134. ),
  135. 'allow_telemetry' => $allow_tel,
  136. ));
  137. wp_enqueue_script( 'nf-ninja-modal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js' );
  138. wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' );
  139. wp_enqueue_script( 'ninja_forms_admin_menu_settings' );
  140. Ninja_Forms::template( 'admin-menu-settings.html.php', compact( 'tabs', 'active_tab', 'groups', 'grouped_settings', 'save_button_text', 'errors' ) );
  141. }
  142. public function update_settings()
  143. {
  144. if( ! current_user_can( apply_filters( 'ninja_forms_admin_settings_capabilities', 'manage_options' ) ) ) return;
  145. if( ! isset( $_POST[ $this->_prefix ] ) ) return;
  146. $settings = $_POST[ 'ninja_forms' ];
  147. if( isset( $settings[ 'currency' ] ) ){
  148. $currency = sanitize_text_field( $settings[ 'currency' ] );
  149. $currency_symbols = Ninja_Forms::config( 'CurrencySymbol' );
  150. $settings[ 'currency_symbol' ] = ( isset( $currency_symbols[ $currency ] ) ) ? $currency_symbols[ $currency ] : '';
  151. }
  152. foreach( $settings as $id => $value ){
  153. $value = sanitize_text_field( $value );
  154. $value = apply_filters( 'ninja_forms_update_setting_' . $id, $value );
  155. Ninja_Forms()->update_setting( $id, $value );
  156. do_action( 'ninja_forms_save_setting_' . $id, $value );
  157. }
  158. }
  159. private function get_settings()
  160. {
  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