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:
parent
9126db1abb
commit
9f921b8e40
|
@ -3,6 +3,9 @@ namespace Automattic\WooCommerce\Admin\API\Reports;
|
||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WC REST API Reports controller extended
|
* WC REST API Reports controller extended
|
||||||
* to be shared as a generic base for all Analytics controllers.
|
* 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;
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports coupons controller class.
|
* REST API Reports coupons controller class.
|
||||||
|
@ -78,19 +80,12 @@ class Controller extends AbstractController implements ExportableInterface {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
* @param stdClass $report Report data.
|
* @param array $report Report data.
|
||||||
* @param WP_REST_Request $request Request object.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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->add_links( $this->prepare_links( $report ) );
|
$response->add_links( $this->prepare_links( $report ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports coupons stats controller class.
|
* REST API Reports coupons stats controller class.
|
||||||
|
@ -95,12 +97,7 @@ class Controller extends AbstractController {
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = get_object_vars( $report );
|
$data = get_object_vars( $report );
|
||||||
|
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$response = parent::prepare_item_for_response( $data, $request );
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -10,6 +10,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports downloads stats controller class.
|
* REST API Reports downloads stats controller class.
|
||||||
|
@ -89,19 +91,12 @@ class Controller extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
* @param Array $report Report data.
|
* @param array $report Report data.
|
||||||
* @param WP_REST_Request $request Request object.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
|
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
@ -452,19 +454,14 @@ class Controller extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* 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.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $stat_data, $request ) {
|
public function prepare_item_for_response( $stat_data, $request ) {
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$response = parent::prepare_item_for_response( $stat_data, $request );
|
||||||
$data = $this->add_additional_fields_to_object( $stat_data, $request );
|
|
||||||
$data = $this->filter_response_by_context( $data, $context );
|
|
||||||
|
|
||||||
// Wrap the data in a response object.
|
$response->add_links( $this->prepare_links( $stat_data ) );
|
||||||
$response = rest_ensure_response( $data );
|
|
||||||
|
|
||||||
$response->add_links( $this->prepare_links( $data ) );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports products controller class.
|
* REST API Reports products controller class.
|
||||||
|
@ -88,14 +90,7 @@ class Controller extends AbstractController implements ExportableInterface {
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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->add_links( $this->prepare_links( $report ) );
|
$response->add_links( $this->prepare_links( $report ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports products stats controller class.
|
* REST API Reports products stats controller class.
|
||||||
|
@ -102,19 +104,12 @@ class Controller extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
* @param Array $report Report data.
|
* @param array $report Report data.
|
||||||
* @param WP_REST_Request $request Request object.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -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\ExportableInterface;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports revenue stats controller class.
|
* REST API Reports revenue stats controller class.
|
||||||
|
@ -111,19 +113,12 @@ class Controller extends AbstractController implements ExportableInterface {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
* @param Array $report Report data.
|
* @param array $report Report data.
|
||||||
* @param WP_REST_Request $request Request object.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports stock controller class.
|
* 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 ) );
|
$data['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$response = parent::prepare_item_for_response( $data, $request );
|
||||||
$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->add_links( $this->prepare_links( $product ) );
|
$response->add_links( $this->prepare_links( $product ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,6 +12,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
|
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports taxes controller class.
|
* REST API Reports taxes controller class.
|
||||||
|
@ -87,12 +89,7 @@ class Controller extends AbstractController implements ExportableInterface {
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
$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->add_links( $this->prepare_links( $report ) );
|
$response->add_links( $this->prepare_links( $report ) );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,6 +10,8 @@ namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports taxes stats controller class.
|
* REST API Reports taxes stats controller class.
|
||||||
|
@ -120,12 +122,7 @@ class Controller extends AbstractController {
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = get_object_vars( $report );
|
$data = get_object_vars( $report );
|
||||||
|
|
||||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
$response = parent::prepare_item_for_response( $data, $request );
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
|
@ -11,6 +11,8 @@ defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
use Automattic\WooCommerce\Admin\API\Reports\AbstractController;
|
||||||
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
|
||||||
|
use WP_REST_Request;
|
||||||
|
use WP_REST_Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST API Reports variations stats controller class.
|
* REST API Reports variations stats controller class.
|
||||||
|
@ -106,19 +108,12 @@ class Controller extends AbstractController {
|
||||||
/**
|
/**
|
||||||
* Prepare a report object for serialization.
|
* Prepare a report object for serialization.
|
||||||
*
|
*
|
||||||
* @param Array $report Report data.
|
* @param array $report Report data.
|
||||||
* @param WP_REST_Request $request Request object.
|
* @param WP_REST_Request $request Request object.
|
||||||
* @return WP_REST_Response
|
* @return WP_REST_Response
|
||||||
*/
|
*/
|
||||||
public function prepare_item_for_response( $report, $request ) {
|
public function prepare_item_for_response( $report, $request ) {
|
||||||
$data = $report;
|
$response = parent::prepare_item_for_response( $report, $request );
|
||||||
|
|
||||||
$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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a report returned from the API.
|
* Filter a report returned from the API.
|
||||||
|
|
Loading…
Reference in New Issue