| @@ -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 | |
| @@ -267,14 +270,32 @@ | ||
| 267 | 270 | 'required' => false, |
| 268 | 271 | 'type' => 'string', |
| 269 | 272 | 'description' => __( 'App settings - JSON string', 'wp-data-access' ), |
| 270 | 273 | 'sanitize_callback' => function ( $param ) { |
| 271 | - $satitized_settings = $this->sanitize_settings( json_decode( (string) $param, true ) ); | |
| 274 | + $sanitized_settings = $this->sanitize_settings( json_decode( (string) $param, true ) ); | |
| 272 | 275 | // Save sanitized JSON as string |
| 273 | - return json_encode( $satitized_settings ); | |
| 276 | + return json_encode( $sanitized_settings ); | |
| 274 | 277 | }, |
| 275 | 278 | 'validate_callback' => 'rest_validate_request_arg', |
| 276 | 279 | ), |
| 280 | + 'map' => array( | |
| 281 | + 'required' => false, | |
| 282 | + 'type' => 'string', | |
| 283 | + 'description' => __( 'Map settings - JSON string', 'wp-data-access' ), | |
| 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 | + }, | |
| 289 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 290 | + ), | |
| 291 | + 'chart' => array( | |
| 292 | + 'required' => false, | |
| 293 | + 'type' => 'string', | |
| 294 | + 'description' => __( 'Chart settings - JSON string', 'wp-data-access' ), | |
| 295 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 296 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 297 | + ), | |
| 277 | 298 | 'theme' => array( |
| 278 | 299 | 'required' => false, |
| 279 | 300 | 'type' => 'string', |
| 280 | 301 | 'description' => __( 'Theme settings - JSON string', 'wp-data-access' ), |
| @@ -307,14 +328,32 @@ | ||
| 307 | 328 | 'row_count' => $this->get_param( 'row_count' ), |
| 308 | 329 | 'row_count_estimate' => $this->get_param( 'row_count_estimate' ), |
| 309 | 330 | 'media' => $this->get_param( 'media' ), |
| 310 | 331 | 'rel_tab' => $this->get_param( 'rel_tab' ), |
| 311 | - 'client_side' => array( | |
| 332 | + 'client_side' => $this->get_param( 'client_side' ), | |
| 333 | + 'geo_radius' => array( | |
| 312 | 334 | 'required' => false, |
| 313 | - 'type' => 'boolean', | |
| 314 | - 'description' => __( 'Server side processing', 'wp-data-access' ), | |
| 315 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 316 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 335 | + 'type' => 'mixed', | |
| 336 | + 'description' => __( 'Geo radius segment', 'wp-data-access' ), | |
| 337 | + 'sanitize_callback' => function ( $param ) { | |
| 338 | + $geo_radius = array(); | |
| 339 | + if ( isset( | |
| 340 | + $param['col']['lat'], | |
| 341 | + $param['col']['lng'], | |
| 342 | + $param['loc']['lat'], | |
| 343 | + $param['loc']['lng'], | |
| 344 | + $param['radius'], | |
| 345 | + $param['unit'] | |
| 346 | + ) && is_numeric( $param['loc']['lat'] ) && is_numeric( $param['loc']['lng'] ) && is_numeric( $param['radius'] ) && ('km' === $param['unit'] || 'miles' === $param['unit']) ) { | |
| 347 | + $geo_radius['col']['lat'] = WPDA::remove_backticks( sanitize_text_field( $param['col']['lat'] ) ); | |
| 348 | + $geo_radius['col']['lng'] = WPDA::remove_backticks( sanitize_text_field( $param['col']['lng'] ) ); | |
| 349 | + $geo_radius['loc']['lat'] = (float) sanitize_text_field( $param['loc']['lat'] ); | |
| 350 | + $geo_radius['loc']['lng'] = (float) sanitize_text_field( $param['loc']['lng'] ); | |
| 351 | + $geo_radius['radius'] = (float) sanitize_text_field( $param['radius'] ); | |
| 352 | + $geo_radius['unit'] = sanitize_text_field( $param['unit'] ); | |
| 353 | + } | |
| 354 | + return $geo_radius; | |
| 355 | + }, | |
| 317 | 356 | ), |
| 318 | 357 | ), |
| 319 | 358 | ) ); |
| 320 | 359 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/get', array( |
| @@ -424,8 +463,9 @@ | ||
| 424 | 463 | 'search_params' => $this->get_param( 'search_params' ), |
| 425 | 464 | 'shortcode_params' => $this->get_param( 'search_params' ), |
| 426 | 465 | 'md' => $this->get_param( 'md' ), |
| 427 | 466 | 'cascade' => $this->get_param( 'cascade' ), |
| 467 | + 'values' => $this->get_param( 'md' ), | |
| 428 | 468 | ), |
| 429 | 469 | ) ); |
| 430 | 470 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup/dbs', array( |
| 431 | 471 | 'methods' => array('POST'), |
| @@ -466,9 +506,11 @@ | ||
| 466 | 506 | 'methods' => array('POST'), |
| 467 | 507 | 'callback' => array($this, 'app_chart_data'), |
| 468 | 508 | 'permission_callback' => '__return_true', |
| 469 | 509 | 'args' => array( |
| 470 | - 'app_id' => $this->get_param( 'app_id' ), | |
| 510 | + 'app_id' => $this->get_param( 'app_id' ), | |
| 511 | + 'search_custom' => $this->get_param( 'search_custom' ), | |
| 512 | + 'shortcode_params' => $this->get_param( 'search_params' ), | |
| 471 | 513 | ), |
| 472 | 514 | ) ); |
| 473 | 515 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/dbs/rename', array( |
| 474 | 516 | 'methods' => array('POST'), |
| @@ -476,12 +518,195 @@ | ||
| 476 | 518 | 'permission_callback' => '__return_true', |
| 477 | 519 | 'args' => array( |
| 478 | 520 | 'dbs_source' => $this->get_param( 'dbs' ), |
| 479 | 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 | + ), | |
| 480 | 529 | ), |
| 481 | 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 | + ) ); | |
| 482 | 638 | } |
| 483 | 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 | + | |
| 484 | 709 | private function get_app_columns( $columns ) { |
| 485 | 710 | if ( !is_array( $columns ) ) { |
| 486 | 711 | return false; |
| 487 | 712 | } |
| @@ -596,9 +821,9 @@ | ||
| 596 | 821 | $tbl, |
| 597 | 822 | $msg, |
| 598 | 823 | $settings |
| 599 | 824 | ) ) { |
| 600 | - $container = WPDA_App_Container_Model::get_container( $cnt_id ); | |
| 825 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id ); | |
| 601 | 826 | if ( !isset( $container[0] ) ) { |
| 602 | 827 | return $this->bad_request(); |
| 603 | 828 | } |
| 604 | 829 | return $this->get_app_container_meta( $app_id, $container, $rel_tab ); |
| @@ -625,17 +850,17 @@ | ||
| 625 | 850 | $tbl, |
| 626 | 851 | $msg, |
| 627 | 852 | $settings |
| 628 | 853 | ) ) { |
| 629 | - $container = WPDA_App_Container_Model::get_container( $cnt_id_from ); | |
| 854 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_from ); | |
| 630 | 855 | if ( !isset( $container[0] ) ) { |
| 631 | 856 | return $this->bad_request(); |
| 632 | 857 | } |
| 633 | - $container = WPDA_App_Container_Model::get_container( $cnt_id_to ); | |
| 858 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id_to ); | |
| 634 | 859 | if ( !isset( $container[0] ) ) { |
| 635 | 860 | return $this->bad_request(); |
| 636 | 861 | } |
| 637 | - return $this->reorder_details( $cnt_id_from, $cnt_id_to ); | |
| 862 | + return $this->reorder_details( $app_id, $cnt_id_from, $cnt_id_to ); | |
| 638 | 863 | } else { |
| 639 | 864 | if ( 'rest_cookie_invalid_nonce' === $msg ) { |
| 640 | 865 | return $this->invalid_nonce(); |
| 641 | 866 | } else { |
| @@ -715,8 +940,9 @@ | ||
| 715 | 940 | $search_column_fns, |
| 716 | 941 | &$default_where, |
| 717 | 942 | &$lookups |
| 718 | 943 | ) { |
| 944 | + return $this->bad_request(); | |
| 719 | 945 | } |
| 720 | 946 | |
| 721 | 947 | private function build_relationships( |
| 722 | 948 | $container, |
| @@ -723,8 +949,9 @@ | ||
| 723 | 949 | &$m2m_relationship, |
| 724 | 950 | $tbl, |
| 725 | 951 | &$default_where |
| 726 | 952 | ) { |
| 953 | + return $this->bad_request(); | |
| 727 | 954 | } |
| 728 | 955 | |
| 729 | 956 | public function app_select( $request ) { |
| 730 | 957 | $app_id = $request->get_param( 'app_id' ); |
| @@ -746,12 +973,19 @@ | ||
| 746 | 973 | $row_count_estimate = $request->get_param( 'row_count_estimate' ); |
| 747 | 974 | $media = $request->get_param( 'media' ); |
| 748 | 975 | $rel_tab = $request->get_param( 'rel_tab' ); |
| 749 | 976 | $client_side = '1' === $request->get_param( 'client_side' ); |
| 977 | + $geo_radius = $request->get_param( 'geo_radius' ); | |
| 978 | + $docs = array(); | |
| 750 | 979 | $default_where = ''; |
| 751 | 980 | $default_orderby = ''; |
| 752 | 981 | $lookups = array(); |
| 753 | 982 | $m2m_relationship = array(); |
| 983 | + if ( $client_side ) { | |
| 984 | + // Delete search values on refresh | |
| 985 | + $search = ''; | |
| 986 | + $search_columns = array(); | |
| 987 | + } | |
| 754 | 988 | if ( $this->check_app_access( |
| 755 | 989 | $app_id, |
| 756 | 990 | $cnt_id, |
| 757 | 991 | 'select', |
| @@ -759,27 +993,57 @@ | ||
| 759 | 993 | $tbl, |
| 760 | 994 | $msg, |
| 761 | 995 | $settings |
| 762 | 996 | ) ) { |
| 763 | - $container = WPDA_App_Container_Model::get_container( $cnt_id ); | |
| 764 | - if ( '1' === $rel_tab ) { | |
| 997 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id ); | |
| 998 | + $app = WPDA_App_Model::get_by_id( $app_id ); | |
| 999 | + if ( isset( $app[0]['app_type'], $container[0]['cnt_map'] ) && '2' == $app[0]['app_type'] && null !== $container[0]['cnt_map'] ) { | |
| 1000 | + // App = Map | |
| 1001 | + // Get default where map | |
| 1002 | + $map_json = json_decode( (string) $container[0]['cnt_map'], true ); | |
| 1003 | + if ( isset( $map_json['setup']['defaultWhere'] ) && null !== $map_json['setup']['defaultWhere'] && '' !== trim( $map_json['setup']['defaultWhere'] ) ) { | |
| 1004 | + $default_where = $map_json['setup']['defaultWhere']; | |
| 1005 | + } | |
| 765 | 1006 | } else { |
| 766 | - $table_settings = $settings['table'] ?? array(); | |
| 767 | - // Get default where clause | |
| 768 | - if ( isset( $table_settings['table']['defaultWhere'] ) ) { | |
| 769 | - $default_where = $table_settings['table']['defaultWhere']; | |
| 1007 | + // All other apps (not being a map) | |
| 1008 | + if ( '1' === $rel_tab ) { | |
| 1009 | + } else { | |
| 1010 | + $table_settings = $settings['table'] ?? array(); | |
| 1011 | + // Get default where clause | |
| 1012 | + if ( isset( $table_settings['table']['defaultWhere'] ) ) { | |
| 1013 | + $default_where = $table_settings['table']['defaultWhere']; | |
| 1014 | + } | |
| 1015 | + // Get default order by | |
| 1016 | + if ( isset( $table_settings['table']['defaultOrderBy'] ) ) { | |
| 1017 | + $default_orderby_db = $table_settings['table']['defaultOrderBy']; | |
| 1018 | + if ( is_array( $default_orderby_db ) ) { | |
| 1019 | + foreach ( $default_orderby_db as $orderby ) { | |
| 1020 | + if ( isset( $orderby['columnName'], $orderby['order'] ) && '' !== trim( $orderby['columnName'] ) ) { | |
| 1021 | + $default_orderby .= (( '' === $default_orderby ? 'order by ' : ',' )) . '`' . WPDA::remove_backticks( $orderby['columnName'] ) . '` ' . (( 'desc' === $orderby['order'] ? 'desc' : 'asc' )); | |
| 1022 | + } | |
| 1023 | + } | |
| 1024 | + } | |
| 1025 | + } | |
| 770 | 1026 | } |
| 771 | - // Get default order by | |
| 772 | - if ( isset( $table_settings['table']['defaultOrderBy'] ) ) { | |
| 773 | - $default_orderby_db = $table_settings['table']['defaultOrderBy']; | |
| 774 | - if ( is_array( $default_orderby_db ) ) { | |
| 775 | - foreach ( $default_orderby_db as $orderby ) { | |
| 776 | - if ( isset( $orderby['columnName'], $orderby['order'] ) && '' !== trim( $orderby['columnName'] ) ) { | |
| 777 | - $default_orderby .= (( '' === $default_orderby ? 'order by ' : ',' )) . '`' . WPDA::remove_backticks( $orderby['columnName'] ) . '` ' . (( 'desc' === $orderby['order'] ? 'desc' : 'asc' )); | |
| 778 | - } | |
| 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']; | |
| 779 | 1034 | } |
| 780 | 1035 | } |
| 781 | 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 | + } | |
| 782 | 1046 | } |
| 783 | 1047 | $table_api = new WPDA_Table(); |
| 784 | 1048 | return $table_api->select( |
| 785 | 1049 | $dbs, |
| @@ -804,9 +1068,11 @@ | ||
| 804 | 1068 | $lookups, |
| 805 | 1069 | $md, |
| 806 | 1070 | $m2m_relationship, |
| 807 | 1071 | $search_data_types, |
| 808 | - $client_side | |
| 1072 | + $client_side, | |
| 1073 | + $geo_radius, | |
| 1074 | + $docs | |
| 809 | 1075 | ); |
| 810 | 1076 | } else { |
| 811 | 1077 | if ( 'rest_cookie_invalid_nonce' === $msg ) { |
| 812 | 1078 | return $this->invalid_nonce(); |
| @@ -830,8 +1096,9 @@ | ||
| 830 | 1096 | return array_map( function ( $value ) { |
| 831 | 1097 | if ( true === $value['isSelected'] ) { |
| 832 | 1098 | return $value['columnName']; |
| 833 | 1099 | } |
| 1100 | + return null; | |
| 834 | 1101 | }, $columns ); |
| 835 | 1102 | } |
| 836 | 1103 | |
| 837 | 1104 | public function app_get( $request ) { |
| @@ -839,8 +1106,9 @@ | ||
| 839 | 1106 | $cnt_id = $request->get_param( 'cnt_id' ); |
| 840 | 1107 | $key = $request->get_param( 'key' ); |
| 841 | 1108 | $media = $request->get_param( 'media' ); |
| 842 | 1109 | $rel_tab = $request->get_param( 'rel_tab' ); |
| 1110 | + $docs = array(); | |
| 843 | 1111 | if ( $this->check_app_access( |
| 844 | 1112 | $app_id, |
| 845 | 1113 | $cnt_id, |
| 846 | 1114 | 'select', |
| @@ -860,9 +1128,10 @@ | ||
| 860 | 1128 | $tbl, |
| 861 | 1129 | $key, |
| 862 | 1130 | $media, |
| 863 | 1131 | $column_names, |
| 864 | - $default_where | |
| 1132 | + $default_where, | |
| 1133 | + $docs | |
| 865 | 1134 | ); |
| 866 | 1135 | } else { |
| 867 | 1136 | if ( 'rest_cookie_invalid_nonce' === $msg ) { |
| 868 | 1137 | return $this->invalid_nonce(); |
| @@ -951,53 +1220,9 @@ | ||
| 951 | 1220 | } |
| 952 | 1221 | } |
| 953 | 1222 | |
| 954 | 1223 | public function app_update_inline( $request ) { |
| 955 | - $app_id = $request->get_param( 'app_id' ); | |
| 956 | - $cnt_id = $request->get_param( 'cnt_id' ); | |
| 957 | - $key = $request->get_param( 'key' ); | |
| 958 | - $val = $request->get_param( 'val' ); | |
| 959 | - if ( $this->check_app_access( | |
| 960 | - $app_id, | |
| 961 | - $cnt_id, | |
| 962 | - 'select', | |
| 963 | - $dbs, | |
| 964 | - $tbl, | |
| 965 | - $msg, | |
| 966 | - $settings | |
| 967 | - ) ) { | |
| 968 | - foreach ( $val as $column_name => $column ) { | |
| 969 | - $found = false; | |
| 970 | - if ( isset( $settings['table']['columns'] ) ) { | |
| 971 | - foreach ( $settings['table']['columns'] as $settings_column ) { | |
| 972 | - if ( isset( $settings_column['columnName'] ) && $column_name === $settings_column['columnName'] ) { | |
| 973 | - $found = true; | |
| 974 | - } | |
| 975 | - } | |
| 976 | - if ( !$found ) { | |
| 977 | - return $this->unauthorized(); | |
| 978 | - } | |
| 979 | - } | |
| 980 | - } | |
| 981 | - $column_names = $this->get_app_form_columns( $settings ); | |
| 982 | - if ( false === $column_names ) { | |
| 983 | - $column_names = array(); | |
| 984 | - } | |
| 985 | - $table_api = new WPDA_Table(); | |
| 986 | - return $table_api->update( | |
| 987 | - $dbs, | |
| 988 | - $tbl, | |
| 989 | - $key, | |
| 990 | - $val, | |
| 991 | - $column_names | |
| 992 | - ); | |
| 993 | - } else { | |
| 994 | - if ( 'rest_cookie_invalid_nonce' === $msg ) { | |
| 995 | - return $this->invalid_nonce(); | |
| 996 | - } else { | |
| 997 | - return $this->unauthorized(); | |
| 998 | - } | |
| 999 | - } | |
| 1224 | + return $this->bad_request(); | |
| 1000 | 1225 | } |
| 1001 | 1226 | |
| 1002 | 1227 | public function app_delete( $request ) { |
| 1003 | 1228 | $app_id = $request->get_param( 'app_id' ); |
| @@ -1046,8 +1271,9 @@ | ||
| 1046 | 1271 | $search_params = $request->get_param( 'search_params' ); |
| 1047 | 1272 | $shortcode_params = $request->get_param( 'shortcode_params' ); |
| 1048 | 1273 | $md = $request->get_param( 'md' ); |
| 1049 | 1274 | $cascade = $request->get_param( 'cascade' ); |
| 1275 | + $values = $request->get_param( 'values' ); | |
| 1050 | 1276 | $default_where = ''; |
| 1051 | 1277 | $default_where_lookup = ''; |
| 1052 | 1278 | $lookups = array(); |
| 1053 | 1279 | $m2m_relationship = array(); |
| @@ -1059,9 +1285,9 @@ | ||
| 1059 | 1285 | $tbl, |
| 1060 | 1286 | $msg, |
| 1061 | 1287 | $settings |
| 1062 | 1288 | ) ) { |
| 1063 | - $container = WPDA_App_Container_Model::get_container( $cnt_id ); | |
| 1289 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id ); | |
| 1064 | 1290 | if ( !isset( $container[0] ) ) { |
| 1065 | 1291 | return $this->bad_request(); |
| 1066 | 1292 | } |
| 1067 | 1293 | $lookup = array(); |
| @@ -1112,9 +1338,10 @@ | ||
| 1112 | 1338 | $this->process_params( |
| 1113 | 1339 | $default_where_lookup, |
| 1114 | 1340 | $search_custom, |
| 1115 | 1341 | $search_params, |
| 1116 | - $shortcode_params | |
| 1342 | + $shortcode_params, | |
| 1343 | + $values | |
| 1117 | 1344 | ), |
| 1118 | 1345 | '1' === $cascade, |
| 1119 | 1346 | $tbl, |
| 1120 | 1347 | $col, |
| @@ -1121,9 +1348,10 @@ | ||
| 1121 | 1348 | $this->process_params( |
| 1122 | 1349 | $default_where, |
| 1123 | 1350 | $search_custom, |
| 1124 | 1351 | $search_params, |
| 1125 | - $shortcode_params | |
| 1352 | + $shortcode_params, | |
| 1353 | + $values | |
| 1126 | 1354 | ), |
| 1127 | 1355 | $search, |
| 1128 | 1356 | $cols, |
| 1129 | 1357 | $search_columns, |
| @@ -1144,8 +1372,12 @@ | ||
| 1144 | 1372 | } |
| 1145 | 1373 | } |
| 1146 | 1374 | |
| 1147 | 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 | + } | |
| 1148 | 1380 | $app_id = $request->get_param( 'app_id' ); |
| 1149 | 1381 | $cnt_id = $request->get_param( 'cnt_id' ); |
| 1150 | 1382 | if ( $this->check_app_access( |
| 1151 | 1383 | $app_id, |
| @@ -1168,8 +1400,12 @@ | ||
| 1168 | 1400 | } |
| 1169 | 1401 | } |
| 1170 | 1402 | |
| 1171 | 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 | + } | |
| 1172 | 1408 | $app_id = $request->get_param( 'app_id' ); |
| 1173 | 1409 | $cnt_id = $request->get_param( 'cnt_id' ); |
| 1174 | 1410 | $dbs = $request->get_param( 'dbs' ); |
| 1175 | 1411 | if ( $this->check_app_access( |
| @@ -1193,8 +1429,12 @@ | ||
| 1193 | 1429 | } |
| 1194 | 1430 | } |
| 1195 | 1431 | |
| 1196 | 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 | + } | |
| 1197 | 1437 | $app_id = $request->get_param( 'app_id' ); |
| 1198 | 1438 | $cnt_id = $request->get_param( 'cnt_id' ); |
| 1199 | 1439 | $dbs = $request->get_param( 'dbs' ); |
| 1200 | 1440 | $tbl = $request->get_param( 'tbl' ); |
| @@ -1219,23 +1459,14 @@ | ||
| 1219 | 1459 | } |
| 1220 | 1460 | } |
| 1221 | 1461 | |
| 1222 | 1462 | public function app_qb_list( $request ) { |
| 1223 | - if ( !$this->current_user_can_access() ) { | |
| 1224 | - return $this->unauthorized(); | |
| 1225 | - } | |
| 1226 | - if ( !$this->current_user_token_valid( $request ) ) { | |
| 1227 | - return $this->invalid_nonce(); | |
| 1228 | - } | |
| 1229 | - $queries = get_user_meta( WPDA::get_current_user_id(), 'wpda_query_builder' ); | |
| 1230 | - if ( false === $queries ) { | |
| 1231 | - $queries = array(); | |
| 1232 | - } | |
| 1233 | - return $this->WPDA_Rest_Response( '', $queries ); | |
| 1463 | + $qb = new WPDA_QB(); | |
| 1464 | + return $qb->open( $request ); | |
| 1234 | 1465 | } |
| 1235 | 1466 | |
| 1236 | 1467 | public function app_dbs_rename( $request ) { |
| 1237 | - if ( !$this->current_user_can_access( true ) ) { | |
| 1468 | + if ( !$this->current_user_can_access() ) { | |
| 1238 | 1469 | // Only admins |
| 1239 | 1470 | return $this->unauthorized(); |
| 1240 | 1471 | } |
| 1241 | 1472 | if ( !$this->current_user_token_valid( $request ) ) { |
| @@ -1249,8 +1480,71 @@ | ||
| 1249 | 1480 | )); |
| 1250 | 1481 | } |
| 1251 | 1482 | global $wpdb; |
| 1252 | 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 | + } | |
| 1253 | 1547 | // Rename all occurrences in repository tables and apps |
| 1254 | 1548 | $sqls = array( |
| 1255 | 1549 | "update `{$wpdb->prefix}wpda_publisher` set `pub_schema_name` = %s where `pub_schema_name` = %s", |
| 1256 | 1550 | "update `{$wpdb->prefix}wpda_project_page` set `page_schema_name` = %s where `page_schema_name` = %s", |
| @@ -1258,22 +1552,68 @@ | ||
| 1258 | 1552 | "update `{$wpdb->prefix}wpda_media` set `media_schema_name` = %s where `media_schema_name` = %s", |
| 1259 | 1553 | "update `{$wpdb->prefix}wpda_menus` set `menu_schema_name` = %s where `menu_schema_name` = %s", |
| 1260 | 1554 | "update `{$wpdb->prefix}wpda_table_design` set `wpda_schema_name` = %s where `wpda_schema_name` = %s", |
| 1261 | 1555 | "update `{$wpdb->prefix}wpda_table_settings` set `wpda_schema_name` = %s where `wpda_schema_name` = %s", |
| 1262 | - "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" | |
| 1263 | 1557 | ); |
| 1264 | 1558 | foreach ( $sqls as $sql ) { |
| 1265 | - $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 | + } | |
| 1266 | 1575 | } |
| 1267 | - $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\"%'"); | |
| 1268 | 1577 | foreach ( $sql_content as $sql ) { |
| 1269 | - $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 | + } | |
| 1270 | 1593 | } |
| 1271 | - 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 ); | |
| 1272 | 1610 | } |
| 1273 | 1611 | |
| 1274 | 1612 | public function app_chart_data( $request ) { |
| 1275 | 1613 | $app_id = $request->get_param( 'app_id' ); |
| 1614 | + $search_custom = $request->get_param( 'search_custom' ); | |
| 1615 | + $shortcode_params = $request->get_param( 'shortcode_params' ); | |
| 1276 | 1616 | if ( !$this->main_app_access( $app_id, $msg ) ) { |
| 1277 | 1617 | if ( 'rest_cookie_invalid_nonce' === $msg ) { |
| 1278 | 1618 | return $this->invalid_nonce(); |
| 1279 | 1619 | } |
| @@ -1285,8 +1625,15 @@ | ||
| 1285 | 1625 | $app_container = WPDA_App_Container_Model::select( $app_id, 0 ); |
| 1286 | 1626 | if ( 1 === count( $app_container ) && null !== $app_container[0]['cnt_query'] && '' !== trim( (string) $app_container[0]['cnt_query'] ) ) { |
| 1287 | 1627 | $dbs = $app_container[0]['cnt_dbs']; |
| 1288 | 1628 | $query = $app_container[0]['cnt_query']; |
| 1629 | + // Process shortcode and url parameters | |
| 1630 | + $query = $this->process_params( | |
| 1631 | + $query, | |
| 1632 | + $search_custom, | |
| 1633 | + null, | |
| 1634 | + $shortcode_params | |
| 1635 | + ); | |
| 1289 | 1636 | $wpdadb = WPDADB::get_db_connection( $dbs ); |
| 1290 | 1637 | if ( null === $wpdadb ) { |
| 1291 | 1638 | // Error connecting. |
| 1292 | 1639 | return new \WP_Error('error', "Error connecting to database {$dbs}", array( |
| @@ -1294,14 +1641,23 @@ | ||
| 1294 | 1641 | )); |
| 1295 | 1642 | } |
| 1296 | 1643 | $suppress = $wpdadb->suppress_errors( true ); |
| 1297 | 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 | + } | |
| 1298 | 1654 | $wpdadb->get_results( "create temporary table `wpda_chart_data_types` as {$query}", 'ARRAY_A' ); |
| 1299 | 1655 | $explain = $wpdadb->get_results( "desc `wpda_chart_data_types`", 'ARRAY_A' ); |
| 1300 | 1656 | $wpdadb->get_results( "drop temporary table `wpda_chart_data_types`", 'ARRAY_A' ); |
| 1301 | 1657 | $wpdadb->suppress_errors( $suppress ); |
| 1302 | 1658 | return array( |
| 1303 | - 'data' => $chart_data, | |
| 1659 | + 'data' => $chart_data_converted, | |
| 1304 | 1660 | 'explain' => $explain, |
| 1305 | 1661 | ); |
| 1306 | 1662 | } else { |
| 1307 | 1663 | return new \WP_Error('error', $msg, array( |
| @@ -1346,8 +1702,10 @@ | ||
| 1346 | 1702 | $app_id = $request->get_param( 'app_id' ); |
| 1347 | 1703 | $cnt_id = $request->get_param( 'cnt_id' ); |
| 1348 | 1704 | $target = $request->get_param( 'target' ); |
| 1349 | 1705 | $settings = $request->get_param( 'settings' ); |
| 1706 | + $chart = $request->get_param( 'chart' ); | |
| 1707 | + $map = $request->get_param( 'map' ); | |
| 1350 | 1708 | $theme = $request->get_param( 'theme' ); |
| 1351 | 1709 | return $this->do_app_settings( |
| 1352 | 1710 | $app_id, |
| 1353 | 1711 | $cnt_id, |
| @@ -1352,8 +1710,10 @@ | ||
| 1352 | 1710 | $app_id, |
| 1353 | 1711 | $cnt_id, |
| 1354 | 1712 | $target, |
| 1355 | 1713 | $settings, |
| 1714 | + $chart, | |
| 1715 | + $map, | |
| 1356 | 1716 | $theme |
| 1357 | 1717 | ); |
| 1358 | 1718 | } |
| 1359 | 1719 | |
| @@ -1516,11 +1876,13 @@ | ||
| 1516 | 1876 | $app_id, |
| 1517 | 1877 | $cnt_id, |
| 1518 | 1878 | $target, |
| 1519 | 1879 | $settings, |
| 1880 | + $chart, | |
| 1881 | + $map, | |
| 1520 | 1882 | $theme |
| 1521 | 1883 | ) { |
| 1522 | - if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target ) { | |
| 1884 | + if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'rform' !== $target && 'theme' !== $target && 'chart' !== $target && 'map' !== $target && 'dashboard' !== $target ) { | |
| 1523 | 1885 | return $this->bad_request(); |
| 1524 | 1886 | } |
| 1525 | 1887 | if ( null === $settings || '' === $settings ) { |
| 1526 | 1888 | // Perform reset |
| @@ -1540,8 +1902,24 @@ | ||
| 1540 | 1902 | 'status' => 403, |
| 1541 | 1903 | )); |
| 1542 | 1904 | } |
| 1543 | 1905 | break; |
| 1906 | + case 'chart': | |
| 1907 | + $error_msg = WPDA_App_Container_Model::update_chart_settings( $cnt_id, null ); | |
| 1908 | + if ( '' !== $error_msg ) { | |
| 1909 | + return new \WP_Error('error', $error_msg, array( | |
| 1910 | + 'status' => 403, | |
| 1911 | + )); | |
| 1912 | + } | |
| 1913 | + break; | |
| 1914 | + case 'map': | |
| 1915 | + $error_msg = WPDA_App_Container_Model::update_map_settings( $cnt_id, null ); | |
| 1916 | + if ( '' !== $error_msg ) { | |
| 1917 | + return new \WP_Error('error', $error_msg, array( | |
| 1918 | + 'status' => 403, | |
| 1919 | + )); | |
| 1920 | + } | |
| 1921 | + break; | |
| 1544 | 1922 | case 'theme': |
| 1545 | 1923 | $error_msg = WPDA_App_Model::update_theme( $app_id, null ); |
| 1546 | 1924 | if ( '' !== $error_msg ) { |
| 1547 | 1925 | return new \WP_Error('error', $error_msg, array( |
| @@ -1548,8 +1926,16 @@ | ||
| 1548 | 1926 | 'status' => 403, |
| 1549 | 1927 | )); |
| 1550 | 1928 | } |
| 1551 | 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; | |
| 1552 | 1938 | default: |
| 1553 | 1939 | return $this->bad_request(); |
| 1554 | 1940 | } |
| 1555 | 1941 | return $this->WPDA_Rest_Response( __( 'Reset was successful', 'wp-data-access' ) ); |
| @@ -1561,8 +1947,22 @@ | ||
| 1561 | 1947 | return new \WP_Error('error', $error_msg, array( |
| 1562 | 1948 | 'status' => 403, |
| 1563 | 1949 | )); |
| 1564 | 1950 | } |
| 1951 | + // Update chart settings | |
| 1952 | + $error_msg = WPDA_App_Container_Model::update_chart_settings( $cnt_id, $chart ); | |
| 1953 | + if ( '' !== $error_msg ) { | |
| 1954 | + return new \WP_Error('error', $error_msg, array( | |
| 1955 | + 'status' => 403, | |
| 1956 | + )); | |
| 1957 | + } | |
| 1958 | + // Update map settings | |
| 1959 | + $error_msg = WPDA_App_Container_Model::update_map_settings( $cnt_id, $map ); | |
| 1960 | + if ( '' !== $error_msg ) { | |
| 1961 | + return new \WP_Error('error', $error_msg, array( | |
| 1962 | + 'status' => 403, | |
| 1963 | + )); | |
| 1964 | + } | |
| 1565 | 1965 | } else { |
| 1566 | 1966 | if ( 'rform' === $target ) { |
| 1567 | 1967 | // Update rform settings |
| 1568 | 1968 | $error_msg = WPDA_App_Container_Model::update_rform_settings( $cnt_id, $settings ); |
| @@ -1580,14 +1980,35 @@ | ||
| 1580 | 1980 | 'status' => 403, |
| 1581 | 1981 | )); |
| 1582 | 1982 | } |
| 1583 | 1983 | } else { |
| 1584 | - // Update form settings | |
| 1585 | - $error_msg = WPDA_App_Container_Model::update_form_settings( $cnt_id, $settings ); | |
| 1586 | - if ( '' !== $error_msg ) { | |
| 1587 | - return new \WP_Error('error', $error_msg, array( | |
| 1588 | - 'status' => 403, | |
| 1589 | - )); | |
| 1984 | + if ( 'map' === $target ) { | |
| 1985 | + // Update chart settings | |
| 1986 | + $error_msg = WPDA_App_Container_Model::update_map_settings( $cnt_id, $settings ); | |
| 1987 | + if ( '' !== $error_msg ) { | |
| 1988 | + return new \WP_Error('error', $error_msg, array( | |
| 1989 | + 'status' => 403, | |
| 1990 | + )); | |
| 1991 | + } | |
| 1992 | + } else { | |
| 1993 | + if ( 'form' === $target ) { | |
| 1994 | + // Update form settings | |
| 1995 | + $error_msg = WPDA_App_Container_Model::update_form_settings( $cnt_id, $settings ); | |
| 1996 | + if ( '' !== $error_msg ) { | |
| 1997 | + return new \WP_Error('error', $error_msg, array( | |
| 1998 | + 'status' => 403, | |
| 1999 | + )); | |
| 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 | + } | |
| 2010 | + } | |
| 1590 | 2011 | } |
| 1591 | 2012 | } |
| 1592 | 2013 | } |
| 1593 | 2014 | } |
| @@ -1708,9 +2129,9 @@ | ||
| 1708 | 2129 | private function get_relation_columns( $container ) { |
| 1709 | 2130 | return null; |
| 1710 | 2131 | } |
| 1711 | 2132 | |
| 1712 | - private function reorder_details( $cnt_id_from, $cnt_id_to ) { | |
| 2133 | + private function reorder_details( $app_id, $cnt_id_from, $cnt_id_to ) { | |
| 1713 | 2134 | return $this->bad_request(); |
| 1714 | 2135 | } |
| 1715 | 2136 | |
| 1716 | 2137 | private function get_app_apps_meta( $app, $apps ) { |
| @@ -1717,8 +2138,9 @@ | ||
| 1717 | 2138 | $app_id_details = array_map( function ( $e ) { |
| 1718 | 2139 | if ( isset( $e['app_id_detail'] ) ) { |
| 1719 | 2140 | return $e['app_id_detail']; |
| 1720 | 2141 | } |
| 2142 | + return null; | |
| 1721 | 2143 | }, $apps ); |
| 1722 | 2144 | $app_titles = array(); |
| 1723 | 2145 | foreach ( $app_id_details as $app_id_detail ) { |
| 1724 | 2146 | $app_detail = WPDA_App_Model::get_by_id( $app_id_detail ); |
| @@ -1753,18 +2175,46 @@ | ||
| 1753 | 2175 | } |
| 1754 | 2176 | $settings->env = $this->get_env(); |
| 1755 | 2177 | global $wpdb; |
| 1756 | 2178 | $settings->wp = [ |
| 1757 | - 'roles' => $this->get_wp_roles(), | |
| 1758 | - 'users' => $this->get_wp_users(), | |
| 1759 | - 'home' => admin_url( 'admin.php' ), | |
| 1760 | - 'tables' => array_values( $wpdb->tables() ), | |
| 1761 | - 'date_format' => get_option( 'date_format' ), | |
| 1762 | - '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(), | |
| 1763 | 2190 | ]; |
| 1764 | 2191 | return $settings; |
| 1765 | 2192 | } |
| 1766 | 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 | + | |
| 1767 | 2217 | private function get_app_container_meta( $app_id, $container, $rel_tab = false ) { |
| 1768 | 2218 | $app = WPDA_App_Model::get_by_id( $app_id ); |
| 1769 | 2219 | if ( false === $app ) { |
| 1770 | 2220 | return $this->bad_request(); |
| @@ -1777,9 +2227,9 @@ | ||
| 1777 | 2227 | $response = array( |
| 1778 | 2228 | 'app' => array( |
| 1779 | 2229 | 'app' => $app, |
| 1780 | 2230 | 'container' => array_map( function ( $value ) { |
| 1781 | - $show = current_user_can( 'manage_options' ); | |
| 2231 | + $show = WPDA::current_user_is_admin(); | |
| 1782 | 2232 | if ( !$show ) { |
| 1783 | 2233 | // Hide database and table name in responses for non admin users. |
| 1784 | 2234 | unset($value['cnt_dbs']); |
| 1785 | 2235 | unset($value['cnt_tbl']); |
| @@ -1795,10 +2245,18 @@ | ||
| 1795 | 2245 | 'update' => array(), |
| 1796 | 2246 | 'delete' => array(), |
| 1797 | 2247 | ); |
| 1798 | 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 | + } | |
| 1799 | 2256 | $media = $this->get_media( $dbs, $tbl, $cls->get_table_columns() ); |
| 1800 | - $response['columns'] = $cls->get_table_columns(); | |
| 2257 | + $response['columns'] = $columns; | |
| 2258 | + $response['columns_sorted'] = $columns_sorted; | |
| 1801 | 2259 | $response['table_labels'] = $cls->get_table_header_labels(); |
| 1802 | 2260 | $response['form_labels'] = $cls->get_table_column_headers(); |
| 1803 | 2261 | $response['primary_key'] = $cls->get_table_primary_key(); |
| 1804 | 2262 | $response['access'] = $access; |
| @@ -1814,9 +2272,9 @@ | ||
| 1814 | 2272 | $response['table_info'] = $this->get_table_info( $dbs, $tbl, $default_where ); |
| 1815 | 2273 | return $this->WPDA_Rest_Response( '', $response ); |
| 1816 | 2274 | } |
| 1817 | 2275 | |
| 1818 | - private function get_app_meta( $app_id ) { | |
| 2276 | + public function get_app_meta( $app_id ) { | |
| 1819 | 2277 | $app = WPDA_App_Model::get_by_id( $app_id ); |
| 1820 | 2278 | if ( !isset( $app[0]['app_type'] ) ) { |
| 1821 | 2279 | return $this->bad_request(); |
| 1822 | 2280 | } |
| @@ -1833,30 +2291,46 @@ | ||
| 1833 | 2291 | return $this->get_app_container_meta( $app_id, $container ); |
| 1834 | 2292 | } |
| 1835 | 2293 | } |
| 1836 | 2294 | |
| 1837 | - private function do_app_export_app( $app_id ) { | |
| 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 | + | |
| 2304 | + private function do_app_export_app( $app_id, $main_app_id ) { | |
| 1838 | 2305 | global $wpdb; |
| 1839 | 2306 | $quotes = function ( $value ) { |
| 1840 | 2307 | return str_replace( array( |
| 1841 | 2308 | "'", |
| 1842 | 2309 | '\\"', |
| 2310 | + "\\\\t", | |
| 1843 | 2311 | "\\t", |
| 2312 | + "\\\\n", | |
| 1844 | 2313 | "\\n", |
| 1845 | 2314 | "\\r\\n", |
| 1846 | - "\\r" | |
| 2315 | + "\\r", | |
| 2316 | + "\\d" | |
| 1847 | 2317 | ), array( |
| 1848 | 2318 | "''", |
| 1849 | 2319 | '\\\\"', |
| 2320 | + "\\\\\\t", | |
| 1850 | 2321 | "\\\\t", |
| 2322 | + "\\\\\\n", | |
| 1851 | 2323 | "\\\\n", |
| 1852 | 2324 | "\\\\r\\\\n", |
| 1853 | - "\\\\r" | |
| 2325 | + "\\\\r", | |
| 2326 | + "\\\\\\\\\\d" | |
| 1854 | 2327 | ), $value ); |
| 1855 | 2328 | }; |
| 1856 | 2329 | $app = WPDA_App_Model::get_by_id( $app_id ); |
| 1857 | 2330 | $app_settings = ( null === $app[0]['app_settings'] ? 'null' : "{$quotes( $app[0]['app_settings'] )}" ); |
| 1858 | 2331 | $app_theme = ( null === $app[0]['app_theme'] ? 'null' : "{$quotes( $app[0]['app_theme'] )}" ); |
| 2332 | + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed | |
| 1859 | 2333 | $app_sql = <<<SQL |
| 1860 | 2334 | # Import app |
| 1861 | 2335 | insert into `{wp_prefix}wpda_app` |
| 1862 | 2336 | \t(`app_name` |
| @@ -1875,9 +2349,9 @@ | ||
| 1875 | 2349 | \t,{$app[0]['app_add_to_menu']} |
| 1876 | 2350 | \t); |
| 1877 | 2351 | |
| 1878 | 2352 | SET @APP_ID = LAST_INSERT_ID(); |
| 1879 | -insert into `wpda_transfer_apps` | |
| 2353 | +insert into `wpda_transfer_apps_{$main_app_id}` | |
| 1880 | 2354 | values |
| 1881 | 2355 | ({$app[0]['app_id']} |
| 1882 | 2356 | ,(select LAST_INSERT_ID(`app_id`) from `{wp_prefix}wpda_app` order by 1 desc limit 1) |
| 1883 | 2357 | ); |
| @@ -1886,15 +2360,23 @@ | ||
| 1886 | 2360 | SQL; |
| 1887 | 2361 | $containers = WPDA_App_Container_Model::select_all( $app_id ); |
| 1888 | 2362 | $containers_sql = ''; |
| 1889 | 2363 | foreach ( $containers as $container ) { |
| 1890 | - $cnt_table = ( null === $container['cnt_table'] ? 'null' : "{$quotes( $container['cnt_table'] )}" ); | |
| 1891 | - $cnt_form = ( null === $container['cnt_form'] ? 'null' : "{$quotes( $container['cnt_form'] )}" ); | |
| 1892 | - $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'] )}'" ); | |
| 1893 | 2371 | // Replace default WordPress database with conversion string |
| 1894 | 2372 | $cnt_dbs = ( $wpdb->dbname === $container['cnt_dbs'] ? '{wp_schema}' : "{$quotes( $container['cnt_dbs'] )}" ); |
| 1895 | 2373 | $cnt_table = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_table ); |
| 1896 | 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 | |
| 1897 | 2379 | $containers_sql .= <<<SQL |
| 1898 | 2380 | # Import app container |
| 1899 | 2381 | insert into `{wp_prefix}wpda_app_container` |
| 1900 | 2382 | \t(`cnt_dbs` |
| @@ -1905,8 +2387,12 @@ | ||
| 1905 | 2387 | \t,`cnt_seq_nr` |
| 1906 | 2388 | \t,`cnt_table` |
| 1907 | 2389 | \t,`cnt_form` |
| 1908 | 2390 | \t,`cnt_relation` |
| 2391 | + ,`cnt_rform` | |
| 2392 | + ,`cnt_chart` | |
| 2393 | + ,`cnt_map` | |
| 2394 | + ,`cnt_query` | |
| 1909 | 2395 | \t) |
| 1910 | 2396 | values |
| 1911 | 2397 | \t('{$cnt_dbs}' |
| 1912 | 2398 | \t,'{$quotes( $container['cnt_tbl'] )}' |
| @@ -1913,14 +2399,18 @@ | ||
| 1913 | 2399 | \t,'{$quotes( $container['cnt_cls'] )}' |
| 1914 | 2400 | \t,'{$quotes( $container['cnt_title'] )}' |
| 1915 | 2401 | \t,@APP_ID |
| 1916 | 2402 | \t,{$container['cnt_seq_nr']} |
| 1917 | -\t,'{$cnt_table}' | |
| 1918 | -\t,'{$cnt_form}' | |
| 1919 | -\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} | |
| 1920 | 2410 | \t); |
| 1921 | 2411 | |
| 1922 | -insert into `wpda_transfer_containers` | |
| 2412 | +insert into `wpda_transfer_containers_{$main_app_id}` | |
| 1923 | 2413 | values |
| 1924 | 2414 | ({$container['cnt_id']} |
| 1925 | 2415 | ,(select LAST_INSERT_ID(`cnt_id`) from `{wp_prefix}wpda_app_container` order by 1 desc limit 1) |
| 1926 | 2416 | ); |
| @@ -1928,24 +2418,24 @@ | ||
| 1928 | 2418 | |
| 1929 | 2419 | SQL; |
| 1930 | 2420 | } |
| 1931 | 2421 | // Post update: update master container ids |
| 2422 | + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed | |
| 1932 | 2423 | $containers_sql .= <<<SQL |
| 1933 | 2424 | # Update app master container IDs |
| 1934 | -update `{wp_prefix}wpda_app_container` c | |
| 1935 | -set c.`cnt_relation` = | |
| 2425 | +update `{wp_prefix}wpda_app_container` as a | |
| 2426 | +set a.`cnt_relation` = | |
| 1936 | 2427 | ( |
| 1937 | 2428 | select replace( |
| 1938 | -\t\t\ta.`cnt_relation`, | |
| 1939 | -\t\t\tconcat('"cnt_id_master":"', b.cnt_id_old, '"'), | |
| 1940 | -\t\t\tconcat('"cnt_id_master":"', b.cnt_id_new, '"') | |
| 1941 | -\t\t) | |
| 1942 | -\t\tfrom `{wp_prefix}wpda_app_container` a, `wpda_transfer_containers` b | |
| 1943 | -\t\twhere a.cnt_id = c.cnt_id | |
| 1944 | -\t\t and a.`cnt_relation` like concat('%"cnt_id_master":"', b.cnt_id_old, '"%') | |
| 2429 | + a.`cnt_relation`, | |
| 2430 | + concat('"cnt_id_master":"', b.cnt_id_old, '"'), | |
| 2431 | + concat('"cnt_id_master":"', b.cnt_id_new, '"') | |
| 2432 | + ) | |
| 2433 | + from `wpda_transfer_containers_{$main_app_id}` as b | |
| 2434 | + where a.`cnt_relation` like concat('%"cnt_id_master":"', b.cnt_id_old, '"%') | |
| 1945 | 2435 | ) |
| 1946 | -where c.`app_id` = @APP_ID | |
| 1947 | - and c.`cnt_relation` is not null; | |
| 2436 | +where a.`app_id` = @APP_ID | |
| 2437 | + and a.`cnt_relation` is not null; | |
| 1948 | 2438 | |
| 1949 | 2439 | |
| 1950 | 2440 | SQL; |
| 1951 | 2441 | $apps = WPDA_App_Apps_Model::select_all( $app_id ); |
| @@ -1950,11 +2440,12 @@ | ||
| 1950 | 2440 | SQL; |
| 1951 | 2441 | $apps = WPDA_App_Apps_Model::select_all( $app_id ); |
| 1952 | 2442 | $apps_sql = ''; |
| 1953 | 2443 | foreach ( $apps as $app ) { |
| 1954 | - $apps_sql .= $this->do_app_export_app( $app['app_id_detail'] ); | |
| 2444 | + $apps_sql .= $this->do_app_export_app( $app['app_id_detail'], $main_app_id ); | |
| 1955 | 2445 | } |
| 1956 | 2446 | foreach ( $apps as $app ) { |
| 2447 | + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed | |
| 1957 | 2448 | $apps_sql .= <<<SQL |
| 1958 | 2449 | # Import app relationships |
| 1959 | 2450 | insert into `{wp_prefix}wpda_app_apps` |
| 1960 | 2451 | \t(`app_id` |
| @@ -1961,10 +2452,10 @@ | ||
| 1961 | 2452 | \t,`app_id_detail` |
| 1962 | 2453 | \t,`seq_nr`\t\t\t\t\t |
| 1963 | 2454 | \t) |
| 1964 | 2455 | values |
| 1965 | -\t((select `app_id_new` from `wpda_transfer_apps` where `app_id_old` = {$app['app_id']}) | |
| 1966 | -\t,(select `app_id_new` from `wpda_transfer_apps` where `app_id_old` = {$app['app_id_detail']}) | |
| 2456 | +\t((select `app_id_new` from `wpda_transfer_apps_{$main_app_id}` where `app_id_old` = {$app['app_id']}) | |
| 2457 | +\t,(select `app_id_new` from `wpda_transfer_apps_{$main_app_id}` where `app_id_old` = {$app['app_id_detail']}) | |
| 1967 | 2458 | \t,{$app['seq_nr']}\t\t\t\t\t |
| 1968 | 2459 | \t); |
| 1969 | 2460 | |
| 1970 | 2461 | |
| @@ -1975,8 +2466,9 @@ | ||
| 1975 | 2466 | |
| 1976 | 2467 | private function do_app_export( $app_id ) { |
| 1977 | 2468 | global $wpdb; |
| 1978 | 2469 | $sql = ''; |
| 2470 | + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed | |
| 1979 | 2471 | $begin_sql = <<<SQL |
| 1980 | 2472 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; |
| 1981 | 2473 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; |
| 1982 | 2474 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; |
| @@ -1982,14 +2474,14 @@ | ||
| 1982 | 2474 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; |
| 1983 | 2475 | /*!40101 SET NAMES {$wpdb->charset} */; |
| 1984 | 2476 | |
| 1985 | 2477 | # Create temporary table |
| 1986 | -CREATE TEMPORARY TABLE `wpda_transfer_containers` | |
| 2478 | +CREATE TABLE `wpda_transfer_containers_{$app_id}` | |
| 1987 | 2479 | (cnt_id_old bigint(20) unsigned |
| 1988 | 2480 | ,cnt_id_new bigint(20) unsigned |
| 1989 | 2481 | ); |
| 1990 | 2482 | |
| 1991 | -CREATE TEMPORARY TABLE `wpda_transfer_apps` | |
| 2483 | +CREATE TABLE `wpda_transfer_apps_{$app_id}` | |
| 1992 | 2484 | (app_id_old bigint(20) unsigned |
| 1993 | 2485 | ,app_id_new bigint(20) unsigned |
| 1994 | 2486 | ); |
| 1995 | 2487 | |
| @@ -1996,13 +2488,14 @@ | ||
| 1996 | 2488 | SET @APP_ID = NULL; |
| 1997 | 2489 | |
| 1998 | 2490 | |
| 1999 | 2491 | SQL; |
| 2000 | - $sql .= $this->do_app_export_app( $app_id ); | |
| 2492 | + $sql .= $this->do_app_export_app( $app_id, $app_id ); | |
| 2493 | + // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed | |
| 2001 | 2494 | $end_sql = <<<SQL |
| 2002 | 2495 | # Drop temporary table |
| 2003 | -DROP TEMPORARY TABLE `wpda_transfer_containers`; | |
| 2004 | -DROP TEMPORARY TABLE `wpda_transfer_apps`; | |
| 2496 | +DROP TABLE `wpda_transfer_containers_{$app_id}`; | |
| 2497 | +DROP TABLE `wpda_transfer_apps_{$app_id}`; | |
| 2005 | 2498 | |
| 2006 | 2499 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; |
| 2007 | 2500 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; |
| 2008 | 2501 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
| @@ -2091,9 +2584,9 @@ | ||
| 2091 | 2584 | } |
| 2092 | 2585 | return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) ); |
| 2093 | 2586 | } |
| 2094 | 2587 | |
| 2095 | - private function main_app_access( $app_id, &$msg = '' ) { | |
| 2588 | + private function main_app_access( $app_id, &$msg = '', &$app = null ) { | |
| 2096 | 2589 | // Get app info |
| 2097 | 2590 | $app = WPDA_App_Model::get_by_id( $app_id ); |
| 2098 | 2591 | if ( false === $app ) { |
| 2099 | 2592 | // App not found |
| @@ -2136,9 +2629,9 @@ | ||
| 2136 | 2629 | if ( !$this->main_app_access( $app_id, $msg ) ) { |
| 2137 | 2630 | return false; |
| 2138 | 2631 | } |
| 2139 | 2632 | // Get container |
| 2140 | - $container = WPDA_App_Container_Model::get_container( $cnt_id ); | |
| 2633 | + $container = WPDA_App_Container_Model::get_container( $app_id, $cnt_id ); | |
| 2141 | 2634 | if ( !is_array( $container ) || 0 === count( $container ) ) { |
| 2142 | 2635 | // Container not found |
| 2143 | 2636 | $msg = __( 'Bad request', 'wp-data-access' ); |
| 2144 | 2637 | return false; |
| @@ -2145,12 +2638,14 @@ | ||
| 2145 | 2638 | } |
| 2146 | 2639 | if ( 'select' !== $action ) { |
| 2147 | 2640 | $cnt_table = json_decode( (string) $container[0]['cnt_table'], true ); |
| 2148 | 2641 | if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) { |
| 2149 | - $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true ); | |
| 2150 | - if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $cnt_relation['cnt_id_master'], $action )) ) { | |
| 2151 | - $msg = __( 'Unauthorized', 'wp-data-access' ); | |
| 2152 | - 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 | + } | |
| 2153 | 2648 | } |
| 2154 | 2649 | } |
| 2155 | 2650 | } |
| 2156 | 2651 | // Return database name, table name and columns |
| @@ -2163,10 +2658,10 @@ | ||
| 2163 | 2658 | ); |
| 2164 | 2659 | return true; |
| 2165 | 2660 | } |
| 2166 | 2661 | |
| 2167 | - private function check_master_container_access( $cnt_id, $action ) { | |
| 2168 | - $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 ); | |
| 2169 | 2664 | if ( !is_array( $container ) || 0 === count( $container ) ) { |
| 2170 | 2665 | // Container not found |
| 2171 | 2666 | return false; |
| 2172 | 2667 | } |
| @@ -2172,9 +2667,9 @@ | ||
| 2172 | 2667 | } |
| 2173 | 2668 | $cnt_table = json_decode( (string) $container[0]['cnt_table'], true ); |
| 2174 | 2669 | if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) { |
| 2175 | 2670 | $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true ); |
| 2176 | - 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 )) ) { | |
| 2177 | 2672 | return false; |
| 2178 | 2673 | } |
| 2179 | 2674 | } |
| 2180 | 2675 | return true; |
| @@ -2183,18 +2678,21 @@ | ||
| 2183 | 2678 | private function process_params( |
| 2184 | 2679 | $where, |
| 2185 | 2680 | $search_custom, |
| 2186 | 2681 | $search_params, |
| 2187 | - $shortcode_params | |
| 2682 | + $shortcode_params, | |
| 2683 | + $dynamic_params = array() | |
| 2188 | 2684 | ) { |
| 2189 | 2685 | // Process $search_custom > URL parameters |
| 2190 | 2686 | global $wpdb; |
| 2687 | + $replacements = array(); | |
| 2688 | + $nonce = bin2hex( random_bytes( 8 ) ); | |
| 2191 | 2689 | foreach ( self::METHODS as $method ) { |
| 2192 | 2690 | $offset = 0; |
| 2193 | 2691 | $search = $method . '['; |
| 2692 | + // Get filter | |
| 2194 | 2693 | while ( ($pos_start = stripos( $where, $search, $offset )) !== false ) { |
| 2195 | 2694 | if ( ($pos_end = stripos( $where, ']', $pos_start )) !== false ) { |
| 2196 | - // Get filter | |
| 2197 | 2695 | $filter = substr( $where, $pos_start, $pos_end - $pos_start + 1 ); |
| 2198 | 2696 | // Get name |
| 2199 | 2697 | $arg_name = substr( $where, $pos_start + strlen( $search ), $pos_end - $pos_start - strlen( $search ) ); |
| 2200 | 2698 | // Remove quotes from name |
| @@ -2200,44 +2698,36 @@ | ||
| 2200 | 2698 | // Remove quotes from name |
| 2201 | 2699 | if ( substr( $arg_name, 0, 1 ) === "'" && substr( $arg_name, -1 ) === "'" ) { |
| 2202 | 2700 | $arg_name = substr( $arg_name, 1, -1 ); |
| 2203 | 2701 | } |
| 2204 | - // Remove double quotes from name | |
| 2702 | + // Remove quotes from name | |
| 2205 | 2703 | if ( substr( $arg_name, 0, 1 ) === '"' && substr( $arg_name, -1 ) === '"' ) { |
| 2206 | 2704 | $arg_name = substr( $arg_name, 1, -1 ); |
| 2207 | 2705 | } |
| 2706 | + $arg_value = null; | |
| 2208 | 2707 | // Handle GET args |
| 2209 | - 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] ) { | |
| 2210 | 2713 | if ( isset( $search_custom['get'][$arg_name] ) ) { |
| 2211 | 2714 | $arg_value = sanitize_text_field( wp_unslash( $search_custom['get'][$arg_name] ) ); |
| 2212 | - $where = $wpdb->prepare( substr_replace( | |
| 2213 | - $where, | |
| 2214 | - '%s', | |
| 2215 | - $pos_start, | |
| 2216 | - $pos_end - $pos_start + 1 | |
| 2217 | - ), $arg_value ); | |
| 2218 | - } else { | |
| 2219 | - if ( $method === self::METHODS[0] ) { | |
| 2220 | - $where = str_replace( $filter, 'null', $where ); | |
| 2221 | - } | |
| 2715 | + } elseif ( isset( $search_custom['post'][$arg_name] ) ) { | |
| 2716 | + $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) ); | |
| 2222 | 2717 | } |
| 2223 | 2718 | } |
| 2224 | 2719 | // Handle POST args |
| 2225 | - if ( $method === self::METHODS[1] || $method === self::METHODS[2] ) { | |
| 2226 | - if ( isset( $search_custom['post'][$arg_name] ) ) { | |
| 2227 | - $arg_value = sanitize_text_field( wp_unslash( $search_custom['post'][$arg_name] ) ); | |
| 2228 | - $where = $wpdb->prepare( substr_replace( | |
| 2229 | - $where, | |
| 2230 | - '%s', | |
| 2231 | - $pos_start, | |
| 2232 | - $pos_end - $pos_start + 1 | |
| 2233 | - ), $arg_value ); | |
| 2234 | - } else { | |
| 2235 | - $where = str_replace( $filter, 'null', $where ); | |
| 2236 | - } | |
| 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'; | |
| 2237 | 2727 | } |
| 2238 | 2728 | } |
| 2239 | - $offset = $pos_start + 6; | |
| 2729 | + $offset = $pos_start + 1; | |
| 2240 | 2730 | if ( $offset > strlen( $where ) ) { |
| 2241 | 2731 | $offset = strlen( $where ) - 1; |
| 2242 | 2732 | } |
| 2243 | 2733 | } |
| @@ -2246,21 +2736,16 @@ | ||
| 2246 | 2736 | if ( is_array( $search_params ) && 1 === count( $search_params ) ) { |
| 2247 | 2737 | $filter_field_name = $this->sanitize_db_identifier( array_keys( $search_params )[0] ); |
| 2248 | 2738 | $filter_field_value = sanitize_text_field( $search_params[$filter_field_name] ); |
| 2249 | 2739 | $filter_field_name_array = array_map( 'trim', explode( ',', $filter_field_name ) ); |
| 2250 | - //phpcs:ignore - 8.1 proof | |
| 2251 | 2740 | $filter_field_value_array = array_map( 'trim', explode( ',', $filter_field_value ) ); |
| 2252 | - //phpcs:ignore - 8.1 proof | |
| 2253 | 2741 | if ( count( $filter_field_name_array ) === count( $filter_field_value_array ) ) { |
| 2254 | - //phpcs:ignore - 8.1 proof | |
| 2255 | 2742 | // Add filter to where clause. |
| 2256 | 2743 | for ($i = 0; $i < count( $filter_field_name_array ); $i++) { |
| 2257 | - // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall, Squiz.PHP.DisallowSizeFunctionsInLoops | |
| 2258 | - $where .= (( '' === $where ? '' : ' and ' )) . $wpdb->prepare( | |
| 2259 | - ' `%1s` like %s ', | |
| 2260 | - // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders | |
| 2261 | - array($filter_field_name_array[$i], $filter_field_value_array[$i]) | |
| 2262 | - ); | |
| 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] ); | |
| 2263 | 2748 | } |
| 2264 | 2749 | } |
| 2265 | 2750 | } |
| 2266 | 2751 | // Substitute all shortcode parameters |
| @@ -2267,13 +2752,11 @@ | ||
| 2267 | 2752 | if ( is_array( $shortcode_params ) ) { |
| 2268 | 2753 | foreach ( $shortcode_params as $column_name => $column_value ) { |
| 2269 | 2754 | $occurences = substr_count( strtolower( $where ), strtolower( "shortcodeParam['{$column_name}']" ) ); |
| 2270 | 2755 | if ( 0 < $occurences ) { |
| 2271 | - $column_values = array(); | |
| 2272 | - for ($i = 0; $i < $occurences; $i++) { | |
| 2273 | - $column_values[] = sanitize_text_field( $column_value ); | |
| 2274 | - } | |
| 2275 | - $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 ) ); | |
| 2276 | 2759 | } |
| 2277 | 2760 | } |
| 2278 | 2761 | } |
| 2279 | 2762 | // Substitute all unused shortcode parameter calls with null |
| @@ -2288,8 +2771,18 @@ | ||
| 2288 | 2771 | if ( $offset > strlen( $where ) ) { |
| 2289 | 2772 | $offset = strlen( $where ) - 1; |
| 2290 | 2773 | } |
| 2291 | 2774 | } |
| 2292 | - 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 ); | |
| 2293 | 2786 | } |
| 2294 | 2787 | |
| 2295 | 2788 | } |