|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API Coupons controller |
|
4
|
|
|
* |
|
5
|
|
|
* Handles requests to the /coupons endpoint. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Automattic/WooCommerce/RestApi |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Automattic\WooCommerce\RestApi\Controllers\Version4; |
|
11
|
|
|
|
|
12
|
|
|
defined( 'ABSPATH' ) || exit; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* REST API Coupons controller class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Coupons extends AbstractObjectsController { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Route base. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $rest_base = 'coupons'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Post type. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $post_type = 'shop_coupon'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get object. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 3.0.0 |
|
37
|
|
|
* @param int $id Object ID. |
|
38
|
|
|
* @return \WC_Data |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function get_object( $id ) { |
|
41
|
|
|
return new \WC_Coupon( $id ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get data for this object in the format of this endpoint's schema. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \WC_Coupon $object Object to prepare. |
|
48
|
|
|
* @param \WP_REST_Request $request Request object. |
|
49
|
|
|
* @return array Array of data in the correct format. |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function get_data_for_response( $object, $request ) { |
|
52
|
|
|
$data = $object->get_data(); |
|
53
|
|
|
|
|
54
|
|
|
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' ); |
|
55
|
|
|
$format_date = array( 'date_created', 'date_modified', 'date_expires' ); |
|
56
|
|
|
$format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' ); |
|
57
|
|
|
|
|
58
|
|
|
// Format decimal values. |
|
59
|
|
|
foreach ( $format_decimal as $key ) { |
|
60
|
|
|
$data[ $key ] = wc_format_decimal( $data[ $key ], 2 ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Format date values. |
|
64
|
|
|
foreach ( $format_date as $key ) { |
|
65
|
|
|
$datetime = $data[ $key ]; |
|
66
|
|
|
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); |
|
67
|
|
|
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Format null values. |
|
71
|
|
|
foreach ( $format_null as $key ) { |
|
72
|
|
|
$data[ $key ] = $data[ $key ] ? $data[ $key ] : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return array( |
|
76
|
|
|
'id' => $object->get_id(), |
|
77
|
|
|
'code' => $data['code'], |
|
78
|
|
|
'amount' => $data['amount'], |
|
79
|
|
|
'date_created' => $data['date_created'], |
|
80
|
|
|
'date_created_gmt' => $data['date_created_gmt'], |
|
81
|
|
|
'date_modified' => $data['date_modified'], |
|
82
|
|
|
'date_modified_gmt' => $data['date_modified_gmt'], |
|
83
|
|
|
'discount_type' => $data['discount_type'], |
|
84
|
|
|
'description' => $data['description'], |
|
85
|
|
|
'date_expires' => $data['date_expires'], |
|
86
|
|
|
'date_expires_gmt' => $data['date_expires_gmt'], |
|
87
|
|
|
'usage_count' => $data['usage_count'], |
|
88
|
|
|
'individual_use' => $data['individual_use'], |
|
89
|
|
|
'product_ids' => $data['product_ids'], |
|
90
|
|
|
'excluded_product_ids' => $data['excluded_product_ids'], |
|
91
|
|
|
'usage_limit' => $data['usage_limit'], |
|
92
|
|
|
'usage_limit_per_user' => $data['usage_limit_per_user'], |
|
93
|
|
|
'limit_usage_to_x_items' => $data['limit_usage_to_x_items'], |
|
94
|
|
|
'free_shipping' => $data['free_shipping'], |
|
95
|
|
|
'product_categories' => $data['product_categories'], |
|
96
|
|
|
'excluded_product_categories' => $data['excluded_product_categories'], |
|
97
|
|
|
'exclude_sale_items' => $data['exclude_sale_items'], |
|
98
|
|
|
'minimum_amount' => $data['minimum_amount'], |
|
99
|
|
|
'maximum_amount' => $data['maximum_amount'], |
|
100
|
|
|
'email_restrictions' => $data['email_restrictions'], |
|
101
|
|
|
'used_by' => $data['used_by'], |
|
102
|
|
|
'meta_data' => $data['meta_data'], |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Prepare objects query. |
|
108
|
|
|
* |
|
109
|
|
|
* @since 3.0.0 |
|
110
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function prepare_objects_query( $request ) { |
|
114
|
|
|
$args = parent::prepare_objects_query( $request ); |
|
115
|
|
|
|
|
116
|
|
|
if ( ! empty( $request['code'] ) ) { |
|
117
|
|
|
$id = wc_get_coupon_id_by_code( $request['code'] ); |
|
118
|
|
|
$args['post__in'] = array( $id ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ( ! empty( $request['search'] ) ) { |
|
122
|
|
|
$args['search'] = $request['search']; |
|
123
|
|
|
$args['s'] = false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $args; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Prepare a single coupon for create or update. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \WP_REST_Request $request Request object. |
|
133
|
|
|
* @param bool $creating If is creating a new object. |
|
134
|
|
|
* @return \WP_Error|\WC_Data |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function prepare_object_for_database( $request, $creating = false ) { |
|
137
|
|
|
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; |
|
138
|
|
|
$coupon = new \WC_Coupon( $id ); |
|
139
|
|
|
$schema = $this->get_item_schema(); |
|
140
|
|
|
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); |
|
141
|
|
|
|
|
142
|
|
|
// Validate required POST fields. |
|
143
|
|
|
if ( $creating && empty( $request['code'] ) ) { |
|
144
|
|
|
return new \WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce-rest-api' ), 'code' ), array( 'status' => 400 ) ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Handle all writable props. |
|
148
|
|
|
foreach ( $data_keys as $key ) { |
|
149
|
|
|
$value = $request[ $key ]; |
|
150
|
|
|
|
|
151
|
|
|
if ( ! is_null( $value ) ) { |
|
152
|
|
|
switch ( $key ) { |
|
153
|
|
|
case 'code': |
|
154
|
|
|
$coupon_code = wc_format_coupon_code( $value ); |
|
155
|
|
|
$id = $coupon->get_id() ? $coupon->get_id() : 0; |
|
156
|
|
|
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id ); |
|
157
|
|
|
|
|
158
|
|
|
if ( $id_from_code ) { |
|
159
|
|
|
return new \WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$coupon->set_code( $coupon_code ); |
|
163
|
|
|
break; |
|
164
|
|
|
case 'meta_data': |
|
165
|
|
|
if ( is_array( $value ) ) { |
|
166
|
|
|
foreach ( $value as $meta ) { |
|
167
|
|
|
$coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : 0 ); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
break; |
|
171
|
|
|
case 'description': |
|
172
|
|
|
$coupon->set_description( wp_filter_post_kses( $value ) ); |
|
173
|
|
|
break; |
|
174
|
|
|
default: |
|
175
|
|
|
if ( is_callable( array( $coupon, "set_{$key}" ) ) ) { |
|
176
|
|
|
$coupon->{"set_{$key}"}( $value ); |
|
177
|
|
|
} |
|
178
|
|
|
break; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Filters an object before it is inserted via the REST API. |
|
185
|
|
|
* |
|
186
|
|
|
* The dynamic portion of the hook name, `$this->post_type`, |
|
187
|
|
|
* refers to the object type slug. |
|
188
|
|
|
* |
|
189
|
|
|
* @param \WC_Data $coupon Object object. |
|
190
|
|
|
* @param \WP_REST_Request $request Request object. |
|
191
|
|
|
* @param bool $creating If is creating a new object. |
|
192
|
|
|
*/ |
|
193
|
|
|
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $coupon, $request, $creating ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get the Coupon's schema, conforming to JSON Schema. |
|
198
|
|
|
* |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
|
public function get_item_schema() { |
|
202
|
|
|
$schema = array( |
|
203
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
204
|
|
|
'title' => $this->post_type, |
|
205
|
|
|
'type' => 'object', |
|
206
|
|
|
'properties' => array( |
|
207
|
|
|
'id' => array( |
|
208
|
|
|
'description' => __( 'Unique identifier for the object.', 'woocommerce-rest-api' ), |
|
209
|
|
|
'type' => 'integer', |
|
210
|
|
|
'context' => array( 'view', 'edit' ), |
|
211
|
|
|
'readonly' => true, |
|
212
|
|
|
), |
|
213
|
|
|
'code' => array( |
|
214
|
|
|
'description' => __( 'Coupon code.', 'woocommerce-rest-api' ), |
|
215
|
|
|
'type' => 'string', |
|
216
|
|
|
'context' => array( 'view', 'edit' ), |
|
217
|
|
|
'required' => true, |
|
218
|
|
|
), |
|
219
|
|
|
'amount' => array( |
|
220
|
|
|
'description' => __( 'The amount of discount. Should always be numeric, even if setting a percentage.', 'woocommerce-rest-api' ), |
|
221
|
|
|
'type' => 'string', |
|
222
|
|
|
'context' => array( 'view', 'edit' ), |
|
223
|
|
|
), |
|
224
|
|
|
'date_created' => array( |
|
225
|
|
|
'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce-rest-api' ), |
|
226
|
|
|
'type' => 'date-time', |
|
227
|
|
|
'context' => array( 'view', 'edit' ), |
|
228
|
|
|
'readonly' => true, |
|
229
|
|
|
), |
|
230
|
|
|
'date_created_gmt' => array( |
|
231
|
|
|
'description' => __( 'The date the coupon was created, as GMT.', 'woocommerce-rest-api' ), |
|
232
|
|
|
'type' => 'date-time', |
|
233
|
|
|
'context' => array( 'view', 'edit' ), |
|
234
|
|
|
'readonly' => true, |
|
235
|
|
|
), |
|
236
|
|
|
'date_modified' => array( |
|
237
|
|
|
'description' => __( "The date the coupon was last modified, in the site's timezone.", 'woocommerce-rest-api' ), |
|
238
|
|
|
'type' => 'date-time', |
|
239
|
|
|
'context' => array( 'view', 'edit' ), |
|
240
|
|
|
'readonly' => true, |
|
241
|
|
|
), |
|
242
|
|
|
'date_modified_gmt' => array( |
|
243
|
|
|
'description' => __( 'The date the coupon was last modified, as GMT.', 'woocommerce-rest-api' ), |
|
244
|
|
|
'type' => 'date-time', |
|
245
|
|
|
'context' => array( 'view', 'edit' ), |
|
246
|
|
|
'readonly' => true, |
|
247
|
|
|
), |
|
248
|
|
|
'discount_type' => array( |
|
249
|
|
|
'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce-rest-api' ), |
|
250
|
|
|
'type' => 'string', |
|
251
|
|
|
'default' => 'fixed_cart', |
|
252
|
|
|
'enum' => array_keys( wc_get_coupon_types() ), |
|
253
|
|
|
'context' => array( 'view', 'edit' ), |
|
254
|
|
|
), |
|
255
|
|
|
'description' => array( |
|
256
|
|
|
'description' => __( 'Coupon description.', 'woocommerce-rest-api' ), |
|
257
|
|
|
'type' => 'string', |
|
258
|
|
|
'context' => array( 'view', 'edit' ), |
|
259
|
|
|
), |
|
260
|
|
|
'date_expires' => array( |
|
261
|
|
|
'description' => __( "The date the coupon expires, in the site's timezone.", 'woocommerce-rest-api' ), |
|
262
|
|
|
'type' => 'string', |
|
263
|
|
|
'context' => array( 'view', 'edit' ), |
|
264
|
|
|
), |
|
265
|
|
|
'date_expires_gmt' => array( |
|
266
|
|
|
'description' => __( 'The date the coupon expires, as GMT.', 'woocommerce-rest-api' ), |
|
267
|
|
|
'type' => 'string', |
|
268
|
|
|
'context' => array( 'view', 'edit' ), |
|
269
|
|
|
), |
|
270
|
|
|
'usage_count' => array( |
|
271
|
|
|
'description' => __( 'Number of times the coupon has been used already.', 'woocommerce-rest-api' ), |
|
272
|
|
|
'type' => 'integer', |
|
273
|
|
|
'context' => array( 'view', 'edit' ), |
|
274
|
|
|
'readonly' => true, |
|
275
|
|
|
), |
|
276
|
|
|
'individual_use' => array( |
|
277
|
|
|
'description' => __( 'If true, the coupon can only be used individually. Other applied coupons will be removed from the cart.', 'woocommerce-rest-api' ), |
|
278
|
|
|
'type' => 'boolean', |
|
279
|
|
|
'default' => false, |
|
280
|
|
|
'context' => array( 'view', 'edit' ), |
|
281
|
|
|
), |
|
282
|
|
|
'product_ids' => array( |
|
283
|
|
|
'description' => __( 'List of product IDs the coupon can be used on.', 'woocommerce-rest-api' ), |
|
284
|
|
|
'type' => 'array', |
|
285
|
|
|
'items' => array( |
|
286
|
|
|
'type' => 'integer', |
|
287
|
|
|
), |
|
288
|
|
|
'context' => array( 'view', 'edit' ), |
|
289
|
|
|
), |
|
290
|
|
|
'excluded_product_ids' => array( |
|
291
|
|
|
'description' => __( 'List of product IDs the coupon cannot be used on.', 'woocommerce-rest-api' ), |
|
292
|
|
|
'type' => 'array', |
|
293
|
|
|
'items' => array( |
|
294
|
|
|
'type' => 'integer', |
|
295
|
|
|
), |
|
296
|
|
|
'context' => array( 'view', 'edit' ), |
|
297
|
|
|
), |
|
298
|
|
|
'usage_limit' => array( |
|
299
|
|
|
'description' => __( 'How many times the coupon can be used in total.', 'woocommerce-rest-api' ), |
|
300
|
|
|
'type' => 'integer', |
|
301
|
|
|
'context' => array( 'view', 'edit' ), |
|
302
|
|
|
), |
|
303
|
|
|
'usage_limit_per_user' => array( |
|
304
|
|
|
'description' => __( 'How many times the coupon can be used per customer.', 'woocommerce-rest-api' ), |
|
305
|
|
|
'type' => 'integer', |
|
306
|
|
|
'context' => array( 'view', 'edit' ), |
|
307
|
|
|
), |
|
308
|
|
|
'limit_usage_to_x_items' => array( |
|
309
|
|
|
'description' => __( 'Max number of items in the cart the coupon can be applied to.', 'woocommerce-rest-api' ), |
|
310
|
|
|
'type' => 'integer', |
|
311
|
|
|
'context' => array( 'view', 'edit' ), |
|
312
|
|
|
), |
|
313
|
|
|
'free_shipping' => array( |
|
314
|
|
|
'description' => __( 'If true and if the free shipping method requires a coupon, this coupon will enable free shipping.', 'woocommerce-rest-api' ), |
|
315
|
|
|
'type' => 'boolean', |
|
316
|
|
|
'default' => false, |
|
317
|
|
|
'context' => array( 'view', 'edit' ), |
|
318
|
|
|
), |
|
319
|
|
|
'product_categories' => array( |
|
320
|
|
|
'description' => __( 'List of category IDs the coupon applies to.', 'woocommerce-rest-api' ), |
|
321
|
|
|
'type' => 'array', |
|
322
|
|
|
'items' => array( |
|
323
|
|
|
'type' => 'integer', |
|
324
|
|
|
), |
|
325
|
|
|
'context' => array( 'view', 'edit' ), |
|
326
|
|
|
), |
|
327
|
|
|
'excluded_product_categories' => array( |
|
328
|
|
|
'description' => __( 'List of category IDs the coupon does not apply to.', 'woocommerce-rest-api' ), |
|
329
|
|
|
'type' => 'array', |
|
330
|
|
|
'items' => array( |
|
331
|
|
|
'type' => 'integer', |
|
332
|
|
|
), |
|
333
|
|
|
'context' => array( 'view', 'edit' ), |
|
334
|
|
|
), |
|
335
|
|
|
'exclude_sale_items' => array( |
|
336
|
|
|
'description' => __( 'If true, this coupon will not be applied to items that have sale prices.', 'woocommerce-rest-api' ), |
|
337
|
|
|
'type' => 'boolean', |
|
338
|
|
|
'default' => false, |
|
339
|
|
|
'context' => array( 'view', 'edit' ), |
|
340
|
|
|
), |
|
341
|
|
|
'minimum_amount' => array( |
|
342
|
|
|
'description' => __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'woocommerce-rest-api' ), |
|
343
|
|
|
'type' => 'string', |
|
344
|
|
|
'context' => array( 'view', 'edit' ), |
|
345
|
|
|
), |
|
346
|
|
|
'maximum_amount' => array( |
|
347
|
|
|
'description' => __( 'Maximum order amount allowed when using the coupon.', 'woocommerce-rest-api' ), |
|
348
|
|
|
'type' => 'string', |
|
349
|
|
|
'context' => array( 'view', 'edit' ), |
|
350
|
|
|
), |
|
351
|
|
|
'email_restrictions' => array( |
|
352
|
|
|
'description' => __( 'List of email addresses that can use this coupon.', 'woocommerce-rest-api' ), |
|
353
|
|
|
'type' => 'array', |
|
354
|
|
|
'items' => array( |
|
355
|
|
|
'type' => 'string', |
|
356
|
|
|
), |
|
357
|
|
|
'context' => array( 'view', 'edit' ), |
|
358
|
|
|
), |
|
359
|
|
|
'used_by' => array( |
|
360
|
|
|
'description' => __( 'List of user IDs (or guest email addresses) that have used the coupon.', 'woocommerce-rest-api' ), |
|
361
|
|
|
'type' => 'array', |
|
362
|
|
|
'items' => array( |
|
363
|
|
|
'type' => 'integer', |
|
364
|
|
|
), |
|
365
|
|
|
'context' => array( 'view', 'edit' ), |
|
366
|
|
|
'readonly' => true, |
|
367
|
|
|
), |
|
368
|
|
|
'meta_data' => array( |
|
369
|
|
|
'description' => __( 'Meta data.', 'woocommerce-rest-api' ), |
|
370
|
|
|
'type' => 'array', |
|
371
|
|
|
'context' => array( 'view', 'edit' ), |
|
372
|
|
|
'items' => array( |
|
373
|
|
|
'type' => 'object', |
|
374
|
|
|
'properties' => array( |
|
375
|
|
|
'id' => array( |
|
376
|
|
|
'description' => __( 'Meta ID.', 'woocommerce-rest-api' ), |
|
377
|
|
|
'type' => 'integer', |
|
378
|
|
|
'context' => array( 'view', 'edit' ), |
|
379
|
|
|
'readonly' => true, |
|
380
|
|
|
), |
|
381
|
|
|
'key' => array( |
|
382
|
|
|
'description' => __( 'Meta key.', 'woocommerce-rest-api' ), |
|
383
|
|
|
'type' => 'string', |
|
384
|
|
|
'context' => array( 'view', 'edit' ), |
|
385
|
|
|
), |
|
386
|
|
|
'value' => array( |
|
387
|
|
|
'description' => __( 'Meta value.', 'woocommerce-rest-api' ), |
|
388
|
|
|
'type' => 'mixed', |
|
389
|
|
|
'context' => array( 'view', 'edit' ), |
|
390
|
|
|
), |
|
391
|
|
|
), |
|
392
|
|
|
), |
|
393
|
|
|
), |
|
394
|
|
|
), |
|
395
|
|
|
); |
|
396
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Get the query params for collections of attachments. |
|
401
|
|
|
* |
|
402
|
|
|
* @return array |
|
403
|
|
|
*/ |
|
404
|
|
|
public function get_collection_params() { |
|
405
|
|
|
$params = parent::get_collection_params(); |
|
406
|
|
|
|
|
407
|
|
|
$params['code'] = array( |
|
408
|
|
|
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce-rest-api' ), |
|
409
|
|
|
'type' => 'string', |
|
410
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
|
411
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
|
412
|
|
|
); |
|
413
|
|
|
|
|
414
|
|
|
$params['search'] = array( |
|
415
|
|
|
'description' => __( 'Limit results to coupons with codes matching a given string.', 'woocommerce-rest-api' ), |
|
416
|
|
|
'type' => 'string', |
|
417
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
|
418
|
|
|
); |
|
419
|
|
|
|
|
420
|
|
|
return $params; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* Only return writable props from schema. |
|
425
|
|
|
* |
|
426
|
|
|
* @param array $schema Schema array. |
|
427
|
|
|
* @return bool |
|
428
|
|
|
*/ |
|
429
|
|
|
protected function filter_writable_props( $schema ) { |
|
430
|
|
|
return empty( $schema['readonly'] ); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* Get a collection of posts and add the code search option to \WP_Query. |
|
435
|
|
|
* |
|
436
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
437
|
|
|
* @return \WP_Error|\WP_REST_Response |
|
438
|
|
|
*/ |
|
439
|
|
|
public function get_items( $request ) { |
|
440
|
|
|
add_filter( 'posts_where', array( $this, 'add_wp_query_search_code_filter' ), 10, 2 ); |
|
441
|
|
|
$response = parent::get_items( $request ); |
|
442
|
|
|
remove_filter( 'posts_where', array( $this, 'add_wp_query_search_code_filter' ), 10 ); |
|
443
|
|
|
return $response; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Add code searching to the WP Query |
|
448
|
|
|
* |
|
449
|
|
|
* @param string $where Where clause used to search posts. |
|
450
|
|
|
* @param object $wp_query \WP_Query object. |
|
451
|
|
|
* @return string |
|
452
|
|
|
*/ |
|
453
|
|
|
public function add_wp_query_search_code_filter( $where, $wp_query ) { |
|
454
|
|
|
global $wpdb; |
|
455
|
|
|
|
|
456
|
|
|
$search = $wp_query->get( 'search' ); |
|
457
|
|
|
if ( $search ) { |
|
458
|
|
|
$search = $wpdb->esc_like( $search ); |
|
459
|
|
|
$search = "'%" . $search . "%'"; |
|
460
|
|
|
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE ' . $search; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
return $where; |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
|