| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\WpRest\Classes; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Modules\WpRest\Base\Query as Base; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class User_Query extends Base { |
| 13 |
const ENDPOINT = 'user'; |
| 14 |
const SEARCH_FILTER_ACCEPTED_ARGS = 1; |
| 15 |
/** |
| 16 |
* @param \WP_REST_Request $request |
| 17 |
* @return \WP_REST_Response |
| 18 |
*/ |
| 19 |
protected function get( \WP_REST_Request $request ) { |
| 20 |
$params = $request->get_params(); |
| 21 |
$search_term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' ); |
| 22 |
|
| 23 |
if ( empty( $search_term ) ) { |
| 24 |
return new \WP_REST_Response( [ |
| 25 |
'success' => true, |
| 26 |
'data' => [ |
| 27 |
'value' => [], |
| 28 |
], |
| 29 |
], 200 ); |
| 30 |
} |
| 31 |
|
| 32 |
$keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ]; |
| 33 |
|
| 34 |
$requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0; |
| 35 |
$validated_count = max( $requested_count, 1 ); |
| 36 |
$count = min( $validated_count, self::MAX_RESPONSE_COUNT ); |
| 37 |
|
| 38 |
$query_args = [ |
| 39 |
'number' => $count, |
| 40 |
'search' => "*$search_term*", |
| 41 |
]; |
| 42 |
|
| 43 |
if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) { |
| 44 |
$query_args['meta_query'] = $params[ self::META_QUERY_KEY ]; |
| 45 |
} |
| 46 |
|
| 47 |
$this->add_filter_to_customize_query(); |
| 48 |
$users = Collection::make( get_users( $query_args ) ); |
| 49 |
$this->remove_filter_to_customize_query(); |
| 50 |
|
| 51 |
global $wp_roles; |
| 52 |
$roles = $wp_roles->roles; |
| 53 |
|
| 54 |
return new \WP_REST_Response( [ |
| 55 |
'success' => true, |
| 56 |
'data' => [ |
| 57 |
'value' => array_values( $users |
| 58 |
->map( function ( $user ) use ( $keys_format_map, $roles ) { |
| 59 |
$user_object = (array) $user; |
| 60 |
$user_object['display_name'] = $user->data->display_name; |
| 61 |
|
| 62 |
if ( isset( $user_object['roles'][0] ) ) { |
| 63 |
$user_role = $user_object['roles'][0]; |
| 64 |
$role = $roles[ $user_role ]['name']; |
| 65 |
$user_object['role'] = $role ?? ucfirst( $user_role ); |
| 66 |
} |
| 67 |
|
| 68 |
return $this->translate_keys( $user_object, $keys_format_map ); |
| 69 |
} ) |
| 70 |
->all() ), |
| 71 |
], |
| 72 |
], 200 ); |
| 73 |
} |
| 74 |
|
| 75 |
public function customize_user_query( $columns ) { |
| 76 |
if ( ! in_array( 'ID', $columns, true ) ) { |
| 77 |
$columns[] = 'ID'; |
| 78 |
} |
| 79 |
|
| 80 |
return $columns; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
private function add_filter_to_customize_query() { |
| 87 |
$priority = self::SEARCH_FILTER_PRIORITY; |
| 88 |
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS; |
| 89 |
|
| 90 |
add_filter( 'user_search_columns', [ $this, 'customize_user_query' ], $priority, $accepted_args ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
private function remove_filter_to_customize_query() { |
| 97 |
$priority = self::SEARCH_FILTER_PRIORITY; |
| 98 |
$accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS; |
| 99 |
|
| 100 |
remove_filter( 'user_search_columns', [ $this, 'customize_user_query' ], $priority, $accepted_args ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
protected function get_endpoint_registration_args(): array { |
| 107 |
return [ |
| 108 |
self::SEARCH_TERM_KEY => [ |
| 109 |
'description' => 'Posts to search', |
| 110 |
'type' => 'string', |
| 111 |
'required' => false, |
| 112 |
'default' => '', |
| 113 |
'sanitize_callback' => 'sanitize_text_field', |
| 114 |
], |
| 115 |
self::KEYS_CONVERSION_MAP_KEY => [ |
| 116 |
'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].', |
| 117 |
'type' => [ 'array', 'string' ], |
| 118 |
'required' => false, |
| 119 |
'default' => [ |
| 120 |
'ID' => 'id', |
| 121 |
'display_name' => 'label', |
| 122 |
'role' => 'groupLabel', |
| 123 |
], |
| 124 |
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 125 |
], |
| 126 |
self::ITEMS_COUNT_KEY => [ |
| 127 |
'description' => 'Posts per page', |
| 128 |
'type' => 'integer', |
| 129 |
'required' => false, |
| 130 |
'default' => self::MAX_RESPONSE_COUNT, |
| 131 |
], |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected static function get_allowed_param_keys(): array { |
| 138 |
return [ |
| 139 |
self::KEYS_CONVERSION_MAP_KEY, |
| 140 |
self::ITEMS_COUNT_KEY, |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
protected static function get_keys_to_encode(): array { |
| 145 |
return [ self::KEYS_CONVERSION_MAP_KEY ]; |
| 146 |
} |
| 147 |
} |
| 148 |
|