Merge pull request woocommerce/woocommerce-admin#1404 from woocommerce/fix/customers-date-between-args

Customers Report: REST API: Fix between argument handling
This commit is contained in:
Paul Sealock 2019-01-30 10:32:54 +13:00 committed by GitHub
commit 4854933485
5 changed files with 164 additions and 56 deletions

View File

@ -61,9 +61,11 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$args['last_order_before'] = $request['last_order_before']; $args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after']; $args['last_order_after'] = $request['last_order_after'];
$between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); $normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false );
$args = array_merge( $args, $normalized ); $between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args; return $args;
} }
@ -296,14 +298,14 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['order'] = array( $params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ), 'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'default' => 'desc', 'default' => 'desc',
'enum' => array( 'asc', 'desc' ), 'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orderby'] = array( $params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'wc-admin' ), 'description' => __( 'Sort collection by object attribute.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'default' => 'date_registered', 'default' => 'date_registered',
@ -321,7 +323,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
), ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['match'] = array( $params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'wc-admin' ), 'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'default' => 'all', 'default' => 'all',
@ -331,7 +333,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
), ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['name'] = array( $params['name'] = array(
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
@ -363,34 +365,44 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['registered_before'] = array( $params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ),
);
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['registered_after'] = array( $params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_min'] = array( $params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ),
);
$params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ),
'type' => 'integer', 'type' => 'integer',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_max'] = array( $params['orders_count_max'] = array(
'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'wc-admin' ),
'type' => 'integer', 'type' => 'integer',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_between'] = array( $params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count between two given integers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['total_spend_min'] = array( $params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ),
@ -405,7 +417,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['total_spend_between'] = array( $params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['avg_order_value_min'] = array( $params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ),
@ -420,7 +432,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['avg_order_value_between'] = array( $params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['last_order_before'] = array( $params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),

View File

@ -56,9 +56,11 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$args['last_order_before'] = $request['last_order_before']; $args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after']; $args['last_order_after'] = $request['last_order_after'];
$between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); $normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false );
$args = array_merge( $args, $normalized ); $between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args; return $args;
} }
@ -119,7 +121,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
public function get_item_schema() { public function get_item_schema() {
// TODO: should any of these be 'indicator's? // TODO: should any of these be 'indicator's?
$totals = array( $totals = array(
'customers_count' => array( 'customers_count' => array(
'description' => __( 'Number of customers.', 'wc-admin' ), 'description' => __( 'Number of customers.', 'wc-admin' ),
'type' => 'integer', 'type' => 'integer',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
@ -131,7 +133,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
'readonly' => true, 'readonly' => true,
), ),
'avg_total_spend' => array( 'avg_total_spend' => array(
'description' => __( 'Average total spend per customer.', 'wc-admin' ), 'description' => __( 'Average total spend per customer.', 'wc-admin' ),
'type' => 'number', 'type' => 'number',
'context' => array( 'view', 'edit' ), 'context' => array( 'view', 'edit' ),
@ -234,7 +236,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['match'] = array( $params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'wc-admin' ), 'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'default' => 'all', 'default' => 'all',
@ -244,7 +246,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
), ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['name'] = array( $params['name'] = array(
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
@ -276,34 +278,44 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['registered_before'] = array( $params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ),
);
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['registered_after'] = array( $params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'string', 'type' => 'string',
'format' => 'date-time', 'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_min'] = array( $params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'wc-admin' ),
'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ),
);
$params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ),
'type' => 'integer', 'type' => 'integer',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_max'] = array( $params['orders_count_max'] = array(
'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'wc-admin' ),
'type' => 'integer', 'type' => 'integer',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['orders_count_between'] = array( $params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an order count between two given integers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['total_spend_min'] = array( $params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ),
@ -318,7 +330,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['total_spend_between'] = array( $params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['avg_order_value_min'] = array( $params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ),
@ -333,7 +345,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['avg_order_value_between'] = array( $params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ), 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_arg' ), 'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ),
); );
$params['last_order_before'] = array( $params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ),

View File

@ -494,13 +494,15 @@ class WC_Admin_Reports_Interval {
} }
/** /**
* Normalize "*_between" parameters to "*_min" and "*_max". * Normalize "*_between" parameters to "*_min" and "*_max" for numeric values
* and "*_after" and "*_before" for date values.
* *
* @param array $request Query params from REST API request. * @param array $request Query params from REST API request.
* @param string|array $param_names One or more param names to handle. Should not include "_between" suffix. * @param string|array $param_names One or more param names to handle. Should not include "_between" suffix.
* @param bool $is_date Boolean if the param is date is related.
* @return array Normalized query values. * @return array Normalized query values.
*/ */
public static function normalize_between_params( $request, $param_names ) { public static function normalize_between_params( $request, $param_names, $is_date ) {
if ( ! is_array( $param_names ) ) { if ( ! is_array( $param_names ) ) {
$param_names = array( $param_names ); $param_names = array( $param_names );
} }
@ -518,12 +520,15 @@ class WC_Admin_Reports_Interval {
continue; continue;
} }
$min = $is_date ? '_after' : '_min';
$max = $is_date ? '_before' : '_max';
if ( $range[0] < $range[1] ) { if ( $range[0] < $range[1] ) {
$normalized[ $param_name . '_min' ] = $range[0]; $normalized[ $param_name . $min ] = $range[0];
$normalized[ $param_name . '_max' ] = $range[1]; $normalized[ $param_name . $max ] = $range[1];
} else { } else {
$normalized[ $param_name . '_min' ] = $range[1]; $normalized[ $param_name . $min ] = $range[1];
$normalized[ $param_name . '_max' ] = $range[0]; $normalized[ $param_name . $max ] = $range[0];
} }
} }
@ -538,7 +543,7 @@ class WC_Admin_Reports_Interval {
* @param string $param Parameter name. * @param string $param Parameter name.
* @return WP_Error|boolean * @return WP_Error|boolean
*/ */
public static function rest_validate_between_arg( $value, $request, $param ) { public static function rest_validate_between_numeric_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) { if ( ! wp_is_numeric_array( $value ) ) {
return new WP_Error( return new WP_Error(
'rest_invalid_param', 'rest_invalid_param',
@ -561,4 +566,36 @@ class WC_Admin_Reports_Interval {
return true; return true;
} }
/**
* Validate a "*_between" range argument (an array with 2 date items).
*
* @param mixed $value Parameter value.
* @param WP_REST_Request $request REST Request.
* @param string $param Parameter name.
* @return WP_Error|boolean
*/
public static function rest_validate_between_date_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: 1: parameter name */
sprintf( __( '%1$s is not a numerically indexed array.', 'wc-admin' ), $param )
);
}
if (
2 !== count( $value ) ||
! rest_parse_date( $value[0] ) ||
! rest_parse_date( $value[1] )
) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: parameter name */
sprintf( __( '%s must contain 2 valid dates.', 'wc-admin' ), $param )
);
}
return true;
}
} }

