|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
4
|
|
|
exit; |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract Rest Controler Class |
|
9
|
|
|
* |
|
10
|
|
|
* @author WooThemes |
|
11
|
|
|
* @category API |
|
12
|
|
|
* @package WooCommerce/Abstracts |
|
13
|
|
|
* @extends WP_REST_Controller |
|
14
|
|
|
* @version 2.6.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class WC_REST_Controller extends WP_REST_Controller { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Endpoint namespace. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $namespace = 'wc/v1'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Route base. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rest_base = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add the schema from additional fields to an schema array. |
|
34
|
|
|
* |
|
35
|
|
|
* The type of object is inferred from the passed schema. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $schema Schema array. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function add_additional_fields_schema( $schema ) { |
|
40
|
|
|
if ( empty( $schema['title'] ) ) { |
|
41
|
|
|
return $schema; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Can't use $this->get_object_type otherwise we cause an inf loop. |
|
46
|
|
|
*/ |
|
47
|
|
|
$object_type = $schema['title']; |
|
48
|
|
|
|
|
49
|
|
|
$additional_fields = $this->get_additional_fields( $object_type ); |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
foreach ( $additional_fields as $field_name => $field_options ) { |
|
|
|
|
|
|
52
|
|
|
if ( ! $field_options['schema'] ) { |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$schema['properties'][ $field_name ] = $field_options['schema']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] ); |
|
60
|
|
|
|
|
61
|
|
|
return $schema; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get normalized rest base. |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function get_normalized_rest_base() { |
|
70
|
|
|
return preg_replace( '/\(.*\)\//i', '', $this->rest_base ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check batch limit. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $items Request items. |
|
77
|
|
|
* @return bool|WP_Error |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function check_batch_limit( $items ) { |
|
80
|
|
|
$limit = apply_filters( 'woocommerce_rest_batch_items_limit', 100, $this->get_normalized_rest_base() ); |
|
81
|
|
|
$total = 0; |
|
82
|
|
|
|
|
83
|
|
|
if ( ! empty( $items['create'] ) ) { |
|
84
|
|
|
$total += count( $items['create'] ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ( ! empty( $items['update'] ) ) { |
|
88
|
|
|
$total += count( $items['update'] ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( ! empty( $items['delete'] ) ) { |
|
92
|
|
|
$total += count( $items['delete'] ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
if ( $total > $limit ) { |
|
|
|
|
|
|
96
|
|
|
return new WP_Error( 'woocommerce_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), array( 'status' => 413 ) ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Bulk create, update and delete items. |
|
104
|
|
|
* |
|
105
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
106
|
|
|
* @return array Of WP_Error or WP_REST_Response. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function batch_items( $request ) { |
|
109
|
|
|
/** @var WP_REST_Server $wp_rest_server */ |
|
110
|
|
|
global $wp_rest_server; |
|
111
|
|
|
|
|
112
|
|
|
// Get the request params. |
|
113
|
|
|
$items = array_filter( $request->get_params() ); |
|
114
|
|
|
$response = array(); |
|
115
|
|
|
|
|
116
|
|
|
// Check batch limit. |
|
117
|
|
|
$limit = $this->check_batch_limit( $items ); |
|
118
|
|
|
if ( is_wp_error( $limit ) ) { |
|
119
|
|
|
return $limit; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ( ! empty( $items['create'] ) ) { |
|
123
|
|
|
foreach ( $items['create'] as $item ) { |
|
124
|
|
|
$_item = new WP_REST_Request( 'POST' ); |
|
125
|
|
|
|
|
126
|
|
|
// Default parameters. |
|
127
|
|
|
$defaults = array(); |
|
128
|
|
|
$schema = $this->get_public_item_schema(); |
|
129
|
|
|
foreach ( $schema['properties'] as $arg => $options ) { |
|
130
|
|
|
if ( isset( $options['default'] ) ) { |
|
131
|
|
|
$defaults[ $arg ] = $options['default']; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
$_item->set_default_params( $defaults ); |
|
135
|
|
|
|
|
136
|
|
|
// Set request parameters. |
|
137
|
|
|
$_item->set_body_params( $item ); |
|
138
|
|
|
$_response = $this->create_item( $_item ); |
|
139
|
|
|
|
|
140
|
|
|
if ( is_wp_error( $_response ) ) { |
|
141
|
|
|
$response['create'][] = array( |
|
142
|
|
|
'id' => 0, |
|
143
|
|
|
'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ), |
|
144
|
|
|
); |
|
145
|
|
|
} else { |
|
146
|
|
|
$response['create'][] = $wp_rest_server->response_to_data( $_response, '' ); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
View Code Duplication |
if ( ! empty( $items['update'] ) ) { |
|
|
|
|
|
|
152
|
|
|
foreach ( $items['update'] as $item ) { |
|
153
|
|
|
$_item = new WP_REST_Request( 'PUT' ); |
|
154
|
|
|
$_item->set_body_params( $item ); |
|
155
|
|
|
$_response = $this->update_item( $_item ); |
|
156
|
|
|
|
|
157
|
|
|
if ( is_wp_error( $_response ) ) { |
|
158
|
|
|
$response['update'][] = array( |
|
159
|
|
|
'id' => $item['id'], |
|
160
|
|
|
'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ), |
|
161
|
|
|
); |
|
162
|
|
|
} else { |
|
163
|
|
|
$response['update'][] = $wp_rest_server->response_to_data( $_response, '' ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
if ( ! empty( $items['delete'] ) ) { |
|
|
|
|
|
|
169
|
|
|
foreach ( $items['delete'] as $id ) { |
|
170
|
|
|
$_item = new WP_REST_Request( 'DELETE' ); |
|
171
|
|
|
$_item->set_query_params( array( 'id' => $id, 'force' => true ) ); |
|
172
|
|
|
$_response = $this->delete_item( $_item ); |
|
173
|
|
|
|
|
174
|
|
|
if ( is_wp_error( $_response ) ) { |
|
175
|
|
|
$response['delete'][] = array( |
|
176
|
|
|
'id' => $id, |
|
177
|
|
|
'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ), |
|
178
|
|
|
); |
|
179
|
|
|
} else { |
|
180
|
|
|
$response['delete'][] = $wp_rest_server->response_to_data( $_response, '' ); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $response; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get the batch schema, conforming to JSON Schema. |
|
190
|
|
|
* |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function get_public_batch_schema() { |
|
194
|
|
|
$schema = array( |
|
195
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
196
|
|
|
'title' => 'batch', |
|
197
|
|
|
'type' => 'object', |
|
198
|
|
|
'properties' => array( |
|
199
|
|
|
'create' => array( |
|
200
|
|
|
'description' => __( 'List of created resources.', 'woocommerce' ), |
|
201
|
|
|
'type' => 'array', |
|
202
|
|
|
'context' => array( 'view', 'edit' ), |
|
203
|
|
|
), |
|
204
|
|
|
'update' => array( |
|
205
|
|
|
'description' => __( 'List of updated resources.', 'woocommerce' ), |
|
206
|
|
|
'type' => 'array', |
|
207
|
|
|
'context' => array( 'view', 'edit' ), |
|
208
|
|
|
), |
|
209
|
|
|
'delete' => array( |
|
210
|
|
|
'description' => __( 'List of delete resources.', 'woocommerce' ), |
|
211
|
|
|
'type' => 'array', |
|
212
|
|
|
'context' => array( 'view', 'edit' ), |
|
213
|
|
|
), |
|
214
|
|
|
), |
|
215
|
|
|
); |
|
216
|
|
|
|
|
217
|
|
|
return $schema; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.