From 5eee869a74537fdc4bc5df1da54cc25da51ea3af Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Thu, 6 Dec 2018 10:40:59 +0100 Subject: [PATCH] Added small fixes to set up coupon endpoints. --- ...-admin-rest-reports-coupons-controller.php | 29 ++++++++----------- ...-rest-reports-coupons-stats-controller.php | 9 +++--- .../includes/class-wc-admin-api-init.php | 6 +++- ...admin-reports-coupons-stats-data-store.php | 23 ++++++++------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php index 32773709b83..8d7e9aafcd4 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php @@ -58,20 +58,17 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle */ public function get_items( $request ) { $query_args = $this->prepare_reports_query( $request ); - $coupons_query = new WC_Reports_Orders_Stats_Query( $query_args ); // @todo change to correct class. + $coupons_query = new WC_Admin_Reports_Coupons_Query( $query_args ); $report_data = $coupons_query->get_data(); - $out_data = array( - 'totals' => get_object_vars( $report_data->totals ), - 'intervals' => array(), - ); + $data = array(); - foreach ( $report_data->intervals as $interval_data ) { - $item = $this->prepare_item_for_response( (object) $interval_data, $request ); - $out_data['intervals'][] = $this->prepare_response_for_collection( $item ); + foreach ( $report_data->data as $coupons_data ) { + $item = $this->prepare_item_for_response( $coupons_data, $request ); + $data[] = $this->prepare_response_for_collection( $item ); } - $response = rest_ensure_response( $out_data ); + $response = rest_ensure_response( $data ); $response->header( 'X-WP-Total', (int) $report_data->total ); $response->header( 'X-WP-TotalPages', (int) $report_data->pages ); @@ -103,7 +100,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = get_object_vars( $report ); + $data = $report; $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); @@ -134,7 +131,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle protected function prepare_links( $object ) { $links = array( 'coupon' => array( - 'href' => rest_url( sprintf( '/%s/coupons/%d', $this->namespace, $object->coupon_id ) ), + 'href' => rest_url( sprintf( '/%s/coupons/%d', $this->namespace, $object['coupon_id'] ) ), ), ); @@ -223,13 +220,11 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle $params['orderby'] = array( 'description' => __( 'Sort collection by object attribute.', 'wc-admin' ), 'type' => 'string', - 'default' => 'date', + 'default' => 'coupon_id', 'enum' => array( - 'date', - 'items_sold', - 'gross_revenue', + 'coupon_id', + 'gross_discount', 'orders_count', - 'products_count', ), 'validate_callback' => 'rest_validate_request_arg', ); @@ -247,7 +242,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle ), 'validate_callback' => 'rest_validate_request_arg', ); - $params['coupons'] = array( + $params['code'] = array( 'description' => __( 'Limit result set to items assigned one or more code.', 'wc-admin' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_slug_list', diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php index 9366327b5ca..4fa2f515b93 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php @@ -60,7 +60,7 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con */ public function get_items( $request ) { $query_args = $this->prepare_reports_query( $request ); - $coupons_query = new WC_Reports_Orders_Stats_Query( $query_args ); // @todo change to correct class. + $coupons_query = new WC_Admin_Reports_Coupons_Stats_Query( $query_args ); $report_data = $coupons_query->get_data(); $out_data = array( @@ -270,10 +270,9 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con 'default' => 'date', 'enum' => array( 'date', - 'items_sold', - 'gross_revenue', + 'gross_discount', + 'coupons_count', 'orders_count', - 'products_count', ), 'validate_callback' => 'rest_validate_request_arg', ); @@ -291,7 +290,7 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con ), 'validate_callback' => 'rest_validate_request_arg', ); - $params['coupons'] = array( + $params['code'] = array( 'description' => __( 'Limit result set to items assigned one or more code.', 'wc-admin' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_slug_list', diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php index a317d4f3f09..64b99e76739 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php @@ -51,6 +51,8 @@ class WC_Admin_Api_Init { require_once dirname( __FILE__ ) . '/class-wc-admin-reports-categories-query.php'; require_once dirname( __FILE__ ) . '/class-wc-admin-reports-taxes-query.php'; require_once dirname( __FILE__ ) . '/class-wc-admin-reports-taxes-stats-query.php'; + require_once dirname( __FILE__ ) . '/class-wc-admin-reports-coupons-query.php'; + require_once dirname( __FILE__ ) . '/class-wc-admin-reports-coupons-stats-query.php'; // Data stores. require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-data-store.php'; @@ -61,6 +63,8 @@ class WC_Admin_Api_Init { require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-categories-data-store.php'; require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-taxes-data-store.php'; require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-taxes-stats-data-store.php'; + require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-coupons-data-store.php'; + require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-coupons-stats-data-store.php'; // Data triggers. require_once dirname( __FILE__ ) . '/wc-admin-order-functions.php'; @@ -326,7 +330,7 @@ class WC_Admin_Api_Init { 'report-taxes' => 'WC_Admin_Reports_Taxes_Data_Store', 'report-taxes-stats' => 'WC_Admin_Reports_Taxes_Stats_Data_Store', 'report-coupons' => 'WC_Admin_Reports_Coupons_Data_Store', - 'report-coupons-stats' => 'WC_Admin_Reports_Coupons_Stats_Store', + 'report-coupons-stats' => 'WC_Admin_Reports_Coupons_Stats_Data_Store', 'admin-note' => 'WC_Admin_Notes_Data_Store', ) ); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php index 9247742706b..cad92edbd1c 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php @@ -35,7 +35,7 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons */ protected $report_columns = array( 'gross_discount' => 'SUM(coupon_gross_discount) as gross_discount', - 'coupons_count' => 'COUNT(DISTINCT coupon_id) as products_count', + 'coupons_count' => 'COUNT(DISTINCT coupon_id) as coupons_count', 'orders_count' => 'COUNT(DISTINCT order_id) as orders_count', ); @@ -135,7 +135,8 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons ); // WPCS: cache ok, DB call ok, unprepared SQL ok. $db_interval_count = count( $db_intervals ); - $total_pages = (int) ceil( $db_interval_count / $intervals_query['per_page'] ); + $expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); + $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { return $data; } @@ -156,8 +157,9 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons if ( null === $totals ) { return $data; } + $totals = (object) $this->cast_numbers( $totals[0] ); - $expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); + // Intervals. $this->update_intervals_sql_params( $intervals_query, $query_args, $db_interval_count, $expected_interval_count ); if ( '' !== $selections ) { @@ -188,7 +190,13 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons return $data; } - $totals = (object) $this->cast_numbers( $totals[0] ); + $data = (object) array( + 'totals' => $totals, + 'intervals' => $intervals, + 'total' => $expected_interval_count, + 'pages' => $total_pages, + 'page_no' => (int) $query_args['page'], + ); if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); @@ -199,13 +207,6 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons } $this->create_interval_subtotals( $data->intervals ); - $data = (object) array( - 'data' => $data, - 'total' => $expected_interval_count, - 'pages' => $total_pages, - 'page_no' => (int) $query_args['page'], - ); - wp_cache_set( $cache_key, $data, $this->cache_group ); }