woocommerce/includes/api/class-wc-rest-report-orders...

128 lines
3.1 KiB
PHP
Raw Normal View History

2018-09-05 13:28:22 +00:00
<?php
/**
2018-09-05 13:30:14 +00:00
* REST API Reports Orders Totals controller
2018-09-05 13:28:22 +00:00
*
* Handles requests to the /reports/orders/count endpoint.
*
* @package WooCommerce/API
* @since 3.5.0
*/
defined( 'ABSPATH' ) || exit;
/**
2018-09-05 13:30:14 +00:00
* REST API Reports Orders Totals controller class.
2018-09-05 13:28:22 +00:00
*
* @package WooCommerce/API
* @extends WC_REST_Reports_Controller
*/
2018-09-05 13:30:14 +00:00
class WC_REST_Report_Orders_Totals_Controller extends WC_REST_Reports_Controller {
2018-09-05 13:28:22 +00:00
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v3';
/**
* Route base.
*
* @var string
*/
2018-09-05 13:30:14 +00:00
protected $rest_base = 'reports/orders/totals';
2018-09-05 13:28:22 +00:00
/**
* Get reports list.
*
* @since 3.5.0
* @return array
*/
protected function get_reports() {
2018-09-05 13:30:14 +00:00
$totals = wp_count_posts( 'shop_order' );
2018-09-05 13:28:22 +00:00
$data = array();
foreach ( wc_get_order_statuses() as $slug => $name ) {
2018-09-05 13:30:14 +00:00
if ( ! isset( $totals->$slug ) ) {
2018-09-05 13:28:22 +00:00
continue;
}
$data[] = array(
'slug' => $slug,
'name' => $name,
2018-09-05 13:30:14 +00:00
'total' => (int) $totals->$slug,
2018-09-05 13:28:22 +00:00
);
}
return $data;
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $report, $request ) {
$data = array(
'slug' => $report->slug,
'name' => $report->name,
'total' => $report->total,
);
$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.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_orders_count', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
2018-09-05 13:30:14 +00:00
'title' => 'report_order_total',
2018-09-05 13:28:22 +00:00
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Order status name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'total' => array(
'description' => __( 'Amount of orders.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}