PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.3
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.3
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / API / WPDA_Settings.php

WPDA_Settings.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.3, at WPDataAccess/API/WPDA_Settings.php

646 lines 18.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\API {
4
5 use WPDataAccess\Plugin_Table_Models\WPDA_Media_Model;
6 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
7 use WPDataAccess\Plugin_Table_Models\WPDA_User_Menus_Model;
8 use WPDataAccess\WPDA;
9
10 class WPDA_Settings extends WPDA_API_Core {
11
12 const WPDA_SETTINGS = 'wpda_settings';
13
14 public function register_rest_routes() {
15
16 register_rest_route(
17 WPDA_API::WPDA_NAMESPACE,
18 'save-settings',
19 array(
20 'methods' => array( 'POST' ),
21 'callback' => array( $this, 'save_settings' ),
22 'permission_callback' => '__return_true',
23 'args' => array(
24 'action' => array(
25 'required' => true,
26 'type' => 'string',
27 'description' => __( 'Setting type', 'wp-data-access' ),
28 'sanitize_callback' => 'sanitize_text_field',
29 'validate_callback' => function ( $param ) {
30 return in_array(
31 $param,
32 array(
33 'dashboard_menus',
34 'table_settings',
35 'column_settings',
36 'rest_api',
37 'admin_settings',
38 'explorer_settings'
39 )
40 );
41 },
42 ),
43 'dbs' => $this->get_param( 'dbs' ),
44 'tbl' => $this->get_param( 'tbl' ),
45 'settings' => array(
46 'required' => true,
47 'type' => 'string',
48 'description' => __( 'Settings JSON as string', 'wp-data-access' ),
49 'sanitize_callback' => 'sanitize_text_field',
50 'validate_callback' => 'rest_validate_request_arg',
51 ),
52 ),
53 )
54 );
55
56 }
57
58 /**
59 * Save plugin settings.
60 *
61 * @param WP_REST_Request $request Rest API request.
62 * @return \WP_Error|\WP_REST_Response
63 */
64 public function save_settings( $request ) {
65
66 if ( ! $this->current_user_can_access( true ) ) {
67 return $this->unauthorized();
68 }
69
70 if ( ! $this->current_user_token_valid( $request ) ) {
71 return $this->invalid_nonce();
72 }
73
74 $dbs = $request->get_param( 'dbs' );
75 $tbl = $request->get_param( 'tbl' );
76
77 $settings_string = $request->get_param( 'settings' );
78 if ( ! is_string( $settings_string ) ) {
79 return $this->bad_request();
80 }
81
82 $settings = json_decode( $settings_string, true );
83 if ( false === $settings || is_null( $settings ) ) {
84 return $this->bad_request();
85 }
86
87 switch ( $request->get_param( 'action' ) ) {
88 case 'dashboard_menus':
89 return $this->save_dashboard_menus( $dbs, $tbl, $settings );
90 case 'table_settings':
91 return $this->save_table_settings( $dbs, $tbl, $settings );
92 case 'column_settings':
93 return $this->save_column_settings( $dbs, $tbl, $settings );
94 case 'rest_api':
95 return $this->save_rest_api_settings( $dbs, $tbl, $settings );
96 case 'admin_settings':
97 $theme = $request->get_param( 'theme' );
98 return $this->save_admin_settings( $dbs, $tbl, $settings, $theme );
99 case 'explorer_settings':
100 return $this->save_explorer_settings( $dbs, $tbl, $settings );
101 }
102
103 return $this->bad_request();
104
105 }
106
107 private function save_explorer_settings( $schema_name, $table_name, $settings ) {
108
109 global $wpdb;
110 $dml_failed = 0;
111
112 // Handle REST API
113 $rest_api = $settings['rest_api'] ?? null;
114 $rest_api_settings = get_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS );
115 if ( false === $rest_api_settings ) {
116 $rest_api_settings = array();
117 }
118 if ( null !== $rest_api ) {
119 // Add table REST API
120 $rest_api_settings[ $schema_name ][ $table_name ] = $rest_api;
121 } else {
122 // Delete table REST API
123 unset($rest_api_settings[ $schema_name ][ $table_name ]);
124 }
125 update_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS, $rest_api_settings );
126 unset( $settings['rest_api'] );
127
128 // Handle Media columns
129 $media_columns = $settings['column_media'] ?? null;
130 if ( is_array( $media_columns ) ) {
131 foreach ( $media_columns as $column => $value ) {
132 if ( false === $value || '' === $value ) {
133 // Delete column media (no error handling)
134 WPDA_Media_Model::delete( $table_name, $column, $schema_name );
135 } elseif ( 'string' === gettype( $value) ) {
136 if ( 1 !== WPDA_Media_Model::update( $table_name, $column, $schema_name ) ) {
137 if ( ! WPDA_Media_Model::insert( $table_name, $column, $value, 'Yes', $schema_name ) ) {
138 $dml_failed++;
139 }
140 }
141 }
142 }
143 }
144 unset( $settings['column_media'] );
145
146 // Handle UPDATE | INSERT table and column settings
147 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
148 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
149 // Row found, perform update
150 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
151 foreach ( $settings_from_db as $key => $value ) {
152 if ( ! isset( $settings[ $key ] ) ) {
153 $settings[ $key ] = $value;
154 }
155 }
156
157 WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name );
158 if ( '' !== $wpdb->last_error ) {
159 $dml_failed++;
160 }
161 } else {
162 // No row found, insert new
163 if ( ! WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
164 $dml_failed++;
165 }
166 }
167
168 if ( 0 === $dml_failed ) {
169 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
170 } else {
171 if ( '' === $wpdb->last_error ) {
172 return new \WP_Error(
173 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
174 array( 'status' => 420 )
175 );
176 } else {
177 return new \WP_Error(
178 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
179 array( 'status' => 420 )
180 );
181 }
182 }
183
184 }
185
186 /**
187 * Save table settings.
188 *
189 * @param $schema_name
190 * @param $table_name
191 * @param $settings
192 * @return \WP_Error|\WP_REST_Response
193 */
194 private function save_table_settings( $schema_name, $table_name, $settings ) {
195 if (
196 isset( $settings['table_settings']['hyperlink_definition'] ) &&
197 isset( $settings['unused'] )
198 ) {
199 $sql_dml = $settings['unused'];
200
201 unset( $settings['request_type'] );
202 unset( $settings['unused'] );
203
204 global $wpdb;
205 if ( 'UPDATE' === $sql_dml ) {
206 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
207 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
208 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
209 foreach ( $settings_from_db as $key => $value ) {
210 if ( ! isset( $settings[ $key ] ) ) {
211 $settings[ $key ] = $value;
212 }
213 }
214 }
215
216 if (
217 1 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name ) ||
218 (
219 '' === $wpdb->last_error &&
220 0 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name )
221 )
222 ) {
223 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
224 } else {
225 return new \WP_Error(
226 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
227 array( 'status' => 420 )
228 );
229 }
230 } else {
231 if ( WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
232 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
233 } else {
234 return new \WP_Error(
235 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
236 array( 'status' => 420 )
237 );
238 }
239 }
240 } else {
241 // Nothing really to save, just to satisfy the user experience.
242 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
243 }
244 }
245
246 /**
247 * Save column settings.
248 *
249 * @param $schema_name
250 * @param $table_name
251 * @param $settings
252 * @return \WP_Error|\WP_REST_Response
253 */
254 private function save_column_settings( $schema_name, $table_name, $settings ) {
255 $sql_dml = null;
256 $dml_succeeded = 0;
257 $dml_failed = 0;
258
259 if ( isset( $settings['column_media'] ) ) {
260 // Process media columns.
261 $settings_column_media = $settings['column_media'];
262 foreach ( $settings_column_media as $column => $value ) {
263 if ( isset( $value['value'] ) && isset( $value['dml'] ) ) {
264 if ( 'INSERT' === $value['dml'] ) {
265 if ( WPDA_Media_Model::insert( $table_name, $column, $value['value'], 'Yes', $schema_name ) ) {
266 $dml_succeeded ++;
267 } else {
268 $dml_failed ++;
269 }
270 } elseif ( 'UPDATE' === $value['dml'] ) {
271 if ( '' === $value['value'] ) {
272 if ( 1 === WPDA_Media_Model::delete( $table_name, $column, $schema_name ) ) {
273 $dml_succeeded ++;
274 } else {
275 $dml_failed ++;
276 }
277 } else {
278 if ( 1 === WPDA_Media_Model::update( $table_name, $column, $value['value'], $schema_name ) ) {
279 $dml_succeeded ++;
280 } else {
281 $dml_failed ++;
282 }
283 }
284 }
285 }
286 }
287 unset( $settings['column_media'] );
288 }
289
290 if ( isset( $settings['unused'] ) ) {
291 $settings_unused = $settings['unused'];
292 if ( isset( $settings_unused['sql_dml'] ) ) {
293 $sql_dml = $settings_unused['sql_dml'];
294 }
295 unset( $settings['unused'] );
296 }
297
298 if ( null === $sql_dml ) {
299 return new \WP_Error(
300 sprintf(
301 __( 'Failed to save changes [%s]', 'wp-data-access' ),
302 'please contact the plugin development team'
303 ),
304 array( 'status' => 420 )
305 );
306 } else {
307 // Save settings.
308 if ( isset( $settings['request_type'] ) ) {
309 unset( $settings['request_type'] );
310 }
311
312 if ( 'UPDATE' === $sql_dml ) {
313 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
314 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
315 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
316 foreach ( $settings_from_db as $key => $value ) {
317 if ( ! isset( $settings[ $key ] ) ) {
318 $settings[ $key ] = $value;
319 }
320 }
321 }
322 if ( 1 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name ) ) {
323 $dml_succeeded ++;
324 }
325 } else {
326 if ( WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
327 $dml_succeeded ++;
328 } else {
329 $dml_failed ++;
330 }
331 }
332 }
333
334 if ( $dml_succeeded >= 0 && $dml_failed === 0 ) {
335 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
336 } else {
337 global $wpdb;
338 $msg = '' !== $wpdb->last_error ? " [{$wpdb->last_error}]" : '';
339
340 return new \WP_Error(
341 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $msg ),
342 array( 'status' => 420 )
343 );
344 }
345 }
346
347 /**
348 * Save dashboard menu changes.
349 *
350 * @param $schema_name
351 * @param $table_name
352 * @param $settings
353 * @return \WP_Error|\WP_REST_Response
354 */
355 private function save_dashboard_menus( $schema_name, $table_name, $settings ) {
356 $dml_succeeded = 0;
357 $dml_failed = 0;
358 $new_menus = array();
359
360 if ( isset( $settings['menu'] ) && is_array( $settings['menu'] ) ) {
361 // Process menu items.
362 foreach ( $settings['menu'] as $menu ) {
363 if ( isset( $menu['menu_name'] ) && isset( $menu['menu_slug'] ) ) {
364 if ( isset( $menu['menu_role'] ) ) {
365 if ( is_array( $menu['menu_role'] ) ) {
366 $menu_role = implode( ',', $menu['menu_role'] );
367 } else {
368 $menu_role = $menu['menu_role'];
369 }
370 } else {
371 $menu_role = '';
372 }
373 if ( isset( $menu['menu_id'] ) && '' === $menu['menu_id'] ) {
374 // Add new menu
375 if ( WPDA_User_Menus_Model::insert(
376 $table_name,
377 $menu['menu_name'],
378 $menu['menu_slug'],
379 $menu_role,
380 $schema_name
381 ) ) {
382 $dml_succeeded ++;
383
384 global $wpdb;
385 $new_menus[] = array(
386 'menu_name' => $menu['menu_name'],
387 'menu_slug' => $menu['menu_slug'],
388 'menu_id' => $wpdb->insert_id,
389 );
390 } else {
391 $dml_failed ++;
392 }
393 } else {
394 if ( isset( $menu['menu_id'] ) ) {
395 // Update existing menu.
396 $update_result = WPDA_User_Menus_Model::update(
397 $menu['menu_id'],
398 $table_name,
399 $menu['menu_name'],
400 $menu['menu_slug'],
401 $menu_role,
402 $schema_name
403 );
404 if ( 1 === $update_result ) {
405 $dml_succeeded ++;
406 }
407 }
408 }
409 }
410 }
411 unset( $settings['menu'] );
412 }
413
414 if ( isset( $settings['delete'] ) && is_array( $settings['delete'] ) ) {
415 // Process menu items to be deleted.
416 foreach ( $settings['delete'] as $menu ) {
417 if ( 1 === WPDA_User_Menus_Model::delete( $menu ) ) {
418 $dml_succeeded++;
419 } else {
420 $dml_failed++;
421 }
422 }
423 unset( $settings['delete'] );
424 }
425
426 if ( $dml_succeeded >= 0 && $dml_failed === 0 ) {
427 return $this->WPDA_Rest_Response(
428 sprintf( __( 'Saved dashboard menus for table `%s`', 'wp-data-access' ), $table_name ),
429 $new_menus
430 );
431 } else {
432 global $wpdb;
433 $msg = '' !== $wpdb->last_error ? " [{$wpdb->last_error}]" : '';
434
435 return new \WP_Error(
436 sprintf( __( 'Cannot save dashboard menus for table `%s`%s', 'wp-data-access' ), $table_name, $msg ),
437 array( 'status' => 420 )
438 );
439 }
440 }
441
442 /**
443 * Save rest api settings.
444 *
445 * @param $schema_name
446 * @param $table_name
447 * @param $settings
448 * @return \WP_Error|\WP_REST_Response
449 */
450 private function save_rest_api_settings( $schema_name, $table_name, $settings ) {
451 if ( ! isset( $settings['enabled'] ) ) {
452 return $this->bad_request();
453 }
454
455 $rest_api_settings = get_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS );
456 if ( false === $rest_api_settings ) {
457 $rest_api_settings = array();
458 }
459
460 if ( true === $settings['enabled'] ) {
461 // Add new | update existing REST API rule.
462 $actions = array( 'select', 'insert', 'update', 'delete' );
463 foreach ( $actions as $action ) {
464 if (
465 isset(
466 $settings[ $action ],
467 $settings[ $action ]['authorization'],
468 $settings[ $action ]['methods'],
469 $settings[ $action ]['authorized_roles'],
470 $settings[ $action ]['authorized_users']
471 )
472 ) {
473 unset( $rest_api_settings[ $schema_name ][ $table_name ][ $action ] ); // Unset previous authorization.
474 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorization'] = $settings[ $action ]['authorization'];
475 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['methods'] = $settings[ $action ]['methods'];
476 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorized_roles'] = $settings[ $action ]['authorized_roles'];
477 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorized_users'] = $settings[ $action ]['authorized_users'];
478 }
479 else {
480 return $this->bad_request();
481 }
482 }
483 } elseif ( false === $settings['enabled'] ) {
484 // Remove existing REST API rule.
485 if ( isset( $settings['select'] ) ) {
486 unset( $rest_api_settings[ $schema_name ][ $table_name ]['select'] );
487 if (
488 isset( $rest_api_settings[ $schema_name ][ $table_name ] ) &&
489 0 === count( $rest_api_settings[ $schema_name ][ $table_name ] )//phpcs:ignore - 8.1 proof
490 ) {
491 unset( $rest_api_settings[ $schema_name ][ $table_name ] );
492 if (
493 isset( $rest_api_settings[ $schema_name ] ) &&
494 0 === count( $rest_api_settings[ $schema_name ] )//phpcs:ignore - 8.1 proof
495 ) {
496 unset( $rest_api_settings[ $schema_name ] );
497 }
498 }
499 } else {
500 return $this->bad_request();
501 }
502 } else {
503 return $this->bad_request();
504 }
505
506 update_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS, $rest_api_settings );
507
508 return $this->WPDA_Rest_Response(
509 sprintf( __( 'Saved REST API settings for table `%s`', 'wp-data-access' ), $table_name )
510 );
511 }
512
513 private function save_admin_settings( $schema_name, $table_name, $settings, $theme ) {
514 if (
515 !
516 (
517 isset(
518 $settings['scope'],
519 $settings['target']
520 ) &&
521 (
522 isset(
523 $settings['data']
524 ) ||
525 null === $settings['data']
526 ) &&
527 (
528 'global' === $settings['scope'] ||
529 'user' === $settings['scope']
530 ) &&
531 (
532 'table' === $settings['target'] ||
533 'form' === $settings['target'] ||
534 'theme' === $settings['target']
535 )
536
537 )
538 ) {
539 return $this->bad_request();
540 }
541
542 $admin_settings = WPDA_Settings::get_admin_settings_key(
543 $settings['target'],
544 $schema_name ,
545 $table_name
546 );
547
548 if ( 'global' === $settings['scope'] ) {
549 // Store settings globally.
550 if ( null !== $settings['data'] ) {
551 update_option( $admin_settings, $settings['data'] );
552
553 if ( null !== $theme ) {
554 $theme_settings = WPDA_Settings::get_admin_settings_key(
555 'theme',
556 $schema_name ,
557 $table_name
558 );
559 update_option( $theme_settings, $theme );
560 }
561 } else {
562 delete_option( $admin_settings );
563 }
564 } else {
565 // Store settings for login user.
566 if ( null !== $settings['data'] ) {
567 update_user_option( $admin_settings, $settings['data'] );
568
569 if ( null !== $theme ) {
570 $theme_settings = WPDA_Settings::get_admin_settings_key(
571 'theme',
572 $schema_name ,
573 $table_name
574 );
575 update_user_option( $theme_settings, $theme );
576 }
577 } else {
578 delete_user_option( $admin_settings );
579 }
580 }
581
582 return $this->WPDA_Rest_Response( __( 'Successfully saved changes', 'wp-data-access' ) );
583 }
584
585 public static function get_admin_settings( $schema_name, $table_name ) {
586 return array(
587 'global' => array(
588 'table' => get_option(
589 WPDA_Settings::get_admin_settings_key(
590 'table',
591 $schema_name,
592 $table_name
593 )
594 ),
595 'form' => get_option(
596 WPDA_Settings::get_admin_settings_key(
597 'form',
598 $schema_name,
599 $table_name
600 )
601 ),
602 'theme' => get_option(
603 WPDA_Settings::get_admin_settings_key(
604 'theme',
605 $schema_name,
606 $table_name
607 )
608 ),
609 ),
610 'local' => array(
611 'table' => get_user_option(
612 WPDA_Settings::get_admin_settings_key(
613 'table',
614 $schema_name,
615 $table_name
616 )
617 ),
618 'form' => get_user_option(
619 WPDA_Settings::get_admin_settings_key(
620 'form',
621 $schema_name,
622 $table_name
623 )
624 ),
625 'theme' => get_user_option(
626 WPDA_Settings::get_admin_settings_key(
627 'theme',
628 $schema_name,
629 $table_name
630 )
631 ),
632 ),
633 );
634 }
635
636 private static function get_admin_settings_key( $target, $schema_name, $table_name ) {
637 return WPDA_Settings::WPDA_SETTINGS .
638 '-' . $target .
639 '-' . str_replace( ' ', '*', $schema_name ) .
640 '-' . str_replace( ' ', '*', $table_name );
641 }
642
643 }
644
645 }
646