PluginProbe
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps / 5.5.87
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps v5.5.87
5.5.87 5.5.85 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 All 162 releases
← All changes | WPDataAccess/API/WPDA_Apps.php +561 -130 5.5.40 → 5.5.87 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,227 @@
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 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/proxy', array(
580 + 'methods' => array('GET'),
581 + 'callback' => array($this, 'app_proxy'),
582 + 'permission_callback' => '__return_true',
583 + 'args' => array(
584 + 'app_id' => $this->get_param( 'app_id' ),
585 + 'cnt_id' => $this->get_param( 'cnt_id' ),
586 + 'url' => array(
587 + 'required' => true,
588 + 'type' => 'string',
589 + 'description' => __( 'URL', 'wp-data-access' ),
590 + 'sanitize_callback' => 'sanitize_text_field',
591 + 'validate_callback' => 'rest_validate_request_arg',
592 + ),
593 + ),
594 + ) );
595 + // PWA
596 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/pwa/get', array(
597 + 'methods' => array('POST'),
598 + 'callback' => array($this, 'app_wpa_get'),
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/pwa/activate', array(
605 + 'methods' => array('POST'),
606 + 'callback' => array($this, 'app_wpa_activate'),
607 + 'permission_callback' => '__return_true',
608 + 'args' => array(
609 + 'app_id' => $this->get_param( 'app_id' ),
610 + ),
611 + ) );
612 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/pwa/deactivate', array(
613 + 'methods' => array('POST'),
614 + 'callback' => array($this, 'app_wpa_deactivate'),
615 + 'permission_callback' => '__return_true',
616 + 'args' => array(
617 + 'app_id' => $this->get_param( 'app_id' ),
618 + ),
619 + ) );
620 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/upload', array(
621 + 'methods' => array('POST'),
622 + 'callback' => array($this, 'app_upload'),
623 + 'permission_callback' => '__return_true',
624 + 'args' => array(
625 + 'app_id' => $this->get_param( 'app_id' ),
626 + 'cnt_id' => $this->get_param( 'cnt_id' ),
627 + 'pk' => array(
628 + 'required' => true,
629 + 'type' => 'string',
630 + 'description' => __( 'Primary key in JSON format', 'wp-data-access' ),
631 + 'sanitize_callback' => 'sanitize_text_field',
632 + 'validate_callback' => 'rest_validate_request_arg',
633 + ),
634 + 'col' => $this->get_param( 'col' ),
635 + ),
636 + ) );
637 + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/download', array(
638 + 'methods' => array('POST'),
639 + 'callback' => array($this, 'app_download'),
640 + 'permission_callback' => '__return_true',
641 + 'args' => array(
642 + 'app_id' => $this->get_param( 'app_id' ),
643 + 'cnt_id' => $this->get_param( 'cnt_id' ),
644 + 'pk' => array(
645 + 'required' => true,
646 + 'type' => 'string',
647 + 'description' => __( 'Primary key in JSON format', 'wp-data-access' ),
648 + 'sanitize_callback' => 'sanitize_text_field',
649 + 'validate_callback' => 'rest_validate_request_arg',
650 + ),
651 + 'col' => $this->get_param( 'col' ),
652 + ),
653 + ) );
516 654 }
517 655
656 + public function app_proxy( $request ) {
657 + wp_die();
658 + }
659 +
660 + private function detect_image_mime( $body ) {
661 + return null;
662 + }
663 +
664 + private function is_safe_remote_url( $url, $allowed_hosts ) {
665 + return false;
666 + }
667 +
668 + private function is_public_ip( $ip ) {
669 + return false;
670 + }
671 +
672 + public function app_download( $request ) {
673 + return $this->WPDA_Rest_Response( 'OK' );
674 + }
675 +
676 + public function app_upload( $request ) {
677 + return $this->WPDA_Rest_Response( 'OK' );
678 + }
679 +
680 + public function app_wpa_deactivate( $request ) {
681 + if ( !$this->current_user_can_access() ) {
682 + // Only admins
683 + return $this->unauthorized();
684 + }
685 + if ( !$this->current_user_token_valid( $request ) ) {
686 + return $this->invalid_nonce();
687 + }
688 + return $this->WPDA_Rest_Response( 'OK' );
689 + }
690 +
691 + public function app_wpa_activate( $request ) {
692 + if ( !$this->current_user_can_access() ) {
693 + // Only admins
694 + return $this->unauthorized();
695 + }
696 + if ( !$this->current_user_token_valid( $request ) ) {
697 + return $this->invalid_nonce();
698 + }
699 + return $this->WPDA_Rest_Response( 'OK' );
700 + }
701 +
702 + public function app_wpa_get( $request ) {
703 + if ( !$this->current_user_can_access() ) {
704 + // Only admins
705 + return $this->unauthorized();
706 + }
707 + if ( !$this->current_user_token_valid( $request ) ) {
708 + return $this->invalid_nonce();
709 + }
710 + return $this->WPDA_Rest_Response( 'NOT FOUND' );
711 + }
712 +
713 + public function app_call( $request ) {
714 + return $this->bad_request();
715 + }
716 +
717 + public function app_lang_get( $request ) {
718 + if ( !$this->current_user_can_access() ) {
719 + // Only admins
720 + return $this->unauthorized();
721 + }
722 + if ( !$this->current_user_token_valid( $request ) ) {
723 + return $this->invalid_nonce();
724 + }
725 + return $this->WPDA_Rest_Response( '', WPDA_App_Localization::get() );
726 + }
727 +
728 + public function app_lang_set( $request ) {
729 + if ( !$this->current_user_can_access() ) {
730 + // Only admins
731 + return $this->unauthorized();
732 + }
733 + if ( !$this->current_user_token_valid( $request ) ) {
734 + return $this->invalid_nonce();
735 + }
736 + $localizations = $request->get_param( 'localizations' );
737 + WPDA_App_Localization::set( $localizations );
738 + return $this->WPDA_Rest_Response( 'Translation successfully saved' );
739 + }
740 +
518 741 private function get_app_columns( $columns ) {
519 742 if ( !is_array( $columns ) ) {
520 743 return false;
521 744 }
@@ -630,9 +853,9 @@
630 853 $tbl,
631 854 $msg,
632 855 $settings
633 856 ) ) {
634 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
857 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
635 858 if ( !isset( $container[0] ) ) {
636 859 return $this->bad_request();
637 860 }
638 861 return $this->get_app_container_meta( $app_id, $container, $rel_tab );
@@ -659,17 +882,17 @@
659 882 $tbl,
660 883 $msg,
661 884 $settings
662 885 ) ) {
663 - $container = WPDA_App_Container_Model::get_container( $cnt_id_from );
886 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_from );
664 887 if ( !isset( $container[0] ) ) {
665 888 return $this->bad_request();
666 889 }
667 - $container = WPDA_App_Container_Model::get_container( $cnt_id_to );
890 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_to );
668 891 if ( !isset( $container[0] ) ) {
669 892 return $this->bad_request();
670 893 }
671 - return $this->reorder_details( $cnt_id_from, $cnt_id_to );
894 + return $this->reorder_details( $app_id, $cnt_id_from, $cnt_id_to );
672 895 } else {
673 896 if ( 'rest_cookie_invalid_nonce' === $msg ) {
674 897 return $this->invalid_nonce();
675 898 } else {
@@ -749,8 +972,9 @@
749 972 $search_column_fns,
750 973 &$default_where,
751 974 &$lookups
752 975 ) {
976 + return $this->bad_request();
753 977 }
754 978
755 979 private function build_relationships(
756 980 $container,
@@ -757,8 +981,9 @@
757 981 &$m2m_relationship,
758 982 $tbl,
759 983 &$default_where
760 984 ) {
985 + return $this->bad_request();
761 986 }
762 987
763 988 public function app_select( $request ) {
764 989 $app_id = $request->get_param( 'app_id' );
@@ -781,12 +1006,18 @@
781 1006 $media = $request->get_param( 'media' );
782 1007 $rel_tab = $request->get_param( 'rel_tab' );
783 1008 $client_side = '1' === $request->get_param( 'client_side' );
784 1009 $geo_radius = $request->get_param( 'geo_radius' );
1010 + $docs = array();
785 1011 $default_where = '';
786 1012 $default_orderby = '';
787 1013 $lookups = array();
788 1014 $m2m_relationship = array();
1015 + if ( $client_side ) {
1016 + // Delete search values on refresh
1017 + $search = '';
1018 + $search_columns = array();
1019 + }
789 1020 if ( $this->check_app_access(
790 1021 $app_id,
791 1022 $cnt_id,
792 1023 'select',
@@ -794,9 +1025,9 @@
794 1025 $tbl,
795 1026 $msg,
796 1027 $settings
797 1028 ) ) {
798 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
1029 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
799 1030 $app = WPDA_App_Model::get_by_id( $app_id );
800 1031 if ( isset( $app[0]['app_type'], $container[0]['cnt_map'] ) && '2' == $app[0]['app_type'] && null !== $container[0]['cnt_map'] ) {
801 1032 // App = Map
802 1033 // Get default where map
@@ -825,8 +1056,27 @@
825 1056 }
826 1057 }
827 1058 }
828 1059 }
1060 + if ( isset( $settings['columns'] ) && is_array( $settings['columns'] ) && '1' !== $rel_tab ) {
1061 + $queryable_columns = array();
1062 + if ( isset( $settings['table']['columns'] ) && is_array( $settings['table']['columns'] ) ) {
1063 + foreach ( $settings['table']['columns'] as $column ) {
1064 + if ( isset( $column['queryable'] ) && $column['queryable'] ) {
1065 + $queryable_columns[] = $column['columnName'];
1066 + }
1067 + }
1068 + }
1069 + $app_columns = array();
1070 + foreach ( $settings['columns'] as $column ) {
1071 + if ( isset( $column['columnName'], $column['isSelected'] ) && $column['isSelected'] ) {
1072 + $app_columns[$column['columnName']] = in_array( $column['columnName'], $queryable_columns );
1073 + }
1074 + }
1075 + if ( 0 < count( $app_columns ) ) {
1076 + $col = $app_columns;
1077 + }
1078 + }
829 1079 $table_api = new WPDA_Table();
830 1080 return $table_api->select(
831 1081 $dbs,
832 1082 $tbl,
@@ -851,9 +1101,10 @@
851 1101 $md,
852 1102 $m2m_relationship,
853 1103 $search_data_types,
854 1104 $client_side,
855 - $geo_radius
1105 + $geo_radius,
1106 + $docs
856 1107 );
857 1108 } else {
858 1109 if ( 'rest_cookie_invalid_nonce' === $msg ) {
859 1110 return $this->invalid_nonce();
@@ -877,8 +1128,9 @@
877 1128 return array_map( function ( $value ) {
878 1129 if ( true === $value['isSelected'] ) {
879 1130 return $value['columnName'];
880 1131 }
1132 + return null;
881 1133 }, $columns );
882 1134 }
883 1135
884 1136 public function app_get( $request ) {
@@ -886,8 +1138,9 @@
886 1138 $cnt_id = $request->get_param( 'cnt_id' );
887 1139 $key = $request->get_param( 'key' );
888 1140 $media = $request->get_param( 'media' );
889 1141 $rel_tab = $request->get_param( 'rel_tab' );
1142 + $docs = array();
890 1143 if ( $this->check_app_access(
891 1144 $app_id,
892 1145 $cnt_id,
893 1146 'select',
@@ -907,9 +1160,10 @@
907 1160 $tbl,
908 1161 $key,
909 1162 $media,
910 1163 $column_names,
911 - $default_where
1164 + $default_where,
1165 + $docs
912 1166 );
913 1167 } else {
914 1168 if ( 'rest_cookie_invalid_nonce' === $msg ) {
915 1169 return $this->invalid_nonce();
@@ -998,53 +1252,9 @@
998 1252 }
999 1253 }
1000 1254
1001 1255 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 - }
1256 + return $this->bad_request();
1047 1257 }
1048 1258
1049 1259 public function app_delete( $request ) {
1050 1260 $app_id = $request->get_param( 'app_id' );
@@ -1093,8 +1303,9 @@
1093 1303 $search_params = $request->get_param( 'search_params' );
1094 1304 $shortcode_params = $request->get_param( 'shortcode_params' );
1095 1305 $md = $request->get_param( 'md' );
1096 1306 $cascade = $request->get_param( 'cascade' );
1307 + $values = $request->get_param( 'values' );
1097 1308 $default_where = '';
1098 1309 $default_where_lookup = '';
1099 1310 $lookups = array();
1100 1311 $m2m_relationship = array();
@@ -1106,9 +1317,9 @@
1106 1317 $tbl,
1107 1318 $msg,
1108 1319 $settings
1109 1320 ) ) {
1110 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
1321 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
1111 1322 if ( !isset( $container[0] ) ) {
1112 1323 return $this->bad_request();
1113 1324 }
1114 1325 $lookup = array();
@@ -1159,9 +1370,10 @@
1159 1370 $this->process_params(
1160 1371 $default_where_lookup,
1161 1372 $search_custom,
1162 1373 $search_params,
1163 - $shortcode_params
1374 + $shortcode_params,
1375 + $values
1164 1376 ),
1165 1377 '1' === $cascade,
1166 1378 $tbl,
1167 1379 $col,
@@ -1168,9 +1380,10 @@
1168 1380 $this->process_params(
1169 1381 $default_where,
1170 1382 $search_custom,
1171 1383 $search_params,
1172 - $shortcode_params
1384 + $shortcode_params,
1385 + $values
1173 1386 ),
1174 1387 $search,
1175 1388 $cols,
1176 1389 $search_columns,
@@ -1191,8 +1404,12 @@
1191 1404 }
1192 1405 }
1193 1406
1194 1407 public function app_lookup_dbs( $request ) {
1408 + // Only admins are allowed to configre lookups
1409 + if ( !$this->current_user_can_access() ) {
1410 + return $this->unauthorized();
1411 + }
1195 1412 $app_id = $request->get_param( 'app_id' );
1196 1413 $cnt_id = $request->get_param( 'cnt_id' );
1197 1414 if ( $this->check_app_access(
1198 1415 $app_id,
@@ -1215,8 +1432,12 @@
1215 1432 }
1216 1433 }
1217 1434
1218 1435 public function app_lookup_tbl( $request ) {
1436 + // Only admins are allowed to configre lookups
1437 + if ( !$this->current_user_can_access() ) {
1438 + return $this->unauthorized();
1439 + }
1219 1440 $app_id = $request->get_param( 'app_id' );
1220 1441 $cnt_id = $request->get_param( 'cnt_id' );
1221 1442 $dbs = $request->get_param( 'dbs' );
1222 1443 if ( $this->check_app_access(
@@ -1240,8 +1461,12 @@
1240 1461 }
1241 1462 }
1242 1463
1243 1464 public function app_lookup_cls( $request ) {
1465 + // Only admins are allowed to configre lookups
1466 + if ( !$this->current_user_can_access() ) {
1467 + return $this->unauthorized();
1468 + }
1244 1469 $app_id = $request->get_param( 'app_id' );
1245 1470 $cnt_id = $request->get_param( 'cnt_id' );
1246 1471 $dbs = $request->get_param( 'dbs' );
1247 1472 $tbl = $request->get_param( 'tbl' );
@@ -1287,8 +1512,71 @@
1287 1512 ));
1288 1513 }
1289 1514 global $wpdb;
1290 1515 $renamed = 0;
1516 + $debug_mode = 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG );
1517 + $debug = array();
1518 + $errors = array();
1519 + if ( $request->get_param( 'appDbId' ) ) {
1520 + // Update app only
1521 + $appDbId = $request->get_param( 'appDbId' );
1522 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1523 + $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) );
1524 + $result = $wpdb->query( $sql );
1525 + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
1526 + $renamed += $result;
1527 + if ( $debug_mode ) {
1528 + $debug[] = array(
1529 + 'sql' => $sql,
1530 + 'result' => $result,
1531 + );
1532 + }
1533 + if ( '' !== $wpdb->last_error ) {
1534 + $errors[] = array(
1535 + 'sql' => $sql,
1536 + 'error' => $wpdb->last_error,
1537 + );
1538 + }
1539 + $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");
1540 + foreach ( $sql_content as $sql ) {
1541 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1542 + $result = $wpdb->query( $wpdb->prepare( $sql, array(
1543 + $dbs_source,
1544 + $dbs_destination,
1545 + $dbs_source,
1546 + $appDbId
1547 + ) ) );
1548 + $renamed += $result;
1549 + if ( $debug_mode ) {
1550 + $debug[] = array(
1551 + 'sql' => $sql,
1552 + 'result' => $result,
1553 + );
1554 + }
1555 + if ( '' !== $wpdb->last_error ) {
1556 + $errors[] = array(
1557 + 'sql' => $sql,
1558 + 'error' => $wpdb->last_error,
1559 + );
1560 + }
1561 + }
1562 + $context = array();
1563 + if ( $debug_mode ) {
1564 + $context['debug'] = $debug;
1565 + }
1566 + if ( 0 < count( $errors ) ) {
1567 + $context['errors'] = $errors;
1568 + return new \WP_Error('error', 'Failed renaming database', array(
1569 + 'status' => 401,
1570 + 'context' => $context,
1571 + ));
1572 + }
1573 + return $this->WPDA_Rest_Response( sprintf(
1574 + /* translators: %s = number of database substitutions */
1575 + __( 'Successfully renamed %s database occurrences', 'wp-data-access' ),
1576 + $renamed
1577 + ), null, $context );
1578 + }
1291 1579 // Rename all occurrences in repository tables and apps
1292 1580 $sqls = array(
1293 1581 "update `{$wpdb->prefix}wpda_publisher` set `pub_schema_name` = %s where `pub_schema_name` = %s",
1294 1582 "update `{$wpdb->prefix}wpda_project_page` set `page_schema_name` = %s where `page_schema_name` = %s",
@@ -1296,18 +1584,62 @@
1296 1584 "update `{$wpdb->prefix}wpda_media` set `media_schema_name` = %s where `media_schema_name` = %s",
1297 1585 "update `{$wpdb->prefix}wpda_menus` set `menu_schema_name` = %s where `menu_schema_name` = %s",
1298 1586 "update `{$wpdb->prefix}wpda_table_design` set `wpda_schema_name` = %s where `wpda_schema_name` = %s",
1299 1587 "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"
1588 + "update `{$wpdb->prefix}wpda_app_container` set `cnt_dbs` = %s where `cnt_dbs` = %s"
1301 1589 );
1302 1590 foreach ( $sqls as $sql ) {
1303 - $renamed += $wpdb->query( $wpdb->prepare( $sql, array($dbs_destination, $dbs_source) ) );
1591 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1592 + $result = $wpdb->query( $wpdb->prepare( $sql, array($dbs_destination, $dbs_source) ) );
1593 + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
1594 + $renamed += $result;
1595 + if ( $debug_mode ) {
1596 + $debug[] = array(
1597 + 'sql' => $sql,
1598 + 'result' => $result,
1599 + );
1600 + }
1601 + if ( '' !== $wpdb->last_error ) {
1602 + $errors[] = array(
1603 + 'sql' => $sql,
1604 + 'error' => $wpdb->last_error,
1605 + );
1606 + }
1304 1607 }
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\"%'");
1608 + $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 1609 foreach ( $sql_content as $sql ) {
1307 - $renamed += $wpdb->query( $wpdb->prepare( $sql, array($dbs_source, $dbs_destination, $dbs_source) ) );
1610 + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table
1611 + $result = $wpdb->query( $wpdb->prepare( $sql, array($dbs_source, $dbs_destination, $dbs_source) ) );
1612 + $renamed += $result;
1613 + if ( $debug_mode ) {
1614 + $debug[] = array(
1615 + 'sql' => $sql,
1616 + 'result' => $result,
1617 + );
1618 + }
1619 + if ( '' !== $wpdb->last_error ) {
1620 + $errors[] = array(
1621 + 'sql' => $sql,
1622 + 'error' => $wpdb->last_error,
1623 + );
1624 + }
1308 1625 }
1309 - return $this->WPDA_Rest_Response( sprintf( __( 'Successfully renamed %s database occurrences', 'wp-data-access' ), $renamed ) );
1626 + $context = array();
1627 + if ( $debug_mode ) {
1628 + $context['debug'] = $debug;
1629 + }
1630 + if ( 0 < count( $errors ) ) {
1631 + $context['errors'] = $errors;
1632 + return new \WP_Error('error', 'Failed renaming database', array(
1633 + 'status' => 401,
1634 + 'context' => $context,
1635 + ));
1636 + }
1637 + return $this->WPDA_Rest_Response( sprintf(
1638 + /* translators: %s = number of database substitutions */
1639 + __( 'Successfully renamed %s database occurrences', 'wp-data-access' ),
1640 + $renamed
1641 + ), null, $context );
1310 1642 }
1311 1643
1312 1644 public function app_chart_data( $request ) {
1313 1645 $app_id = $request->get_param( 'app_id' );
@@ -1341,14 +1673,23 @@
1341 1673 ));
1342 1674 }
1343 1675 $suppress = $wpdadb->suppress_errors( true );
1344 1676 $chart_data = $wpdadb->get_results( $query, 'ARRAY_A' );
1677 + // Add an * before each label to support numeric labels
1678 + $chart_data_converted = array();
1679 + foreach ( $chart_data as $data ) {
1680 + $row = array();
1681 + foreach ( $data as $key => $value ) {
1682 + $row['*' . $key] = $value;
1683 + }
1684 + $chart_data_converted[] = $row;
1685 + }
1345 1686 $wpdadb->get_results( "create temporary table `wpda_chart_data_types` as {$query}", 'ARRAY_A' );
1346 1687 $explain = $wpdadb->get_results( "desc `wpda_chart_data_types`", 'ARRAY_A' );
1347 1688 $wpdadb->get_results( "drop temporary table `wpda_chart_data_types`", 'ARRAY_A' );
1348 1689 $wpdadb->suppress_errors( $suppress );
1349 1690 return array(
1350 - 'data' => $chart_data,
1691 + 'data' => $chart_data_converted,
1351 1692 'explain' => $explain,
1352 1693 );
1353 1694 } else {
1354 1695 return new \WP_Error('error', $msg, array(
@@ -1571,9 +1912,9 @@
1571 1912 $chart,
1572 1913 $map,
1573 1914 $theme
1574 1915 ) {
1575 - if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target && 'map' !== $target ) {
1916 + if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target && 'map' !== $target && 'dashboard' !== $target ) {
1576 1917 return $this->bad_request();
1577 1918 }
1578 1919 if ( null === $settings || '' === $settings ) {
1579 1920 // Perform reset
@@ -1617,8 +1958,16 @@
1617 1958 'status' => 403,
1618 1959 ));
1619 1960 }
1620 1961 break;
1962 + case 'dashboard':
1963 + $error_msg = WPDA_App_Container_Model::update_dashboard_settings( $app_id, null );
1964 + if ( '' !== $error_msg ) {
1965 + return new \WP_Error('error', $error_msg, array(
1966 + 'status' => 403,
1967 + ));
1968 + }
1969 + break;
1621 1970 default:
1622 1971 return $this->bad_request();
1623 1972 }
1624 1973 return $this->WPDA_Rest_Response( __( 'Reset was successful', 'wp-data-access' ) );
@@ -1680,8 +2029,17 @@
1680 2029 return new \WP_Error('error', $error_msg, array(
1681 2030 'status' => 403,
1682 2031 ));
1683 2032 }
2033 + } else {
2034 + if ( 'dashboard' === $target ) {
2035 + $error_msg = WPDA_App_Container_Model::update_dashboard_settings( $cnt_id, $settings );
2036 + if ( '' !== $error_msg ) {
2037 + return new \WP_Error('error', $error_msg, array(
2038 + 'status' => 403,
2039 + ));
2040 + }
2041 + }
1684 2042 }
1685 2043 }
1686 2044 }
1687 2045 }
@@ -1803,9 +2161,9 @@
1803 2161 private function get_relation_columns( $container ) {
1804 2162 return null;
1805 2163 }
1806 2164
1807 - private function reorder_details( $cnt_id_from, $cnt_id_to ) {
2165 + private function reorder_details( $app_id, $cnt_id_from, $cnt_id_to ) {
1808 2166 return $this->bad_request();
1809 2167 }
1810 2168
1811 2169 private function get_app_apps_meta( $app, $apps ) {
@@ -1812,8 +2170,9 @@
1812 2170 $app_id_details = array_map( function ( $e ) {
1813 2171 if ( isset( $e['app_id_detail'] ) ) {
1814 2172 return $e['app_id_detail'];
1815 2173 }
2174 + return null;
1816 2175 }, $apps );
1817 2176 $app_titles = array();
1818 2177 foreach ( $app_id_details as $app_id_detail ) {
1819 2178 $app_detail = WPDA_App_Model::get_by_id( $app_id_detail );
@@ -1848,18 +2207,46 @@
1848 2207 }
1849 2208 $settings->env = $this->get_env();
1850 2209 global $wpdb;
1851 2210 $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' ),
2211 + 'roles' => $this->get_wp_roles(),
2212 + 'users' => $this->get_wp_users(),
2213 + 'home' => admin_url( 'admin.php' ),
2214 + 'siteurl' => site_url(),
2215 + 'tables' => array_values( $wpdb->tables() ),
2216 + 'date_format' => get_option( 'date_format' ),
2217 + 'time_format' => get_option( 'time_format' ),
2218 + 'scroll_offset' => WPDA::get_option( WPDA::OPTION_APPS_SCROLL_OFFSET ),
2219 + 'upload' => @ini_get( 'upload_max_filesize' ),
2220 + 'uploadBytes' => $this->uploadToBytes( @ini_get( 'upload_max_filesize' ) ),
2221 + 'locale' => get_locale(),
1858 2222 ];
1859 2223 return $settings;
1860 2224 }
1861 2225
2226 + /**
2227 + * Convert a PHP shorthand notation (e.g., "2M", "512K", "1G") to bytes.
2228 + *
2229 + * @param string $val The shorthand size string.
2230 + *
2231 + * @return int The size in bytes.
2232 + */
2233 + private function uploadToBytes( $val ) : int {
2234 + $val = trim( $val );
2235 + $lastChar = strtolower( $val[strlen( $val ) - 1] );
2236 + $num = (float) $val;
2237 + switch ( $lastChar ) {
2238 + case 'g':
2239 + $num *= 1024;
2240 + case 'm':
2241 + $num *= 1024;
2242 + case 'k':
2243 + $num *= 1024;
2244 + break;
2245 + }
2246 + return (int) round( $num );
2247 + }
2248 +
1862 2249 private function get_app_container_meta( $app_id, $container, $rel_tab = false ) {
1863 2250 $app = WPDA_App_Model::get_by_id( $app_id );
1864 2251 if ( false === $app ) {
1865 2252 return $this->bad_request();
@@ -1890,10 +2277,18 @@
1890 2277 'update' => array(),
1891 2278 'delete' => array(),
1892 2279 );
1893 2280 $cls = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
2281 + $columns = $cls->get_table_columns();
2282 + $columns_sorted = array();
2283 + foreach ( $columns as $column ) {
2284 + if ( isset( $column['column_name'] ) ) {
2285 + $columns_sorted[$column['column_name']] = $column;
2286 + }
2287 + }
1894 2288 $media = $this->get_media( $dbs, $tbl, $cls->get_table_columns() );
1895 - $response['columns'] = $cls->get_table_columns();
2289 + $response['columns'] = $columns;
2290 + $response['columns_sorted'] = $columns_sorted;
1896 2291 $response['table_labels'] = $cls->get_table_header_labels();
1897 2292 $response['form_labels'] = $cls->get_table_column_headers();
1898 2293 $response['primary_key'] = $cls->get_table_primary_key();
1899 2294 $response['access'] = $access;
@@ -1928,8 +2323,17 @@
1928 2323 return $this->get_app_container_meta( $app_id, $container );
1929 2324 }
1930 2325 }
1931 2326
2327 + private function escapeUnicodeForExport( $str ) {
2328 + if ( empty( $str ) ) {
2329 + return $str;
2330 + }
2331 + return preg_replace_callback( '/\\\\(u[0-9a-fA-F]{4})/', function ( $matches ) {
2332 + return '\\\\' . $matches[1];
2333 + }, $str );
2334 + }
2335 +
1932 2336 private function do_app_export_app( $app_id, $main_app_id ) {
1933 2337 global $wpdb;
1934 2338 $quotes = function ( $value ) {
1935 2339 return str_replace( array(
@@ -1934,24 +2338,31 @@
1934 2338 $quotes = function ( $value ) {
1935 2339 return str_replace( array(
1936 2340 "'",
1937 2341 '\\"',
2342 + "\\\\t",
1938 2343 "\\t",
2344 + "\\\\n",
1939 2345 "\\n",
1940 2346 "\\r\\n",
1941 - "\\r"
2347 + "\\r",
2348 + "\\d"
1942 2349 ), array(
1943 2350 "''",
1944 2351 '\\\\"',
2352 + "\\\\\\t",
1945 2353 "\\\\t",
2354 + "\\\\\\n",
1946 2355 "\\\\n",
1947 2356 "\\\\r\\\\n",
1948 - "\\\\r"
2357 + "\\\\r",
2358 + "\\\\\\\\\\d"
1949 2359 ), $value );
1950 2360 };
1951 2361 $app = WPDA_App_Model::get_by_id( $app_id );
1952 2362 $app_settings = ( null === $app[0]['app_settings'] ? 'null' : "{$quotes( $app[0]['app_settings'] )}" );
1953 2363 $app_theme = ( null === $app[0]['app_theme'] ? 'null' : "{$quotes( $app[0]['app_theme'] )}" );
2364 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
1954 2365 $app_sql = <<<SQL
1955 2366 # Import app
1956 2367 insert into `{wp_prefix}wpda_app`
1957 2368 \t(`app_name`
@@ -1981,15 +2392,23 @@
1981 2392 SQL;
1982 2393 $containers = WPDA_App_Container_Model::select_all( $app_id );
1983 2394 $containers_sql = '';
1984 2395 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'] )}" );
2396 + $cnt_table = ( null === $container['cnt_table'] ? 'null' : "'{$quotes( $container['cnt_table'] )}'" );
2397 + $cnt_form = ( null === $container['cnt_form'] ? 'null' : "'{$quotes( $container['cnt_form'] )}'" );
2398 + $cnt_relation = ( null === $container['cnt_relation'] ? 'null' : "'{$quotes( $container['cnt_relation'] )}'" );
2399 + $cnt_rform = ( null === $container['cnt_rform'] ? 'null' : "'{$quotes( $container['cnt_rform'] )}'" );
2400 + $cnt_chart = ( null === $container['cnt_chart'] ? 'null' : "'{$quotes( $container['cnt_chart'] )}'" );
2401 + $cnt_map = ( null === $container['cnt_map'] ? 'null' : "'{$quotes( $container['cnt_map'] )}'" );
2402 + $cnt_query = ( null === $container['cnt_query'] ? 'null' : "'{$quotes( $container['cnt_query'] )}'" );
1988 2403 // Replace default WordPress database with conversion string
1989 2404 $cnt_dbs = ( $wpdb->dbname === $container['cnt_dbs'] ? '{wp_schema}' : "{$quotes( $container['cnt_dbs'] )}" );
1990 2405 $cnt_table = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_table );
1991 2406 $cnt_form = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_form );
2407 + // Replace Unicode characters
2408 + $cnt_table = $this->escapeUnicodeForExport( $cnt_table );
2409 + $cnt_form = $this->escapeUnicodeForExport( $cnt_form );
2410 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
1992 2411 $containers_sql .= <<<SQL
1993 2412 # Import app container
1994 2413 insert into `{wp_prefix}wpda_app_container`
1995 2414 \t(`cnt_dbs`
@@ -2000,8 +2419,12 @@
2000 2419 \t,`cnt_seq_nr`
2001 2420 \t,`cnt_table`
2002 2421 \t,`cnt_form`
2003 2422 \t,`cnt_relation`
2423 + ,`cnt_rform`
2424 + ,`cnt_chart`
2425 + ,`cnt_map`
2426 + ,`cnt_query`
2004 2427 \t)
2005 2428 values
2006 2429 \t('{$cnt_dbs}'
2007 2430 \t,'{$quotes( $container['cnt_tbl'] )}'
@@ -2008,11 +2431,15 @@
2008 2431 \t,'{$quotes( $container['cnt_cls'] )}'
2009 2432 \t,'{$quotes( $container['cnt_title'] )}'
2010 2433 \t,@APP_ID
2011 2434 \t,{$container['cnt_seq_nr']}
2012 -\t,'{$cnt_table}'
2013 -\t,'{$cnt_form}'
2014 -\t,'{$cnt_relation}'
2435 +\t,{$cnt_table}
2436 +\t,{$cnt_form}
2437 +\t,{$cnt_relation}
2438 +\t,{$cnt_rform}
2439 +\t,{$cnt_chart}
2440 +\t,{$cnt_map}
2441 +\t,{$cnt_query}
2015 2442 \t);
2016 2443
2017 2444 insert into `wpda_transfer_containers_{$main_app_id}`
2018 2445 values
@@ -2023,8 +2450,9 @@
2023 2450
2024 2451 SQL;
2025 2452 }
2026 2453 // Post update: update master container ids
2454 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2027 2455 $containers_sql .= <<<SQL
2028 2456 # Update app master container IDs
2029 2457 update `{wp_prefix}wpda_app_container` as a
2030 2458 set a.`cnt_relation` =
@@ -2047,8 +2475,9 @@
2047 2475 foreach ( $apps as $app ) {
2048 2476 $apps_sql .= $this->do_app_export_app( $app['app_id_detail'], $main_app_id );
2049 2477 }
2050 2478 foreach ( $apps as $app ) {
2479 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2051 2480 $apps_sql .= <<<SQL
2052 2481 # Import app relationships
2053 2482 insert into `{wp_prefix}wpda_app_apps`
2054 2483 \t(`app_id`
@@ -2069,8 +2498,9 @@
2069 2498
2070 2499 private function do_app_export( $app_id ) {
2071 2500 global $wpdb;
2072 2501 $sql = '';
2502 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2073 2503 $begin_sql = <<<SQL
2074 2504 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
2075 2505 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
2076 2506 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
@@ -2091,8 +2521,9 @@
2091 2521
2092 2522
2093 2523 SQL;
2094 2524 $sql .= $this->do_app_export_app( $app_id, $app_id );
2525 + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed
2095 2526 $end_sql = <<<SQL
2096 2527 # Drop temporary table
2097 2528 DROP TABLE `wpda_transfer_containers_{$app_id}`;
2098 2529 DROP TABLE `wpda_transfer_apps_{$app_id}`;
@@ -2185,9 +2616,9 @@
2185 2616 }
2186 2617 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
2187 2618 }
2188 2619
2189 - private function main_app_access( $app_id, &$msg = '' ) {
2620 + private function main_app_access( $app_id, &$msg = '', &$app = null ) {
2190 2621 // Get app info
2191 2622 $app = WPDA_App_Model::get_by_id( $app_id );
2192 2623 if ( false === $app ) {
2193 2624 // App not found
@@ -2230,9 +2661,9 @@
2230 2661 if ( !$this->main_app_access( $app_id, $msg ) ) {
2231 2662 return false;
2232 2663 }
2233 2664 // Get container
2234 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
2665 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
2235 2666 if ( !is_array( $container ) || 0 === count( $container ) ) {
2236 2667 // Container not found
2237 2668 $msg = __( 'Bad request', 'wp-data-access' );
2238 2669 return false;
@@ -2239,12 +2670,14 @@
2239 2670 }
2240 2671 if ( 'select' !== $action ) {
2241 2672 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
2242 2673 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;
2674 + if ( !isset( $cnt_table['table']['bulkActions'][$action] ) || false === $cnt_table['table']['bulkActions'][$action] ) {
2675 + $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
2676 + if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $app_id, $cnt_relation['cnt_id_master'], $action )) ) {
2677 + $msg = __( 'Unauthorized', 'wp-data-access' );
2678 + return false;
2679 + }
2247 2680 }
2248 2681 }
2249 2682 }
2250 2683 // Return database name, table name and columns
@@ -2257,10 +2690,10 @@
2257 2690 );
2258 2691 return true;
2259 2692 }
2260 2693
2261 - private function check_master_container_access( $cnt_id, $action ) {
2262 - $container = WPDA_App_Container_Model::get_container( $cnt_id );
2694 + private function check_master_container_access( $app_id, $cnt_id, $action ) {
2695 + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id );
2263 2696 if ( !is_array( $container ) || 0 === count( $container ) ) {
2264 2697 // Container not found
2265 2698 return false;
2266 2699 }
@@ -2266,9 +2699,9 @@
2266 2699 }
2267 2700 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
2268 2701 if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) {
2269 2702 $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 )) ) {
2703 + if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $app_id, $cnt_relation['cnt_id_master'], $action )) ) {
2271 2704 return false;
2272 2705 }
2273 2706 }
2274 2707 return true;
@@ -2277,18 +2710,21 @@
2277 2710 private function process_params(
2278 2711 $where,
2279 2712 $search_custom,
2280 2713 $search_params,
2281 - $shortcode_params
2714 + $shortcode_params,
2715 + $dynamic_params = array()
2282 2716 ) {
2283 2717 // Process $search_custom > URL parameters
2284 2718 global $wpdb;
2719 + $replacements = array();
2720 + $nonce = bin2hex( random_bytes( 8 ) );
2285 2721 foreach ( self::METHODS as $method ) {
2286 2722 $offset = 0;
2287 2723 $search = $method . '[';
2724 + // Get filter
2288 2725 while ( ($pos_start = stripos( $where, $search, $offset )) !== false ) {
2289 2726 if ( ($pos_end = stripos( $where, ']', $pos_start )) !== false ) {
2290 - // Get filter
2291 2727 $filter = substr( $where, $pos_start, $pos_end - $pos_start + 1 );
2292 2728 // Get name
2293 2729 $arg_name = substr( $where, $pos_start + strlen( $search ), $pos_end - $pos_start - strlen( $search ) );
2294 2730 // Remove quotes from name
@@ -2294,44 +2730,36 @@
2294 2730 // Remove quotes from name
2295 2731 if ( substr( $arg_name, 0, 1 ) === "'" && substr( $arg_name, -1 ) === "'" ) {
2296 2732 $arg_name = substr( $arg_name, 1, -1 );
2297 2733 }
2298 - // Remove double quotes from name
2734 + // Remove quotes from name
2299 2735 if ( substr( $arg_name, 0, 1 ) === '"' && substr( $arg_name, -1 ) === '"' ) {
2300 2736 $arg_name = substr( $arg_name, 1, -1 );
2301 2737 }
2738 + $arg_value = null;
2302 2739 // Handle GET args
2303 - if ( $method === self::METHODS[0] || $method === self::METHODS[2] ) {
2740 + if ( $method === self::METHODS[0] && isset( $search_custom['get'][$arg_name] ) ) {
2741 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['get'][$arg_name] ) );
2742 + } elseif ( $method === self::METHODS[1] && isset( $search_custom['post'][$arg_name] ) ) {
2743 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) );
2744 + } elseif ( $method === self::METHODS[2] ) {
2304 2745 if ( isset( $search_custom['get'][$arg_name] ) ) {
2305 2746 $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 - }
2747 + } elseif ( isset( $search_custom['post'][$arg_name] ) ) {
2748 + $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) );
2316 2749 }
2317 2750 }
2318 2751 // 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 - }
2752 + $placeholder = '###URL_PLACEHOLDER_' . $nonce . '_' . md5( $filter ) . '###';
2753 + $where = str_replace( $filter, $placeholder, $where );
2754 + // Handle REQUEST args
2755 + if ( $arg_value !== null ) {
2756 + $replacements[$placeholder] = $wpdb->prepare( '%s', $arg_value );
2757 + } else {
2758 + $replacements[$placeholder] = 'null';
2331 2759 }
2332 2760 }
2333 - $offset = $pos_start + 6;
2761 + $offset = $pos_start + 1;
2334 2762 if ( $offset > strlen( $where ) ) {
2335 2763 $offset = strlen( $where ) - 1;
2336 2764 }
2337 2765 }
@@ -2340,21 +2768,16 @@
2340 2768 if ( is_array( $search_params ) && 1 === count( $search_params ) ) {
2341 2769 $filter_field_name = $this->sanitize_db_identifier( array_keys( $search_params )[0] );
2342 2770 $filter_field_value = sanitize_text_field( $search_params[$filter_field_name] );
2343 2771 $filter_field_name_array = array_map( 'trim', explode( ',', $filter_field_name ) );
2344 - //phpcs:ignore - 8.1 proof
2345 2772 $filter_field_value_array = array_map( 'trim', explode( ',', $filter_field_value ) );
2346 - //phpcs:ignore - 8.1 proof
2347 2773 if ( count( $filter_field_name_array ) === count( $filter_field_value_array ) ) {
2348 - //phpcs:ignore - 8.1 proof
2349 2774 // Add filter to where clause.
2350 2775 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 - );
2776 + $where .= ( '' === $where ? '' : ' and ' );
2777 + $placeholder = '###SHORTCODE_PARAM_' . $nonce . '_' . md5( $filter_field_name_array[$i] ) . '###';
2778 + $where .= ' `' . $filter_field_name_array[$i] . '` like ' . $placeholder;
2779 + $replacements[$placeholder] = $wpdb->prepare( '%s', $filter_field_value_array[$i] );
2357 2780 }
2358 2781 }
2359 2782 }
2360 2783 // Substitute all shortcode parameters
@@ -2361,13 +2784,11 @@
2361 2784 if ( is_array( $shortcode_params ) ) {
2362 2785 foreach ( $shortcode_params as $column_name => $column_value ) {
2363 2786 $occurences = substr_count( strtolower( $where ), strtolower( "shortcodeParam['{$column_name}']" ) );
2364 2787 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 );
2788 + $placeholder = '###SHORTCODE_PARAM_' . $nonce . '_' . md5( $column_name ) . '###';
2789 + $where = str_ireplace( "shortcodeParam['{$column_name}']", $placeholder, $where );
2790 + $replacements[$placeholder] = $wpdb->prepare( '%s', sanitize_text_field( $column_value ) );
2370 2791 }
2371 2792 }
2372 2793 }
2373 2794 // Substitute all unused shortcode parameter calls with null
@@ -2382,8 +2803,18 @@
2382 2803 if ( $offset > strlen( $where ) ) {
2383 2804 $offset = strlen( $where ) - 1;
2384 2805 }
2385 2806 }
2386 - return $where;
2807 + // Substitute all dynamic parameters
2808 + if ( is_array( $dynamic_params ) && 0 < count( $dynamic_params ) ) {
2809 + foreach ( $dynamic_params as $column_name => $column_value ) {
2810 + if ( stripos( $where, "{:{$column_name}}" ) !== false ) {
2811 + $placeholder = '###DYNAMIC_PARAM_' . $nonce . '_' . md5( $column_name ) . '###';
2812 + $where = str_ireplace( "{:{$column_name}}", $placeholder, $where );
2813 + $replacements[$placeholder] = $wpdb->prepare( '%s', $column_value );
2814 + }
2815 + }
2816 + }
2817 + return strtr( $where, $replacements );
2387 2818 }
2388 2819
2389 2820 }