WC_REST_Reports_V1_Controller

REST API Reports controller class.

Defined (1)

The class is defined in the following location(s).

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