| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\WpRest\Base; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Api\Error_Builder; |
| 6 |
use Elementor\Core\Utils\Collection; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
abstract class Query { |
| 13 |
const NAMESPACE = 'elementor/v1'; |
| 14 |
const NONCE_KEY = 'x_wp_nonce'; |
| 15 |
|
| 16 |
const KEYS_CONVERSION_MAP_KEY = 'keys_conversion_map'; |
| 17 |
const IS_PUBLIC_KEY = 'is_public'; |
| 18 |
const TAX_QUERY_KEY = 'tax_query'; |
| 19 |
const META_QUERY_KEY = 'meta_query'; |
| 20 |
|
| 21 |
const MAX_RESPONSE_COUNT = 100; |
| 22 |
const ITEMS_COUNT_KEY = 'items_count'; |
| 23 |
|
| 24 |
const INCLUDED_TYPE_KEY = 'included_types'; |
| 25 |
const EXCLUDED_TYPE_KEY = 'excluded_types'; |
| 26 |
const HIDE_EMPTY_KEY = 'hide_empty'; |
| 27 |
|
| 28 |
const SEARCH_TERM_KEY = 'term'; |
| 29 |
const SEARCH_FILTER_PRIORITY = 10; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param \WP_REST_Request $request |
| 33 |
* @return \WP_REST_Response |
| 34 |
**/ |
| 35 |
abstract protected function get( \WP_REST_Request $request ); |
| 36 |
|
| 37 |
abstract protected static function get_allowed_param_keys(): array; |
| 38 |
|
| 39 |
abstract protected static function get_keys_to_encode(): array; |
| 40 |
|
| 41 |
abstract protected function get_endpoint_registration_args(): array; |
| 42 |
|
| 43 |
public function register( $endpoint, bool $override_existing_endpoints = false ): void { |
| 44 |
register_rest_route( self::NAMESPACE, $endpoint, [ |
| 45 |
[ |
| 46 |
'methods' => \WP_REST_Server::READABLE, |
| 47 |
'permission_callback' => fn ( \WP_REST_Request $request ) => $this->validate_access_permission( $request ), |
| 48 |
'args' => $this->get_endpoint_registration_args(), |
| 49 |
'callback' => fn ( \WP_REST_Request $request ) => $this->send( fn () => $this->get( $request ) ), |
| 50 |
], |
| 51 |
], $override_existing_endpoints ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array $item The input array with original keys. |
| 56 |
* @param array $dictionary An associative array mapping old keys to new keys. |
| 57 |
* @return array The array with translated keys. |
| 58 |
*/ |
| 59 |
public function translate_keys( array $item, array $dictionary ): array { |
| 60 |
if ( empty( $dictionary ) ) { |
| 61 |
return $item; |
| 62 |
} |
| 63 |
|
| 64 |
$replaced = []; |
| 65 |
|
| 66 |
foreach ( $item as $key => $value ) { |
| 67 |
if ( ! isset( $dictionary[ $key ] ) ) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$replaced[ $dictionary[ $key ] ] = $value; |
| 72 |
} |
| 73 |
|
| 74 |
return $replaced; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param array<string>|string $input The input data, expected to be an array or JSON-encoded string. |
| 79 |
* @return array The sanitized array of strings. |
| 80 |
*/ |
| 81 |
public static function sanitize_string_array( $input ) { |
| 82 |
if ( ! is_array( $input ) ) { |
| 83 |
$raw = sanitize_text_field( $input ); |
| 84 |
$decoded = json_decode( $raw, true ); |
| 85 |
if ( is_array( $decoded ) ) { |
| 86 |
$input = $decoded; |
| 87 |
} else { |
| 88 |
$input = false !== strpos( $raw, ',' ) ? explode( ',', $raw ) : ( '' !== $raw ? [ $raw ] : [] ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return Collection::make( $input ) |
| 93 |
->reduce( function ( $carry, $value, $key ) { |
| 94 |
if ( $value ) { |
| 95 |
$carry[ $key ] = is_array( $value ) ? self::sanitize_string_array( $value ) : sanitize_text_field( $value ); |
| 96 |
} |
| 97 |
|
| 98 |
return $carry; |
| 99 |
}, [] ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
private function validate_access_permission( $request ): bool { |
| 104 |
$nonce = $request->get_header( self::NONCE_KEY ); |
| 105 |
|
| 106 |
return current_user_can( 'edit_posts' ) && wp_verify_nonce( $nonce, 'wp_rest' ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param callable $cb The route callback. |
| 111 |
* @return \WP_REST_Response | \WP_Error |
| 112 |
*/ |
| 113 |
private function send( callable $cb ) { |
| 114 |
try { |
| 115 |
$response = $cb(); |
| 116 |
} catch ( \Exception $e ) { |
| 117 |
return Error_Builder::make( $e->getCode() ) |
| 118 |
->set_message( $e->getMessage() ) |
| 119 |
->build(); |
| 120 |
} |
| 121 |
|
| 122 |
return $response; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param $args array{ |
| 127 |
* excluded_types: array, |
| 128 |
* included_types: array, |
| 129 |
* keys_conversion_map: array, |
| 130 |
* } The query parameters |
| 131 |
* @return array The query parameters. |
| 132 |
*/ |
| 133 |
public static function build_query_params( array $args ): array { |
| 134 |
$allowed_keys = static::get_allowed_param_keys(); |
| 135 |
$keys_to_encode = static::get_keys_to_encode(); |
| 136 |
$params = []; |
| 137 |
|
| 138 |
foreach ( $args as $key => $value ) { |
| 139 |
if ( ! in_array( $key, $allowed_keys, true ) || ! isset( $value ) ) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! in_array( $key, $keys_to_encode, true ) ) { |
| 144 |
$params[ $key ] = $value; |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
$params[ $key ] = wp_json_encode( $value ); |
| 149 |
} |
| 150 |
|
| 151 |
return $params; |
| 152 |
} |
| 153 |
} |
| 154 |
|