WC_REST_Reports_Controller::get_item_schema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
eloc 17
nc 1
nop 0
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    2.6.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * REST API Reports controller class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Controller
22
 */
23
class WC_REST_Reports_Controller extends WC_REST_Controller {
24
25
	/**
26
	 * Endpoint namespace.
27
	 *
28
	 * @var string
29
	 */
30
	protected $namespace = 'wc/v1';
31
32
	/**
33
	 * Route base.
34
	 *
35
	 * @var string
36
	 */
37
	protected $rest_base = 'reports';
38
39
	/**
40
	 * Register the routes for reports.
41
	 */
42 View Code Duplication
	public function register_routes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
44
			array(
45
				'methods'             => WP_REST_Server::READABLE,
46
				'callback'            => array( $this, 'get_items' ),
47
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
48
				'args'                => $this->get_collection_params(),
49
			),
50
			'schema' => array( $this, 'get_public_item_schema' ),
51
		) );
52
	}
53
54
	/**
55
	 * Check whether a given request has permission to read reports.
56
	 *
57
	 * @param  WP_REST_Request $request Full details about the request.
58
	 * @return WP_Error|boolean
59
	 */
60 View Code Duplication
	public function get_items_permissions_check( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
		if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
62
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
63
		}
64
65
		return true;
66
	}
67
68
	/**
69
	 * Get all reports.
70
	 *
71
	 * @param WP_REST_Request $request
72
	 * @return array|WP_Error
73
	 */
74
	public function get_items( $request ) {
75
		$data    = array();
76
		$reports = array(
77
			array(
78
				'slug'        => 'sales',
79
				'description' => __( 'List of sales reports.', 'woocommerce' ),
80
			),
81
			array(
82
				'slug'        => 'top_sellers',
83
				'description' => __( 'List of top sellers products.', 'woocommerce' ),
84
			),
85
		);
86
87
		foreach ( $reports as $report ) {
88
			$item   = $this->prepare_item_for_response( (object) $report, $request );
89
			$data[] = $this->prepare_response_for_collection( $item );
90
		}
91
92
		return rest_ensure_response( $data );
93
	}
94
95
	/**
96
	 * Prepare a report object for serialization.
97
	 *
98
	 * @param stdClass $report Report data.
99
	 * @param WP_REST_Request $request Request object.
100
	 * @return WP_REST_Response $response Response data.
101
	 */
102 View Code Duplication
	public function prepare_item_for_response( $report, $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
		$data = array(
104
			'slug'        => $report->slug,
105
			'description' => $report->description,
106
		);
107
108
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
109
		$data = $this->add_additional_fields_to_object( $data, $request );
110
		$data = $this->filter_response_by_context( $data, $context );
111
112
		// Wrap the data in a response object.
113
		$response = rest_ensure_response( $data );
114
		$response->add_links( array(
115
			'self' => array(
116
				'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
117
			),
118
			'collection' => array(
119
				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
120
			),
121
		) );
122
123
		/**
124
		 * Filter a report returned from the API.
125
		 *
126
		 * Allows modification of the report data right before it is returned.
127
		 *
128
		 * @param WP_REST_Response $response The response object.
129
		 * @param object           $report   The original report object.
130
		 * @param WP_REST_Request  $request  Request used to generate the response.
131
		 */
132
		return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request );
133
	}
134
135
	/**
136
	 * Get the Report's schema, conforming to JSON Schema.
137
	 *
138
	 * @return array
139
	 */
140
	public function get_item_schema() {
141
		$schema = array(
142
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
143
			'title'      => 'report',
144
			'type'       => 'object',
145
			'properties' => array(
146
				'slug' => array(
147
					'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
148
					'type'        => 'string',
149
					'context'     => array( 'view' ),
150
					'readonly'    => true,
151
				),
152
				'description' => array(
153
					'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
154
					'type'        => 'string',
155
					'context'     => array( 'view' ),
156
					'readonly'    => true,
157
				),
158
			),
159
		);
160
161
		return $this->add_additional_fields_schema( $schema );
162
	}
163
164
	/**
165
	 * Get the query params for collections.
166
	 *
167
	 * @return array
168
	 */
169
	public function get_collection_params() {
170
		return array(
171
			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
172
		);
173
	}
174
}
175