woocommerce/includes/api/class-wc-rest-reports-contr...

175 lines
4.5 KiB
PHP
Raw Normal View History

2016-02-17 19:29:09 +00:00
<?php
/**
* REST API Reports controller
*
* Handles requests to the reports endpoint.
*
* @author WooThemes
* @category API
* @package WooCommerce/API
* @since 2.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Reports controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
2016-02-17 19:29:09 +00:00
*/
class WC_REST_Reports_Controller extends WP_REST_Controller {
2016-02-17 19:29:09 +00:00
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v1';
2016-02-17 19:29:09 +00:00
/**
* Route base.
*
* @var string
*/
2016-02-22 18:49:38 +00:00
protected $rest_base = 'reports';
2016-02-17 19:29:09 +00:00
/**
* Register the routes for coupons.
*/
public function register_routes() {
2016-03-10 00:46:31 +00:00
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Check whether a given request has permission to read reports.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
2016-03-30 17:33:33 +00:00
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
2016-03-10 00:46:31 +00:00
}
return true;
}
/**
2016-03-10 02:19:36 +00:00
* Get all reports.
2016-03-10 00:46:31 +00:00
*
* @param WP_REST_Request $request
* @return array|WP_Error
*/
public function get_items( $request ) {
$data = array();
$reports = array(
array(
'slug' => 'sales',
'description' => __( 'List of sales reports.', 'woocommerce' ),
),
array(
'slug' => 'top_sellers',
'description' => __( 'List of top sellers products.', 'woocommerce' ),
),
);
foreach ( $reports as $report ) {
$item = $this->prepare_item_for_response( (object) $report, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
2016-02-17 19:29:09 +00:00
2016-03-10 00:46:31 +00:00
return rest_ensure_response( $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,
'description' => $report->description,
);
$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( array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
) );
/**
* 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', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
2016-03-10 02:19:36 +00:00
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report',
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
2016-03-10 00:46:31 +00:00
),
2016-03-10 02:19:36 +00:00
'description' => array(
'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
2016-03-10 00:46:31 +00:00
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
);
2016-02-17 19:29:09 +00:00
}
}