From 9f921b8e404f8ff9dc323ecc674a4e1602fe0050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Fri, 28 Apr 2023 16:41:20 +0200 Subject: [PATCH] Move common `prepare_item_for_response` bits to the shared class to make the code DRYier. `Reports/Controller` differs, so its children will still contain redundant code. --- .../Admin/API/Reports/AbstractController.php | 21 +++++++++++++++++++ .../Admin/API/Reports/Coupons/Controller.php | 13 ++++-------- .../API/Reports/Coupons/Stats/Controller.php | 9 +++----- .../Reports/Downloads/Stats/Controller.php | 13 ++++-------- .../PerformanceIndicators/Controller.php | 13 +++++------- .../Admin/API/Reports/Products/Controller.php | 11 +++------- .../API/Reports/Products/Stats/Controller.php | 13 ++++-------- .../API/Reports/Revenue/Stats/Controller.php | 13 ++++-------- .../Admin/API/Reports/Stock/Controller.php | 9 +++----- .../Admin/API/Reports/Taxes/Controller.php | 9 +++----- .../API/Reports/Taxes/Stats/Controller.php | 9 +++----- .../Reports/Variations/Stats/Controller.php | 13 ++++-------- 12 files changed, 61 insertions(+), 85 deletions(-) diff --git a/plugins/woocommerce/src/Admin/API/Reports/AbstractController.php b/plugins/woocommerce/src/Admin/API/Reports/AbstractController.php index 4af587c7e75..6c3dae89b2c 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/AbstractController.php +++ b/plugins/woocommerce/src/Admin/API/Reports/AbstractController.php @@ -3,6 +3,9 @@ namespace Automattic\WooCommerce\Admin\API\Reports; defined( 'ABSPATH' ) || exit; +use WP_REST_Request; +use WP_REST_Response; + /** * WC REST API Reports controller extended * to be shared as a generic base for all Analytics controllers. @@ -120,4 +123,22 @@ abstract class AbstractController extends \WC_REST_Reports_Controller { return $params; } + + /** + * Prepare a report object for serialization. + * + * @param array $report Report data. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response + */ + public function prepare_item_for_response( $report, $request ) { + $data = $report; + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + return rest_ensure_response( $data ); + } } diff --git a/plugins/woocommerce/src/Admin/API/Reports/Coupons/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Coupons/Controller.php index 9705868edc0..57930617fe6 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Coupons/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Coupons/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports coupons controller class. @@ -78,19 +80,12 @@ class Controller extends AbstractController implements ExportableInterface { /** * Prepare a report object for serialization. * - * @param stdClass $report Report data. + * @param array $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); $response->add_links( $this->prepare_links( $report ) ); /** 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 2e4cd0badc1..9debc1c2b74 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Coupons/Stats/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ParameterException; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports coupons stats controller class. @@ -95,12 +97,7 @@ class Controller extends AbstractController { public function prepare_item_for_response( $report, $request ) { $data = get_object_vars( $report ); - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $data, $request ); /** * Filter a report returned from the API. 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 90a7fc6dc2f..86381bcf183 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Downloads/Stats/Controller.php @@ -10,6 +10,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats; defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports downloads stats controller class. @@ -89,19 +91,12 @@ class Controller extends AbstractController { /** * Prepare a report object for serialization. * - * @param Array $report Report data. + * @param array $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); /** * Filter a report returned from the API. diff --git a/plugins/woocommerce/src/Admin/API/Reports/PerformanceIndicators/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/PerformanceIndicators/Controller.php index 06207990b12..4329f365a59 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/PerformanceIndicators/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/PerformanceIndicators/Controller.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\TimeInterval; +use WP_REST_Request; +use WP_REST_Response; defined( 'ABSPATH' ) || exit; @@ -452,19 +454,14 @@ class Controller extends AbstractController { /** * Prepare a report object for serialization. * - * @param stdClass $stat_data Report data. + * @param array $stat_data Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $stat_data, $request ) { - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $stat_data, $request ); - $data = $this->filter_response_by_context( $data, $context ); + $response = parent::prepare_item_for_response( $stat_data, $request ); - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $data ) ); + $response->add_links( $this->prepare_links( $stat_data ) ); /** * Filter a report returned from the API. diff --git a/plugins/woocommerce/src/Admin/API/Reports/Products/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Products/Controller.php index 14514315abb..05c60eec2dc 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Products/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Products/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports products controller class. @@ -88,14 +90,7 @@ class Controller extends AbstractController implements ExportableInterface { * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); $response->add_links( $this->prepare_links( $report ) ); /** 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 442d0c8242c..8b821b7850f 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Products/Stats/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ParameterException; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports products stats controller class. @@ -102,19 +104,12 @@ class Controller extends AbstractController { /** * Prepare a report object for serialization. * - * @param Array $report Report data. + * @param array $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); /** * Filter a report returned from the API. 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 2d8825f7fb5..065134340d9 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Revenue/Stats/Controller.php @@ -14,6 +14,8 @@ use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; use Automattic\WooCommerce\Admin\API\Reports\ParameterException; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports revenue stats controller class. @@ -111,19 +113,12 @@ class Controller extends AbstractController implements ExportableInterface { /** * Prepare a report object for serialization. * - * @param Array $report Report data. + * @param array $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); /** * Filter a report returned from the API. diff --git a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php index 401e534ff07..6bb8412454a 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports stock controller class. @@ -296,12 +298,7 @@ class Controller extends AbstractController implements ExportableInterface { $data['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); } - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $data, $request ); $response->add_links( $this->prepare_links( $product ) ); /** diff --git a/plugins/woocommerce/src/Admin/API/Reports/Taxes/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Taxes/Controller.php index 8c9f8b37e95..3a57fb6b0f8 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Taxes/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Taxes/Controller.php @@ -12,6 +12,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports taxes controller class. @@ -87,12 +89,7 @@ class Controller extends AbstractController implements ExportableInterface { * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $report = $this->add_additional_fields_to_object( $report, $request ); - $report = $this->filter_response_by_context( $report, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $report ); + $response = parent::prepare_item_for_response( $report, $request ); $response->add_links( $this->prepare_links( $report ) ); /** 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 17e867149dd..7fae669875c 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Taxes/Stats/Controller.php @@ -10,6 +10,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports taxes stats controller class. @@ -120,12 +122,7 @@ class Controller extends AbstractController { public function prepare_item_for_response( $report, $request ) { $data = get_object_vars( $report ); - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $data, $request ); /** * Filter a report returned from the API. 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 d8ba166f57f..3d6f11bb336 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Variations/Stats/Controller.php @@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\AbstractController; use Automattic\WooCommerce\Admin\API\Reports\ParameterException; +use WP_REST_Request; +use WP_REST_Response; /** * REST API Reports variations stats controller class. @@ -106,19 +108,12 @@ class Controller extends AbstractController { /** * Prepare a report object for serialization. * - * @param Array $report Report data. + * @param array $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $data = $report; - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); + $response = parent::prepare_item_for_response( $report, $request ); /** * Filter a report returned from the API.