fixed coding standards in includes/api/class-wc-api-customers.php
This commit is contained in:
parent
3b4b73851b
commit
f2d8912bc1
|
@ -7,7 +7,7 @@
|
||||||
* @author WooThemes
|
* @author WooThemes
|
||||||
* @category API
|
* @category API
|
||||||
* @package WooCommerce/API
|
* @package WooCommerce/API
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||||
|
@ -49,7 +49,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
* GET /customers/<id>
|
* GET /customers/<id>
|
||||||
* GET /customers/<id>/orders
|
* GET /customers/<id>/orders
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param array $routes
|
* @param array $routes
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -135,7 +135,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
/**
|
/**
|
||||||
* Get all customers
|
* Get all customers
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param array $fields
|
* @param array $fields
|
||||||
* @param array $filter
|
* @param array $filter
|
||||||
* @param int $page
|
* @param int $page
|
||||||
|
@ -149,10 +149,11 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
|
|
||||||
$customers = array();
|
$customers = array();
|
||||||
|
|
||||||
foreach( $query->get_results() as $user_id ) {
|
foreach ( $query->get_results() as $user_id ) {
|
||||||
|
|
||||||
if ( ! $this->is_readable( $user_id ) )
|
if ( ! $this->is_readable( $user_id ) ) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$customers[] = current( $this->get_customer( $user_id, $fields ) );
|
$customers[] = current( $this->get_customer( $user_id, $fields ) );
|
||||||
}
|
}
|
||||||
|
@ -165,7 +166,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
/**
|
/**
|
||||||
* Get the customer for the given ID
|
* Get the customer for the given ID
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param int $id the customer ID
|
* @param int $id the customer ID
|
||||||
* @param string $fields
|
* @param string $fields
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -175,8 +176,9 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
|
|
||||||
$id = $this->validate_request( $id, 'customer', 'read' );
|
$id = $this->validate_request( $id, 'customer', 'read' );
|
||||||
|
|
||||||
if ( is_wp_error( $id ) )
|
if ( is_wp_error( $id ) ) {
|
||||||
return $id;
|
return $id;
|
||||||
|
}
|
||||||
|
|
||||||
$customer = new WP_User( $id );
|
$customer = new WP_User( $id );
|
||||||
|
|
||||||
|
@ -256,7 +258,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
/**
|
/**
|
||||||
* Get the total number of customers
|
* Get the total number of customers
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param array $filter
|
* @param array $filter
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -264,8 +266,9 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
|
|
||||||
$query = $this->query_customers( $filter );
|
$query = $this->query_customers( $filter );
|
||||||
|
|
||||||
if ( ! current_user_can( 'list_users' ) )
|
if ( ! current_user_can( 'list_users' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_read_customers_count', __( 'You do not have permission to read the customers count', 'woocommerce' ), array( 'status' => 401 ) );
|
return new WP_Error( 'woocommerce_api_user_cannot_read_customers_count', __( 'You do not have permission to read the customers count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||||
|
}
|
||||||
|
|
||||||
return array( 'count' => count( $query->get_results() ) );
|
return array( 'count' => count( $query->get_results() ) );
|
||||||
}
|
}
|
||||||
|
@ -414,7 +417,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
/**
|
/**
|
||||||
* Get the orders for a customer
|
* Get the orders for a customer
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param int $id the customer ID
|
* @param int $id the customer ID
|
||||||
* @param string $fields fields to include in response
|
* @param string $fields fields to include in response
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -424,8 +427,9 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
|
|
||||||
$id = $this->validate_request( $id, 'customer', 'read' );
|
$id = $this->validate_request( $id, 'customer', 'read' );
|
||||||
|
|
||||||
if ( is_wp_error( $id ) )
|
if ( is_wp_error( $id ) ) {
|
||||||
return $id;
|
return $id;
|
||||||
|
}
|
||||||
|
|
||||||
$order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id
|
$order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id
|
||||||
FROM $wpdb->posts AS posts
|
FROM $wpdb->posts AS posts
|
||||||
|
@ -436,8 +440,9 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
AND posts.post_status = 'publish'
|
AND posts.post_status = 'publish'
|
||||||
", $id ) );
|
", $id ) );
|
||||||
|
|
||||||
if ( empty( $order_ids ) )
|
if ( empty( $order_ids ) ) {
|
||||||
return array( 'orders' => array() );
|
return array( 'orders' => array() );
|
||||||
|
}
|
||||||
|
|
||||||
$orders = array();
|
$orders = array();
|
||||||
|
|
||||||
|
@ -567,17 +572,19 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
/**
|
/**
|
||||||
* Modify the WP_User_Query to support filtering on the date the customer was created
|
* Modify the WP_User_Query to support filtering on the date the customer was created
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @param WP_User_Query $query
|
* @param WP_User_Query $query
|
||||||
*/
|
*/
|
||||||
public function modify_user_query( $query ) {
|
public function modify_user_query( $query ) {
|
||||||
|
|
||||||
if ( $this->created_at_min )
|
if ( $this->created_at_min ) {
|
||||||
$query->query_where .= sprintf( " AND user_registered >= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i:%%s' )", esc_sql( $this->created_at_min ) );
|
$query->query_where .= sprintf( " AND user_registered >= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i:%%s' )", esc_sql( $this->created_at_min ) );
|
||||||
|
}
|
||||||
|
|
||||||
if ( $this->created_at_max )
|
if ( $this->created_at_max ) {
|
||||||
$query->query_where .= sprintf( " AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i:%%s' )", esc_sql( $this->created_at_max ) );
|
$query->query_where .= sprintf( " AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i:%%s' )", esc_sql( $this->created_at_max ) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for @see get_avatar() which doesn't simply return
|
* Wrapper for @see get_avatar() which doesn't simply return
|
||||||
|
@ -605,7 +612,7 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
* 2) the ID returns a valid WP_User
|
* 2) the ID returns a valid WP_User
|
||||||
* 3) the current user has the proper permissions
|
* 3) the current user has the proper permissions
|
||||||
*
|
*
|
||||||
* @since 2.1
|
* @since 2.2
|
||||||
* @see WC_API_Resource::validate_request()
|
* @see WC_API_Resource::validate_request()
|
||||||
* @param string|int $id the customer ID
|
* @param string|int $id the customer ID
|
||||||
* @param string $type the request type, unused because this method overrides the parent class
|
* @param string $type the request type, unused because this method overrides the parent class
|
||||||
|
@ -617,31 +624,36 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
// validate ID
|
// validate ID
|
||||||
if ( empty( $id ) )
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_customer_id', __( 'Invalid customer ID', 'woocommerce' ), array( 'status' => 404 ) );
|
return new WP_Error( 'woocommerce_api_invalid_customer_id', __( 'Invalid customer ID', 'woocommerce' ), array( 'status' => 404 ) );
|
||||||
|
}
|
||||||
|
|
||||||
// non-existent IDs return a valid WP_User object with the user ID = 0
|
// non-existent IDs return a valid WP_User object with the user ID = 0
|
||||||
$customer = new WP_User( $id );
|
$customer = new WP_User( $id );
|
||||||
|
|
||||||
if ( 0 === $customer->ID )
|
if ( 0 === $customer->ID ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_customer', __( 'Invalid customer', 'woocommerce' ), array( 'status' => 404 ) );
|
return new WP_Error( 'woocommerce_api_invalid_customer', __( 'Invalid customer', 'woocommerce' ), array( 'status' => 404 ) );
|
||||||
|
}
|
||||||
|
|
||||||
// validate permissions
|
// validate permissions
|
||||||
switch ( $context ) {
|
switch ( $context ) {
|
||||||
|
|
||||||
case 'read':
|
case 'read':
|
||||||
if ( ! current_user_can( 'list_users' ) )
|
if ( ! current_user_can( 'list_users' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_read_customer', __( 'You do not have permission to read this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
return new WP_Error( 'woocommerce_api_user_cannot_read_customer', __( 'You do not have permission to read this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'edit':
|
case 'edit':
|
||||||
if ( ! current_user_can( 'edit_users' ) )
|
if ( ! current_user_can( 'edit_users' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_edit_customer', __( 'You do not have permission to edit this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
return new WP_Error( 'woocommerce_api_user_cannot_edit_customer', __( 'You do not have permission to edit this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'delete':
|
case 'delete':
|
||||||
if ( ! current_user_can( 'delete_users' ) )
|
if ( ! current_user_can( 'delete_users' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_delete_customer', __( 'You do not have permission to delete this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
return new WP_Error( 'woocommerce_api_user_cannot_delete_customer', __( 'You do not have permission to delete this customer', 'woocommerce' ), array( 'status' => 401 ) );
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue