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

304 lines 9.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 * This function is responsible for
166 * save all settings data, including checking the disable field to prevent
167 * users manipulation.
168 *
169 * @param array $values
170 * @return void
171 */
172 public static function save_settings( $posted_fields = [] ){
173 $settings_args = self::settings_args();
174 $fields = self::get_settings_fields( $settings_args );
175 $data = [];
176 if( ! empty( $posted_fields ) ) {
177 $new_posted_fields = [];
178 foreach( $posted_fields as $posted_field ) {
179 preg_match("/(.*)\[(.*)\]/", $posted_field['name'], $matches);
180 if( ! empty( $matches ) ) {
181 $name = $matches[1];
182 $sub_name = $matches[2];
183 if( ! empty( $sub_name ) ) {
184 $new_posted_fields[ $name ][ $sub_name ] = $posted_field['value'];
185 } else {
186 $new_posted_fields[ $name ][] = $posted_field['value'];
187 }
188 } else {
189 $new_posted_fields[ $posted_field['name'] ] = $posted_field['value'];
190 }
191 }
192 }
193 $fields_keys = array_fill_keys( array_keys( $fields ), 'off' );
194
195 foreach( $new_posted_fields as $key => $new_posted_field ) {
196 if( array_key_exists( $key, $fields ) ) {
197 unset( $fields_keys[ $key ] );
198 if( empty( $new_posted_field ) ) {
199 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
200 }
201 if( isset( $fields[ $key ]['disable'] ) && $fields[ $key ]['disable'] === true ) {
202 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
203 }
204 $posted_value = BetterDocs_Helper::sanitize_field( $fields[ $key ], $new_posted_field );
205 if( isset( $data[ $key ] ) ) {
206 if( is_array( $data[ $key ] ) ) {
207 $data[ $key ][] = $posted_value;
208 } else {
209 $data[ $key ] = array( $posted_value, $data[ $key ] );
210 }
211 } else {
212 $data[ $key ] = $posted_value;
213 }
214 }
215 }
216
217 $data = array_merge( $fields_keys, $data );
218 BetterDocs_DB::update_settings( $data );
219 }
220
221 public static function general_settings_ac(){
222 /**
223 * Verify the Nonce
224 */
225 if ( ( ! isset( $_POST['nonce'] ) && ! isset( $_POST['key'] ) ) || !
226 wp_verify_nonce( $_POST['nonce'], 'betterdocs_'. $_POST['key'] .'_nonce' ) ) {
227 return;
228 }
229
230 if( isset( $_POST['form_data'] ) ) {
231 self::save_settings( $_POST['form_data'] );
232 wp_send_json_success("success");
233 } else {
234 wp_send_json_error("error");
235 }
236
237 die;
238 }
239 /**
240 * Get Roles except subscriber
241 * dynamically
242 * @return array
243 */
244 public static function get_roles(){
245 $roles = wp_roles()->role_names;
246 unset( $roles['subscriber'] );
247 return $roles;
248 }
249
250 /**
251 * Get All Roles
252 * dynamically
253 * @return array
254 */
255 public static function get_all_user_roles(){
256 $users = array(
257 'all' => 'All logged in users'
258 );
259 $roles = wp_roles()->role_names;
260 return array_merge($users, $roles);
261 }
262
263 /**
264 * Get All Registered Texanomy
265 * dynamically
266 * @return array
267 */
268 public static function get_texanomy() {
269 $taxonomies = get_taxonomies( array (
270 'object_type' => array( 'docs' )
271 ), 'objects'
272 );
273 $docs_tax = array(
274 'all' => 'All Docs Archive',
275 'docs' => 'Docs Page'
276 );
277 foreach($taxonomies as $key=>$value) {
278 $docs_tax[$key] = $value->label;
279 }
280 unset( $docs_tax['doc_tag'] );
281 return $docs_tax;
282 }
283
284 /**
285 * Get Terms List
286 * dynamically
287 * @return array
288 */
289 public static function get_terms_list($texanomy) {
290 $get_terms = get_terms(
291 array(
292 'taxonomy' => $texanomy,
293 'hide_empty' => false,
294 )
295 );
296 $terms = array(
297 'all' => 'All'
298 );
299 foreach($get_terms as $key=>$value) {
300 $terms[$value->slug] = $value->name;
301 }
302 return $terms;
303 }
304 }