-
Notifications
You must be signed in to change notification settings - Fork 12
/
class-mb-rest-api.php
287 lines (261 loc) · 6.92 KB
/
class-mb-rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* The REST API main class.
*
* @package Meta Box
* @subpackage MB Rest API
*/
/**
* Meta Box Rest API class.
*/
class MB_Rest_API {
/**
* List of media fields to filter.
*
* @var array
*/
protected $media_fields = array(
'media',
'file',
'file_upload',
'file_advanced',
'image',
'image_upload',
'image_advanced',
'plupload_image',
'thickbox_image',
);
/**
* List of fields that have no values.
*
* @var array
*/
protected $no_value_fields = array(
'heading',
'custom_html',
'divider',
'button',
);
/**
* Register new field 'meta_box' for all meta box's fields.
*/
public function init() {
register_rest_field(
$this->get_types(),
'meta_box',
array(
'get_callback' => array( $this, 'get_post_meta' ),
'update_callback' => array( $this, 'update_post_meta' ),
)
);
register_rest_field(
$this->get_types( 'taxonomy' ),
'meta_box',
array(
'get_callback' => array( $this, 'get_term_meta' ),
'update_callback' => array( $this, 'update_term_meta' ),
)
);
register_rest_field(
'user',
'meta_box',
array(
'get_callback' => array( $this, 'get_user_meta' ),
'update_callback' => array( $this, 'update_user_meta' ),
)
);
}
/**
* Get post meta for the rest API.
*
* @param array $object Post object.
*
* @return array
*/
public function get_post_meta( $object ) {
$meta_boxes = rwmb_get_registry( 'meta_box' )->get_by( array( 'object_type' => 'post' ) );
foreach ( $meta_boxes as $key => $meta_box ) {
if ( ! in_array( $object['type'], $meta_box->post_types, true ) ) {
unset( $meta_boxes[ $key ] );
}
}
return $this->get_values( $meta_boxes, $object['id'] );
}
/**
* Update post meta for the rest API.
*
* @param string|array $data Post meta values in either JSON or array format.
* @param object $object Post object.
*/
public function update_post_meta( $data, $object ) {
if ( is_string( $data ) ) {
$data = json_decode( $data, true );
if ( JSON_ERROR_NONE === json_last_error() ) {
return;
}
}
foreach ( $data as $field_id => $value ) {
$field = rwmb_get_registry( 'field' )->get( $field_id, $object->post_type );
$this->update_value( $field, $value, $object->ID );
}
}
/**
* Get term meta for the rest API.
*
* @param array $object Term object.
*
* @return array
*/
public function get_term_meta( $object ) {
$output = array();
if ( ! class_exists( 'MB_Term_Meta_Box' ) ) {
return $output;
}
$meta_boxes = rwmb_get_registry( 'meta_box' )->get_by(
array(
'object_type' => 'term',
)
);
foreach ( $meta_boxes as $key => $meta_box ) {
if ( ! in_array( $object['taxonomy'], $meta_box->taxonomies, true ) ) {
unset( $meta_boxes[ $key ] );
}
}
return $this->get_values( $meta_boxes, $object['id'], array( 'object_type' => 'term' ) );
}
/**
* Update term meta for the rest API.
*
* @param string|array $data Term meta values in either JSON or array format.
* @param object $object Term object.
*/
public function update_term_meta( $data, $object ) {
if ( is_string( $data ) ) {
$data = json_decode( $data, true );
if ( JSON_ERROR_NONE === json_last_error() ) {
return;
}
}
foreach ( $data as $field_id => $value ) {
$field = rwmb_get_registry( 'field' )->get( $field_id, $object->taxonomy );
$this->update_value( $field, $value, $object->term_id );
}
}
/**
* Get user meta for the rest API.
*
* @param array $object User object.
*
* @return array
*/
public function get_user_meta( $object ) {
$output = array();
if ( ! class_exists( 'MB_User_Meta_Box' ) ) {
return $output;
}
$meta_boxes = rwmb_get_registry( 'meta_box' )->get_by(
array(
'object_type' => 'user',
)
);
return $this->get_values( $meta_boxes, $object['id'], array( 'object_type' => 'user' ) );
}
/**
* Update user meta for the rest API.
*
* @param string|array $data User meta values in either JSON or array format.
* @param object $object User object.
*/
public function update_user_meta( $data, $object ) {
if ( is_string( $data ) ) {
$data = json_decode( $data, true );
if ( JSON_ERROR_NONE === json_last_error() ) {
return;
}
}
foreach ( $data as $field_id => $value ) {
$field = rwmb_get_registry( 'field' )->get( $field_id, 'user' );
$this->update_value( $field, $value, $object->ID );
}
}
/**
* Update field value.
*
* @param array $field Field data.
* @param mixed $value Field value.
* @param int $object_id Object ID.
*/
protected function update_value( $field, $value, $object_id ) {
$old = RWMB_Field::call( $field, 'raw_meta', $object_id );
$new = $value;
// Allow field class change the value.
if ( $field['clone'] ) {
$new = RWMB_Clone::value( $new, $old, $object_id, $field );
} else {
$new = RWMB_Field::call( $field, 'value', $new, $old, $object_id );
$new = RWMB_Field::filter( 'sanitize', $new, $field );
}
$new = RWMB_Field::filter( 'value', $new, $field, $old );
$new = RWMB_Field::filter( 'rest_value', $new, $field, $old, $object_id );
// Call defined method to save meta value, if there's no methods, call common one.
RWMB_Field::call( $field, 'save', $new, $old, $object_id );
}
/**
* Get supported types in Rest API.
*
* @param string $type 'post' or 'taxonomy'.
*
* @return array
*/
protected function get_types( $type = 'post' ) {
$types = get_post_types( array(), 'objects' );
if ( 'taxonomy' === $type ) {
$types = get_taxonomies( array(), 'objects' );
}
foreach ( $types as $type => $object ) {
if ( empty( $object->show_in_rest ) ) {
unset( $types[ $type ] );
}
}
return array_keys( $types );
}
/**
* Get all fields' values from list of meta boxes.
*
* @param array $meta_boxes Array of meta box object.
*
* @param int $object_id Object ID.
* @param array $args Additional params for helper function.
*
* @return array
*/
protected function get_values( $meta_boxes, $object_id, $args = array() ) {
$fields = array();
foreach ( $meta_boxes as $meta_box ) {
$fields = array_merge( $fields, $meta_box->fields );
}
$fields = array_filter( $fields, array( $this, 'has_value' ) );
$values = array();
foreach ( $fields as $field ) {
$value = rwmb_get_value( $field['id'], $args, $object_id );
/*
* Make sure values of file/image fields are always indexed 0, 1, 2, ...
* @link https://github.com/wpmetabox/mb-rest-api/commit/31aa8fa445c188e8a71ebff80027acbcaa0fd268
*/
if ( is_array( $value ) && in_array( $field['type'], $this->media_fields, true ) ) {
$value = array_values( $value );
}
$values[ $field['id'] ] = $value;
}
return $values;
}
/**
* Check if a field has values.
*
* @param array $field Field settings.
* @return bool
*/
public function has_value( $field ) {
return ! empty( $field['id'] ) && ! in_array( $field['type'], $this->no_value_fields, true );
}
}