PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 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 All 160 releases
← All changes | WPDataAccess/API/WPDA_Apps.php +529 -130 5.5.405.5.84 View file →
@@ -8,8 +8,9 @@
8 8 use WPDataAccess\Plugin_Table_Models\WPDA_App_Container_Model;
9 9 use WPDataAccess\Plugin_Table_Models\WPDA_App_Apps_Model;
10 10 use WPDataAccess\Plugin_Table_Models\WPDA_App_Model;
11 11 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
12 +use WPDataAccess\Utilities\WPDA_App_Localization;
12 13 use WPDataAccess\WPDA;
13 14 class WPDA_Apps extends WPDA_API_Core {
14 15 const METHODS = array('httpGet', 'httpPost', 'httpRequest');
15 16
@@ -26,8 +27,9 @@
26 27 $value->{$property_name} = $this->sanitize_settings( $property_value );
27 28 }
28 29 } else {
29 30 // Allow HTML and onclick for computed fields
31 + // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
30 32 $value = apply_filters(
31 33 'wp_kses_post',
32 34 $value,
33 35 "",
@@ -32,8 +34,9 @@
32 34 $value,
33 35 "",
34 36 ["onclick"]
35 37 );
38 + // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
36 39 }
37 40 return $value;
38 41 }
39 42
@@ -277,9 +280,13 @@
277 280 'map' => array(
278 281 'required' => false,
279 282 'type' => 'string',
280 283 'description' => __( 'Map settings - JSON string', 'wp-data-access' ),
281 - 'sanitize_callback' => 'wp_kses_post',
284 + 'sanitize_callback' => function ( $param ) {
285 + $sanitized_settings = $this->sanitize_settings( json_decode( (string) $param, true ) );
286 + // Save sanitized JSON as string
287 + return json_encode( $sanitized_settings );
288 + },
282 289 'validate_callback' => 'rest_validate_request_arg',
283 290 ),
284 291 'chart' => array(
285 292 'required' => false,
@@ -456,8 +463,9 @@
456 463 'search_params' => $this->get_param( 'search_params' ),
457 464 'shortcode_params' => $this->get_param( 'search_params' ),
458 465 'md' => $this->get_param( 'md' ),
459 466 'cascade' => $this->get_param( 'cascade' ),
467 + 'values' => $this->get_param( 'md' ),
460 468 ),
461 469 ) );
462 470 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup/dbs', array(
463 471 'methods' => array('POST'),
@@ -510,12 +518,195 @@
510 518 'permission_callback' => '__return_true',
511 519 'args' => array(
512 520 'dbs_source' => $this->get_param( 'dbs' ),
513 521 'dbs_destination' => $this->get_param( 'dbs' ),
522 + 'appDbId' => array(
523 + 'required' => false,
524 + 'type' => 'integer',
525 + 'description' => __( 'App Id', 'wp-data-access' ),
526 + 'sanitize_callback' => 'absint',
527 + 'validate_callback' => 'rest_validate_request_arg',
528 + ),
514 529 ),
515 530 ) );
531 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lang/get', array(
532 + 'methods' => array('POST'),
533 + 'callback' => array($this, 'app_lang_get'),
534 + 'permission_callback' => '__return_true',
535 + ) );
536 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lang/set', array(
537 + 'methods' => array('POST'),
538 + 'callback' => array($this, 'app_lang_set'),
539 + 'permission_callback' => '__return_true',
540 + 'args' => array(
541 + 'localizations' => array(
542 + 'required' => true,
543 + 'type' => 'mixed',
544 + 'description' => __( 'Custom side translations (JSON as string)', 'wp-data-access' ),
545 + 'sanitize_callback' => 'sanitize_text_field',
546 + 'validate_callback' => 'rest_validate_request_arg',
547 + ),
548 + ),
549 + ) );
550 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/call', array(
551 + 'methods' => array('POST'),
552 + 'callback' => array($this, 'app_call'),
553 + 'permission_callback' => '__return_true',
554 + 'args' => array(
555 + 'app_id' => $this->get_param( 'app_id' ),
556 + 'cls' => array(
557 + 'required' => true,
558 + 'type' => 'string',
559 + 'description' => __( 'Class name', 'wp-data-access' ),
560 + 'sanitize_callback' => 'sanitize_text_field',
561 + 'validate_callback' => 'rest_validate_request_arg',
562 + ),
563 + 'fnc' => array(
564 + 'required' => true,
565 + 'type' => 'string',
566 + 'description' => __( 'Function name', 'wp-data-access' ),
567 + 'sanitize_callback' => 'sanitize_text_field',
568 + 'validate_callback' => 'rest_validate_request_arg',
569 + ),
570 + 'arg' => array(
571 + 'required' => true,
572 + 'type' => 'mixed',
573 + 'description' => __( 'Arguments', 'wp-data-access' ),
574 + 'sanitize_callback' => 'sanitize_text_field',
575 + 'validate_callback' => 'rest_validate_request_arg',
576 + ),
577 + ),
578 + ) );
579 + // PWA
580 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/pwa/get', array(
581 + 'methods' => array('POST'),
582 + 'callback' => array($this, 'app_wpa_get'),
583 + 'permission_callback' => '__return_true',
584 + 'args' => array(
585 + 'app_id' => $this->get_param( 'app_id' ),
586 + ),
587 + ) );
588 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/pwa/activate', array(
589 + 'methods' => array('POST'),
590 + 'callback' => array($this, 'app_wpa_activate'),
591 + 'permission_callback' => '__return_true',
592 + 'args' => array(
593 + 'app_id' => $this->get_param( 'app_id' ),
594 + ),
595 + ) );
596 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/pwa/deactivate', array(
597 + 'methods' => array('POST'),
598 + 'callback' => array($this, 'app_wpa_deactivate'),
599 + 'permission_callback' => '__return_true',
600 + 'args' => array(
601 + 'app_id' => $this->get_param( 'app_id' ),
602 + ),
603 + ) );
604 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/upload', array(
605 + 'methods' => array('POST'),
606 + 'callback' => array($this, 'app_upload'),
607 + 'permission_callback' => '__return_true',
608 + 'args' => array(
609 + 'app_id' => $this->get_param( 'app_id' ),
610 + 'cnt_id' => $this->get_param( 'cnt_id' ),
611 + 'pk' => array(
612 + 'required' => true,
613 + 'type' => 'string',
614 + 'description' => __( 'Primary key in JSON format', 'wp-data-access' ),
615 + 'sanitize_callback' => 'sanitize_text_field',
616 + 'validate_callback' => 'rest_validate_request_arg',
617 + ),
618 + 'col' => $this->get_param( 'col' ),
619 + ),
620 + ) );
621 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/download', array(
622 + 'methods' => array('POST'),
623 + 'callback' => array($this, 'app_download'),
624 + 'permission_callback' => '__return_true',
625 + 'args' => array(
626 + 'app_id' => $this->get_param( 'app_id' ),
627 + 'cnt_id' => $this->get_param( 'cnt_id' ),
628 + 'pk' => array(
629 + 'required' => true,
630 + 'type' => 'string',
631 + 'description' => __( 'Primary key in JSON format', 'wp-data-access' ),
632 + 'sanitize_callback' => 'sanitize_text_field',
633 + 'validate_callback' => 'rest_validate_request_arg',
634 + ),
635 + 'col' => $this->get_param( 'col' ),
636 + ),
637 + ) );
516 638 }
517 639
640 + public function app_download( $request ) {
641 + return $this->WPDA_Rest_Response( 'OK' );
642 + }
643 +
644 + public function app_upload( $request ) {
645 + return $this->WPDA_Rest_Response( 'OK' );
646 + }
647 +
648 + public function app_wpa_deactivate( $request ) {
649 + if ( !$this->current_user_can_access() ) {
650 + // Only admins
651 + return $this->unauthorized();
652 + }
653 + if ( !$this->current_user_token_valid( $request ) ) {
654 + return $this->invalid_nonce();
655 + }
656 + return $this->WPDA_Rest_Response( 'OK' );
657 + }
658 +
659 + public function app_wpa_activate( $request ) {
660 + if ( !$this->current_user_can_access() ) {
661 + // Only admins
662 + return $this->unauthorized();
663 + }
664 + if ( !$this->current_user_token_valid( $request ) ) {
665 + return $this->invalid_nonce();
666 + }
667 + return $this->WPDA_Rest_Response( 'OK' );
668 + }
669 +
670 + public function app_wpa_get( $request ) {
671 + if ( !$this->current_user_can_access() ) {
672 + // Only admins
673 + return $this->unauthorized();
674 + }
675 + if ( !$this->current_user_token_valid( $request ) ) {
676 + return $this->invalid_nonce();
677 + }
678 + return $this->WPDA_Rest_Response( 'NOT FOUND' );
679 + }
680 +
681 + public function app_call( $request ) {
682 + return $this->bad_request();
683 + }
684 +
685 + public function app_lang_get( $request ) {
686 + if ( !$this->current_user_can_access() ) {
687 + // Only admins
688 + return $this->unauthorized();
689 + }
690 + if ( !$this->current_user_token_valid( $request ) ) {
691 + return $this->invalid_nonce();
692 + }
693 + return $this->WPDA_Rest_Response( '', WPDA_App_Localization::get() );
694 + }
695 +
696 + public function app_lang_set( $request ) {
697 + if ( !$this->current_user_can_access() ) {
698 + // Only admins
699 + return $this->unauthorized();
700 + }
701 + if ( !$this->current_user_token_valid( $request ) ) {
702 + return $this->invalid_nonce();
703 + }
704 + $localizations = $request->get_param( 'localizations' );
705 + WPDA_App_Localization::set( $localizations );
706 + return $this->WPDA_Rest_Response( 'Translation successfully saved' );
707 + }
708 +
518 709 private function get_app_columns( $columns ) {
519 710 if ( !is_array( $columns ) ) {
520 711 return false;
521 712 }
@@ -630,9 +821,9 @@
630 821 $tbl,
631 822 $msg,
632 823 $settings
633 824 ) ) {
634 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
825 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
635 826 if ( !isset( $container[0] ) ) {
636 827 return $this->bad_request();
637 828 }
638 829 return $this->get_app_container_meta( $app_id, $container, $rel_tab );
@@ -659,17 +850,17 @@
659 850 $tbl,
660 851 $msg,
661 852 $settings
662 853 ) ) {
663 - $container = WPDA_App_Container_Model::get_container( $cnt_id_from );
854 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_from );
664 855 if ( !isset( $container[0] ) ) {
665 856 return $this->bad_request();
666 857 }
667 - $container = WPDA_App_Container_Model::get_container( $cnt_id_to );
858 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_to );
668 859 if ( !isset( $container[0] ) ) {
669 860 return $this->bad_request();
670 861 }
671 - return $this->reorder_details( $cnt_id_from, $cnt_id_to );
862 + return $this->reorder_details( $app_id, $cnt_id_from, $cnt_id_to );
672 863 } else {
673 864 if ( 'rest_cookie_invalid_nonce' === $msg ) {
674 865 return $this->invalid_nonce();
675 866 } else {
@@ -749,8 +940,9 @@
749 940 $search_column_fns,
750 941 &$default_where,
751 942 &$lookups
752 943 ) {
944 + return $this->bad_request();
753 945 }
754 946
755 947 private function build_relationships(
756 948 $container,
@@ -757,8 +949,9 @@
757 949 &$m2m_relationship,
758 950 $tbl,
759 951 &$default_where
760 952 ) {
953 + return $this->bad_request();
761 954 }
762 955
763 956 public function app_select( $request ) {
764 957 $app_id = $request->get_param( 'app_id' );
@@ -781,12 +974,18 @@
781 974 $media = $request->get_param( 'media' );
782 975 $rel_tab = $request->get_param( 'rel_tab' );
783 976 $client_side = '1' === $request->get_param( 'client_side' );
784 977 $geo_radius = $request->get_param( 'geo_radius' );
978 + $docs = array();
785 979 $default_where = '';
786 980 $default_orderby = '';
787 981 $lookups = array();
788 982 $m2m_relationship = array();
983 + if ( $client_side ) {
984 + // Delete search values on refresh
985 + $search = '';
986 + $search_columns = array();
987 + }
789 988 if ( $this->check_app_access(
790 989 $app_id,
791 990 $cnt_id,
792 991 'select',
@@ -794,9 +993,9 @@
794 993 $tbl,
795 994 $msg,
796 995 $settings
797 996 ) ) {
798 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
997 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
799 998 $app = WPDA_App_Model::get_by_id( $app_id );
800 999 if ( isset( $app[0]['app_type'], $container[0]['cnt_map'] ) && '2' == $app[0]['app_type'] && null !== $container[0]['cnt_map'] ) {
801 1000 // App = Map
802 1001 // Get default where map
@@ -825,8 +1024,27 @@
825 1024 }
826 1025 }
827 1026 }
828 1027 }
1028 + if ( isset( $settings['columns'] ) && is_array( $settings['columns'] ) && '1' !== $rel_tab ) {
1029 + $queryable_columns = array();
1030 + if ( isset( $settings['table']['columns'] ) && is_array( $settings['table']['columns'] ) ) {
1031 + foreach ( $settings['table']['columns'] as $column ) {
1032 + if ( isset( $column['queryable'] ) && $column['queryable'] ) {
1033 + $queryable_columns[] = $column['columnName'];
1034 + }
1035 + }
1036 + }
1037 + $app_columns = array();
1038 + foreach ( $settings['columns'] as $column ) {
1039 + if ( isset( $column['columnName'], $column['isSelected'] ) && $column['isSelected'] ) {
1040 + $app_columns[$column['columnName']] = in_array( $column['columnName'], $queryable_columns );
1041 + }
1042 + }
1043 + if ( 0 < count( $app_columns ) ) {
1044 + $col = $app_columns;
1045 + }
1046 + }
829 1047 $table_api = new WPDA_Table();
830 1048 return $table_api->select(
831 1049 $dbs,
832 1050 $tbl,
@@ -851,9 +1069,10 @@
851 1069 $md,
852 1070 $m2m_relationship,
853 1071 $search_data_types,
854 1072 $client_side,
855 - $geo_radius
1073 + $geo_radius,
1074 + $docs
856 1075 );
857 1076 } else {
858 1077 if ( 'rest_cookie_invalid_nonce' === $msg ) {
859 1078 return $this->invalid_nonce();
@@ -877,8 +1096,9 @@
877 1096 return array_map( function ( $value ) {
878 1097 if ( true === $value['isSelected'] ) {
879 1098 return $value['columnName'];
880 1099 }
1100 + return null;
881 1101 }, $columns );
882 1102 }
883 1103
884 1104 public function app_get( $request ) {
@@ -886,8 +1106,9 @@
886 1106 $cnt_id = $request->get_param( 'cnt_id' );
887 1107 $key = $request->get_param( 'key' );
888 1108 $media = $request->get_param( 'media' );
889 1109 $rel_tab = $request->get_param( 'rel_tab' );
1110 + $docs = array();
890 1111 if ( $this->check_app_access(
891 1112 $app_id,
892 1113 $cnt_id,
893 1114 'select',
@@ -907,9 +1128,10 @@
907 1128 $tbl,
908 1129 $key,
909 1130 $media,
910 1131 $column_names,
911 - $default_where
1132 + $default_where,
1133 + $docs
912 1134 );
913 1135 } else {
914 1136 if ( 'rest_cookie_invalid_nonce' === $msg ) {
915 1137 return $this->invalid_nonce();
@@ -998,53 +1220,9 @@
998 1220 }
999 1221 }
1000 1222
1001 1223 public function app_update_inline( $request ) {
1002 - $app_id = $request->get_param( 'app_id' );
1003 - $cnt_id = $request->get_param( 'cnt_id' );
1004 - $key = $request->get_param( 'key' );
1005 - $val = $request->get_param( 'val' );
1006 - if ( $this->check_app_access(
1007 - $app_id,
1008 - $cnt_id,
1009 - 'select',
1010 - $dbs,
1011 - $tbl,
1012 - $msg,
1013 - $settings
1014 - ) ) {
1015 - foreach ( $val as $column_name => $column ) {
1016 - $found = false;
1017 - if ( isset( $settings['table']['columns'] ) ) {
1018 - foreach ( $settings['table']['columns'] as $settings_column ) {
1019 - if ( isset( $settings_column['columnName'] ) && $column_name === $settings_column['columnName'] ) {
1020 - $found = true;
1021 - }
1022 - }
1023 - if ( !$found ) {
1024 - return $this->unauthorized();
1025 - }
1026 - }
1027 - }
1028 - $column_names = $this->get_app_form_columns( $settings );
1029 - if ( false === $column_names ) {
1030 - $column_names = array();
1031 - }
1032 - $table_api = new WPDA_Table();
1033 - return $table_api->update(
1034 - $dbs,
1035 - $tbl,
1036 - $key,
1037 - $val,
1038 - $column_names
1039 - );
1040 - } else {
1041 - if ( 'rest_cookie_invalid_nonce' === $msg ) {
1042 - return $this->invalid_nonce();
1043 - } else {
1044 - return $this->unauthorized();
1045 - }
1046 - }
1224 + return $this->bad_request();
1047 1225 }
1048 1226
1049 1227 public function app_delete( $request ) {
1050 1228 $app_id = $request->get_param( 'app_id' );
@@ -1093,8 +1271,9 @@
1093 1271 $search_params = $request->get_param( 'search_params' );
1094 1272 $shortcode_params = $request->get_param( 'shortcode_params' );
1095 1273 $md = $request->get_param( 'md' );
1096 1274 $cascade = $request->get_param( 'cascade' );
1275 + $values = $request->get_param( 'values' );
1097 1276 $default_where = '';
1098 1277 $default_where_lookup = '';
1099 1278 $lookups = array();
1100 1279 $m2m_relationship = array();
@@ -1106,9 +1285,9 @@
1106 1285 $tbl,
1107 1286 $msg,
1108 1287 $settings
1109 1288 ) ) {
1110 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
1289 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
1111 1290 if ( !isset( $container[0] ) ) {
1112 1291 return $this->bad_request();
1113 1292 }
1114 1293 $lookup = array();
@@ -1159,9 +1338,10 @@
1159 1338 $this->process_params(
1160 1339 $default_where_lookup,
1161 1340 $search_custom,
1162 1341 $search_params,
1163 - $shortcode_params
1342 + $shortcode_params,
1343 + $values
1164 1344 ),
1165 1345 '1' === $cascade,
1166 1346 $tbl,
1167 1347 $col,
@@ -1168,9 +1348,10 @@
1168 1348 $this->process_params(
1169 1349 $default_where,
1170 1350 $search_custom,
1171 1351 $search_params,
1172 - $shortcode_params
1352 + $shortcode_params,
1353 + $values
1173 1354 ),
1174 1355 $search,
1175 1356 $cols,
1176 1357 $search_columns,
@@ -1191,8 +1372,12 @@
1191 1372 }
1192 1373 }
1193 1374
1194 1375 public function app_lookup_dbs( $request ) {
1376 + // Only admins are allowed to configre lookups
1377 + if ( !$this->current_user_can_access() ) {
1378 + return $this->unauthorized();
1379 + }
1195 1380 $app_id = $request->get_param( 'app_id' );
1196 1381 $cnt_id = $request->get_param( 'cnt_id' );
1197 1382 if ( $this->check_app_access(
1198 1383 $app_id,
@@ -1215,8 +1400,12 @@
1215 1400 }
1216 1401 }
1217 1402
1218 1403 public function app_lookup_tbl( $request ) {
1404 + // Only admins are allowed to configre lookups
1405 + if ( !$this->current_user_can_access() ) {
1406 + return $this->unauthorized();
1407 + }
1219 1408 $app_id = $request->get_param( 'app_id' );
1220 1409 $cnt_id = $request->get_param( 'cnt_id' );
1221 1410 $dbs = $request->get_param( 'dbs' );
1222 1411 if ( $this->check_app_access(
@@ -1240,8 +1429,12 @@
1240 1429 }
1241 1430 }
1242 1431
1243 1432 public function app_lookup_cls( $request ) {
1433 + // Only admins are allowed to configre lookups
1434 + if ( !$this->current_user_can_access() ) {
1435 + return $this->unauthorized();
1436 + }
1244 1437 $app_id = $request->get_param( 'app_id' );
1245 1438 $cnt_id = $request->get_param( 'cnt_id' );
1246 1439 $dbs = $request->get_param( 'dbs' );
1247 1440 $tbl = $request->get_param( 'tbl' );
@@ -1287,8 +1480,71 @@
1287 1480 ));
1288 1481 }
1289 1482 global $wpdb;
1290 1483 $renamed = 0;
1484 + $debug_mode = 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG );
1485 + $debug = array();
1486 + $errors = array();
1487 + if ( $request->get_param( 'appDbId' ) ) {
1488 + // Update app only
1489 + $appDbId = $request->get_param( 'appDbId' );
1490 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1491 + $sql = $wpdb->prepare( "update `{$wpdb->prefix}wpda_app_container` set `cnt_dbs` = %s where `cnt_dbs` = %s and app_id = %d", array($dbs_destination, $dbs_source, $appDbId) );
1492 + $result = $wpdb->query( $sql );
1493 + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
1494 + $renamed += $result;
1495 + if ( $debug_mode ) {
1496 + $debug[] = array(
1497 + 'sql' => $sql,
1498 + 'result' => $result,
1499 + );
1500 + }
1501 + if ( '' !== $wpdb->last_error ) {
1502 + $errors[] = array(
1503 + 'sql' => $sql,
1504 + 'error' => $wpdb->last_error,
1505 + );
1506 + }
1507 + $sql_content = array("update `{$wpdb->prefix}wpda_app_container` set `cnt_table` = replace(`cnt_table`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_table` like '%\"dbs\":\"%1s\"%' and app_id = %d", "update `{$wpdb->prefix}wpda_app_container` set `cnt_form` = replace(`cnt_form`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_form` like '%\"dbs\":\"%1s\"%' and app_id = %d");
1508 + foreach ( $sql_content as $sql ) {
1509 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1510 + $result = $wpdb->query( $wpdb->prepare( $sql, array(
1511 + $dbs_source,
1512 + $dbs_destination,
1513 + $dbs_source,
1514 + $appDbId
1515 + ) ) );
1516 + $renamed += $result;
1517 + if ( $debug_mode ) {
1518 + $debug[] = array(
1519 + 'sql' => $sql,
1520 + 'result' => $result,
1521 + );
1522 + }
1523 + if ( '' !== $wpdb->last_error ) {
1524 + $errors[] = array(
1525 + 'sql' => $sql,
1526 + 'error' => $wpdb->last_error,
1527 + );
1528 + }
1529 + }
1530 + $context = array();
1531 + if ( $debug_mode ) {
1532 + $context['debug'] = $debug;
1533 + }
1534 + if ( 0 < count( $errors ) ) {
1535 + $context['errors'] = $errors;
1536 + return new \WP_Error('error', 'Failed renaming database', array(
1537 + 'status' => 401,
1538 + 'context' => $context,
1539 + ));
1540 + }
1541 + return $this->WPDA_Rest_Response( sprintf(
1542 + /* translators: %s = number of database substitutions */
1543 + __( 'Successfully renamed %s database occurrences', 'wp-data-access' ),
1544 + $renamed
1545 + ), null, $context );
1546 + }
1291 1547 // Rename all occurrences in repository tables and apps
1292 1548 $sqls = array(
1293 1549 "update `{$wpdb->prefix}wpda_publisher` set `pub_schema_name` = %s where `pub_schema_name` = %s",
1294 1550 "update `{$wpdb->prefix}wpda_project_page` set `page_schema_name` = %s where `page_schema_name` = %s",
@@ -1296,18 +1552,62 @@
1296 1552 "update `{$wpdb->prefix}wpda_media` set `media_schema_name` = %s where `media_schema_name` = %s",
1297 1553 "update `{$wpdb->prefix}wpda_menus` set `menu_schema_name` = %s where `menu_schema_name` = %s",
1298 1554 "update `{$wpdb->prefix}wpda_table_design` set `wpda_schema_name` = %s where `wpda_schema_name` = %s",
1299 1555 "update `{$wpdb->prefix}wpda_table_settings` set `wpda_schema_name` = %s where `wpda_schema_name` = %s",
1300 - "update `{$wpdb->prefix}wpda_container` set `cnt_dbs` = %s where `cnt_dbs` = %s"
1556 + "update `{$wpdb->prefix}wpda_app_container` set `cnt_dbs` = %s where `cnt_dbs` = %s"
1301 1557 );
1302 1558 foreach ( $sqls as $sql ) {
1303 - $renamed += $wpdb->query( $wpdb->prepare( $sql, array($dbs_destination, $dbs_source) ) );
1559 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1560 + $result = $wpdb->query( $wpdb->prepare( $sql, array($dbs_destination, $dbs_source) ) );
1561 + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
1562 + $renamed += $result;
1563 + if ( $debug_mode ) {
1564 + $debug[] = array(
1565 + 'sql' => $sql,
1566 + 'result' => $result,
1567 + );
1568 + }
1569 + if ( '' !== $wpdb->last_error ) {
1570 + $errors[] = array(
1571 + 'sql' => $sql,
1572 + 'error' => $wpdb->last_error,
1573 + );
1574 + }
1304 1575 }
1305 - $sql_content = array("update `{$wpdb->prefix}wpda_container` set `cnt_table` = replace(`cnt_table`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_table` like '%\"dbs\":\"%1s\"%'", "update `{$wpdb->prefix}wpda_container` set `cnt_form` = replace(`cnt_form`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_form` like '%\"dbs\":\"%1s\"%'");
1576 + $sql_content = array("update `{$wpdb->prefix}wpda_app_container` set `cnt_table` = replace(`cnt_table`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_table` like '%\"dbs\":\"%1s\"%'", "update `{$wpdb->prefix}wpda_app_container` set `cnt_form` = replace(`cnt_form`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_form` like '%\"dbs\":\"%1s\"%'");
1306 1577 foreach ( $sql_content as $sql ) {
1307 - $renamed += $wpdb->query( $wpdb->prepare( $sql, array($dbs_source, $dbs_destination, $dbs_source) ) );
1578 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1579 + $result = $wpdb->query( $wpdb->prepare( $sql, array($dbs_source, $dbs_destination, $dbs_source) ) );
1580 + $renamed += $result;
1581 + if ( $debug_mode ) {
1582 + $debug[] = array(
1583 + 'sql' => $sql,
1584 + 'result' => $result,
1585 + );
1586 + }
1587 + if ( '' !== $wpdb->last_error ) {
1588 + $errors[] = array(
1589 + 'sql' => $sql,
1590 + 'error' => $wpdb->last_error,
1591 + );
1592 + }
1308 1593 }
1309 - return $this->WPDA_Rest_Response( sprintf( __( 'Successfully renamed %s database occurrences', 'wp-data-access' ), $renamed ) );
1594 + $context = array();
1595 + if ( $debug_mode ) {
1596 + $context['debug'] = $debug;
1597 + }
1598 + if ( 0 < count( $errors ) ) {
1599 + $context['errors'] = $errors;
1600 + return new \WP_Error('error', 'Failed renaming database', array(
1601 + 'status' => 401,
1602 + 'context' => $context,
1603 + ));
1604 + }
1605 + return $this->WPDA_Rest_Response( sprintf(
1606 + /* translators: %s = number of database substitutions */
1607 + __( 'Successfully renamed %s database occurrences', 'wp-data-access' ),
1608 + $renamed
1609 + ), null, $context );
1310 1610 }
1311 1611
1312 1612 public function app_chart_data( $request ) {
1313 1613 $app_id = $request->get_param( 'app_id' );
@@ -1341,14 +1641,23 @@
1341 1641 ));
1342 1642 }
1343 1643 $suppress = $wpdadb->suppress_errors( true );
1344 1644 $chart_data = $wpdadb->get_results( $query, 'ARRAY_A' );
1645 + // Add an * before each label to support numeric labels
1646 + $chart_data_converted = array();
1647 + foreach ( $chart_data as $data ) {
1648 + $row = array();
1649 + foreach ( $data as $key => $value ) {
1650 + $row['*' . $key] = $value;
1651 + }
1652 + $chart_data_converted[] = $row;
1653 + }
1345 1654 $wpdadb->get_results( "create temporary table `wpda_chart_data_types` as {$query}", 'ARRAY_A' );
1346 1655 $explain = $wpdadb->get_results( "desc `wpda_chart_data_types`", 'ARRAY_A' );
1347 1656 $wpdadb->get_results( "drop temporary table `wpda_chart_data_types`", 'ARRAY_A' );
1348 1657 $wpdadb->suppress_errors( $suppress );
1349 1658 return array(
1350 - 'data' => $chart_data,
1659 + 'data' => $chart_data_converted,
1351 1660 'explain' => $explain,
1352 1661 );
1353 1662 } else {
1354 1663 return new \WP_Error('error', $msg, array(
@@ -1571,9 +1880,9 @@
1571 1880 $chart,
1572 1881 $map,
1573 1882 $theme
1574 1883 ) {
1575 - if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target && 'map' !== $target ) {
1884 + if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target && 'map' !== $target && 'dashboard' !== $target ) {
1576 1885 return $this->bad_request();
1577 1886 }
1578 1887 if ( null === $settings || '' === $settings ) {
1579 1888 // Perform reset
@@ -1617,8 +1926,16 @@
1617 1926 'status' => 403,
1618 1927 ));
1619 1928 }
1620 1929 break;
1930 + case 'dashboard':
1931 + $error_msg = WPDA_App_Container_Model::update_dashboard_settings( $app_id, null );
1932 + if ( '' !== $error_msg ) {
1933 + return new \WP_Error('error', $error_msg, array(
1934 + 'status' => 403,
1935 + ));
1936 + }
1937 + break;
1621 1938 default:
1622 1939 return $this->bad_request();
1623 1940 }
1624 1941 return $this->WPDA_Rest_Response( __( 'Reset was successful', 'wp-data-access' ) );
@@ -1680,8 +1997,17 @@
1680 1997 return new \WP_Error('error', $error_msg, array(
1681 1998 'status' => 403,
1682 1999 ));
1683 2000 }
2001 + } else {
2002 + if ( 'dashboard' === $target ) {
2003 + $error_msg = WPDA_App_Container_Model::update_dashboard_settings( $cnt_id, $settings );
2004 + if ( '' !== $error_msg ) {
2005 + return new \WP_Error('error', $error_msg, array(
2006 + 'status' => 403,
2007 + ));
2008 + }
2009 + }
1684 2010 }
1685 2011 }
1686 2012 }
1687 2013 }
@@ -1803,9 +2129,9 @@
1803 2129 private function get_relation_columns( $container ) {
1804 2130 return null;
1805 2131 }
1806 2132
1807 - private function reorder_details( $cnt_id_from, $cnt_id_to ) {
2133 + private function reorder_details( $app_id, $cnt_id_from, $cnt_id_to ) {
1808 2134 return $this->bad_request();
1809 2135 }
1810 2136
1811 2137 private function get_app_apps_meta( $app, $apps ) {
@@ -1812,8 +2138,9 @@
1812 2138 $app_id_details = array_map( function ( $e ) {
1813 2139 if ( isset( $e['app_id_detail'] ) ) {
1814 2140 return $e['app_id_detail'];
1815 2141 }
2142 + return null;
1816 2143 }, $apps );
1817 2144 $app_titles = array();
1818 2145 foreach ( $app_id_details as $app_id_detail ) {
1819 2146 $app_detail = WPDA_App_Model::get_by_id( $app_id_detail );
@@ -1848,18 +2175,46 @@
1848 2175 }
1849 2176 $settings->env = $this->get_env();
1850 2177 global $wpdb;
1851 2178 $settings->wp = [
1852 - 'roles' => $this->get_wp_roles(),
1853 - 'users' => $this->get_wp_users(),
1854 - 'home' => admin_url( 'admin.php' ),
1855 - 'tables' => array_values( $wpdb->tables() ),
1856 - 'date_format' => get_option( 'date_format' ),
1857 - 'time_format' => get_option( 'time_format' ),
2179 + 'roles' => $this->get_wp_roles(),
2180 + 'users' => $this->get_wp_users(),
2181 + 'home' => admin_url( 'admin.php' ),
2182 + 'siteurl' => site_url(),
2183 + 'tables' => array_values( $wpdb->tables() ),
2184 + 'date_format' => get_option( 'date_format' ),
2185 + 'time_format' => get_option( 'time_format' ),
2186 + 'scroll_offset' => WPDA::get_option( WPDA::OPTION_APPS_SCROLL_OFFSET ),
2187 + 'upload' => @ini_get( 'upload_max_filesize' ),
2188 + 'uploadBytes' => $this->uploadToBytes( @ini_get( 'upload_max_filesize' ) ),
2189 + 'locale' => get_locale(),
1858 2190 ];
1859 2191 return $settings;
1860 2192 }
1861 2193
2194 + /**
2195 + * Convert a PHP shorthand notation (e.g., "2M", "512K", "1G") to bytes.
2196 + *
2197 + * @param string $val The shorthand size string.
2198 + *
2199 + * @return int The size in bytes.
2200 + */
2201 + private function uploadToBytes( $val ) : int {
2202 + $val = trim( $val );
2203 + $lastChar = strtolower( $val[strlen( $val ) - 1] );
2204 + $num = (float) $val;
2205 + switch ( $lastChar ) {
2206 + case 'g':
2207 + $num *= 1024;
2208 + case 'm':
2209 + $num *= 1024;
2210 + case 'k':
2211 + $num *= 1024;
2212 + break;
2213 + }
2214 + return (int) round( $num );
2215 + }
2216 +
1862 2217 private function get_app_container_meta( $app_id, $container, $rel_tab = false ) {
1863 2218 $app = WPDA_App_Model::get_by_id( $app_id );
1864 2219 if ( false === $app ) {
1865 2220 return $this->bad_request();
@@ -1890,10 +2245,18 @@
1890 2245 'update' => array(),
1891 2246 'delete' => array(),
1892 2247 );
1893 2248 $cls = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
2249 + $columns = $cls->get_table_columns();
2250 + $columns_sorted = array();
2251 + foreach ( $columns as $column ) {
2252 + if ( isset( $column['column_name'] ) ) {
2253 + $columns_sorted[$column['column_name']] = $column;
2254 + }
2255 + }
1894 2256 $media = $this->get_media( $dbs, $tbl, $cls->get_table_columns() );
1895 - $response['columns'] = $cls->get_table_columns();
2257 + $response['columns'] = $columns;
2258 + $response['columns_sorted'] = $columns_sorted;
1896 2259 $response['table_labels'] = $cls->get_table_header_labels();
1897 2260 $response['form_labels'] = $cls->get_table_column_headers();
1898 2261 $response['primary_key'] = $cls->get_table_primary_key();
1899 2262 $response['access'] = $access;
@@ -1928,8 +2291,17 @@
1928 2291 return $this->get_app_container_meta( $app_id, $container );
1929 2292 }
1930 2293 }
1931 2294
2295 + private function escapeUnicodeForExport( $str ) {
2296 + if ( empty( $str ) ) {
2297 + return $str;
2298 + }
2299 + return preg_replace_callback( '/\\\\(u[0-9a-fA-F]{4})/', function ( $matches ) {
2300 + return '\\\\' . $matches[1];
2301 + }, $str );
2302 + }
2303 +
1932 2304 private function do_app_export_app( $app_id, $main_app_id ) {
1933 2305 global $wpdb;
1934 2306 $quotes = function ( $value ) {
1935 2307 return str_replace( array(
@@ -1934,24 +2306,31 @@
1934 2306 $quotes = function ( $value ) {
1935 2307 return str_replace( array(
1936 2308 "'",
1937 2309 '\\"',
2310 + "\\\\t",
1938 2311 "\\t",
2312 + "\\\\n",
1939 2313 "\\n",
1940 2314 "\\r\\n",
1941 - "\\r"
2315 + "\\r",
2316 + "\\d"
1942 2317 ), array(
1943 2318 "''",
1944 2319 '\\\\"',
2320 + "\\\\\\t",
1945 2321 "\\\\t",
2322 + "\\\\\\n",
1946 2323 "\\\\n",
1947 2324 "\\\\r\\\\n",
1948 - "\\\\r"
2325 + "\\\\r",
2326 + "\\\\\\\\\\d"
1949 2327 ), $value );
1950 2328 };
1951 2329 $app = WPDA_App_Model::get_by_id( $app_id );
1952 2330 $app_settings = ( null === $app[0]['app_settings'] ? 'null' : "{$quotes( $app[0]['app_settings'] )}" );
1953 2331 $app_theme = ( null === $app[0]['app_theme'] ? 'null' : "{$quotes( $app[0]['app_theme'] )}" );
2332 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
1954 2333 $app_sql = <<<SQL
1955 2334 # Import app
1956 2335 insert into `{wp_prefix}wpda_app`
1957 2336 \t(`app_name`
@@ -1981,15 +2360,23 @@
1981 2360 SQL;
1982 2361 $containers = WPDA_App_Container_Model::select_all( $app_id );
1983 2362 $containers_sql = '';
1984 2363 foreach ( $containers as $container ) {
1985 - $cnt_table = ( null === $container['cnt_table'] ? 'null' : "{$quotes( $container['cnt_table'] )}" );
1986 - $cnt_form = ( null === $container['cnt_form'] ? 'null' : "{$quotes( $container['cnt_form'] )}" );
1987 - $cnt_relation = ( null === $container['cnt_relation'] ? 'null' : "{$quotes( $container['cnt_relation'] )}" );
2364 + $cnt_table = ( null === $container['cnt_table'] ? 'null' : "'{$quotes( $container['cnt_table'] )}'" );
2365 + $cnt_form = ( null === $container['cnt_form'] ? 'null' : "'{$quotes( $container['cnt_form'] )}'" );
2366 + $cnt_relation = ( null === $container['cnt_relation'] ? 'null' : "'{$quotes( $container['cnt_relation'] )}'" );
2367 + $cnt_rform = ( null === $container['cnt_rform'] ? 'null' : "'{$quotes( $container['cnt_rform'] )}'" );
2368 + $cnt_chart = ( null === $container['cnt_chart'] ? 'null' : "'{$quotes( $container['cnt_chart'] )}'" );
2369 + $cnt_map = ( null === $container['cnt_map'] ? 'null' : "'{$quotes( $container['cnt_map'] )}'" );
2370 + $cnt_query = ( null === $container['cnt_query'] ? 'null' : "'{$quotes( $container['cnt_query'] )}'" );
1988 2371 // Replace default WordPress database with conversion string
1989 2372 $cnt_dbs = ( $wpdb->dbname === $container['cnt_dbs'] ? '{wp_schema}' : "{$quotes( $container['cnt_dbs'] )}" );
1990 2373 $cnt_table = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_table );
1991 2374 $cnt_form = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_form );
2375 + // Replace Unicode characters
2376 + $cnt_table = $this->escapeUnicodeForExport( $cnt_table );
2377 + $cnt_form = $this->escapeUnicodeForExport( $cnt_form );
2378 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
1992 2379 $containers_sql .= <<<SQL
1993 2380 # Import app container
1994 2381 insert into `{wp_prefix}wpda_app_container`
1995 2382 \t(`cnt_dbs`
@@ -2000,8 +2387,12 @@
2000 2387 \t,`cnt_seq_nr`
2001 2388 \t,`cnt_table`
2002 2389 \t,`cnt_form`
2003 2390 \t,`cnt_relation`
2391 + ,`cnt_rform`
2392 + ,`cnt_chart`
2393 + ,`cnt_map`
2394 + ,`cnt_query`
2004 2395 \t)
2005 2396 values
2006 2397 \t('{$cnt_dbs}'
2007 2398 \t,'{$quotes( $container['cnt_tbl'] )}'
@@ -2008,11 +2399,15 @@
2008 2399 \t,'{$quotes( $container['cnt_cls'] )}'
2009 2400 \t,'{$quotes( $container['cnt_title'] )}'
2010 2401 \t,@APP_ID
2011 2402 \t,{$container['cnt_seq_nr']}
2012 -\t,'{$cnt_table}'
2013 -\t,'{$cnt_form}'
2014 -\t,'{$cnt_relation}'
2403 +\t,{$cnt_table}
2404 +\t,{$cnt_form}
2405 +\t,{$cnt_relation}
2406 +\t,{$cnt_rform}
2407 +\t,{$cnt_chart}
2408 +\t,{$cnt_map}
2409 +\t,{$cnt_query}
2015 2410 \t);
2016 2411
2017 2412 insert into `wpda_transfer_containers_{$main_app_id}`
2018 2413 values
@@ -2023,8 +2418,9 @@
2023 2418
2024 2419 SQL;
2025 2420 }
2026 2421 // Post update: update master container ids
2422 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2027 2423 $containers_sql .= <<<SQL
2028 2424 # Update app master container IDs
2029 2425 update `{wp_prefix}wpda_app_container` as a
2030 2426 set a.`cnt_relation` =
@@ -2047,8 +2443,9 @@
2047 2443 foreach ( $apps as $app ) {
2048 2444 $apps_sql .= $this->do_app_export_app( $app['app_id_detail'], $main_app_id );
2049 2445 }
2050 2446 foreach ( $apps as $app ) {
2447 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2051 2448 $apps_sql .= <<<SQL
2052 2449 # Import app relationships
2053 2450 insert into `{wp_prefix}wpda_app_apps`
2054 2451 \t(`app_id`
@@ -2069,8 +2466,9 @@
2069 2466
2070 2467 private function do_app_export( $app_id ) {
2071 2468 global $wpdb;
2072 2469 $sql = '';
2470 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2073 2471 $begin_sql = <<<SQL
2074 2472 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
2075 2473 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
2076 2474 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
@@ -2091,8 +2489,9 @@
2091 2489
2092 2490
2093 2491 SQL;
2094 2492 $sql .= $this->do_app_export_app( $app_id, $app_id );
2493 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2095 2494 $end_sql = <<<SQL
2096 2495 # Drop temporary table
2097 2496 DROP TABLE `wpda_transfer_containers_{$app_id}`;
2098 2497 DROP TABLE `wpda_transfer_apps_{$app_id}`;
@@ -2185,9 +2584,9 @@
2185 2584 }
2186 2585 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
2187 2586 }
2188 2587
2189 - private function main_app_access( $app_id, &$msg = '' ) {
2588 + private function main_app_access( $app_id, &$msg = '', &$app = null ) {
2190 2589 // Get app info
2191 2590 $app = WPDA_App_Model::get_by_id( $app_id );
2192 2591 if ( false === $app ) {
2193 2592 // App not found
@@ -2230,9 +2629,9 @@
2230 2629 if ( !$this->main_app_access( $app_id, $msg ) ) {
2231 2630 return false;
2232 2631 }
2233 2632 // Get container
2234 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
2633 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
2235 2634 if ( !is_array( $container ) || 0 === count( $container ) ) {
2236 2635 // Container not found
2237 2636 $msg = __( 'Bad request', 'wp-data-access' );
2238 2637 return false;
@@ -2239,12 +2638,14 @@
2239 2638 }
2240 2639 if ( 'select' !== $action ) {
2241 2640 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
2242 2641 if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) {
2243 - $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
2244 - if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $cnt_relation['cnt_id_master'], $action )) ) {
2245 - $msg = __( 'Unauthorized', 'wp-data-access' );
2246 - return false;
2642 + if ( !isset( $cnt_table['table']['bulkActions'][$action] ) || false === $cnt_table['table']['bulkActions'][$action] ) {
2643 + $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
2644 + if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $app_id, $cnt_relation['cnt_id_master'], $action )) ) {
2645 + $msg = __( 'Unauthorized', 'wp-data-access' );
2646 + return false;
2647 + }
2247 2648 }
2248 2649 }
2249 2650 }
2250 2651 // Return database name, table name and columns
@@ -2257,10 +2658,10 @@
2257 2658 );
2258 2659 return true;
2259 2660 }
2260 2661
2261 - private function check_master_container_access( $cnt_id, $action ) {
2262 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
2662 + private function check_master_container_access( $app_id, $cnt_id, $action ) {
2663 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
2263 2664 if ( !is_array( $container ) || 0 === count( $container ) ) {
2264 2665 // Container not found
2265 2666 return false;
2266 2667 }
@@ -2266,9 +2667,9 @@
2266 2667 }
2267 2668 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
2268 2669 if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) {
2269 2670 $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
2270 - if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $cnt_relation['cnt_id_master'], $action )) ) {
2671 + if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $app_id, $cnt_relation['cnt_id_master'], $action )) ) {
2271 2672 return false;
2272 2673 }
2273 2674 }
2274 2675 return true;
@@ -2277,18 +2678,21 @@
2277 2678 private function process_params(
2278 2679 $where,
2279 2680 $search_custom,
2280 2681 $search_params,
2281 - $shortcode_params
2682 + $shortcode_params,
2683 + $dynamic_params = array()
2282 2684 ) {
2283 2685 // Process $search_custom > URL parameters
2284 2686 global $wpdb;
2687 + $replacements = array();
2688 + $nonce = bin2hex( random_bytes( 8 ) );
2285 2689 foreach ( self::METHODS as $method ) {
2286 2690 $offset = 0;
2287 2691 $search = $method . '[';
2692 + // Get filter
2288 2693 while ( ($pos_start = stripos( $where, $search, $offset )) !== false ) {
2289 2694 if ( ($pos_end = stripos( $where, ']', $pos_start )) !== false ) {
2290 - // Get filter
2291 2695 $filter = substr( $where, $pos_start, $pos_end - $pos_start + 1 );
2292 2696 // Get name
2293 2697 $arg_name = substr( $where, $pos_start + strlen( $search ), $pos_end - $pos_start - strlen( $search ) );
2294 2698 // Remove quotes from name
@@ -2294,44 +2698,36 @@
2294 2698 // Remove quotes from name
2295 2699 if ( substr( $arg_name, 0, 1 ) === "'" && substr( $arg_name, -1 ) === "'" ) {
2296 2700 $arg_name = substr( $arg_name, 1, -1 );
2297 2701 }
2298 - // Remove double quotes from name
2702 + // Remove quotes from name
2299 2703 if ( substr( $arg_name, 0, 1 ) === '"' && substr( $arg_name, -1 ) === '"' ) {
2300 2704 $arg_name = substr( $arg_name, 1, -1 );
2301 2705 }
2706 + $arg_value = null;
2302 2707 // Handle GET args
2303 - if ( $method === self::METHODS[0] || $method === self::METHODS[2] ) {
2708 + if ( $method === self::METHODS[0] && isset( $search_custom['get'][$arg_name] ) ) {
2709 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['get'][$arg_name] ) );
2710 + } elseif ( $method === self::METHODS[1] && isset( $search_custom['post'][$arg_name] ) ) {
2711 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) );
2712 + } elseif ( $method === self::METHODS[2] ) {
2304 2713 if ( isset( $search_custom['get'][$arg_name] ) ) {
2305 2714 $arg_value = sanitize_text_field( wp_unslash( $search_custom['get'][$arg_name] ) );
2306 - $where = $wpdb->prepare( substr_replace(
2307 - $where,
2308 - '%s',
2309 - $pos_start,
2310 - $pos_end - $pos_start + 1
2311 - ), $arg_value );
2312 - } else {
2313 - if ( $method === self::METHODS[0] ) {
2314 - $where = str_replace( $filter, 'null', $where );
2315 - }
2715 + } elseif ( isset( $search_custom['post'][$arg_name] ) ) {
2716 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) );
2316 2717 }
2317 2718 }
2318 2719 // Handle POST args
2319 - if ( $method === self::METHODS[1] || $method === self::METHODS[2] ) {
2320 - if ( isset( $search_custom['post'][$arg_name] ) ) {
2321 - $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) );
2322 - $where = $wpdb->prepare( substr_replace(
2323 - $where,
2324 - '%s',
2325 - $pos_start,
2326 - $pos_end - $pos_start + 1
2327 - ), $arg_value );
2328 - } else {
2329 - $where = str_replace( $filter, 'null', $where );
2330 - }
2720 + $placeholder = '###URL_PLACEHOLDER_' . $nonce . '_' . md5( $filter ) . '###';
2721 + $where = str_replace( $filter, $placeholder, $where );
2722 + // Handle REQUEST args
2723 + if ( $arg_value !== null ) {
2724 + $replacements[$placeholder] = $wpdb->prepare( '%s', $arg_value );
2725 + } else {
2726 + $replacements[$placeholder] = 'null';
2331 2727 }
2332 2728 }
2333 - $offset = $pos_start + 6;
2729 + $offset = $pos_start + 1;
2334 2730 if ( $offset > strlen( $where ) ) {
2335 2731 $offset = strlen( $where ) - 1;
2336 2732 }
2337 2733 }
@@ -2340,21 +2736,16 @@
2340 2736 if ( is_array( $search_params ) && 1 === count( $search_params ) ) {
2341 2737 $filter_field_name = $this->sanitize_db_identifier( array_keys( $search_params )[0] );
2342 2738 $filter_field_value = sanitize_text_field( $search_params[$filter_field_name] );
2343 2739 $filter_field_name_array = array_map( 'trim', explode( ',', $filter_field_name ) );
2344 - //phpcs:ignore - 8.1 proof
2345 2740 $filter_field_value_array = array_map( 'trim', explode( ',', $filter_field_value ) );
2346 - //phpcs:ignore - 8.1 proof
2347 2741 if ( count( $filter_field_name_array ) === count( $filter_field_value_array ) ) {
2348 - //phpcs:ignore - 8.1 proof
2349 2742 // Add filter to where clause.
2350 2743 for ($i = 0; $i < count( $filter_field_name_array ); $i++) {
2351 - // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall, Squiz.PHP.DisallowSizeFunctionsInLoops
2352 - $where .= (( '' === $where ? '' : ' and ' )) . $wpdb->prepare(
2353 - ' `%1s` like %s ',
2354 - // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
2355 - array($filter_field_name_array[$i], $filter_field_value_array[$i])
2356 - );
2744 + $where .= ( '' === $where ? '' : ' and ' );
2745 + $placeholder = '###SHORTCODE_PARAM_' . $nonce . '_' . md5( $filter_field_name_array[$i] ) . '###';
2746 + $where .= ' `' . $filter_field_name_array[$i] . '` like ' . $placeholder;
2747 + $replacements[$placeholder] = $wpdb->prepare( '%s', $filter_field_value_array[$i] );
2357 2748 }
2358 2749 }
2359 2750 }
2360 2751 // Substitute all shortcode parameters
@@ -2361,13 +2752,11 @@
2361 2752 if ( is_array( $shortcode_params ) ) {
2362 2753 foreach ( $shortcode_params as $column_name => $column_value ) {
2363 2754 $occurences = substr_count( strtolower( $where ), strtolower( "shortcodeParam['{$column_name}']" ) );
2364 2755 if ( 0 < $occurences ) {
2365 - $column_values = array();
2366 - for ($i = 0; $i < $occurences; $i++) {
2367 - $column_values[] = sanitize_text_field( $column_value );
2368 - }
2369 - $where = $wpdb->prepare( str_ireplace( "shortcodeParam['{$column_name}']", '%s', $where ), $column_values );
2756 + $placeholder = '###SHORTCODE_PARAM_' . $nonce . '_' . md5( $column_name ) . '###';
2757 + $where = str_ireplace( "shortcodeParam['{$column_name}']", $placeholder, $where );
2758 + $replacements[$placeholder] = $wpdb->prepare( '%s', sanitize_text_field( $column_value ) );
2370 2759 }
2371 2760 }
2372 2761 }
2373 2762 // Substitute all unused shortcode parameter calls with null
@@ -2382,8 +2771,18 @@
2382 2771 if ( $offset > strlen( $where ) ) {
2383 2772 $offset = strlen( $where ) - 1;
2384 2773 }
2385 2774 }
2386 - return $where;
2775 + // Substitute all dynamic parameters
2776 + if ( is_array( $dynamic_params ) && 0 < count( $dynamic_params ) ) {
2777 + foreach ( $dynamic_params as $column_name => $column_value ) {
2778 + if ( stripos( $where, "{:{$column_name}}" ) !== false ) {
2779 + $placeholder = '###DYNAMIC_PARAM_' . $nonce . '_' . md5( $column_name ) . '###';
2780 + $where = str_ireplace( "{:{$column_name}}", $placeholder, $where );
2781 + $replacements[$placeholder] = $wpdb->prepare( '%s', $column_value );
2782 + }
2783 + }
2784 + }
2785 + return strtr( $where, $replacements );
2387 2786 }
2388 2787
2389 2788 }