|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Meta Data model |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Glynn Quelch <[email protected]> |
|
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
22
|
|
|
* @package PinkCrab\Registerables |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace PinkCrab\Registerables; |
|
26
|
|
|
|
|
27
|
|
|
use PinkCrab\Registerables\Registration_Middleware\Registerable; |
|
28
|
|
|
|
|
29
|
|
|
class Meta_Data { |
|
30
|
|
|
/** |
|
31
|
|
|
* Object type meta applies to |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $meta_type = 'post'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Holds a secondary object type, used for post type and taxonomy. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string|null |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $object_subtype = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Value type. |
|
46
|
|
|
* accepts 'string', 'boolean', 'integer', 'number', 'array', and 'object' |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $type = 'string'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Meta description |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $description = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Meta value is single value or array |
|
61
|
|
|
* |
|
62
|
|
|
* @var bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $single = false; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Default value |
|
68
|
|
|
* Should match same type as $type defined above. |
|
69
|
|
|
* |
|
70
|
|
|
* @var mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $default = ''; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The meta fields callbacks |
|
76
|
|
|
* |
|
77
|
|
|
* @var array{ |
|
|
|
|
|
|
78
|
|
|
* sanitize: null|callable, |
|
79
|
|
|
* permissions: null|callable, |
|
80
|
|
|
* rest_view: null|callable(mixed[]): void, |
|
81
|
|
|
* rest_update: null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void |
|
82
|
|
|
* } |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $callbacks = array( |
|
85
|
|
|
'sanitize' => null, |
|
86
|
|
|
'permissions' => null, |
|
87
|
|
|
'rest_view' => null, |
|
88
|
|
|
'rest_update' => null, |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Rest schema definitions |
|
93
|
|
|
* |
|
94
|
|
|
* @var bool|array<mixed>|\PinkCrab\WP_Rest_Schema\Argument\Argument |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $rest_schema = false; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Meta key |
|
100
|
|
|
* |
|
101
|
|
|
* @var string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected $meta_key; |
|
104
|
|
|
|
|
105
|
|
|
public function __construct( string $meta_key ) { |
|
106
|
|
|
$this->meta_key = $meta_key; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set object type meta applies to |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $meta_type Object type meta applies to |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
|
|
public function meta_type( string $meta_type ): self { |
|
117
|
|
|
$this->meta_type = $meta_type; |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set holds a secondary object type, used for post type and taxonomy. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string|null $object_subtype Holds a secondary object type, used for post type and taxonomy. |
|
125
|
|
|
* @return self |
|
126
|
|
|
*/ |
|
127
|
|
|
public function object_subtype( ?string $object_subtype ): self { |
|
128
|
|
|
$this->object_subtype = $object_subtype ?? ''; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets the values type. |
|
134
|
|
|
* accepts 'string', 'boolean', 'integer', 'number', 'array', and 'object' |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $type |
|
137
|
|
|
* @return self |
|
138
|
|
|
*/ |
|
139
|
|
|
public function type( string $type ): self { |
|
140
|
|
|
$this->type = $type; |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Sets the meta type as POST and defined the post type (object_subtype) |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $post_type |
|
148
|
|
|
* @return self |
|
149
|
|
|
*/ |
|
150
|
|
|
public function post_type( string $post_type ): self { |
|
151
|
|
|
$this->meta_type( 'post' ); |
|
152
|
|
|
$this->object_subtype( $post_type ); |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets the meta type as TERM and defined the taxonomy (object_subtype) |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $taxonomy |
|
160
|
|
|
* @return self |
|
161
|
|
|
*/ |
|
162
|
|
|
public function taxonomy( string $taxonomy ): self { |
|
163
|
|
|
$this->meta_type( 'term' ); |
|
164
|
|
|
$this->object_subtype( $taxonomy ); |
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set meta description |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $description Meta description |
|
172
|
|
|
* |
|
173
|
|
|
* @return self |
|
174
|
|
|
*/ |
|
175
|
|
|
public function description( string $description ): self { |
|
176
|
|
|
$this->description = $description; |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set meta value is single value or array |
|
182
|
|
|
* |
|
183
|
|
|
* @param bool $single Meta value is single value or array |
|
184
|
|
|
* @return self |
|
185
|
|
|
*/ |
|
186
|
|
|
public function single( bool $single = true ): self { |
|
187
|
|
|
$this->single = $single; |
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set should match same type as $type defined above. |
|
193
|
|
|
* |
|
194
|
|
|
* @param mixed $default |
|
195
|
|
|
* @return self |
|
196
|
|
|
*/ |
|
197
|
|
|
public function default( $default ): self { |
|
198
|
|
|
$this->default = $default; |
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Set the sanitization callback for setting values. |
|
204
|
|
|
* |
|
205
|
|
|
* @param callable(mixed):mixed $callback |
|
206
|
|
|
* @return self |
|
207
|
|
|
*/ |
|
208
|
|
|
public function sanitize( callable $callback ): self { |
|
209
|
|
|
$this->callbacks['sanitize'] = $callback; |
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Set the permission callback for setting/getting values |
|
215
|
|
|
* |
|
216
|
|
|
* @param callable $callback |
|
217
|
|
|
* @return self |
|
218
|
|
|
*/ |
|
219
|
|
|
public function permissions( callable $callback ): self { |
|
220
|
|
|
$this->callbacks['permissions'] = $callback; |
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Set rest schema definitions |
|
226
|
|
|
* |
|
227
|
|
|
* @param bool|array<mixed> $rest_schema|PinkCrab\WP_Rest_Schema\Argument\Argument Rest schema definitions |
|
228
|
|
|
* @return self |
|
229
|
|
|
*/ |
|
230
|
|
|
public function rest_schema( $rest_schema ): self { |
|
231
|
|
|
$this->rest_schema = $rest_schema; |
|
232
|
|
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Sets the GET callback for REST requests. |
|
237
|
|
|
* |
|
238
|
|
|
* @param callable|null $callback |
|
239
|
|
|
* @return self |
|
240
|
|
|
*/ |
|
241
|
|
|
public function rest_view( ?callable $callback ): self { |
|
242
|
|
|
$this->callbacks['rest_view'] = $callback; |
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Sets the UPDATE callback for REST requests. |
|
248
|
|
|
* |
|
249
|
|
|
* @param null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment):void $callback |
|
250
|
|
|
* @return self |
|
251
|
|
|
*/ |
|
252
|
|
|
public function rest_update( ?callable $callback ): self { |
|
253
|
|
|
$this->callbacks['rest_update'] = $callback; |
|
254
|
|
|
return $this; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Builds the args array for registering metadata |
|
259
|
|
|
* |
|
260
|
|
|
* @return array<string, mixed> |
|
261
|
|
|
*/ |
|
262
|
|
|
public function parse_args(): array { |
|
263
|
|
|
$args = array( |
|
264
|
|
|
'type' => $this->type, |
|
265
|
|
|
'description' => $this->description, |
|
266
|
|
|
'default' => $this->default, |
|
267
|
|
|
'single' => $this->single, |
|
268
|
|
|
'sanitize_callback' => $this->callbacks['sanitize'], |
|
269
|
|
|
'auth_callback' => $this->callbacks['permissions'], |
|
270
|
|
|
'show_in_rest' => $this->rest_schema, |
|
271
|
|
|
); |
|
272
|
|
|
|
|
273
|
|
|
// Set subtype. |
|
274
|
|
|
if ( $this->object_subtype !== null ) { |
|
275
|
|
|
$args['object_subtype'] = $this->object_subtype; |
|
276
|
|
|
} |
|
277
|
|
|
return $args; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Get meta key |
|
282
|
|
|
* |
|
283
|
|
|
* @return string |
|
284
|
|
|
*/ |
|
285
|
|
|
public function get_meta_key(): string { |
|
286
|
|
|
return $this->meta_key; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Get object type meta applies to |
|
291
|
|
|
* |
|
292
|
|
|
* @return string |
|
293
|
|
|
*/ |
|
294
|
|
|
public function get_meta_type(): string { |
|
295
|
|
|
return $this->meta_type; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Gets the rest schema definition. |
|
300
|
|
|
* |
|
301
|
|
|
* @return bool|array<mixed>|\PinkCrab\WP_Rest_Schema\Argument\Argument |
|
302
|
|
|
*/ |
|
303
|
|
|
public function get_rest_schema() { |
|
304
|
|
|
return $this->rest_schema; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Get holds a secondary object type, used for post type and taxonomy. |
|
309
|
|
|
* |
|
310
|
|
|
* @return string|null |
|
311
|
|
|
*/ |
|
312
|
|
|
public function get_subtype(): ?string { |
|
313
|
|
|
return $this->object_subtype; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Gets the GET callback for REST requests. |
|
318
|
|
|
* |
|
319
|
|
|
* @return null|callable(mixed[]): void |
|
320
|
|
|
*/ |
|
321
|
|
|
public function get_rest_view(): ?callable { |
|
322
|
|
|
return $this->callbacks['rest_view']; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Sets the GET callback for REST requests. |
|
327
|
|
|
* |
|
328
|
|
|
* @return null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void |
|
329
|
|
|
*/ |
|
330
|
|
|
public function get_rest_update(): ?callable { |
|
331
|
|
|
return $this->callbacks['rest_update']; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
} |
|
335
|
|
|
|