Issues (12)

src/Meta_Data.php (1 issue)

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
class Meta_Data {
28
	/**
29
	 * Object type meta applies to
30
	 *
31
	 * @var string
32
	 */
33
	protected string $meta_type = 'post';
34
35
	/**
36
	 * Holds a secondary object type, used for post type and taxonomy.
37
	 *
38
	 * @var string|null
39
	 */
40
	protected ?string $object_subtype = null;
41
42
	/**
43
	 * Value type.
44
	 * accepts 'string', 'boolean', 'integer', 'number', 'array', and 'object'
45
	 *
46
	 * @var string
47
	 */
48
	protected string $type = 'string';
49
50
	/**
51
	 * Meta description
52
	 *
53
	 * @var string
54
	 */
55
	protected string $description = '';
56
57
	/**
58
	 * Meta value is single value or array
59
	 *
60
	 * @var bool
61
	 */
62
	protected bool $single = false;
63
64
	/**
65
	 * Default value
66
	 * Should match same type as $type defined above.
67
	 *
68
	 * @var mixed
69
	 */
70
	protected $default = '';
71
72
	/**
73
	* The meta fields callbacks
74
	*
75
	* @var array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
76
	*  sanitize: null|callable,
77
	*  permissions: null|callable,
78
	*  rest_view: null|callable(mixed[]): void,
79
	*  rest_update: null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void
80
	* }
81
	*/
82
	protected array $callbacks = array(
83
		'sanitize'    => null,
84
		'permissions' => null,
85
		'rest_view'   => null,
86
		'rest_update' => null,
87
	);
88
89
	/**
90
	 * Rest schema definitions
91
	 *
92
	 * @var bool|array<mixed>|\PinkCrab\WP_Rest_Schema\Argument\Argument
93
	 */
94
	protected $rest_schema = false;
95
96
	/**
97
	 * Meta key
98
	 *
99
	 * @var string
100
	 */
101
	protected string $meta_key;
102
103
	public function __construct( string $meta_key ) {
104
		$this->meta_key = $meta_key;
105
	}
106
107
108
	/**
109
	 * Set object type meta applies to
110
	 *
111
	 * @param string $meta_type Object type meta applies to
112
	 * @return self
113
	 */
114
	public function meta_type( string $meta_type ): self {
115
		$this->meta_type = $meta_type;
116
		return $this;
117
	}
118
119
	/**
120
	 * Set holds a secondary object type, used for post type and taxonomy.
121
	 *
122
	 * @param string|null $object_subtype Holds a secondary object type, used for post type and taxonomy.
123
	 * @return self
124
	 */
125
	public function object_subtype( ?string $object_subtype ): self {
126
		$this->object_subtype = $object_subtype ?? '';
127
		return $this;
128
	}
129
130
	/**
131
	 * Sets the values type.
132
	 * accepts 'string', 'boolean', 'integer', 'number', 'array', and 'object'
133
	 *
134
	 * @param string $type
135
	 * @return self
136
	 */
137
	public function type( string $type ): self {
138
		$this->type = $type;
139
		return $this;
140
	}
141
142
	/**
143
	 * Sets the meta type as POST and defined the post type (object_subtype)
144
	 *
145
	 * @param string $post_type
146
	 * @return self
147
	 */
148
	public function post_type( string $post_type ): self {
149
		$this->meta_type( 'post' );
150
		$this->object_subtype( $post_type );
151
		return $this;
152
	}
153
154
	/**
155
	 * Sets the meta type as TERM and defined the taxonomy (object_subtype)
156
	 *
157
	 * @param string $taxonomy
158
	 * @return self
159
	 */
160
	public function taxonomy( string $taxonomy ): self {
161
		$this->meta_type( 'term' );
162
		$this->object_subtype( $taxonomy );
163
		return $this;
164
	}
165
166
	/**
167
	 * Set meta description
168
	 *
169
	 * @param string $description Meta description
170
	 *
171
	 * @return self
172
	 */
173
	public function description( string $description ): self {
174
		$this->description = $description;
175
		return $this;
176
	}
177
178
	/**
179
	 * Set meta value is single value or array
180
	 *
181
	 * @param boolean $single Meta value is single value or array
182
	 * @return self
183
	 */
184
	public function single( bool $single = true ): self {
185
		$this->single = $single;
186
		return $this;
187
	}
188
189
	/**
190
	 * Set should match same type as $type defined above.
191
	 *
192
	 * @param mixed $default_value
193
	 * @return self
194
	 */
195
	public function default( $default_value ): self {
196
		$this->default = $default_value;
197
		return $this;
198
	}
199
200
	/**
201
	 * Set the sanitization callback for setting values.
202
	 *
203
	 * @param callable(mixed):mixed $callback
204
	 * @return self
205
	 */
206
	public function sanitize( callable $callback ): self {
207
		$this->callbacks['sanitize'] = $callback;
208
		return $this;
209
	}
210
211
	/**
212
	 * Set the permission callback for setting/getting values
213
	 *
214
	 * @param callable $callback
215
	 * @return self
216
	 */
217
	public function permissions( callable $callback ): self {
218
		$this->callbacks['permissions'] = $callback;
219
		return $this;
220
	}
221
222
	/**
223
	 * Set rest schema definitions
224
	 *
225
	 * @param boolean|array<mixed> $rest_schema|PinkCrab\WP_Rest_Schema\Argument\Argument Rest schema definitions
226
	 * @return self
227
	 */
228
	public function rest_schema( $rest_schema ): self {
229
		$this->rest_schema = $rest_schema;
230
		return $this;
231
	}
232
233
	/**
234
	* Sets the GET callback for REST requests.
235
	*
236
	* @param callable|null $callback
237
	* @return self
238
	*/
239
	public function rest_view( ?callable $callback ): self {
240
		$this->callbacks['rest_view'] = $callback;
241
		return $this;
242
	}
243
244
	/**
245
	 * Sets the UPDATE callback for REST requests.
246
	 *
247
	 * @param null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment):void $callback
248
	 * @return self
249
	 */
250
	public function rest_update( ?callable $callback ): self {
251
		$this->callbacks['rest_update'] = $callback;
252
		return $this;
253
	}
254
255
	/**
256
	 * Builds the args array for registering metadata
257
	 *
258
	 * @return array<string, mixed>
259
	 */
260
	public function parse_args(): array {
261
		$args = array(
262
			'type'              => $this->type,
263
			'description'       => $this->description,
264
			'default'           => $this->default,
265
			'single'            => $this->single,
266
			'sanitize_callback' => $this->callbacks['sanitize'],
267
			'auth_callback'     => $this->callbacks['permissions'],
268
			'show_in_rest'      => $this->rest_schema,
269
		);
270
271
		// Set subtype.
272
		if ( $this->object_subtype !== null ) {
273
			$args['object_subtype'] = $this->object_subtype;
274
		}
275
		return $args;
276
	}
277
278
	/**
279
	 * Get meta key
280
	 *
281
	 * @return string
282
	 */
283
	public function get_meta_key(): string {
284
		return $this->meta_key;
285
	}
286
287
	/**
288
	 * Get object type meta applies to
289
	 *
290
	 * @return string
291
	 */
292
	public function get_meta_type(): string {
293
		return $this->meta_type;
294
	}
295
296
	/**
297
	 * Gets the rest schema definition.
298
	 *
299
	 * @return boolean|array<string, mixed>|\PinkCrab\WP_Rest_Schema\Argument\Argument
300
	 */
301
	public function get_rest_schema() {
302
		return $this->rest_schema;
303
	}
304
305
	/**
306
	 * Get holds a secondary object type, used for post type and taxonomy.
307
	 *
308
	 * @return string|null
309
	 */
310
	public function get_subtype(): ?string {
311
		return $this->object_subtype;
312
	}
313
314
	/**
315
	* Gets the GET callback for REST requests.
316
	*
317
	* @return null|callable(mixed[]): void
318
	*/
319
	public function get_rest_view(): ?callable {
320
		return $this->callbacks['rest_view'];
321
	}
322
323
	/**
324
	 * Sets the GET callback for REST requests.
325
	 *
326
	 * @return null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void
327
	 */
328
	public function get_rest_update(): ?callable {
329
		return $this->callbacks['rest_update'];
330
	}
331
332
	/**
333
	 * Returns the value type.
334
	 *
335
	 * @return string
336
	 */
337
	public function get_value_type(): string {
338
		return $this->type;
339
	}
340
}
341