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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 / Coupons / Controller.php
woocommerce / src / Admin / API / Reports / Coupons Last commit date
Stats 1 month ago Controller.php 1 year ago DataStore.php 2 months ago Query.php 1 year ago
Controller.php
275 lines
1 <?php
2 /**
3 * REST API Reports coupons controller
4 *
5 * Handles requests to the /reports/coupons endpoint.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Automattic\WooCommerce\Admin\API\Reports\GenericController;
13 use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
14 use Automattic\WooCommerce\Admin\API\Reports\GenericQuery;
15 use WP_REST_Request;
16 use WP_REST_Response;
17
18 /**
19 * REST API Reports coupons controller class.
20 *
21 * @internal
22 * @extends GenericController
23 */
24 class Controller extends GenericController implements ExportableInterface {
25
26 /**
27 * Route base.
28 *
29 * @var string
30 */
31 protected $rest_base = 'reports/coupons';
32
33 /**
34 * Get data from `'coupons'` GenericQuery.
35 *
36 * @override GenericController::get_datastore_data()
37 *
38 * @param array $query_args Query arguments.
39 * @return mixed Results from the data store.
40 */
41 protected function get_datastore_data( $query_args = array() ) {
42 $query = new GenericQuery( $query_args, 'coupons' );
43 return $query->get_data();
44 }
45
46 /**
47 * Maps query arguments from the REST request.
48 *
49 * @param array $request Request array.
50 * @return array
51 */
52 protected function prepare_reports_query( $request ) {
53 $args = array();
54 $args['before'] = $request['before'];
55 $args['after'] = $request['after'];
56 $args['page'] = $request['page'];
57 $args['per_page'] = $request['per_page'];
58 $args['orderby'] = $request['orderby'];
59 $args['order'] = $request['order'];
60 $args['coupons'] = (array) $request['coupons'];
61 $args['extended_info'] = $request['extended_info'];
62 $args['force_cache_refresh'] = $request['force_cache_refresh'];
63 return $args;
64 }
65
66 /**
67 * Prepare a report data item for serialization.
68 *
69 * @param array $report Report data item as returned from Data Store.
70 * @param \WP_REST_Request $request Request object.
71 * @return \WP_REST_Response
72 */
73 public function prepare_item_for_response( $report, $request ) {
74 $response = parent::prepare_item_for_response( $report, $request );
75 $response->add_links( $this->prepare_links( $report ) );
76
77 /**
78 * Filter a report returned from the API.
79 *
80 * Allows modification of the report data right before it is returned.
81 *
82 * @param WP_REST_Response $response The response object.
83 * @param object $report The original report object.
84 * @param WP_REST_Request $request Request used to generate the response.
85 */
86 return apply_filters( 'woocommerce_rest_prepare_report_coupons', $response, $report, $request );
87 }
88
89 /**
90 * Prepare links for the request.
91 *
92 * @param WC_Reports_Query $object Object data.
93 * @return array
94 */
95 protected function prepare_links( $object ) {
96 $links = array(
97 'coupon' => array(
98 'href' => rest_url( sprintf( '/%s/coupons/%d', $this->namespace, $object['coupon_id'] ) ),
99 ),
100 );
101
102 return $links;
103 }
104
105 /**
106 * Get the Report's schema, conforming to JSON Schema.
107 *
108 * @return array
109 */
110 public function get_item_schema() {
111 $schema = array(
112 '$schema' => 'http://json-schema.org/draft-04/schema#',
113 'title' => 'report_coupons',
114 'type' => 'object',
115 'properties' => array(
116 'coupon_id' => array(
117 'description' => __( 'Coupon ID.', 'woocommerce' ),
118 'type' => 'integer',
119 'context' => array( 'view', 'edit' ),
120 'readonly' => true,
121 ),
122 'amount' => array(
123 'description' => __( 'Net discount amount.', 'woocommerce' ),
124 'type' => 'number',
125 'context' => array( 'view', 'edit' ),
126 'readonly' => true,
127 ),
128 'orders_count' => array(
129 'description' => __( 'Number of orders.', 'woocommerce' ),
130 'type' => 'integer',
131 'context' => array( 'view', 'edit' ),
132 'readonly' => true,
133 ),
134 'extended_info' => array(
135 'code' => array(
136 'type' => 'string',
137 'readonly' => true,
138 'context' => array( 'view', 'edit' ),
139 'description' => __( 'Coupon code.', 'woocommerce' ),
140 ),
141 'date_created' => array(
142 'type' => 'date-time',
143 'readonly' => true,
144 'context' => array( 'view', 'edit' ),
145 'description' => __( 'Coupon creation date.', 'woocommerce' ),
146 ),
147 'date_created_gmt' => array(
148 'type' => 'date-time',
149 'readonly' => true,
150 'context' => array( 'view', 'edit' ),
151 'description' => __( 'Coupon creation date in GMT.', 'woocommerce' ),
152 ),
153 'date_expires' => array(
154 'type' => 'date-time',
155 'readonly' => true,
156 'context' => array( 'view', 'edit' ),
157 'description' => __( 'Coupon expiration date.', 'woocommerce' ),
158 ),
159 'date_expires_gmt' => array(
160 'type' => 'date-time',
161 'readonly' => true,
162 'context' => array( 'view', 'edit' ),
163 'description' => __( 'Coupon expiration date in GMT.', 'woocommerce' ),
164 ),
165 'discount_type' => array(
166 'type' => 'string',
167 'readonly' => true,
168 'context' => array( 'view', 'edit' ),
169 'enum' => array_keys( wc_get_coupon_types() ),
170 'description' => __( 'Coupon discount type.', 'woocommerce' ),
171 ),
172 ),
173 ),
174 );
175
176 return $this->add_additional_fields_schema( $schema );
177 }
178
179 /**
180 * Get the query params for collections.
181 *
182 * @return array
183 */
184 public function get_collection_params() {
185 $params = parent::get_collection_params();
186 $params['orderby']['default'] = 'coupon_id';
187 $params['orderby']['enum'] = $this->apply_custom_orderby_filters(
188 array(
189 'coupon_id',
190 'code',
191 'amount',
192 'orders_count',
193 )
194 );
195 $params['coupons'] = array(
196 'description' => __( 'Limit result set to coupons assigned specific coupon IDs.', 'woocommerce' ),
197 'type' => 'array',
198 'sanitize_callback' => 'wp_parse_id_list',
199 'validate_callback' => 'rest_validate_request_arg',
200 'items' => array(
201 'type' => 'integer',
202 ),
203 );
204 $params['extended_info'] = array(
205 'description' => __( 'Add additional piece of info about each coupon to the report.', 'woocommerce' ),
206 'type' => 'boolean',
207 'default' => false,
208 'sanitize_callback' => 'wc_string_to_bool',
209 'validate_callback' => 'rest_validate_request_arg',
210 );
211
212 return $params;
213 }
214
215 /**
216 * Get the column names for export.
217 *
218 * @return array Key value pair of Column ID => Label.
219 */
220 public function get_export_columns() {
221 $export_columns = array(
222 'code' => __( 'Coupon code', 'woocommerce' ),
223 'orders_count' => __( 'Orders', 'woocommerce' ),
224 'amount' => __( 'Amount discounted', 'woocommerce' ),
225 'created' => __( 'Created', 'woocommerce' ),
226 'expires' => __( 'Expires', 'woocommerce' ),
227 'type' => __( 'Type', 'woocommerce' ),
228 );
229
230 /**
231 * Filter to add or remove column names from the coupons report for
232 * export.
233 *
234 * @since 1.6.0
235 */
236 return apply_filters(
237 'woocommerce_report_coupons_export_columns',
238 $export_columns
239 );
240 }
241
242 /**
243 * Get the column values for export.
244 *
245 * @param array $item Single report item/row.
246 * @return array Key value pair of Column ID => Row Value.
247 */
248 public function prepare_item_for_export( $item ) {
249 $date_expires = empty( $item['extended_info']['date_expires'] )
250 ? __( 'N/A', 'woocommerce' )
251 : $item['extended_info']['date_expires'];
252
253 $export_item = array(
254 'code' => $item['extended_info']['code'],
255 'orders_count' => $item['orders_count'],
256 'amount' => $item['amount'],
257 'created' => $item['extended_info']['date_created'],
258 'expires' => $date_expires,
259 'type' => $item['extended_info']['discount_type'],
260 );
261
262 /**
263 * Filter to prepare extra columns in the export item for the coupons
264 * report.
265 *
266 * @since 1.6.0
267 */
268 return apply_filters(
269 'woocommerce_report_coupons_prepare_export_item',
270 $export_item,
271 $item
272 );
273 }
274 }
275