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.
This commit is contained in:
Tomek Wytrębowicz 2023-04-28 16:41:20 +02:00
parent 9126db1abb
commit 9f921b8e40
12 changed files with 61 additions and 85 deletions

View File

@ -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 );
}
}

View File

@ -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 ) );
/**

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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 ) );
/**

View File

@ -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.

View File

@ -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.

View File

@ -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 ) );
/**

View File

@ -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 ) );
/**

View File

@ -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.

View File

@ -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.