first commit
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Elementor_Post_Meta {
|
||||
|
||||
public function register(): void {
|
||||
$post_types = get_post_types_by_support( 'elementor' );
|
||||
|
||||
foreach ( $post_types as $post_type ) {
|
||||
$this->register_edit_mode_meta( $post_type );
|
||||
$this->register_template_type_meta( $post_type );
|
||||
$this->register_elementor_data_meta( $post_type );
|
||||
$this->register_page_settings_meta( $post_type );
|
||||
|
||||
if ( Utils::has_pro() ) {
|
||||
$this->register_conditions_meta( $post_type );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function register_edit_mode_meta( string $post_type ): void {
|
||||
register_meta( 'post', '_elementor_edit_mode', [
|
||||
'single' => true,
|
||||
'object_subtype' => $post_type,
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'title' => 'Elementor edit mode',
|
||||
'description' => 'Elementor edit mode, `builder` is required for Elementor editing',
|
||||
'type' => 'string',
|
||||
'enum' => [ '', 'builder' ],
|
||||
'default' => '',
|
||||
'context' => [ 'edit' ],
|
||||
],
|
||||
],
|
||||
'auth_callback' => [ $this, 'check_edit_permission' ],
|
||||
]);
|
||||
}
|
||||
|
||||
private function register_template_type_meta( string $post_type ): void {
|
||||
$document_types = Plugin::$instance->documents->get_document_types();
|
||||
|
||||
register_meta( 'post', '_elementor_template_type', [
|
||||
'single' => true,
|
||||
'object_subtype' => $post_type,
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'title' => 'Elementor template type',
|
||||
'description' => 'Elementor document type',
|
||||
'type' => 'string',
|
||||
'enum' => array_merge( array_keys( $document_types ), [ '' ] ),
|
||||
'default' => '',
|
||||
'context' => [ 'edit' ],
|
||||
],
|
||||
],
|
||||
'auth_callback' => [ $this, 'check_edit_permission' ],
|
||||
]);
|
||||
}
|
||||
|
||||
private function register_elementor_data_meta( string $post_type ): void {
|
||||
register_meta( 'post', '_elementor_data', [
|
||||
'single' => true,
|
||||
'object_subtype' => $post_type,
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'title' => 'Elementor data',
|
||||
'description' => 'Elementor JSON as a string',
|
||||
'type' => 'string',
|
||||
'default' => '',
|
||||
'context' => [ 'edit' ],
|
||||
],
|
||||
],
|
||||
'auth_callback' => [ $this, 'check_edit_permission' ],
|
||||
]);
|
||||
}
|
||||
|
||||
private function register_page_settings_meta( string $post_type ): void {
|
||||
register_meta( 'post', '_elementor_page_settings', [
|
||||
'single' => true,
|
||||
'object_subtype' => $post_type,
|
||||
'type' => 'object',
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'title' => 'Elementor page settings',
|
||||
'description' => 'Elementor page level settings',
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'hide_title' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'yes', 'no' ],
|
||||
'default' => '',
|
||||
],
|
||||
],
|
||||
'default' => '{}',
|
||||
'additionalProperties' => true,
|
||||
'context' => [ 'edit' ],
|
||||
],
|
||||
],
|
||||
'auth_callback' => [ $this, 'check_edit_permission' ],
|
||||
]);
|
||||
}
|
||||
|
||||
private function register_conditions_meta( string $post_type ): void {
|
||||
register_meta( 'post', '_elementor_conditions', [
|
||||
'object_subtype' => $post_type,
|
||||
'type' => 'object',
|
||||
'title' => 'Elementor conditions',
|
||||
'description' => 'Elementor conditions',
|
||||
'single' => true,
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'description' => 'Elementor conditions',
|
||||
'type' => 'array',
|
||||
'additionalProperties' => true,
|
||||
'default' => [],
|
||||
'context' => [ 'edit' ],
|
||||
],
|
||||
],
|
||||
'auth_callback' => [ $this, 'check_edit_permission' ],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user has permission to edit the specific post with elementor
|
||||
*
|
||||
* @param bool $allowed Whether the user can add the post meta. Default false.
|
||||
* @param string $meta_key The meta key.
|
||||
* @param int $post_id Post ID.
|
||||
* @return bool
|
||||
* @since 3.27.0
|
||||
*/
|
||||
public function check_edit_permission( bool $allowed, string $meta_key, int $post_id ): bool {
|
||||
$document = Plugin::$instance->documents->get( $post_id );
|
||||
|
||||
return $document && $document->is_editable_by_current_user();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Elementor_Settings {
|
||||
|
||||
public function register(): void {
|
||||
register_rest_route('elementor/v1', '/settings/(?P<key>[\w_-]+)', [
|
||||
[
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'permission_callback' => function (): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
},
|
||||
'sanitize_callback' => function ( string $param ): string {
|
||||
return esc_attr( $param );
|
||||
},
|
||||
'validate_callback' => function ( \WP_REST_Request $request ): bool {
|
||||
$params = $request->get_params();
|
||||
|
||||
return 0 === strpos( $params['key'], 'elementor' );
|
||||
},
|
||||
'callback' => function ( $request ): \WP_REST_Response {
|
||||
try {
|
||||
$key = $request->get_param( 'key' );
|
||||
$current_value = get_option( $key );
|
||||
|
||||
return new \WP_REST_Response([
|
||||
'success' => true,
|
||||
// Nest in order to allow extending the response with more details.
|
||||
'data' => [
|
||||
'value' => $current_value,
|
||||
],
|
||||
], 200);
|
||||
} catch ( \Exception $e ) {
|
||||
return new \WP_REST_Response([
|
||||
'success' => false,
|
||||
'data' => [
|
||||
'message' => $e->getMessage(),
|
||||
],
|
||||
], 500);
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
register_rest_route('elementor/v1', '/settings/(?P<key>[\w_-]+)', [
|
||||
[
|
||||
'methods' => \WP_REST_Server::EDITABLE,
|
||||
'permission_callback' => function (): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
},
|
||||
'sanitize_callback' => function ( string $param ): string {
|
||||
return esc_attr( $param );
|
||||
},
|
||||
'validate_callback' => function ( \WP_REST_Request $request ): bool {
|
||||
$params = $request->get_params();
|
||||
return 0 === strpos( $params['key'], 'elementor' ) && isset( $params['value'] );
|
||||
},
|
||||
'callback' => function ( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$key = $request->get_param( 'key' );
|
||||
$new_value = $request->get_param( 'value' );
|
||||
$current_value = get_option( $key );
|
||||
|
||||
if ( $new_value === $current_value ) {
|
||||
return new \WP_REST_Response([
|
||||
'success' => true,
|
||||
], 200);
|
||||
}
|
||||
|
||||
$success = update_option( $key, $new_value );
|
||||
if ( $success ) {
|
||||
return new \WP_REST_Response([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'message' => 'Setting updated successfully.',
|
||||
],
|
||||
], 200);
|
||||
} else {
|
||||
return new \WP_REST_Response([
|
||||
'success' => false,
|
||||
'data' => [
|
||||
'message' => 'Failed to update setting.',
|
||||
],
|
||||
], 500);
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Elementor_User_Meta {
|
||||
|
||||
private function get_meta_config(): array {
|
||||
return [
|
||||
'elementor_introduction' => [
|
||||
'schema' => [
|
||||
'description' => 'Elementor user meta data',
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'ai_get_started' => [
|
||||
'type' => 'boolean',
|
||||
],
|
||||
],
|
||||
'additionalProperties' => true,
|
||||
'context' => [ 'view', 'edit' ],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
foreach ( $this->get_meta_config() as $key => $config ) {
|
||||
$config['get_callback'] = function( $user, $field_name, $request ) {
|
||||
return get_user_meta( $user['id'], $field_name, true );
|
||||
};
|
||||
|
||||
$config['update_callback'] = function( $meta_value, \WP_User $user, $field_name, $request ) {
|
||||
if ( 'PATCH' === $request->get_method() ) {
|
||||
$existing = get_user_meta( $user->ID, $field_name, true );
|
||||
if ( is_array( $existing ) && is_array( $meta_value ) ) {
|
||||
$meta_value = array_merge( $existing, $meta_value );
|
||||
}
|
||||
}
|
||||
|
||||
return update_user_meta( $user->ID, $field_name, $meta_value );
|
||||
};
|
||||
|
||||
register_rest_field( 'user', $key, $config );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\WpRest\Base\Query as Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Post_Query extends Base {
|
||||
const ENDPOINT = 'post';
|
||||
const SEARCH_FILTER_ACCEPTED_ARGS = 2;
|
||||
const DEFAULT_FORBIDDEN_POST_TYPES = [ 'e-floating-buttons', 'e-landing-page', 'elementor_library', 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset' ];
|
||||
|
||||
/**
|
||||
* @param string $search_term The original search query.
|
||||
* @param \WP_Query $wp_query The WP_Query instance.
|
||||
* @return string Modified search query.
|
||||
*/
|
||||
public function customize_post_query( string $search_term, \WP_Query $wp_query ) {
|
||||
$term = $wp_query->get( 'search_term' ) ?? '';
|
||||
$is_custom_search = $wp_query->get( 'custom_search' ) ?? false;
|
||||
|
||||
if ( $is_custom_search && ! empty( $term ) ) {
|
||||
$escaped = esc_sql( $term );
|
||||
$search_term .= ' AND (';
|
||||
$search_term .= "post_title LIKE '%{$escaped}%'";
|
||||
if ( ctype_digit( $term ) ) {
|
||||
$search_term .= ' OR ID = ' . intval( $term );
|
||||
} else {
|
||||
$search_term .= " OR ID LIKE '%{$escaped}%'";
|
||||
}
|
||||
$search_term .= ')';
|
||||
}
|
||||
|
||||
return $search_term;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
protected function get( \WP_REST_Request $request ) {
|
||||
$params = $request->get_params();
|
||||
$term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
|
||||
|
||||
if ( empty( $term ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => [],
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
$keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ];
|
||||
$requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
|
||||
$validated_count = max( $requested_count, 1 );
|
||||
$post_count = min( $validated_count, self::MAX_RESPONSE_COUNT );
|
||||
$is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true;
|
||||
$post_types = $this->get_post_types_from_params( $params );
|
||||
|
||||
$query_args = [
|
||||
'post_type' => array_keys( $post_types ),
|
||||
'numberposts' => $post_count,
|
||||
'suppress_filters' => false,
|
||||
'custom_search' => true,
|
||||
'search_term' => $term,
|
||||
'post_status' => $is_public_only ? 'publish' : 'any',
|
||||
'orderby' => 'ID',
|
||||
'order' => 'ASC',
|
||||
];
|
||||
|
||||
if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
|
||||
$query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
|
||||
}
|
||||
|
||||
if ( ! empty( $params[ self::TAX_QUERY_KEY ] ) && is_array( $params[ self::TAX_QUERY_KEY ] ) ) {
|
||||
$query_args['tax_query'] = $params[ self::TAX_QUERY_KEY ];
|
||||
}
|
||||
|
||||
$this->add_filter_to_customize_query();
|
||||
$posts = new Collection( get_posts( $query_args ) );
|
||||
$this->remove_filter_to_customize_query();
|
||||
|
||||
$post_type_labels = ( new Collection( $post_types ) )
|
||||
->map( function ( $pt ) {
|
||||
return $pt->label;
|
||||
} )
|
||||
->all();
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => $posts
|
||||
->map( function ( $post ) use ( $keys_format_map, $post_type_labels ) {
|
||||
$post_object = (array) $post;
|
||||
|
||||
if ( isset( $post_object['post_type'] ) ) {
|
||||
$pt_name = $post_object['post_type'];
|
||||
if ( isset( $post_type_labels[ $pt_name ] ) ) {
|
||||
$post_object['post_type'] = $post_type_labels[ $pt_name ];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->translate_keys( $post_object, $keys_format_map );
|
||||
} )
|
||||
->all(),
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function add_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
add_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function remove_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
remove_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
protected function get_endpoint_registration_args(): array {
|
||||
return [
|
||||
self::INCLUDED_TYPE_KEY => [
|
||||
'description' => 'Included post types',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::EXCLUDED_TYPE_KEY => [
|
||||
'description' => 'Post type to exclude',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => self::DEFAULT_FORBIDDEN_POST_TYPES,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::SEARCH_TERM_KEY => [
|
||||
'description' => 'Posts to search',
|
||||
'type' => 'string',
|
||||
'required' => false,
|
||||
'default' => '',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
],
|
||||
self::KEYS_CONVERSION_MAP_KEY => [
|
||||
'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => [
|
||||
'ID' => 'id',
|
||||
'post_title' => 'label',
|
||||
'post_type' => 'groupLabel',
|
||||
],
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::ITEMS_COUNT_KEY => [
|
||||
'description' => 'Posts per page',
|
||||
'type' => 'integer',
|
||||
'required' => false,
|
||||
'default' => self::MAX_RESPONSE_COUNT,
|
||||
],
|
||||
self::IS_PUBLIC_KEY => [
|
||||
'description' => 'Whether to include only public post types',
|
||||
'type' => 'boolean',
|
||||
'required' => false,
|
||||
'default' => true,
|
||||
],
|
||||
self::META_QUERY_KEY => [
|
||||
'description' => 'WP_Query meta_query array',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::TAX_QUERY_KEY => [
|
||||
'description' => 'WP_Query tax_query array',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected static function get_allowed_param_keys(): array {
|
||||
return [
|
||||
self::EXCLUDED_TYPE_KEY,
|
||||
self::INCLUDED_TYPE_KEY,
|
||||
self::KEYS_CONVERSION_MAP_KEY,
|
||||
self::META_QUERY_KEY,
|
||||
self::TAX_QUERY_KEY,
|
||||
self::IS_PUBLIC_KEY,
|
||||
self::ITEMS_COUNT_KEY,
|
||||
];
|
||||
}
|
||||
|
||||
protected static function get_keys_to_encode(): array {
|
||||
return [
|
||||
self::EXCLUDED_TYPE_KEY,
|
||||
self::INCLUDED_TYPE_KEY,
|
||||
self::KEYS_CONVERSION_MAP_KEY,
|
||||
self::META_QUERY_KEY,
|
||||
self::TAX_QUERY_KEY,
|
||||
];
|
||||
}
|
||||
|
||||
private function get_post_types_from_params( $params ) {
|
||||
$included_types = $params[ self::INCLUDED_TYPE_KEY ];
|
||||
$excluded_types = $params[ self::EXCLUDED_TYPE_KEY ];
|
||||
$post_type_query_args = [
|
||||
'public' => true,
|
||||
];
|
||||
|
||||
$post_types = get_post_types( $post_type_query_args, 'objects' );
|
||||
|
||||
return Collection::make( $post_types )
|
||||
->filter( function ( $slug, $post_type ) use ( $included_types, $excluded_types ) {
|
||||
return ( empty( $included_types ) || in_array( $post_type, $included_types ) ) &&
|
||||
( empty( $excluded_types ) || ! in_array( $post_type, $excluded_types ) );
|
||||
} )->all();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\WpRest\Base\Query as Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Term_Query extends Base {
|
||||
const ENDPOINT = 'term';
|
||||
const SEARCH_FILTER_ACCEPTED_ARGS = 3;
|
||||
|
||||
/**
|
||||
* @param array $clauses Associative array of the clauses for the query.
|
||||
* @param array $taxonomies Array of taxonomy names.
|
||||
* @param array $args The args passed to 'get_terms()'.
|
||||
* @return array Modified clauses.
|
||||
*/
|
||||
public function customize_terms_query( $clauses, $taxonomies, $args ) {
|
||||
if ( ! $args['custom_search'] ) {
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
if ( is_numeric( $args['name__like'] ) ) {
|
||||
$clauses['where'] = '(' . $clauses['where'] . ' OR t.term_id = ' . $args['name__like'] . ')';
|
||||
}
|
||||
|
||||
if ( empty( $args['excluded_taxonomies'] ) ) {
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
$excluded_taxonomies = $args['excluded_taxonomies'];
|
||||
$escaped = array_map( 'esc_sql', $excluded_taxonomies );
|
||||
$list = "'" . implode( "','", $escaped ) . "'";
|
||||
$clauses['where'] .= " AND tt.taxonomy NOT IN ({$list})";
|
||||
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
protected function get( \WP_REST_Request $request ) {
|
||||
$params = $request->get_params();
|
||||
$term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
|
||||
|
||||
if ( empty( $term ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => [],
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
$included_taxonomies = $params[ self::INCLUDED_TYPE_KEY ];
|
||||
$excluded_taxonomies = $params[ self::EXCLUDED_TYPE_KEY ];
|
||||
|
||||
$keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ];
|
||||
|
||||
$requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
|
||||
$validated_count = max( $requested_count, 1 );
|
||||
$count = min( $validated_count, self::MAX_RESPONSE_COUNT );
|
||||
|
||||
$should_hide_empty = $params[ self::HIDE_EMPTY_KEY ] ?? false;
|
||||
|
||||
$query_args = [
|
||||
'number' => $count,
|
||||
'name__like' => $term,
|
||||
'hide_empty' => $should_hide_empty,
|
||||
'taxonomy' => ! empty( $included_taxonomies ) ? $included_taxonomies : null,
|
||||
'excluded_taxonomies' => $excluded_taxonomies ?? [],
|
||||
'suppress_filter' => false,
|
||||
'custom_search' => true,
|
||||
];
|
||||
|
||||
if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
|
||||
$query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
|
||||
}
|
||||
|
||||
$this->add_filter_to_customize_query();
|
||||
$terms = new Collection( get_terms( $query_args ) );
|
||||
$this->remove_filter_to_customize_query();
|
||||
|
||||
$term_group_labels = $terms
|
||||
->reduce( function ( $term_types, $term ) {
|
||||
if ( ! isset( $term_types[ $term->taxonomy ] ) ) {
|
||||
$taxonomy = get_taxonomy( $term->taxonomy );
|
||||
$term_types[ $term->taxonomy ] = $taxonomy->labels->name ?? $term->labels;
|
||||
}
|
||||
|
||||
return $term_types;
|
||||
}, [] );
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => $terms
|
||||
->map( function ( $term ) use ( $keys_format_map, $term_group_labels ) {
|
||||
$term_object = (array) $term;
|
||||
|
||||
if ( isset( $term_object['taxonomy'] ) ) {
|
||||
$group_name = $term_object['taxonomy'];
|
||||
|
||||
if ( isset( $term_group_labels[ $group_name ] ) ) {
|
||||
$term_object['taxonomy'] = $term_group_labels[ $group_name ];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->translate_keys( $term_object, $keys_format_map );
|
||||
} )
|
||||
->all(),
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function add_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
add_filter( 'terms_clauses', [ $this, 'customize_terms_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function remove_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
remove_filter( 'terms_clauses', [ $this, 'customize_terms_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_endpoint_registration_args(): array {
|
||||
return [
|
||||
self::INCLUDED_TYPE_KEY => [
|
||||
'description' => 'Included taxonomy containing terms (categories, tags, etc...)',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::EXCLUDED_TYPE_KEY => [
|
||||
'description' => 'Excluded taxonomy containing terms (categories, tags, etc...)',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::SEARCH_TERM_KEY => [
|
||||
'description' => 'Terms to search',
|
||||
'type' => 'string',
|
||||
'required' => false,
|
||||
'default' => '',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
],
|
||||
self::KEYS_CONVERSION_MAP_KEY => [
|
||||
'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => [
|
||||
'term_id' => 'id',
|
||||
'name' => 'label',
|
||||
'taxonomy' => 'groupLabel',
|
||||
],
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::ITEMS_COUNT_KEY => [
|
||||
'description' => 'Terms per request',
|
||||
'type' => 'integer',
|
||||
'required' => false,
|
||||
'default' => self::MAX_RESPONSE_COUNT,
|
||||
],
|
||||
self::HIDE_EMPTY_KEY => [
|
||||
'description' => 'Whether to include only public terms',
|
||||
'type' => 'boolean',
|
||||
'required' => false,
|
||||
'default' => false,
|
||||
],
|
||||
self::META_QUERY_KEY => [
|
||||
'description' => 'WP_Query meta_query array',
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected static function get_allowed_param_keys(): array {
|
||||
return [
|
||||
self::EXCLUDED_TYPE_KEY,
|
||||
self::INCLUDED_TYPE_KEY,
|
||||
self::KEYS_CONVERSION_MAP_KEY,
|
||||
self::META_QUERY_KEY,
|
||||
self::TAX_QUERY_KEY,
|
||||
self::ITEMS_COUNT_KEY,
|
||||
];
|
||||
}
|
||||
|
||||
protected static function get_keys_to_encode(): array {
|
||||
return [
|
||||
self::EXCLUDED_TYPE_KEY,
|
||||
self::INCLUDED_TYPE_KEY,
|
||||
self::KEYS_CONVERSION_MAP_KEY,
|
||||
self::META_QUERY_KEY,
|
||||
self::TAX_QUERY_KEY,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\WpRest\Classes;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\WpRest\Base\Query as Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class User_Query extends Base {
|
||||
const ENDPOINT = 'user';
|
||||
const SEARCH_FILTER_ACCEPTED_ARGS = 1;
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
protected function get( \WP_REST_Request $request ) {
|
||||
$params = $request->get_params();
|
||||
$search_term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
|
||||
|
||||
if ( empty( $search_term ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => [],
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
$keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ];
|
||||
|
||||
$requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
|
||||
$validated_count = max( $requested_count, 1 );
|
||||
$count = min( $validated_count, self::MAX_RESPONSE_COUNT );
|
||||
|
||||
$query_args = [
|
||||
'number' => $count,
|
||||
'search' => "*$search_term*",
|
||||
];
|
||||
|
||||
if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
|
||||
$query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
|
||||
}
|
||||
|
||||
$this->add_filter_to_customize_query();
|
||||
$users = Collection::make( get_users( $query_args ) );
|
||||
$this->remove_filter_to_customize_query();
|
||||
|
||||
global $wp_roles;
|
||||
$roles = $wp_roles->roles;
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'value' => array_values( $users
|
||||
->map( function ( $user ) use ( $keys_format_map, $roles ) {
|
||||
$user_object = (array) $user;
|
||||
$user_object['display_name'] = $user->data->display_name;
|
||||
|
||||
if ( isset( $user_object['roles'][0] ) ) {
|
||||
$user_role = $user_object['roles'][0];
|
||||
$role = $roles[ $user_role ]['name'];
|
||||
$user_object['role'] = $role ?? ucfirst( $user_role );
|
||||
}
|
||||
|
||||
return $this->translate_keys( $user_object, $keys_format_map );
|
||||
} )
|
||||
->all() ),
|
||||
],
|
||||
], 200 );
|
||||
}
|
||||
|
||||
public function customize_user_query( $columns ) {
|
||||
if ( ! in_array( 'ID', $columns, true ) ) {
|
||||
$columns[] = 'ID';
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function add_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
add_filter( 'user_search_columns', [ $this, 'customize_user_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function remove_filter_to_customize_query() {
|
||||
$priority = self::SEARCH_FILTER_PRIORITY;
|
||||
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
|
||||
|
||||
remove_filter( 'user_search_columns', [ $this, 'customize_user_query' ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_endpoint_registration_args(): array {
|
||||
return [
|
||||
self::SEARCH_TERM_KEY => [
|
||||
'description' => 'Posts to search',
|
||||
'type' => 'string',
|
||||
'required' => false,
|
||||
'default' => '',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
],
|
||||
self::KEYS_CONVERSION_MAP_KEY => [
|
||||
'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].',
|
||||
'type' => [ 'array', 'string' ],
|
||||
'required' => false,
|
||||
'default' => [
|
||||
'ID' => 'id',
|
||||
'display_name' => 'label',
|
||||
'role' => 'groupLabel',
|
||||
],
|
||||
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
|
||||
],
|
||||
self::ITEMS_COUNT_KEY => [
|
||||
'description' => 'Posts per page',
|
||||
'type' => 'integer',
|
||||
'required' => false,
|
||||
'default' => self::MAX_RESPONSE_COUNT,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected static function get_allowed_param_keys(): array {
|
||||
return [
|
||||
self::KEYS_CONVERSION_MAP_KEY,
|
||||
self::ITEMS_COUNT_KEY,
|
||||
];
|
||||
}
|
||||
|
||||
protected static function get_keys_to_encode(): array {
|
||||
return [ self::KEYS_CONVERSION_MAP_KEY ];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user