Allow filter customers by role or email

This commit is contained in:
Claudio Sanches 2016-03-30 18:33:37 -03:00
parent f78a2ec68b
commit 1722f95a95
1 changed files with 29 additions and 7 deletions

View File

@ -212,13 +212,16 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
}
if ( ! empty( $request['slug'] ) ) {
$prepared_args['search'] = $request['slug'];
$prepared_args['search_columns'] = array( 'user_nicename' );
// Filter by email.
if ( ! empty( $request['email'] ) ) {
$prepared_args['search'] = $request['email'];
$prepared_args['search_columns'] = array( 'user_email' );
}
// Show only customers.
$prepared_args['role'] = 'customer';
// Filter by role.
if ( 'all' !== $request['role'] ) {
$prepared_args['role'] = $request['role'];
}
/**
* Filter arguments, before passing to WP_User_Query, when querying users via the REST API.
@ -822,6 +825,17 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
return $this->add_additional_fields_schema( $schema );
}
/**
* Get role names.
*
* @return array
*/
protected function get_role_names() {
global $wp_roles;
return array_keys( $wp_roles->role_names );
}
/**
* Get the query params for collections.
*
@ -871,9 +885,17 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['slug'] = array(
'description' => __( 'Limit result set to resources with a specific slug.', 'woocommerce' ),
$params['email'] = array(
'description' => __( 'Limit result set to resources with a specific email.', 'woocommerce' ),
'type' => 'string',
'format' => 'email',
'validate_callback' => 'rest_validate_request_arg',
);
$params['role'] = array(
'description' => __( 'Limit result set to resources with a specific role.', 'woocommerce' ),
'type' => 'string',
'default' => 'customer',
'enum' => array_merge( array( 'all' ), $this->get_role_names() ),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;