From bca0785e642dada42d8bc791137ed018a20e038c Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 08:58:07 +1300 Subject: [PATCH 01/10] Customers Report: support between args for dates --- ...dmin-rest-reports-customers-controller.php | 33 +++++++++++-------- ...est-reports-customers-stats-controller.php | 24 +++++++------- .../class-wc-admin-reports-interval.php | 33 ++++++++++++++++++- ...-wc-admin-reports-customers-data-store.php | 26 +++++++-------- .../tests/api/reports-interval.php | 8 ++--- 5 files changed, 81 insertions(+), 43 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 8c01d455a69..4b5883d06e0 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -52,6 +52,8 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['country'] = $request['country']; $args['last_active_before'] = $request['last_active_before']; $args['last_active_after'] = $request['last_active_after']; + $args['last_active_min'] = $request['last_active_min']; + $args['last_active_max'] = $request['last_active_max']; $args['orders_count_min'] = $request['orders_count_min']; $args['orders_count_max'] = $request['orders_count_max']; $args['total_spend_min'] = $request['total_spend_min']; @@ -61,7 +63,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_before'] = $request['last_order_before']; $args['last_order_after'] = $request['last_order_after']; - $between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $between_params = array( 'orders_count', 'total_spend', 'avg_order_value', 'last_active' ); $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); $args = array_merge( $args, $normalized ); @@ -296,14 +298,14 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ); - $params['order'] = array( + $params['order'] = array( 'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ), 'type' => 'string', 'default' => 'desc', 'enum' => array( 'asc', 'desc' ), 'validate_callback' => 'rest_validate_request_arg', ); - $params['orderby'] = array( + $params['orderby'] = array( 'description' => __( 'Sort collection by object attribute.', 'wc-admin' ), 'type' => 'string', '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', ); - $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' ), 'type' => 'string', 'default' => 'all', @@ -331,7 +333,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control ), 'validate_callback' => 'rest_validate_request_arg', ); - $params['name'] = array( + $params['name'] = array( 'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', @@ -363,34 +365,39 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control 'format' => 'date-time', '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' ), 'type' => 'string', 'format' => 'date-time', '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' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); - $params['orders_count_min'] = array( + $params['orders_count_min'] = array( 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ), 'type' => 'integer', 'sanitize_callback' => 'absint', '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' ), 'type' => 'integer', 'sanitize_callback' => 'absint', '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' ), '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( 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ), @@ -405,7 +412,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $params['total_spend_between'] = array( 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ), '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( 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ), @@ -420,7 +427,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $params['avg_order_value_between'] = array( 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ), '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( 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index af24ea6119d..2b906f6c049 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -119,7 +119,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C public function get_item_schema() { // TODO: should any of these be 'indicator's? $totals = array( - 'customers_count' => array( + 'customers_count' => array( 'description' => __( 'Number of customers.', 'wc-admin' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), @@ -131,7 +131,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'avg_total_spend' => array( + 'avg_total_spend' => array( 'description' => __( 'Average total spend per customer.', 'wc-admin' ), 'type' => 'number', 'context' => array( 'view', 'edit' ), @@ -234,7 +234,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'format' => 'date-time', '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' ), 'type' => 'string', 'default' => 'all', @@ -244,7 +244,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C ), 'validate_callback' => 'rest_validate_request_arg', ); - $params['name'] = array( + $params['name'] = array( 'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', @@ -276,34 +276,34 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); - $params['registered_before'] = array( + $params['registered_before'] = array( 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), 'type' => 'string', 'format' => 'date-time', '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' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); - $params['orders_count_min'] = array( + $params['orders_count_min'] = array( 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'wc-admin' ), 'type' => 'integer', 'sanitize_callback' => 'absint', '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' ), 'type' => 'integer', 'sanitize_callback' => 'absint', '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' ), '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( 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'wc-admin' ), @@ -318,7 +318,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $params['total_spend_between'] = array( 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'wc-admin' ), '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( 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'wc-admin' ), @@ -333,7 +333,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $params['avg_order_value_between'] = array( 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'wc-admin' ), '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( 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'wc-admin' ), diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index 27e576e6065..166bd8484e5 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -538,7 +538,7 @@ class WC_Admin_Reports_Interval { * @param string $param Parameter name. * @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 ) ) { return new WP_Error( 'rest_invalid_param', @@ -561,4 +561,35 @@ class WC_Admin_Reports_Interval { 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 ) + ); + } + + // check for dates here. + if ( + 2 !== count( $value ) + ) { + return new WP_Error( + 'rest_invalid_param', + /* translators: %s: parameter name */ + sprintf( __( '%s must contain 2 dates.', 'wc-admin' ), $param ) + ); + } + + return true; + } } diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php index 17e88cba209..1c0d3cba50f 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php @@ -25,11 +25,11 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store * @var array */ protected $column_types = array( - 'customer_id' => 'intval', - 'user_id' => 'intval', - 'orders_count' => 'intval', - 'total_spend' => 'floatval', - 'avg_order_value' => 'floatval', + 'customer_id' => 'intval', + 'user_id' => 'intval', + 'orders_count' => 'intval', + 'total_spend' => 'floatval', + 'avg_order_value' => 'floatval', ); /** @@ -60,7 +60,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store global $wpdb; // 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"; } @@ -274,7 +274,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store } 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 ); } @@ -284,7 +284,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store } 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 ); } @@ -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'; // 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' ), 'page' => 1, '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. * * @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 ) { 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. * * @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 ) { 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. * * @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 ) { global $wpdb; @@ -554,7 +554,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store if ( $customer_id ) { // Preserve customer_id for existing user_id. $data['customer_id'] = $customer_id; - $format[] = '%d'; + $format[] = '%d'; } return $wpdb->replace( $wpdb->prefix . self::TABLE_NAME, $data, $format ); diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index 566153a0796..78db9e8c26c 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -836,19 +836,19 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { /** * Test function that validates *_between query parameters. */ - public function test_rest_validate_between_arg() { + public function test_rest_validate_between_numeric_arg() { $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.' ); $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.' ); $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' ) ); } } From 3d0890a704d1f9631c41264904832d52db409f90 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 09:21:11 +1300 Subject: [PATCH 02/10] basic working --- ...dmin-rest-reports-customers-controller.php | 8 ++-- ...est-reports-customers-stats-controller.php | 2 +- .../class-wc-admin-reports-interval.php | 39 ++++++++++++++++++- .../tests/api/reports-interval.php | 4 +- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 4b5883d06e0..3d1c2421286 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -63,9 +63,11 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_before'] = $request['last_order_before']; $args['last_order_after'] = $request['last_order_after']; - $between_params = array( 'orders_count', 'total_spend', 'avg_order_value', 'last_active' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); - $args = array_merge( $args, $normalized ); + $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $date_between_params = array( 'last_active' ); + $normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $numeric_between_params ); + $normalized_dates = WC_Admin_Reports_Interval::normalize_date_between_params( $request, $date_between_params ); + $args = array_merge( $args, $normalized, $normalized_dates ); return $args; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index 2b906f6c049..8946ddeddd0 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -57,7 +57,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['last_order_after'] = $request['last_order_after']; $between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); + $normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $between_params ); $args = array_merge( $args, $normalized ); return $args; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index 166bd8484e5..bf868b473cc 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -500,7 +500,7 @@ class WC_Admin_Reports_Interval { * @param string|array $param_names One or more param names to handle. Should not include "_between" suffix. * @return array Normalized query values. */ - public static function normalize_between_params( $request, $param_names ) { + public static function normalize_numeric_between_params( $request, $param_names ) { if ( ! is_array( $param_names ) ) { $param_names = array( $param_names ); } @@ -530,6 +530,43 @@ class WC_Admin_Reports_Interval { return $normalized; } + /** + * Normalize "*_between" parameters to "*_after" and "*_before". + * + * @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. + * @return array Normalized query values. + */ + public static function normalize_date_between_params( $request, $param_names ) { + if ( ! is_array( $param_names ) ) { + $param_names = array( $param_names ); + } + + $normalized = array(); + + foreach ( $param_names as $param_name ) { + if ( ! is_array( $request[ $param_name . '_between' ] ) ) { + continue; + } + + $range = $request[ $param_name . '_between' ]; + + if ( 2 !== count( $range ) ) { + continue; + } + + if ( $range[0] < $range[1] ) { + $normalized[ $param_name . '_after' ] = $range[0]; + $normalized[ $param_name . '_before' ] = $range[1]; + } else { + $normalized[ $param_name . '_after' ] = $range[1]; + $normalized[ $param_name . '_before' ] = $range[0]; + } + } + + return $normalized; + } + /** * Validate a "*_between" range argument (an array with 2 numeric items). * diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index 78db9e8c26c..bcce5694f55 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -813,7 +813,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { /** * Test function that normalizes *_between query parameters to *_min & *_max. */ - public function test_normalize_between_params() { + public function test_normalize_numeric_between_params() { $request = array( 'a_between' => 'malformed', // won't be normalized (not an array). 'b_between' => array( 1, 5 ), // results in min=1, max=5. @@ -822,7 +822,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { '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 ); + $result = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $params ); $expected = array( 'b_min' => 1, 'b_max' => 5, From 2f022ffdba1c8ce01b81a999c3d5df62a3da2f1a Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 10:23:44 +1300 Subject: [PATCH 03/10] dry interval.php --- ...dmin-rest-reports-customers-controller.php | 10 ++-- ...est-reports-customers-stats-controller.php | 2 +- .../class-wc-admin-reports-interval.php | 49 +++---------------- .../tests/api/reports-interval.php | 4 +- 4 files changed, 15 insertions(+), 50 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 3d1c2421286..c46e8c84afd 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -52,8 +52,6 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['country'] = $request['country']; $args['last_active_before'] = $request['last_active_before']; $args['last_active_after'] = $request['last_active_after']; - $args['last_active_min'] = $request['last_active_min']; - $args['last_active_max'] = $request['last_active_max']; $args['orders_count_min'] = $request['orders_count_min']; $args['orders_count_max'] = $request['orders_count_max']; $args['total_spend_min'] = $request['total_spend_min']; @@ -64,10 +62,10 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_after'] = $request['last_order_after']; $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $date_between_params = array( 'last_active' ); - $normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $numeric_between_params ); - $normalized_dates = WC_Admin_Reports_Interval::normalize_date_between_params( $request, $date_between_params ); - $args = array_merge( $args, $normalized, $normalized_dates ); + $date_between_params = array( 'last_active' ); + $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); + $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); + $args = array_merge( $args, $normalized, $normalized_dates ); return $args; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index 8946ddeddd0..2b906f6c049 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -57,7 +57,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['last_order_after'] = $request['last_order_after']; $between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $between_params ); + $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); $args = array_merge( $args, $normalized ); return $args; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index bf868b473cc..dcb90e23daf 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -498,9 +498,10 @@ class WC_Admin_Reports_Interval { * * @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 bool $is_date Boolean if the param is date is related. * @return array Normalized query values. */ - public static function normalize_numeric_between_params( $request, $param_names ) { + public static function normalize_between_params( $request, $param_names, $is_date ) { if ( ! is_array( $param_names ) ) { $param_names = array( $param_names ); } @@ -518,49 +519,15 @@ class WC_Admin_Reports_Interval { continue; } - if ( $range[0] < $range[1] ) { - $normalized[ $param_name . '_min' ] = $range[0]; - $normalized[ $param_name . '_max' ] = $range[1]; - } else { - $normalized[ $param_name . '_min' ] = $range[1]; - $normalized[ $param_name . '_max' ] = $range[0]; - } - } - - return $normalized; - } - - /** - * Normalize "*_between" parameters to "*_after" and "*_before". - * - * @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. - * @return array Normalized query values. - */ - public static function normalize_date_between_params( $request, $param_names ) { - if ( ! is_array( $param_names ) ) { - $param_names = array( $param_names ); - } - - $normalized = array(); - - foreach ( $param_names as $param_name ) { - if ( ! is_array( $request[ $param_name . '_between' ] ) ) { - continue; - } - - $range = $request[ $param_name . '_between' ]; - - if ( 2 !== count( $range ) ) { - continue; - } + $min = $is_date ? '_after' : '_min'; + $max = $is_date ? '_before' : '_max'; if ( $range[0] < $range[1] ) { - $normalized[ $param_name . '_after' ] = $range[0]; - $normalized[ $param_name . '_before' ] = $range[1]; + $normalized[ $param_name . $min ] = $range[0]; + $normalized[ $param_name . $max ] = $range[1]; } else { - $normalized[ $param_name . '_after' ] = $range[1]; - $normalized[ $param_name . '_before' ] = $range[0]; + $normalized[ $param_name . $min ] = $range[1]; + $normalized[ $param_name . $max ] = $range[0]; } } diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index bcce5694f55..78db9e8c26c 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -813,7 +813,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { /** * Test function that normalizes *_between query parameters to *_min & *_max. */ - public function test_normalize_numeric_between_params() { + public function test_normalize_between_params() { $request = array( 'a_between' => 'malformed', // won't be normalized (not an array). 'b_between' => array( 1, 5 ), // results in min=1, max=5. @@ -822,7 +822,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { 'f_between' => array( 10, 12 ), // not in params, skipped. ); $params = array( 'a', 'b', 'c', 'd' ); - $result = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $params ); + $result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params ); $expected = array( 'b_min' => 1, 'b_max' => 5, From c6d1ce98c0179e4877856f1a9f95660dc3c4d798 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 10:45:49 +1300 Subject: [PATCH 04/10] rest_validate_between_date_arg --- .../includes/class-wc-admin-reports-interval.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index dcb90e23daf..5036d2fcab4 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -583,14 +583,15 @@ class WC_Admin_Reports_Interval { ); } - // check for dates here. if ( - 2 !== count( $value ) + 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 dates.', 'wc-admin' ), $param ) + sprintf( __( '%s must contain 2 valid dates.', 'wc-admin' ), $param ) ); } From 483c199fbf735b24932eb48fd0aba30eeadd8c5c Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 10:53:56 +1300 Subject: [PATCH 05/10] tests working --- ...dmin-rest-reports-customers-stats-controller.php | 13 ++++++++++--- .../tests/api/reports-interval.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index 2b906f6c049..c05b5d237c9 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -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_after'] = $request['last_order_after']; - $between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params ); - $args = array_merge( $args, $normalized ); + $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $date_between_params = array( 'last_active' ); + $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); + $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); + $args = array_merge( $args, $normalized, $normalized_dates ); return $args; } @@ -276,6 +278,11 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); + $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' ), 'type' => 'string', diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index 78db9e8c26c..f6b094cfcde 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -822,7 +822,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { '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 ); + $result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, false ); $expected = array( 'b_min' => 1, 'b_max' => 5, From 3e16a37be08b94dcba016d6d614e49730a387241 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 11:01:25 +1300 Subject: [PATCH 06/10] add tests --- .../tests/api/reports-interval.php | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index f6b094cfcde..112891efe6d 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -834,7 +834,30 @@ 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_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( @@ -851,4 +874,28 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { 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' ) + ); + } } From 370a747c2b7cc9e4ece0923a7875f27b58005de6 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 11:05:27 +1300 Subject: [PATCH 07/10] add registered --- .../class-wc-admin-rest-reports-customers-controller.php | 7 ++++++- ...ss-wc-admin-rest-reports-customers-stats-controller.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index c46e8c84afd..99c093f587f 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -62,7 +62,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_after'] = $request['last_order_after']; $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $date_between_params = array( 'last_active' ); + $date_between_params = array( 'last_active', 'registered' ); $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); $args = array_merge( $args, $normalized, $normalized_dates ); @@ -382,6 +382,11 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); + $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' ), 'type' => 'integer', diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index c05b5d237c9..fadb5b8736f 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -57,7 +57,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['last_order_after'] = $request['last_order_after']; $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $date_between_params = array( 'last_active' ); + $date_between_params = array( 'last_active', 'registered' ); $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); $args = array_merge( $args, $normalized, $normalized_dates ); @@ -295,6 +295,11 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); + $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' ), 'type' => 'integer', From 41bec03880e9456fdfc55a76583c33abdc2ed692 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 11:28:58 +1300 Subject: [PATCH 08/10] tweeks --- .../class-wc-admin-rest-reports-customers-controller.php | 8 ++++---- ...s-wc-admin-rest-reports-customers-stats-controller.php | 8 ++++---- .../includes/class-wc-admin-reports-interval.php | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 99c093f587f..33c67a1fd39 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -61,10 +61,10 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_before'] = $request['last_order_before']; $args['last_order_after'] = $request['last_order_after']; - $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $date_between_params = array( 'last_active', 'registered' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); - $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); + $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $between_params_date = array( 'last_active', 'registered' ); + $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); $args = array_merge( $args, $normalized, $normalized_dates ); return $args; diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index fadb5b8736f..aa9a7f163ca 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -56,10 +56,10 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['last_order_before'] = $request['last_order_before']; $args['last_order_after'] = $request['last_order_after']; - $numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $date_between_params = array( 'last_active', 'registered' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false ); - $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true ); + $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $between_params_date = array( 'last_active', 'registered' ); + $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); $args = array_merge( $args, $normalized, $normalized_dates ); return $args; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index 5036d2fcab4..2bc1465c147 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -494,7 +494,8 @@ 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 string|array $param_names One or more param names to handle. Should not include "_between" suffix. From e6b42631f38faa524a62990fbe2493588d54c62b Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 11:48:40 +1300 Subject: [PATCH 09/10] formatting --- ...ss-wc-admin-rest-reports-customers-controller.php | 12 ++++++------ ...admin-rest-reports-customers-stats-controller.php | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 33c67a1fd39..800e44b0e1f 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -61,11 +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_after'] = $request['last_order_after']; - $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $between_params_date = array( 'last_active', 'registered' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); - $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); - $args = array_merge( $args, $normalized, $normalized_dates ); + $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $normalized_numeric_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $between_params_date = array( 'last_active', 'registered' ); + $normalized_date_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); + $args = array_merge( $args, $normalized_numeric_params, $normalized_date_params ); return $args; } @@ -382,7 +382,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); - $params['registered_between'] = 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' ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index aa9a7f163ca..3265e26f2ad 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -56,11 +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_after'] = $request['last_order_after']; - $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $between_params_date = array( 'last_active', 'registered' ); - $normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); - $normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); - $args = array_merge( $args, $normalized, $normalized_dates ); + $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); + $normalized_numeric_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $between_params_date = array( 'last_active', 'registered' ); + $normalized_date_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); + $args = array_merge( $args, $normalized_numeric_params, $normalized_date_params ); return $args; } @@ -295,7 +295,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); - $params['registered_between'] = 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' ), From 765a0074918f803f2b87c302b9b85e06d1f36f52 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 12:08:44 +1300 Subject: [PATCH 10/10] tweeek --- .../class-wc-admin-rest-reports-customers-controller.php | 6 +++--- ...ass-wc-admin-rest-reports-customers-stats-controller.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 800e44b0e1f..f599cbe5455 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -62,10 +62,10 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['last_order_after'] = $request['last_order_after']; $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $normalized_numeric_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); $between_params_date = array( 'last_active', 'registered' ); - $normalized_date_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); - $args = array_merge( $args, $normalized_numeric_params, $normalized_date_params ); + $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; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php index 3265e26f2ad..d500b888ff7 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php @@ -57,10 +57,10 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['last_order_after'] = $request['last_order_after']; $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); - $normalized_numeric_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); + $normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); $between_params_date = array( 'last_active', 'registered' ); - $normalized_date_params = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); - $args = array_merge( $args, $normalized_numeric_params, $normalized_date_params ); + $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; }