Passed
Push — master ( c9620c...0d5a04 )
by Brian
04:05
created

GetPaid_REST_Reports_Controller::get_items()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * REST API reports controller
4
 *
5
 * Handles requests to the /reports endpoint.
6
 *
7
 * @package GetPaid
8
 * @subpackage REST API
9
 * @since   2.0.0
10
 */
11
12
defined( 'ABSPATH' ) || exit;
13
14
/**
15
 * GetPaid REST reports controller class.
16
 *
17
 * @package Invoicing
18
 */
19
class GetPaid_REST_Reports_Controller extends GetPaid_REST_Controller {
20
21
	/**
22
	 * Route base.
23
	 *
24
	 * @var string
25
	 */
26
	protected $rest_base = 'reports';
27
28
	/**
29
	 * Registers the routes for the objects of the controller.
30
	 *
31
	 * @since 2.0.0
32
	 *
33
	 * @see register_rest_route()
34
	 */
35
	public function register_namespace_routes( $namespace ) {
36
37
		// List all available reports.
38
		register_rest_route(
39
			$namespace,
40
			$this->rest_base,
41
			array(
42
				array(
43
					'methods'             => WP_REST_Server::READABLE,
44
					'callback'            => array( $this, 'get_items' ),
45
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
46
					'args'                => $this->get_collection_params(),
47
				),
48
				'schema' => array( $this, 'get_public_item_schema' ),
49
			)
50
		);
51
52
	}
53
54
	/**
55
	 * Makes sure the current user has access to READ the report APIs.
56
	 *
57
	 * @since  2.0.0
58
	 * @param WP_REST_Request $request Full data about the request.
59
	 * @return WP_Error|boolean
60
	 */
61
	public function get_items_permissions_check( $request ) {
62
63
		if ( ! wpinv_current_user_can_manage_invoicing() ) {
64
			return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
65
		}
66
67
		return true;
68
	}
69
70
	/**
71
	 * Get reports list.
72
	 *
73
	 * @since 2.0.0
74
	 * @return array
75
	 */
76
	protected function get_reports() {
77
78
		$reports = array(
79
			array(
80
				'slug'        => 'sales',
81
				'description' => __( 'List of sales reports.', 'invoicing' ),
82
			),
83
			array(
84
				'slug'        => 'top_sellers',
85
				'description' => __( 'List of top selling items.', 'invoicing' ),
86
			),
87
			array(
88
				'slug'        => 'invoices/totals',
89
				'description' => __( 'Invoice totals.', 'invoicing' ),
90
			),
91
			array(
92
				'slug'        => 'items/totals',
93
				'description' => __( 'Item totals.', 'invoicing' ),
94
			),
95
			array(
96
				'slug'        => 'customers/totals',
97
				'description' => __( 'Customer totals.', 'invoicing' ),
98
			),
99
			array(
100
				'slug'        => 'discounts/totals',
101
				'description' => __( 'Discount totals.', 'invoicing' ),
102
			)
103
		);
104
105
		return apply_filters( 'getpaid_available_api_reports', $reports );
106
107
	}
108
109
	/**
110
	 * Get all reports.
111
	 *
112
	 * @since 2.0.0
113
	 * @param WP_REST_Request $request
114
	 * @return array|WP_Error
115
	 */
116
	public function get_items( $request ) {
117
		$data    = array();
118
		$reports = $this->get_reports();
119
120
		foreach ( $reports as $report ) {
121
			$item   = $this->prepare_item_for_response( (object) $report, $request );
122
			$data[] = $this->prepare_response_for_collection( $item );
123
		}
124
125
		return rest_ensure_response( $data );
0 ignored issues
show
Bug Best Practice introduced by
The expression return rest_ensure_response($data) returns the type WP_REST_Response which is incompatible with the documented return type WP_Error|array.
Loading history...
126
	}
127
128
	/**
129
	 * Prepare a report object for serialization.
130
	 *
131
	 * @since 2.0.0
132
	 * @param stdClass $report Report data.
133
	 * @param WP_REST_Request $request Request object.
134
	 * @return WP_REST_Response $response Response data.
135
	 */
136
	public function prepare_item_for_response( $report, $request ) {
137
		$data = array(
138
			'slug'        => $report->slug,
139
			'description' => $report->description,
140
		);
141
142
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
143
		$data = $this->add_additional_fields_to_object( $data, $request );
144
		$data = $this->filter_response_by_context( $data, $context );
145
146
		// Wrap the data in a response object.
147
		$response = rest_ensure_response( $data );
148
		$response->add_links( array(
149
			'self' => array(
150
				'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ),
151
			),
152
			'collection' => array(
153
				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
154
			),
155
		) );
156
157
		return apply_filters( 'getpaid_rest_prepare_report', $response, $report, $request );
158
	}
159
160
	/**
161
	 * Get the Report's schema, conforming to JSON Schema.
162
	 *
163
	 * @since 2.0.0
164
	 * @return array
165
	 */
166
	public function get_item_schema() {
167
		$schema = array(
168
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
169
			'title'      => 'report',
170
			'type'       => 'object',
171
			'properties' => array(
172
				'slug' => array(
173
					'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ),
174
					'type'        => 'string',
175
					'context'     => array( 'view' ),
176
					'readonly'    => true,
177
				),
178
				'description' => array(
179
					'description' => __( 'A human-readable description of the resource.', 'invoicing' ),
180
					'type'        => 'string',
181
					'context'     => array( 'view' ),
182
					'readonly'    => true,
183
				),
184
			),
185
		);
186
187
		return $this->add_additional_fields_schema( $schema );
188
	}
189
190
	/**
191
	 * Get the query params for collections.
192
	 *
193
	 * @since 2.0.0
194
	 * @return array
195
	 */
196
	public function get_collection_params() {
197
		return array(
198
			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
199
		);
200
	}
201
}
202