PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 1.3.4
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v1.3.4
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / admin / includes / class-betterdocs-settings.php

class-betterdocs-settings.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 1.3.4, at admin/includes/class-betterdocs-settings.php

245 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This class is responsible for all settings things happening in Betterdocs Plugin
4 *
5 * @link https://wpdeveloper.net
6 * @since 1.0.0
7 *
8 * @package BetterDocs
9 * @subpackage BetterDocs/admin
10 */
11 class BetterDocs_Settings {
12 public static function init(){
13 add_action( 'betterdocs_settings_header', array( __CLASS__, 'header_template' ), 10 );
14 add_action( 'wp_ajax_betterdocs_general_settings', array( __CLASS__, 'general_settings_ac' ), 10 );
15 }
16
17 /**
18 * This function is responsible for settings page header
19 *
20 * @hooked betterdocs_settings_header
21 * @return void
22 */
23 public static function header_template(){
24 ?>
25 <div class="betterdocs-settings-header">
26 <div class="betterdocs-header-full">
27 <img src="<?php echo BETTERDOCS_ADMIN_URL ?>assets/img/betterdocs-icon.svg" alt="">
28 <h2 class="title"><?php _e( 'BetterDocs Settings', 'betterdocs' ); ?></h2>
29 </div>
30 </div>
31 <?php
32 }
33 /**
34 * Get all settings fields
35 *
36 * @param array $settings
37 * @return array
38 */
39 public static function get_settings_fields( $settings ){
40 $new_fields = [];
41
42 foreach( $settings as $setting ) {
43
44 // if( isset( $setting['fields'] ) ) {
45
46 // }
47
48 $sections = isset( $setting['sections'] ) ? $setting['sections'] : [];
49 if( ! empty( $sections ) ) {
50 foreach( $sections as $section ) {
51 $fields = isset( $section['fields'] ) ? $section['fields'] : [];
52 if( empty( $fields ) ) {
53 $tabs = isset( $section['tabs'] ) ? $section['tabs'] : [];
54 if( ! empty( $tabs ) ) {
55 foreach( $tabs as $id => $tab ) {
56 $fields = isset( $tab['fields'] ) ? $tab['fields'] : [];
57 if( ! empty( $fields ) ) {
58 foreach( $fields as $id => $field ) {
59 $new_fields[ $id ] = $field;
60 }
61 }
62 }
63 }
64 } else {
65 if( ! empty( $fields ) ) {
66 foreach( $fields as $id => $field ) {
67 $new_fields[ $id ] = $field;
68 }
69 }
70 }
71 }
72 }
73 }
74
75 return apply_filters( 'betterdocs_settings_fields', $new_fields );
76 }
77 /**
78 * Get the whole settings array
79 *
80 * @return void
81 */
82 public static function settings_args(){
83 if( ! function_exists( 'betterdocs_settings_args' ) ) {
84 require BETTERDOCS_ADMIN_DIR_PATH . 'includes/betterdocs-settings-page-helper.php';
85 }
86 do_action( 'betterdocs_before_settings_load' );
87 return betterdocs_settings_args();
88 }
89 /**
90 * Render the settings page
91 *
92 * @return void
93 */
94 public static function settings_page(){
95 $settings_args = self::settings_args();
96 $value = BetterDocs_DB::get_settings();
97
98 include_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-settings-display.php';
99 }
100 /**
101 * This function is responsible for render settings field
102 *
103 * @param string $key
104 * @param array $field
105 * @return void
106 */
107 public static function render_field( $key = '', $field = [] ) {
108 $post_id = '';
109 $name = $key;
110 $id = BetterDocs_Metabox::get_row_id( $key );
111 $file_name = isset( $field['type'] ) ? $field['type'] : 'text';
112
113 if( 'template' === $file_name ) {
114 $default = isset( $field['defaults'] ) ? $field['defaults'] : [];
115 } else {
116 $default = isset( $field['default'] ) ? $field['default'] : '';
117 }
118
119 $saved_value = BetterDocs_DB::get_settings( $name );
120
121 if( ! empty( $saved_value ) ) {
122 $value = $saved_value;
123 } else {
124 $value = $default;
125 }
126
127 $class = 'betterdocs-settings-field';
128 $row_class = BetterDocs_Metabox::get_row_class( $file_name );
129
130 $attrs = '';
131
132 if( isset( $field['toggle'] ) && in_array( $file_name, array( 'checkbox', 'select', 'toggle', 'theme' ) ) ) {
133 $attrs .= ' data-toggle="' . esc_attr( json_encode( $field['toggle'] ) ) . '"';
134 }
135
136 if( isset( $field['hide'] ) && $file_name == 'select' ) {
137 $attrs .= ' data-hide="' . esc_attr( json_encode( $field['hide'] ) ) . '"';
138 }
139
140 $field_id = $name;
141
142 include BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-field-display.php';
143 }
144 public static function save_default_settings(){
145 $settings_args = self::settings_args();
146 $fields = self::get_settings_fields( $settings_args );
147 $data = [];
148 $saved_settings = BetterDocs_DB::get_settings();
149 if( ! empty( $saved_settings ) ) {
150 return false;
151 }
152 if( ! empty( $fields ) ) {
153 foreach( $fields as $name => $posted_field ) {
154 $data[ $name ] = isset( $posted_field['default'] ) ? $posted_field['default'] : 0;
155 }
156 return BetterDocs_DB::update_settings( $data );
157 }
158 return false;
159 }
160 /**
161 * This function is responsible for
162 * save all settings data, including checking the disable field to prevent
163 * users manipulation.
164 *
165 * @param array $values
166 * @return void
167 */
168 public static function save_settings( $posted_fields = [] ){
169 $settings_args = self::settings_args();
170 $fields = self::get_settings_fields( $settings_args );
171 $data = [];
172 if( ! empty( $posted_fields ) ) {
173 $new_posted_fields = [];
174 foreach( $posted_fields as $posted_field ) {
175 preg_match("/(.*)\[(.*)\]/", $posted_field['name'], $matches);
176 if( ! empty( $matches ) ) {
177 $name = $matches[1];
178 $sub_name = $matches[2];
179 if( ! empty( $sub_name ) ) {
180 $new_posted_fields[ $name ][ $sub_name ] = $posted_field['value'];
181 } else {
182 $new_posted_fields[ $name ][] = $posted_field['value'];
183 }
184 } else {
185 $new_posted_fields[ $posted_field['name'] ] = $posted_field['value'];
186 }
187 }
188 }
189 $fields_keys = array_fill_keys( array_keys( $fields ), 'off' );
190
191 foreach( $new_posted_fields as $key => $new_posted_field ) {
192 if( array_key_exists( $key, $fields ) ) {
193 unset( $fields_keys[ $key ] );
194 if( empty( $new_posted_field ) ) {
195 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
196 }
197 if( isset( $fields[ $key ]['disable'] ) && $fields[ $key ]['disable'] === true ) {
198 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
199 }
200 $posted_value = BetterDocs_Helper::sanitize_field( $fields[ $key ], $new_posted_field );
201 if( isset( $data[ $key ] ) ) {
202 if( is_array( $data[ $key ] ) ) {
203 $data[ $key ][] = $posted_value;
204 } else {
205 $data[ $key ] = array( $posted_value, $data[ $key ] );
206 }
207 } else {
208 $data[ $key ] = $posted_value;
209 }
210 }
211 }
212
213 $data = array_merge( $fields_keys, $data );
214 BetterDocs_DB::update_settings( $data );
215 }
216
217 public static function general_settings_ac(){
218 /**
219 * Verify the Nonce
220 */
221 if ( ( ! isset( $_POST['nonce'] ) && ! isset( $_POST['key'] ) ) || !
222 wp_verify_nonce( $_POST['nonce'], 'betterdocs_'. $_POST['key'] .'_nonce' ) ) {
223 return;
224 }
225
226 if( isset( $_POST['form_data'] ) ) {
227 self::save_settings( $_POST['form_data'] );
228 wp_send_json_success("success");
229 } else {
230 wp_send_json_error("error");
231 }
232
233 die;
234 }
235 /**
236 * Get All Roles
237 * dynamically
238 * @return array
239 */
240 public static function get_roles(){
241 $roles = wp_roles()->role_names;
242 unset( $roles['subscriber'] );
243 return $roles;
244 }
245 }