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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / Variations / Stats / Controller.php
woocommerce / src / Admin / API / Reports / Variations / Stats Last commit date
Controller.php 1 year ago DataStore.php 4 weeks ago Query.php 1 year ago Segmenter.php 2 years ago
Controller.php
305 lines
1 <?php
2 /**
3 * REST API Reports variations stats controller
4 *
5 * Handles requests to the /reports/variations/stats endpoint.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Automattic\WooCommerce\Admin\API\Reports\GenericQuery;
13 use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
14 use WP_REST_Request;
15 use WP_REST_Response;
16
17 /**
18 * REST API Reports variations stats controller class.
19 *
20 * @internal
21 * @extends GenericStatsController
22 */
23 class Controller extends GenericStatsController {
24
25 /**
26 * Route base.
27 *
28 * @var string
29 */
30 protected $rest_base = 'reports/variations/stats';
31
32 /**
33 * Mapping between external parameter name and name used in query class.
34 *
35 * @var array
36 */
37 protected $param_mapping = array(
38 'variations' => 'variation_includes',
39 );
40
41 /**
42 * Constructor.
43 */
44 public function __construct() {
45 add_filter( 'woocommerce_analytics_variations_stats_select_query', array( $this, 'set_default_report_data' ) );
46 }
47
48 /**
49 * Get data from `'variations-stats'` GenericQuery.
50 *
51 * @override GenericController::get_datastore_data()
52 *
53 * @param array $query_args Query arguments.
54 * @return mixed Results from the data store.
55 */
56 protected function get_datastore_data( $query_args = array() ) {
57 $query = new GenericQuery( $query_args, 'variations-stats' );
58 return $query->get_data();
59 }
60
61 /**
62 * Maps query arguments from the REST request, to be fed to Query.
63 *
64 * @param \WP_REST_Request $request Full request object.
65 * @return array Simplified array of params.
66 */
67 protected function prepare_reports_query( $request ) {
68 $query_args = array(
69 'fields' => array(
70 'items_sold',
71 'net_revenue',
72 'orders_count',
73 'variations_count',
74 ),
75 );
76 /**
77 * Experimental: Filter the list of parameters provided when querying data from the data store.
78 *
79 * @ignore
80 *
81 * @param array $collection_params List of parameters.
82 */
83 $collection_params = apply_filters( 'experimental_woocommerce_analytics_variations_stats_collection_params', $this->get_collection_params() );
84 $registered = array_keys( $collection_params );
85 foreach ( $registered as $param_name ) {
86 if ( isset( $request[ $param_name ] ) ) {
87 if ( isset( $this->param_mapping[ $param_name ] ) ) {
88 $query_args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
89 } else {
90 $query_args[ $param_name ] = $request[ $param_name ];
91 }
92 }
93 }
94
95 return $query_args;
96 }
97
98 /**
99 * Prepare a report data item for serialization.
100 *
101 * @param array $report Report data item as returned from Data Store.
102 * @param WP_REST_Request $request Request object.
103 * @return WP_REST_Response
104 */
105 public function prepare_item_for_response( $report, $request ) {
106 $response = parent::prepare_item_for_response( $report, $request );
107
108 /**
109 * Filter a report returned from the API.
110 *
111 * Allows modification of the report data right before it is returned.
112 *
113 * @param WP_REST_Response $response The response object.
114 * @param object $report The original report object.
115 * @param WP_REST_Request $request Request used to generate the response.
116 */
117 return apply_filters( 'woocommerce_rest_prepare_report_variations_stats', $response, $report, $request );
118 }
119
120 /**
121 * Get the Report's item properties schema.
122 * Will be used by `get_item_schema` as `totals` and `subtotals`.
123 *
124 * @return array
125 */
126 protected function get_item_properties_schema() {
127 return array(
128 'items_sold' => array(
129 'title' => __( 'Variations Sold', 'woocommerce' ),
130 'description' => __( 'Number of variation items sold.', 'woocommerce' ),
131 'type' => 'integer',
132 'context' => array( 'view', 'edit' ),
133 'readonly' => true,
134 'indicator' => true,
135 ),
136 'net_revenue' => array(
137 'description' => __( 'Net sales.', 'woocommerce' ),
138 'type' => 'number',
139 'context' => array( 'view', 'edit' ),
140 'readonly' => true,
141 'format' => 'currency',
142 ),
143 'orders_count' => array(
144 'description' => __( 'Number of orders.', 'woocommerce' ),
145 'type' => 'integer',
146 'context' => array( 'view', 'edit' ),
147 'readonly' => true,
148 ),
149 );
150 }
151
152 /**
153 * Get the Report's schema, conforming to JSON Schema.
154 *
155 * @return array
156 */
157 public function get_item_schema() {
158 $schema = parent::get_item_schema();
159 $schema['title'] = 'report_variations_stats';
160
161 $segment_label = array(
162 'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ),
163 'type' => 'string',
164 'context' => array( 'view', 'edit' ),
165 'readonly' => true,
166 'enum' => array( 'day', 'week', 'month', 'year' ),
167 );
168
169 $schema['properties']['totals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
170 $schema['properties']['intervals']['items']['properties']['subtotals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
171
172 return $this->add_additional_fields_schema( $schema );
173 }
174
175 /**
176 * Set the default results to 0 if API returns an empty array
177 *
178 * @param Mixed $results Report data.
179 * @return object
180 */
181 public function set_default_report_data( $results ) {
182 if ( empty( $results ) ) {
183 $results = new \stdClass();
184 $results->total = 0;
185 $results->totals = new \stdClass();
186 $results->totals->items_sold = 0;
187 $results->totals->net_revenue = 0;
188 $results->totals->orders_count = 0;
189 $results->intervals = array();
190 $results->pages = 1;
191 $results->page_no = 1;
192 }
193 return $results;
194 }
195
196 /**
197 * Get the query params for collections.
198 *
199 * @return array
200 */
201 public function get_collection_params() {
202 $params = parent::get_collection_params();
203 $params['match'] = array(
204 'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
205 'type' => 'string',
206 'default' => 'all',
207 'enum' => array(
208 'all',
209 'any',
210 ),
211 'validate_callback' => 'rest_validate_request_arg',
212 );
213 $params['orderby']['enum'] = $this->apply_custom_orderby_filters(
214 array(
215 'date',
216 'net_revenue',
217 'coupons',
218 'refunds',
219 'shipping',
220 'taxes',
221 'net_revenue',
222 'orders_count',
223 'items_sold',
224 )
225 );
226 $params['category_includes'] = array(
227 'description' => __( 'Limit result to items from the specified categories.', 'woocommerce' ),
228 'type' => 'array',
229 'sanitize_callback' => 'wp_parse_id_list',
230 'validate_callback' => 'rest_validate_request_arg',
231 'items' => array(
232 'type' => 'integer',
233 ),
234 );
235 $params['category_excludes'] = array(
236 'description' => __( 'Limit result set to variations not in the specified categories.', 'woocommerce' ),
237 'type' => 'array',
238 'sanitize_callback' => 'wp_parse_id_list',
239 'validate_callback' => 'rest_validate_request_arg',
240 'items' => array(
241 'type' => 'integer',
242 ),
243 );
244 $params['product_includes'] = array(
245 'description' => __( 'Limit result set to items that have the specified parent product(s).', 'woocommerce' ),
246 'type' => 'array',
247 'items' => array(
248 'type' => 'integer',
249 ),
250 'default' => array(),
251 'sanitize_callback' => 'wp_parse_id_list',
252 'validate_callback' => 'rest_validate_request_arg',
253 );
254 $params['product_excludes'] = array(
255 'description' => __( 'Limit result set to items that don\'t have the specified parent product(s).', 'woocommerce' ),
256 'type' => 'array',
257 'items' => array(
258 'type' => 'integer',
259 ),
260 'default' => array(),
261 'validate_callback' => 'rest_validate_request_arg',
262 'sanitize_callback' => 'wp_parse_id_list',
263 );
264 $params['variations'] = array(
265 'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ),
266 'type' => 'array',
267 'sanitize_callback' => 'wp_parse_id_list',
268 'validate_callback' => 'rest_validate_request_arg',
269 'items' => array(
270 'type' => 'integer',
271 ),
272 );
273 $params['segmentby'] = array(
274 'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
275 'type' => 'string',
276 'enum' => array(
277 'product',
278 'category',
279 'variation',
280 ),
281 'validate_callback' => 'rest_validate_request_arg',
282 );
283 $params['attribute_is'] = array(
284 'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ),
285 'type' => 'array',
286 'items' => array(
287 'type' => 'array',
288 ),
289 'default' => array(),
290 'validate_callback' => 'rest_validate_request_arg',
291 );
292 $params['attribute_is_not'] = array(
293 'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ),
294 'type' => 'array',
295 'items' => array(
296 'type' => 'array',
297 ),
298 'default' => array(),
299 'validate_callback' => 'rest_validate_request_arg',
300 );
301
302 return $params;
303 }
304 }
305