class-wc-rest-reports-controller.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * REST API Reports controller
  4. *
  5. * Handles requests to the reports endpoint.
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 3.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * REST API Reports controller class.
  17. *
  18. * @package WooCommerce/API
  19. * @extends WC_REST_Controller
  20. */
  21. class WC_REST_Reports_V1_Controller extends WC_REST_Controller {
  22. /**
  23. * Endpoint namespace.
  24. *
  25. * @var string
  26. */
  27. protected $namespace = 'wc/v1';
  28. /**
  29. * Route base.
  30. *
  31. * @var string
  32. */
  33. protected $rest_base = 'reports';
  34. /**
  35. * Register the routes for reports.
  36. */
  37. public function register_routes() {
  38. register_rest_route( $this->namespace, '/' . $this->rest_base, array(
  39. array(
  40. 'methods' => WP_REST_Server::READABLE,
  41. 'callback' => array( $this, 'get_items' ),
  42. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  43. 'args' => $this->get_collection_params(),
  44. ),
  45. 'schema' => array( $this, 'get_public_item_schema' ),
  46. ) );
  47. }
  48. /**
  49. * Check whether a given request has permission to read reports.
  50. *
  51. * @param WP_REST_Request $request Full details about the request.
  52. * @return WP_Error|boolean
  53. */
  54. public function get_items_permissions_check( $request ) {
  55. if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
  56. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  57. }
  58. return true;
  59. }
  60. /**
  61. * Get all reports.
  62. *
  63. * @param WP_REST_Request $request
  64. * @return array|WP_Error
  65. */
  66. public function get_items( $request ) {
  67. $data = array();
  68. $reports = array(
  69. array(
  70. 'slug' => 'sales',
  71. 'description' => __( 'List of sales reports.', 'woocommerce' ),
  72. ),
  73. array(
  74. 'slug' => 'top_sellers',
  75. 'description' => __( 'List of top sellers products.', 'woocommerce' ),
  76. ),
  77. );
  78. foreach ( $reports as $report ) {
  79. $item = $this->prepare_item_for_response( (object) $report, $request );
  80. $data[] = $this->prepare_response_for_collection( $item );
  81. }
  82. return rest_ensure_response( $data );
  83. }
  84. /**
  85. * Prepare a report object for serialization.
  86. *
  87. * @param stdClass $report Report data.
  88. * @param WP_REST_Request $request Request object.
  89. * @return WP_REST_Response $response Response data.
  90. */
  91. public function prepare_item_for_response( $report, $request ) {
  92. $data = array(
  93. 'slug' => $report->slug,
  94. 'description' => $report->description,
  95. );
  96. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  97. $data = $this->add_additional_fields_to_object( $data, $request );
  98. $data = $this->filter_response_by_context( $data, $context );
  99. // Wrap the data in a response object.
  100. $response = rest_ensure_response( $data );
  101. $response->add_links( array(
  102. 'self' => array(
  103. 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
  104. ),
  105. 'collection' => array(
  106. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  107. ),
  108. ) );
  109. /**
  110. * Filter a report returned from the API.
  111. *
  112. * Allows modification of the report data right before it is returned.
  113. *
  114. * @param WP_REST_Response $response The response object.
  115. * @param object $report The original report object.
  116. * @param WP_REST_Request $request Request used to generate the response.
  117. */
  118. return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request );
  119. }
  120. /**
  121. * Get the Report's schema, conforming to JSON Schema.
  122. *
  123. * @return array
  124. */
  125. public function get_item_schema() {
  126. $schema = array(
  127. '$schema' => 'http://json-schema.org/draft-04/schema#',
  128. 'title' => 'report',
  129. 'type' => 'object',
  130. 'properties' => array(
  131. 'slug' => array(
  132. 'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
  133. 'type' => 'string',
  134. 'context' => array( 'view' ),
  135. 'readonly' => true,
  136. ),
  137. 'description' => array(
  138. 'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
  139. 'type' => 'string',
  140. 'context' => array( 'view' ),
  141. 'readonly' => true,
  142. ),
  143. ),
  144. );
  145. return $this->add_additional_fields_schema( $schema );
  146. }
  147. /**
  148. * Get the query params for collections.
  149. *
  150. * @return array
  151. */
  152. public function get_collection_params() {
  153. return array(
  154. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  155. );
  156. }
  157. }