View File

@ -25,11 +25,11 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* @var array * @var array
*/ */
protected $column_types = array( protected $column_types = array(
'customer_id' => 'intval', 'customer_id' => 'intval',
'user_id' => 'intval', 'user_id' => 'intval',
'orders_count' => 'intval', 'orders_count' => 'intval',
'total_spend' => 'floatval', 'total_spend' => 'floatval',
'avg_order_value' => 'floatval', 'avg_order_value' => 'floatval',
); );
/** /**
@ -60,7 +60,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
global $wpdb; global $wpdb;
// Initialize some report columns that need disambiguation. // Initialize some report columns that need disambiguation.
$this->report_columns['customer_id'] = $wpdb->prefix . self::TABLE_NAME . '.customer_id'; $this->report_columns['customer_id'] = $wpdb->prefix . self::TABLE_NAME . '.customer_id';
$this->report_columns['date_last_order'] = "MAX( {$wpdb->prefix}wc_order_stats.date_created ) as date_last_order"; $this->report_columns['date_last_order'] = "MAX( {$wpdb->prefix}wc_order_stats.date_created ) as date_last_order";
} }
@ -274,7 +274,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
} }
if ( $where_clauses ) { if ( $where_clauses ) {
$preceding_match = empty( $sql_query_params['where_time_clause'] ) ? ' AND ' : " {$match_operator} "; $preceding_match = empty( $sql_query_params['where_time_clause'] ) ? ' AND ' : " {$match_operator} ";
$sql_query_params['where_clause'] = $preceding_match . implode( " {$match_operator} ", $where_clauses ); $sql_query_params['where_clause'] = $preceding_match . implode( " {$match_operator} ", $where_clauses );
} }
@ -284,7 +284,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
} }
if ( $having_clauses ) { if ( $having_clauses ) {
$preceding_match = empty( $sql_query_params['having_clause'] ) ? ' AND ' : " {$match_operator} "; $preceding_match = empty( $sql_query_params['having_clause'] ) ? ' AND ' : " {$match_operator} ";
$sql_query_params['having_clause'] .= $preceding_match . implode( " {$match_operator} ", $having_clauses ); $sql_query_params['having_clause'] .= $preceding_match . implode( " {$match_operator} ", $having_clauses );
} }
@ -304,7 +304,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
$order_stats_table_name = $wpdb->prefix . 'wc_order_stats'; $order_stats_table_name = $wpdb->prefix . 'wc_order_stats';
// These defaults are only partially applied when used via REST API, as that has its own defaults. // These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array( $defaults = array(
'per_page' => get_option( 'posts_per_page' ), 'per_page' => get_option( 'posts_per_page' ),
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
@ -442,7 +442,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* Retrieve a guest (no user_id) customer row by email. * Retrieve a guest (no user_id) customer row by email.
* *
* @param string $email Email address. * @param string $email Email address.
* @returns false|array Customer array if found, boolean false if not. * @return false|array Customer array if found, boolean false if not.
*/ */
public function get_guest_by_email( $email ) { public function get_guest_by_email( $email ) {
global $wpdb; global $wpdb;
@ -467,7 +467,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* Retrieve a registered customer row by user_id. * Retrieve a registered customer row by user_id.
* *
* @param string|int $user_id User ID. * @param string|int $user_id User ID.
* @returns false|array Customer array if found, boolean false if not. * @return false|array Customer array if found, boolean false if not.
*/ */
public function get_customer_by_user_id( $user_id ) { public function get_customer_by_user_id( $user_id ) {
global $wpdb; global $wpdb;
@ -492,7 +492,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* Retrieve a registered customer row id by user_id. * Retrieve a registered customer row id by user_id.
* *
* @param string|int $user_id User ID. * @param string|int $user_id User ID.
* @returns false|int Customer ID if found, boolean false if not. * @return false|int Customer ID if found, boolean false if not.
*/ */
public static function get_customer_id_by_user_id( $user_id ) { public static function get_customer_id_by_user_id( $user_id ) {
global $wpdb; global $wpdb;
@ -554,7 +554,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
if ( $customer_id ) { if ( $customer_id ) {
// Preserve customer_id for existing user_id. // Preserve customer_id for existing user_id.
$data['customer_id'] = $customer_id; $data['customer_id'] = $customer_id;
$format[] = '%d'; $format[] = '%d';
} }
return $wpdb->replace( $wpdb->prefix . self::TABLE_NAME, $data, $format ); return $wpdb->replace( $wpdb->prefix . self::TABLE_NAME, $data, $format );

View File

@ -822,7 +822,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
'f_between' => array( 10, 12 ), // not in params, skipped. 'f_between' => array( 10, 12 ), // not in params, skipped.
); );
$params = array( 'a', 'b', 'c', 'd' ); $params = array( 'a', 'b', 'c', 'd' );
$result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params ); $result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, false );
$expected = array( $expected = array(
'b_min' => 1, 'b_min' => 1,
'b_max' => 5, 'b_max' => 5,
@ -834,21 +834,68 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
} }
/** /**
* Test function that validates *_between query parameters. * Test function that normalizes *_between query parameters for dates to *_after & *_before.
*/ */
public function test_rest_validate_between_arg() { public function test_normalize_between_date_params() {
$request = array(
'a_between' => 'malformed', // won't be normalized (not an array).
'b_between' => array( 1, 5 ), // results in after=1, before=5.
'c_between' => array( 4, 2 ), // results in after=2, before=4.
'd_between' => array( 7 ), // won't be normalized (only 1 item).
'f_between' => array( 10, 12 ), // not in params, skipped.
);
$params = array( 'a', 'b', 'c', 'd' );
$result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, true );
$expected = array(
'b_after' => 1,
'b_before' => 5,
'c_after' => 2,
'c_before' => 4,
);
$this->assertEquals( $result, $expected );
}
/**
* Test function that validates *_between query parameters for numeric values.
*/
public function test_rest_validate_between_numeric_arg() {
$this->assertIsWPError( $this->assertIsWPError(
WC_Admin_Reports_Interval::rest_validate_between_arg( 'not array', null, 'param' ), WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( 'not array', null, 'param' ),
'param is not a numerically indexed array.' 'param is not a numerically indexed array.'
); );
$this->assertIsWPError( $this->assertIsWPError(
WC_Admin_Reports_Interval::rest_validate_between_arg( array( 1 ), null, 'param' ), WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1 ), null, 'param' ),
'param must contain 2 numbers.' 'param must contain 2 numbers.'
); );
$this->assertTrue( $this->assertTrue(
WC_Admin_Reports_Interval::rest_validate_between_arg( array( 1, 2 ), null, 'param' ) WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1, 2 ), null, 'param' )
);
}
/**
* Test function that validates *_between query parameters for date values.
*/
public function rest_validate_between_date_arg() {
$this->assertIsWPError(
WC_Admin_Reports_Interval::rest_validate_between_date_arg( 'not array', null, 'param' ),
'param is not a numerically indexed array.'
);
$this->assertIsWPError(
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00' ), null, 'param' ),
'param must contain 2 valid dates.'
);
$this->assertIsWPError(
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( 'not a valid date' ), null, 'param' ),
'param must contain 2 valid dates.'
);
$this->assertTrue(
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00', '2019-01-15T00:00:00' ), null, 'param' )
); );
} }
} }