From 0f927acdd3a354f9bec7a20607a7d77345902bc1 Mon Sep 17 00:00:00 2001 From: Bryce Date: Wed, 4 Feb 2015 18:59:25 +0700 Subject: [PATCH] Allow people to override roles when querying customers API Just fixing core and closes #7289 --- includes/api/class-wc-api-customers.php | 28 ++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/includes/api/class-wc-api-customers.php b/includes/api/class-wc-api-customers.php index 10f606aaf09..e903a5965d7 100644 --- a/includes/api/class-wc-api-customers.php +++ b/includes/api/class-wc-api-customers.php @@ -513,13 +513,20 @@ class WC_API_Customers extends WC_API_Resource { * * Note that WP_User_Query does not have built-in pagination so limit & offset are used to provide limited * pagination support + * + * The filter for role can be either a string or an array of roles (or blank). * - * @since 2.1 + * @since 2.3 * @param array $args request arguments for filtering query * @return WP_User_Query */ private function query_customers( $args = array() ) { + global $wpdb; + + // Blog ID + $blog_id = get_current_blog_id(); + // default users per page $users_per_page = get_option( 'posts_per_page' ); @@ -531,6 +538,25 @@ class WC_API_Customers extends WC_API_Resource { 'number' => $users_per_page, ); + // Custom Role(s) + if ( ! empty( $args['role'] ) ) { + if ( is_array( $args['role'] ) ) { + unset( $query_args['role'] ); + $query_args['meta_query'] = array( + 'relation' => 'OR', + ); + foreach ( $args['role'] as $role ) { + $query_args['meta_query'][] = array( + 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', + 'value' => $role, + 'compare' => 'like' + ); + } + } else { + $query_args['role'] = $args['role']; + } + } + // Search if ( ! empty( $args['q'] ) ) { $query_args['search'] = $args['q'];