PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.0.14
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.0.14
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 2.0.14, at admin/includes/class-betterdocs-settings.php

383 lines 12.9 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.com
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
88 if (!empty($_GET['page']) && !empty($_GET['saved']) && $_GET['page'] === 'betterdocs-settings' && $_GET['saved'] == true) {
89 flush_rewrite_rules();
90 }
91 return betterdocs_settings_args();
92 }
93 /**
94 * Render the settings page
95 *
96 * @return void
97 */
98 public static function settings_page(){
99 $settings_args = self::settings_args();
100 $value = BetterDocs_DB::get_settings();
101
102 include_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-settings-display.php';
103 }
104 /**
105 * This function is responsible for render settings field
106 *
107 * @param string $key
108 * @param array $field
109 * @return void
110 */
111 public static function render_field( $key = '', $field = [] ) {
112 $post_id = '';
113 $name = $key;
114 $id = BetterDocs_Metabox::get_row_id( $key );
115 $file_name = isset( $field['type'] ) ? $field['type'] : 'text';
116
117 if( 'template' === $file_name ) {
118 $default = isset( $field['defaults'] ) ? $field['defaults'] : [];
119 } else {
120 $default = isset( $field['default'] ) ? $field['default'] : '';
121 }
122
123 $saved_value = BetterDocs_DB::get_settings( $name );
124
125 if( ! empty( $saved_value ) ) {
126 $value = $saved_value;
127 } else {
128 $value = $default;
129 }
130
131 $class = 'betterdocs-settings-field';
132 $row_class = BetterDocs_Metabox::get_row_class( $file_name );
133
134 $attrs = '';
135
136 if( isset( $field['toggle'] ) && in_array( $file_name, array( 'checkbox', 'select', 'toggle', 'theme' ) ) ) {
137 $attrs .= ' data-toggle="' . esc_attr( json_encode( $field['toggle'] ) ) . '"';
138 }
139
140 if( isset( $field['hide'] ) && $file_name == 'select' ) {
141 $attrs .= ' data-hide="' . esc_attr( json_encode( $field['hide'] ) ) . '"';
142 }
143
144 $field_id = $name;
145
146 include BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-field-display.php';
147 }
148 public static function save_default_settings(){
149 $settings_args = self::settings_args();
150 $fields = self::get_settings_fields( $settings_args );
151 $data = [];
152 $saved_settings = BetterDocs_DB::get_settings();
153 if( ! empty( $saved_settings ) ) {
154 return false;
155 }
156 if( ! empty( $fields ) ) {
157 foreach( $fields as $name => $posted_field ) {
158 $data[ $name ] = isset( $posted_field['default'] ) ? $posted_field['default'] : 0;
159 }
160 return BetterDocs_DB::update_settings( $data );
161 }
162 return false;
163 }
164
165 public static function has_administrator( &$roles ){
166 if( $roles === 'off' || empty( $roles ) ) {
167 $roles = [ 'administrator' ];
168 }
169
170 if( ! is_array( $roles ) ) {
171 $roles = [ $roles ];
172 }
173
174 if( ! in_array( 'administrator', $roles, true ) ) {
175 $roles[] = 'administrator';
176 }
177
178 return array_unique( $roles );
179 }
180
181 public static function get_selected_roles( $settings = array() ) {
182 $article_roles = isset( $settings['article_roles'] ) ? $settings['article_roles'] : Betterdocs_DB::get_settings('article_roles') ;
183 $settings_roles = isset( $settings['settings_roles'] ) ? $settings['settings_roles'] : Betterdocs_DB::get_settings('settings_roles');
184 $analytics_roles = isset( $settings['analytics_roles'] ) ? $settings['analytics_roles'] : BetterDocs_DB::get_settings('analytics_roles');
185
186 $article_roles = self::has_administrator( $article_roles );
187 $settings_roles = self::has_administrator( $settings_roles );
188 $analytics_roles = self::has_administrator( $analytics_roles );
189
190 return array(
191 'article_roles' => $article_roles,
192 'settings_roles' => $settings_roles,
193 'analytics_roles' => $analytics_roles
194 );
195 }
196
197 public static function get_role_cap_mapper( $settings = array() ) {
198 if( empty( $settings ) ) {
199 $settings = self::get_selected_roles();
200 }
201
202 return array(
203 'write_docs' => array(
204 'roles' => $settings['article_roles']
205 ),
206 'edit_docs_settings' => array(
207 'roles' => $settings['settings_roles']
208 ),
209 'read_docs_analytics' => array(
210 'roles' => $settings['analytics_roles']
211 )
212 );
213 }
214
215 /**
216 * This function is responsible for
217 * save all settings data, including checking the disable field to prevent
218 * users manipulation.
219 *
220 * @param array $values
221 * @return void
222 */
223 public static function save_settings( $posted_fields = [] ){
224 $settings_args = self::settings_args();
225 $fields = self::get_settings_fields( $settings_args );
226 $data = [];
227 $new_posted_fields = [];
228 if( ! empty( $posted_fields ) ) {
229 foreach( $posted_fields as $posted_field ) {
230 preg_match("/(.*)\[(.*)\]/", $posted_field['name'], $matches);
231 if( ! empty( $matches ) ) {
232 $name = $matches[1];
233 $sub_name = $matches[2];
234 if( ! empty( $sub_name ) ) {
235 $new_posted_fields[ $name ][ $sub_name ] = $posted_field['value'];
236 } else {
237 $new_posted_fields[ $name ][] = $posted_field['value'];
238 }
239 } else {
240 $new_posted_fields[ $posted_field['name'] ] = $posted_field['value'];
241 }
242 }
243 }
244 $fields_keys = array_fill_keys( array_keys( $fields ), 'off' );
245
246 $builtin_doc_page = $new_posted_fields['builtin_doc_page'] ? $fields_keys['builtin_doc_page'] : '';
247 $docs_slug = $new_posted_fields['docs_slug'];
248 $docs_page = $new_posted_fields['docs_page'];
249 if ($builtin_doc_page == 1 && $docs_slug) {
250 $docs_slug = $docs_slug;
251 } elseif ($builtin_doc_page != 1 && $docs_page) {
252 $post_info = get_post($docs_page);
253 $docs_slug = $post_info->post_name;
254 } else {
255 $docs_slug = 'docs';
256 }
257
258 do_action( 'betterdocs_settings_before_save', $new_posted_fields );
259
260 foreach ( $new_posted_fields as $key => $new_posted_field ) {
261 if ( array_key_exists( $key, $fields ) ) {
262 unset( $fields_keys[ $key ] );
263 if( empty( $new_posted_field ) ) {
264 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
265 }
266 if( isset( $fields[ $key ]['disable'] ) && $fields[ $key ]['disable'] === true ) {
267 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
268 }
269
270 $posted_value = BetterDocs_Helper::sanitize_field( $fields[ $key ], $new_posted_field );
271
272 if( $key == 'permalink_structure' ) {
273 $permalink_stracture = explode('%', $posted_value);
274 if ($permalink_stracture[0] == '/') {
275 $posted_value = $docs_slug . $posted_value;
276 } else if ($permalink_stracture[0] == '') {
277 $posted_value = $docs_slug . '/' . $posted_value;
278 }
279 }
280
281 if( isset( $data[ $key ] ) ) {
282 if( is_array( $data[ $key ] ) ) {
283 $data[ $key ][] = $posted_value;
284 } else {
285 $data[ $key ] = array( $posted_value, $data[ $key ] );
286 }
287 } else {
288 $data[ $key ] = $posted_value;
289 }
290 }
291 }
292
293 $data = array_merge( $fields_keys, $data );
294 $roles_rechecked = self::get_selected_roles($data);
295 $data = array_merge( $data, $roles_rechecked );
296 BetterDocs_DB::update_settings( $data );
297 do_action( 'bdocs_settings_saved', $data );
298 }
299
300 public static function general_settings_ac(){
301 /**
302 * Verify the Nonce
303 */
304 if ( ( ! isset( $_POST['nonce'] ) && ! isset( $_POST['key'] ) ) || !
305 wp_verify_nonce( $_POST['nonce'], 'betterdocs_'. $_POST['key'] .'_nonce' ) ) {
306 return;
307 }
308
309 if( isset( $_POST['form_data'] ) ) {
310 self::save_settings( $_POST['form_data'] );
311 wp_send_json_success("success");
312 } else {
313 wp_send_json_error("error");
314 }
315
316 die;
317 }
318 /**
319 * Get Roles except subscriber
320 * dynamically
321 * @return array
322 */
323 public static function get_roles(){
324 $roles = wp_roles()->role_names;
325 unset( $roles['subscriber'] );
326 return $roles;
327 }
328
329 /**
330 * Get All Roles
331 * dynamically
332 * @return array
333 */
334 public static function get_all_user_roles(){
335 $users = array(
336 'all' => 'All logged in users'
337 );
338 $roles = wp_roles()->role_names;
339 return array_merge($users, $roles);
340 }
341
342 /**
343 * Get All Registered Texanomy
344 * dynamically
345 * @return array
346 */
347 public static function get_texanomy() {
348 $taxonomies = get_taxonomies( array (
349 'object_type' => array( 'docs' )
350 ), 'objects'
351 );
352 $docs_tax = array(
353 'all' => 'All Docs Archive',
354 'docs' => 'Docs Page'
355 );
356 foreach($taxonomies as $key=>$value) {
357 $docs_tax[$key] = $value->label;
358 }
359 unset( $docs_tax['doc_tag'] );
360 return $docs_tax;
361 }
362
363 /**
364 * Get Terms List
365 * dynamically
366 * @return array
367 */
368 public static function get_terms_list($texanomy) {
369 $get_terms = get_terms(
370 array(
371 'taxonomy' => $texanomy,
372 'hide_empty' => false,
373 )
374 );
375 $terms = array(
376 'all' => 'All'
377 );
378 foreach($get_terms as $key=>$value) {
379 $terms[$value->slug] = $value->name;
380 }
381 return $terms;
382 }
383 }