From b0b80445aba7e476fac2229f722ed786ba6a49b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Mon, 1 May 2023 23:31:05 +0200 Subject: [PATCH] Move common stats item schema bits to a shared class --- .../API/Reports/Coupons/Stats/Controller.php | 108 ++------------- .../Reports/Downloads/Stats/Controller.php | 17 ++- .../API/Reports/GenericStatsController.php | 117 +++++++++++++++++ .../API/Reports/Products/Stats/Controller.php | 122 ++++------------- .../API/Reports/Revenue/Stats/Controller.php | 123 +++--------------- .../API/Reports/Taxes/Stats/Controller.php | 108 ++------------- .../Reports/Variations/Stats/Controller.php | 122 ++++------------- 7 files changed, 220 insertions(+), 497 deletions(-) diff --git a/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php index 12d4ef74b45..0c8c8a7765f 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php @@ -112,12 +112,13 @@ class Controller extends GenericStatsController { } /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $data_values = array( + public function get_item_properties_schema() { + return array( 'amount' => array( 'description' => __( 'Net discount amount.', 'woocommerce' ), 'type' => 'number', @@ -141,99 +142,16 @@ class Controller extends GenericStatsController { 'indicator' => true, ), ); + } - $segments = array( - 'segments' => array( - 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'segment_id' => array( - 'description' => __( 'Segment identificator.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $data_values, - ), - ), - ), - ), - ); - - $totals = array_merge( $data_values, $segments ); - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'report_coupons_stats', - 'type' => 'object', - 'properties' => array( - 'totals' => array( - 'description' => __( 'Totals data.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - 'intervals' => array( - 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'interval' => array( - 'description' => __( 'Type of interval.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'date_start' => array( - 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_start_gmt' => array( - 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end' => array( - 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end_gmt' => array( - 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - ), - ), - ), - ), - ); + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + $schema['title'] = 'report_coupons_stats'; return $this->add_additional_fields_schema( $schema ); } diff --git a/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php index 847d5ef76b3..ad7eca546f2 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php @@ -110,13 +110,15 @@ class Controller extends GenericStatsController { return apply_filters( 'woocommerce_rest_prepare_report_downloads_stats', $response, $report, $request ); } + /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $totals = array( + public function get_item_properties_schema() { + return array( 'download_count' => array( 'title' => __( 'Downloads', 'woocommerce' ), 'description' => __( 'Number of downloads.', 'woocommerce' ), @@ -126,6 +128,15 @@ class Controller extends GenericStatsController { 'indicator' => true, ), ); + } + /** + * Get the Report's schema, conforming to JSON Schema. + * It does not have the segments as in GenericStatsController. + * + * @return array + */ + public function get_item_schema() { + $totals = $this->get_item_properties_schema(); $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', diff --git a/plugins/woocommerce/src/Admin/API/Reports/GenericStatsController.php b/plugins/woocommerce/src/Admin/API/Reports/GenericStatsController.php index 9468e03bfb3..ea97bb5a534 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/GenericStatsController.php +++ b/plugins/woocommerce/src/Admin/API/Reports/GenericStatsController.php @@ -38,4 +38,121 @@ abstract class GenericStatsController extends GenericController { return $params; } + + /** + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. + * + * @return array + */ + abstract function get_item_properties_schema(); + + /** + * Get the Report's schema, conforming to JSON Schema. + * + * Please note, it does not call add_additional_fields_schema, + * as you may want to update the `title` first. + * + * @return array + */ + public function get_item_schema() { + $data_values = $this->get_item_properties_schema(); + + $segments = array( + 'segments' => array( + 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), + 'type' => 'array', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'segment_id' => array( + 'description' => __( 'Segment identificator.', 'woocommerce' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'subtotals' => array( + 'description' => __( 'Interval subtotals.', 'woocommerce' ), + 'type' => 'object', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'properties' => $data_values, + ), + ), + ), + ), + ); + + $totals = array_merge( $data_values, $segments ); + + return array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'report_stats', + 'type' => 'object', + 'properties' => array( + 'totals' => array( + 'description' => __( 'Totals data.', 'woocommerce' ), + 'type' => 'object', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'properties' => $totals, + ), + 'intervals' => array( + 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), + 'type' => 'array', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'interval' => array( + 'description' => __( 'Type of interval.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'enum' => array( 'day', 'week', 'month', 'year' ), + ), + 'date_start' => array( + 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), + 'type' => 'string', + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'date_start_gmt' => array( + 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), + 'type' => 'string', + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'date_end' => array( + 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), + 'type' => 'string', + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'date_end_gmt' => array( + 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), + 'type' => 'string', + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'subtotals' => array( + 'description' => __( 'Interval subtotals.', 'woocommerce' ), + 'type' => 'object', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'properties' => $totals, + ), + ), + ), + ), + ), + ); + } } diff --git a/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php index 8b6760114e7..ad060d0d934 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php @@ -124,12 +124,13 @@ class Controller extends GenericStatsController { } /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $data_values = array( + public function get_item_properties_schema() { + return array( 'items_sold' => array( 'title' => __( 'Products sold', 'woocommerce' ), 'description' => __( 'Number of product items sold.', 'woocommerce' ), @@ -152,106 +153,27 @@ class Controller extends GenericStatsController { 'readonly' => true, ), ); + } - $segments = array( - 'segments' => array( - 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'segment_id' => array( - 'description' => __( 'Segment identificator.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'segment_label' => array( - 'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $data_values, - ), - ), - ), - ), + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + $schema['title'] = 'report_products_stats'; + + $segment_label = array( + 'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'enum' => array( 'day', 'week', 'month', 'year' ), ); - $totals = array_merge( $data_values, $segments ); - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'report_products_stats', - 'type' => 'object', - 'properties' => array( - 'totals' => array( - 'description' => __( 'Totals data.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - 'intervals' => array( - 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'interval' => array( - 'description' => __( 'Type of interval.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'date_start' => array( - 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_start_gmt' => array( - 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end' => array( - 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end_gmt' => array( - 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - ), - ), - ), - ), - ); + $schema['properties']['totals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label; + $schema['properties']['intervals']['items']['properties']['subtotals']['properties'] = $segment_label; return $this->add_additional_fields_schema( $schema ); } diff --git a/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php index 22d0534c815..95e3176a396 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php @@ -133,12 +133,13 @@ class Controller extends GenericStatsController implements ExportableInterface { } /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $data_values = array( + public function get_item_properties_schema() { + return array( 'total_sales' => array( 'description' => __( 'Total sales.', 'woocommerce' ), 'type' => 'number', @@ -205,12 +206,6 @@ class Controller extends GenericStatsController implements ExportableInterface { 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'products' => array( - 'description' => __( 'Products sold.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), 'gross_sales' => array( 'description' => __( 'Gross sales.', 'woocommerce' ), 'type' => 'number', @@ -220,103 +215,23 @@ class Controller extends GenericStatsController implements ExportableInterface { 'format' => 'currency', ), ); + } - $segments = array( - 'segments' => array( - 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'segment_id' => array( - 'description' => __( 'Segment identificator.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $data_values, - ), - ), - ), - ), - ); + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + $schema['title'] = 'report_revenue_stats'; - $totals = array_merge( $data_values, $segments ); - - // Products is not shown in intervals. - unset( $data_values['products'] ); - - $intervals = array_merge( $data_values, $segments ); - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'report_revenue_stats', - 'type' => 'object', - 'properties' => array( - 'totals' => array( - 'description' => __( 'Totals data.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - 'intervals' => array( - 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'interval' => array( - 'description' => __( 'Type of interval.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'date_start' => array( - 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_start_gmt' => array( - 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end' => array( - 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end_gmt' => array( - 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $intervals, - ), - ), - ), - ), - ), + // Products is not shown in intervals, only in totals. + $schema['properties']['totals']['properties']['products'] = array( + 'description' => __( 'Products sold.', 'woocommerce' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, ); return $this->add_additional_fields_schema( $schema ); diff --git a/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php index a487ca34adf..a17e1b9b1d6 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php @@ -137,12 +137,13 @@ class Controller extends GenericStatsController { } /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $data_values = array( + public function get_item_properties_schema() { + return array( 'total_tax' => array( 'description' => __( 'Total tax.', 'woocommerce' ), 'type' => 'number', @@ -180,99 +181,16 @@ class Controller extends GenericStatsController { 'readonly' => true, ), ); + } - $segments = array( - 'segments' => array( - 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'segment_id' => array( - 'description' => __( 'Segment identificator.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $data_values, - ), - ), - ), - ), - ); - - $totals = array_merge( $data_values, $segments ); - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'report_taxes_stats', - 'type' => 'object', - 'properties' => array( - 'totals' => array( - 'description' => __( 'Totals data.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - 'intervals' => array( - 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'interval' => array( - 'description' => __( 'Type of interval.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'date_start' => array( - 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_start_gmt' => array( - 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end' => array( - 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end_gmt' => array( - 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - ), - ), - ), - ), - ); + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + $schema['title'] = 'report_taxes_stats'; return $this->add_additional_fields_schema( $schema ); } diff --git a/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php index 34b868e9022..eb0421f596d 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php @@ -128,12 +128,13 @@ class Controller extends GenericStatsController { } /** - * Get the Report's schema, conforming to JSON Schema. + * Get the Report's item properties schema. + * Will be used by `get_item_schema` as `totals` and `subtotals`. * * @return array */ - public function get_item_schema() { - $data_values = array( + public function get_item_properties_schema() { + return array( 'items_sold' => array( 'title' => __( 'Variations Sold', 'woocommerce' ), 'description' => __( 'Number of variation items sold.', 'woocommerce' ), @@ -156,106 +157,27 @@ class Controller extends GenericStatsController { 'readonly' => true, ), ); + } - $segments = array( - 'segments' => array( - 'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'segment_id' => array( - 'description' => __( 'Segment identificator.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'segment_label' => array( - 'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $data_values, - ), - ), - ), - ), + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + $schema['title'] = 'report_variations_stats'; + + $segment_label = array( + 'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'enum' => array( 'day', 'week', 'month', 'year' ), ); - $totals = array_merge( $data_values, $segments ); - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'report_variations_stats', - 'type' => 'object', - 'properties' => array( - 'totals' => array( - 'description' => __( 'Totals data.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - 'intervals' => array( - 'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ), - 'type' => 'array', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'interval' => array( - 'description' => __( 'Type of interval.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'day', 'week', 'month', 'year' ), - ), - 'date_start' => array( - 'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_start_gmt' => array( - 'description' => __( 'The date the report start, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end' => array( - 'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_end_gmt' => array( - 'description' => __( 'The date the report end, as GMT.', 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'subtotals' => array( - 'description' => __( 'Interval subtotals.', 'woocommerce' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'properties' => $totals, - ), - ), - ), - ), - ), - ); + $schema['properties']['totals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label; + $schema['properties']['intervals']['items']['properties']['subtotals']['properties'] = $segment_label; return $this->add_additional_fields_schema( $schema ); }