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

301 lines 9.8 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 if(flush_rewrite_rules());
88 return betterdocs_settings_args();
89 }
90 /**
91 * Render the settings page
92 *
93 * @return void
94 */
95 public static function settings_page(){
96 $settings_args = self::settings_args();
97 $value = BetterDocs_DB::get_settings();
98
99 include_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-settings-display.php';
100 }
101 /**
102 * This function is responsible for render settings field
103 *
104 * @param string $key
105 * @param array $field
106 * @return void
107 */
108 public static function render_field( $key = '', $field = [] ) {
109 $post_id = '';
110 $name = $key;
111 $id = BetterDocs_Metabox::get_row_id( $key );
112 $file_name = isset( $field['type'] ) ? $field['type'] : 'text';
113
114 if( 'template' === $file_name ) {
115 $default = isset( $field['defaults'] ) ? $field['defaults'] : [];
116 } else {
117 $default = isset( $field['default'] ) ? $field['default'] : '';
118 }
119
120 $saved_value = BetterDocs_DB::get_settings( $name );
121
122 if( ! empty( $saved_value ) ) {
123 $value = $saved_value;
124 } else {
125 $value = $default;
126 }
127
128 $class = 'betterdocs-settings-field';
129 $row_class = BetterDocs_Metabox::get_row_class( $file_name );
130
131 $attrs = '';
132
133 if( isset( $field['toggle'] ) && in_array( $file_name, array( 'checkbox', 'select', 'toggle', 'theme' ) ) ) {
134 $attrs .= ' data-toggle="' . esc_attr( json_encode( $field['toggle'] ) ) . '"';
135 }
136
137 if( isset( $field['hide'] ) && $file_name == 'select' ) {
138 $attrs .= ' data-hide="' . esc_attr( json_encode( $field['hide'] ) ) . '"';
139 }
140
141 $field_id = $name;
142
143 include BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-field-display.php';
144 }
145 public static function save_default_settings(){
146 $settings_args = self::settings_args();
147 $fields = self::get_settings_fields( $settings_args );
148 $data = [];
149 $saved_settings = BetterDocs_DB::get_settings();
150 if( ! empty( $saved_settings ) ) {
151 return false;
152 }
153 if( ! empty( $fields ) ) {
154 foreach( $fields as $name => $posted_field ) {
155 $data[ $name ] = isset( $posted_field['default'] ) ? $posted_field['default'] : 0;
156 }
157 return BetterDocs_DB::update_settings( $data );
158 }
159 return false;
160 }
161 /**
162 * This function is responsible for
163 * save all settings data, including checking the disable field to prevent
164 * users manipulation.
165 *
166 * @param array $values
167 * @return void
168 */
169 public static function save_settings( $posted_fields = [] ){
170 $settings_args = self::settings_args();
171 $fields = self::get_settings_fields( $settings_args );
172 $data = [];
173 if( ! empty( $posted_fields ) ) {
174 $new_posted_fields = [];
175 foreach( $posted_fields as $posted_field ) {
176 preg_match("/(.*)\[(.*)\]/", $posted_field['name'], $matches);
177 if( ! empty( $matches ) ) {
178 $name = $matches[1];
179 $sub_name = $matches[2];
180 if( ! empty( $sub_name ) ) {
181 $new_posted_fields[ $name ][ $sub_name ] = $posted_field['value'];
182 } else {
183 $new_posted_fields[ $name ][] = $posted_field['value'];
184 }
185 } else {
186 $new_posted_fields[ $posted_field['name'] ] = $posted_field['value'];
187 }
188 }
189 }
190 $fields_keys = array_fill_keys( array_keys( $fields ), 'off' );
191
192 foreach( $new_posted_fields as $key => $new_posted_field ) {
193 if( array_key_exists( $key, $fields ) ) {
194 unset( $fields_keys[ $key ] );
195 if( empty( $new_posted_field ) ) {
196 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
197 }
198 if( isset( $fields[ $key ]['disable'] ) && $fields[ $key ]['disable'] === true ) {
199 $posted_value = isset( $fields[ $key ]['default'] ) ? $fields[ $key ]['default'] : '';
200 }
201 $posted_value = BetterDocs_Helper::sanitize_field( $fields[ $key ], $new_posted_field );
202 if( isset( $data[ $key ] ) ) {
203 if( is_array( $data[ $key ] ) ) {
204 $data[ $key ][] = $posted_value;
205 } else {
206 $data[ $key ] = array( $posted_value, $data[ $key ] );
207 }
208 } else {
209 $data[ $key ] = $posted_value;
210 }
211 }
212 }
213
214 $data = array_merge( $fields_keys, $data );
215 BetterDocs_DB::update_settings( $data );
216 }
217
218 public static function general_settings_ac(){
219 /**
220 * Verify the Nonce
221 */
222 if ( ( ! isset( $_POST['nonce'] ) && ! isset( $_POST['key'] ) ) || !
223 wp_verify_nonce( $_POST['nonce'], 'betterdocs_'. $_POST['key'] .'_nonce' ) ) {
224 return;
225 }
226
227 if( isset( $_POST['form_data'] ) ) {
228 self::save_settings( $_POST['form_data'] );
229 wp_send_json_success("success");
230 } else {
231 wp_send_json_error("error");
232 }
233
234 die;
235 }
236 /**
237 * Get Roles except subscriber
238 * dynamically
239 * @return array
240 */
241 public static function get_roles(){
242 $roles = wp_roles()->role_names;
243 unset( $roles['subscriber'] );
244 return $roles;
245 }
246
247 /**
248 * Get All Roles
249 * dynamically
250 * @return array
251 */
252 public static function get_all_user_roles(){
253 $users = array(
254 'all' => 'All logged in users'
255 );
256 $roles = wp_roles()->role_names;
257 return array_merge($users, $roles);
258 }
259
260 /**
261 * Get All Registered Texanomy
262 * dynamically
263 * @return array
264 */
265 public static function get_texanomy() {
266 $taxonomies = get_taxonomies( array (
267 'object_type' => array( 'docs' )
268 ), 'objects'
269 );
270 $docs_tax = array(
271 'all' => 'All Docs Archive',
272 'docs' => 'Docs Page'
273 );
274 foreach($taxonomies as $key=>$value) {
275 $docs_tax[$key] = $value->label;
276 }
277 unset( $docs_tax['doc_tag'] );
278 return $docs_tax;
279 }
280
281 /**
282 * Get Terms List
283 * dynamically
284 * @return array
285 */
286 public static function get_terms_list($texanomy) {
287 $get_terms = get_terms(
288 array(
289 'taxonomy' => $texanomy,
290 'hide_empty' => false,
291 )
292 );
293 $terms = array(
294 'all' => 'All'
295 );
296 foreach($get_terms as $key=>$value) {
297 $terms[$value->slug] = $value->name;
298 }
299 return $terms;
300 }
301 }