PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.73
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.73
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.73, at WPDataAccess/API/WPDA_Settings.php

616 lines 18.0 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() ) {
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 return $this->save_admin_settings( $dbs, $tbl, $settings );
98 case 'explorer_settings':
99 return $this->save_explorer_settings( $dbs, $tbl, $settings );
100 }
101
102 return $this->bad_request();
103
104 }
105
106 private function save_explorer_settings( $schema_name, $table_name, $settings ) {
107
108 global $wpdb;
109 $dml_failed = 0;
110
111 // Handle REST API
112 $rest_api = $settings['rest_api'] ?? null;
113 $rest_api_settings = get_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS );
114 if ( false === $rest_api_settings ) {
115 $rest_api_settings = array();
116 }
117 if ( null !== $rest_api ) {
118 // Add table REST API
119 $rest_api_settings[ $schema_name ][ $table_name ] = $rest_api;
120 } else {
121 // Delete table REST API
122 unset($rest_api_settings[ $schema_name ][ $table_name ]);
123 }
124 update_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS, $rest_api_settings );
125 unset( $settings['rest_api'] );
126
127 // Handle Media columns
128 $media_columns = $settings['column_media'] ?? null;
129 if ( is_array( $media_columns ) ) {
130 foreach ( $media_columns as $column => $value ) {
131 if ( false === $value || '' === $value ) {
132 // Delete column media (no error handling)
133 WPDA_Media_Model::delete( $table_name, $column, $schema_name );
134 } elseif ( 'string' === gettype( $value) ) {
135 if ( false === WPDA_Media_Model::get_column_media( $table_name, $column, $schema_name ) ) {
136 if ( ! WPDA_Media_Model::insert( $table_name, $column, $value, 'Yes', $schema_name ) ) {
137 $dml_failed++;
138 }
139 } else {
140 if ( false === WPDA_Media_Model::update( $table_name, $column, $value, $schema_name ) ) {
141 $dml_failed++;
142 }
143 }
144 }
145 }
146 }
147 unset( $settings['column_media'] );
148
149 // Handle UPDATE | INSERT table and column settings
150 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
151 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
152 // Row found, perform update
153 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
154 foreach ( $settings_from_db as $key => $value ) {
155 if ( ! isset( $settings[ $key ] ) ) {
156 $settings[ $key ] = $value;
157 }
158 }
159
160 WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name );
161 if ( '' !== $wpdb->last_error ) {
162 $dml_failed++;
163 }
164 } else {
165 // No row found, insert new
166 if ( ! WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
167 $dml_failed++;
168 }
169 }
170
171 if ( 0 === $dml_failed ) {
172 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
173 } else {
174 if ( '' === $wpdb->last_error ) {
175 return new \WP_Error(
176 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
177 array( 'status' => 420 )
178 );
179 } else {
180 return new \WP_Error(
181 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
182 array( 'status' => 420 )
183 );
184 }
185 }
186
187 }
188
189 /**
190 * Save table settings.
191 *
192 * @param $schema_name
193 * @param $table_name
194 * @param $settings
195 * @return \WP_Error|\WP_REST_Response
196 */
197 private function save_table_settings( $schema_name, $table_name, $settings ) {
198 if (
199 isset( $settings['table_settings']['hyperlink_definition'] ) &&
200 isset( $settings['unused'] )
201 ) {
202 $sql_dml = $settings['unused'];
203
204 unset( $settings['request_type'] );
205 unset( $settings['unused'] );
206
207 global $wpdb;
208 if ( 'UPDATE' === $sql_dml ) {
209 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
210 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
211 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
212 foreach ( $settings_from_db as $key => $value ) {
213 if ( ! isset( $settings[ $key ] ) ) {
214 $settings[ $key ] = $value;
215 }
216 }
217 }
218
219 if (
220 1 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name ) ||
221 (
222 '' === $wpdb->last_error &&
223 0 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name )
224 )
225 ) {
226 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
227 } else {
228 return new \WP_Error(
229 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
230 array( 'status' => 420 )
231 );
232 }
233 } else {
234 if ( WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
235 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
236 } else {
237 return new \WP_Error(
238 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $wpdb->last_error ),
239 array( 'status' => 420 )
240 );
241 }
242 }
243 } else {
244 // Nothing really to save, just to satisfy the user experience.
245 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
246 }
247 }
248
249 /**
250 * Save column settings.
251 *
252 * @param $schema_name
253 * @param $table_name
254 * @param $settings
255 * @return \WP_Error|\WP_REST_Response
256 */
257 private function save_column_settings( $schema_name, $table_name, $settings ) {
258 $sql_dml = null;
259 $dml_succeeded = 0;
260 $dml_failed = 0;
261
262 if ( isset( $settings['column_media'] ) ) {
263 // Process media columns.
264 $settings_column_media = $settings['column_media'];
265 foreach ( $settings_column_media as $column => $value ) {
266 if ( isset( $value['value'] ) && isset( $value['dml'] ) ) {
267 if ( 'INSERT' === $value['dml'] ) {
268 if ( WPDA_Media_Model::insert( $table_name, $column, $value['value'], 'Yes', $schema_name ) ) {
269 $dml_succeeded ++;
270 } else {
271 $dml_failed ++;
272 }
273 } elseif ( 'UPDATE' === $value['dml'] ) {
274 if ( '' === $value['value'] ) {
275 if ( 1 === WPDA_Media_Model::delete( $table_name, $column, $schema_name ) ) {
276 $dml_succeeded ++;
277 } else {
278 $dml_failed ++;
279 }
280 } else {
281 if ( 1 === WPDA_Media_Model::update( $table_name, $column, $value['value'], $schema_name ) ) {
282 $dml_succeeded ++;
283 } else {
284 $dml_failed ++;
285 }
286 }
287 }
288 }
289 }
290 unset( $settings['column_media'] );
291 }
292
293 if ( isset( $settings['unused'] ) ) {
294 $settings_unused = $settings['unused'];
295 if ( isset( $settings_unused['sql_dml'] ) ) {
296 $sql_dml = $settings_unused['sql_dml'];
297 }
298 unset( $settings['unused'] );
299 }
300
301 if ( null === $sql_dml ) {
302 return new \WP_Error(
303 sprintf(
304 __( 'Failed to save changes [%s]', 'wp-data-access' ),
305 'please contact the plugin development team'
306 ),
307 array( 'status' => 420 )
308 );
309 } else {
310 // Save settings.
311 if ( isset( $settings['request_type'] ) ) {
312 unset( $settings['request_type'] );
313 }
314
315 if ( 'UPDATE' === $sql_dml ) {
316 $settings_db = WPDA_Table_Settings_Model::query( $table_name, $schema_name );
317 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
318 $settings_from_db = json_decode( $settings_db[0]['wpda_table_settings'], true );
319 foreach ( $settings_from_db as $key => $value ) {
320 if ( ! isset( $settings[ $key ] ) ) {
321 $settings[ $key ] = $value;
322 }
323 }
324 }
325 if ( 1 === WPDA_Table_Settings_Model::update( $table_name, json_encode( $settings ), $schema_name ) ) {
326 $dml_succeeded ++;
327 }
328 } else {
329 if ( WPDA_Table_Settings_Model::insert( $table_name, json_encode( $settings ), $schema_name ) ) {
330 $dml_succeeded ++;
331 } else {
332 $dml_failed ++;
333 }
334 }
335 }
336
337 if ( $dml_succeeded >= 0 && $dml_failed === 0 ) {
338 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
339 } else {
340 global $wpdb;
341 $msg = '' !== $wpdb->last_error ? " [{$wpdb->last_error}]" : '';
342
343 return new \WP_Error(
344 sprintf( __( 'Failed to save changes [%s]', 'wp-data-access' ), $msg ),
345 array( 'status' => 420 )
346 );
347 }
348 }
349
350 /**
351 * Save dashboard menu changes.
352 *
353 * @param $schema_name
354 * @param $table_name
355 * @param $settings
356 * @return \WP_Error|\WP_REST_Response
357 */
358 private function save_dashboard_menus( $schema_name, $table_name, $settings ) {
359 $dml_succeeded = 0;
360 $dml_failed = 0;
361 $new_menus = array();
362
363 if ( isset( $settings['menu'] ) && is_array( $settings['menu'] ) ) {
364 // Process menu items.
365 foreach ( $settings['menu'] as $menu ) {
366 if ( isset( $menu['menu_name'] ) && isset( $menu['menu_slug'] ) ) {
367 if ( isset( $menu['menu_role'] ) ) {
368 if ( is_array( $menu['menu_role'] ) ) {
369 $menu_role = implode( ',', $menu['menu_role'] );
370 } else {
371 $menu_role = $menu['menu_role'];
372 }
373 } else {
374 $menu_role = '';
375 }
376 if ( isset( $menu['menu_id'] ) && '' === $menu['menu_id'] ) {
377 // Add new menu
378 if ( WPDA_User_Menus_Model::insert(
379 $table_name,
380 $menu['menu_name'],
381 $menu['menu_slug'],
382 $menu_role,
383 $schema_name
384 ) ) {
385 $dml_succeeded ++;
386
387 global $wpdb;
388 $new_menus[] = array(
389 'menu_name' => $menu['menu_name'],
390 'menu_slug' => $menu['menu_slug'],
391 'menu_id' => $wpdb->insert_id,
392 );
393 } else {
394 $dml_failed ++;
395 }
396 } else {
397 if ( isset( $menu['menu_id'] ) ) {
398 // Update existing menu.
399 $update_result = WPDA_User_Menus_Model::update(
400 $menu['menu_id'],
401 $table_name,
402 $menu['menu_name'],
403 $menu['menu_slug'],
404 $menu_role,
405 $schema_name
406 );
407 if ( 1 === $update_result ) {
408 $dml_succeeded ++;
409 }
410 }
411 }
412 }
413 }
414 unset( $settings['menu'] );
415 }
416
417 if ( isset( $settings['delete'] ) && is_array( $settings['delete'] ) ) {
418 // Process menu items to be deleted.
419 foreach ( $settings['delete'] as $menu ) {
420 if ( 1 === WPDA_User_Menus_Model::delete( $menu ) ) {
421 $dml_succeeded++;
422 } else {
423 $dml_failed++;
424 }
425 }
426 unset( $settings['delete'] );
427 }
428
429 if ( $dml_succeeded >= 0 && $dml_failed === 0 ) {
430 return $this->WPDA_Rest_Response(
431 sprintf( __( 'Saved dashboard menus for table `%s`', 'wp-data-access' ), $table_name ),
432 $new_menus
433 );
434 } else {
435 global $wpdb;
436 $msg = '' !== $wpdb->last_error ? " [{$wpdb->last_error}]" : '';
437
438 return new \WP_Error(
439 sprintf( __( 'Cannot save dashboard menus for table `%s`%s', 'wp-data-access' ), $table_name, $msg ),
440 array( 'status' => 420 )
441 );
442 }
443 }
444
445 /**
446 * Save rest api settings.
447 *
448 * @param $schema_name
449 * @param $table_name
450 * @param $settings
451 * @return \WP_Error|\WP_REST_Response
452 */
453 private function save_rest_api_settings( $schema_name, $table_name, $settings ) {
454 if ( ! isset( $settings['enabled'] ) ) {
455 return $this->bad_request();
456 }
457
458 $rest_api_settings = get_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS );
459 if ( false === $rest_api_settings ) {
460 $rest_api_settings = array();
461 }
462
463 if ( true === $settings['enabled'] ) {
464 // Add new | update existing REST API rule.
465 $actions = array( 'select', 'insert', 'update', 'delete' );
466 foreach ( $actions as $action ) {
467 if (
468 isset(
469 $settings[ $action ],
470 $settings[ $action ]['authorization'],
471 $settings[ $action ]['methods'],
472 $settings[ $action ]['authorized_roles'],
473 $settings[ $action ]['authorized_users']
474 )
475 ) {
476 unset( $rest_api_settings[ $schema_name ][ $table_name ][ $action ] ); // Unset previous authorization.
477 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorization'] = $settings[ $action ]['authorization'];
478 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['methods'] = $settings[ $action ]['methods'];
479 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorized_roles'] = $settings[ $action ]['authorized_roles'];
480 $rest_api_settings[ $schema_name ][ $table_name ][ $action ]['authorized_users'] = $settings[ $action ]['authorized_users'];
481 }
482 else {
483 return $this->bad_request();
484 }
485 }
486 } elseif ( false === $settings['enabled'] ) {
487 // Remove existing REST API rule.
488 if ( isset( $settings['select'] ) ) {
489 unset( $rest_api_settings[ $schema_name ][ $table_name ]['select'] );
490 if (
491 isset( $rest_api_settings[ $schema_name ][ $table_name ] ) &&
492 0 === count( $rest_api_settings[ $schema_name ][ $table_name ] )//phpcs:ignore - 8.1 proof
493 ) {
494 unset( $rest_api_settings[ $schema_name ][ $table_name ] );
495 if (
496 isset( $rest_api_settings[ $schema_name ] ) &&
497 0 === count( $rest_api_settings[ $schema_name ] )//phpcs:ignore - 8.1 proof
498 ) {
499 unset( $rest_api_settings[ $schema_name ] );
500 }
501 }
502 } else {
503 return $this->bad_request();
504 }
505 } else {
506 return $this->bad_request();
507 }
508
509 update_option( WPDA_API::WPDA_REST_API_TABLE_ACCESS, $rest_api_settings );
510
511 return $this->WPDA_Rest_Response(
512 sprintf( __( 'Saved REST API settings for table `%s`', 'wp-data-access' ), $table_name )
513 );
514 }
515
516 private function save_admin_settings( $schema_name, $table_name, $settings ) {
517 if (
518 !
519 (
520 isset(
521 $settings['scope'],
522 $settings['target']
523 ) &&
524 (
525 isset(
526 $settings['data']
527 ) ||
528 null === $settings['data']
529 ) &&
530 (
531 'global' === $settings['scope'] ||
532 'user' === $settings['scope']
533 ) &&
534 (
535 'table' === $settings['target'] ||
536 'form' === $settings['target']
537 )
538
539 )
540 ) {
541 return $this->bad_request();
542 }
543
544 $admin_settings = WPDA_Settings::get_admin_settings_key(
545 $settings['target'],
546 $schema_name ,
547 $table_name
548 );
549
550 if ( 'global' === $settings['scope'] ) {
551 // Store settings globally.
552 if ( null !== $settings['data'] ) {
553 update_option( $admin_settings, $settings['data'] );
554 } else {
555 delete_option( $admin_settings );
556 }
557 } else {
558 // Store settings for login user.
559 if ( null !== $settings['data'] ) {
560 update_user_option( $admin_settings, $settings['data'] );
561 } else {
562 delete_user_option( $admin_settings );
563 }
564 }
565
566 return $this->WPDA_Rest_Response( __( 'Successfully saved changes', 'wp-data-access' ) );
567 }
568
569 public static function get_admin_settings( $schema_name, $table_name ) {
570 return array(
571 'global' => array(
572 'table' => get_option(
573 WPDA_Settings::get_admin_settings_key(
574 'table',
575 $schema_name,
576 $table_name
577 )
578 ),
579 'form' => get_option(
580 WPDA_Settings::get_admin_settings_key(
581 'form',
582 $schema_name,
583 $table_name
584 )
585 ),
586 ),
587 'local' => array(
588 'table' => get_user_option(
589 WPDA_Settings::get_admin_settings_key(
590 'table',
591 $schema_name,
592 $table_name
593 )
594 ),
595 'form' => get_user_option(
596 WPDA_Settings::get_admin_settings_key(
597 'form',
598 $schema_name,
599 $table_name
600 )
601 ),
602 ),
603 );
604 }
605
606 private static function get_admin_settings_key( $target, $schema_name, $table_name ) {
607 return WPDA_Settings::WPDA_SETTINGS .
608 '-' . $target .
609 '-' . str_replace( ' ', '*', $schema_name ) .
610 '-' . str_replace( ' ', '*', $table_name );
611 }
612
613 }
614
615 }
616