From 2f022ffdba1c8ce01b81a999c3d5df62a3da2f1a Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 29 Jan 2019 10:23:44 +1300 Subject: [PATCH] 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,