PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / trunk
Pods – Custom Content Types and Fields vtrunk
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Whatsit / Block_Field.php
pods / src / Pods / Whatsit Last commit date
Storage 4 months ago Block.php 4 months ago Block_Collection.php 4 months ago Block_Field.php 4 months ago Field.php 4 months ago Group.php 4 months ago Legacy_Object.php 4 months ago Object_Field.php 4 months ago Page.php 4 months ago Pod.php 4 months ago Storage.php 4 months ago Store.php 4 months ago Template.php 4 months ago
Block_Field.php
385 lines
1 <?php
2
3 namespace Pods\Whatsit;
4
5 // Don't load directly.
6 if ( ! defined( 'ABSPATH' ) ) {
7 die( '-1' );
8 }
9
10 use Pods\Whatsit;
11
12 /**
13 * Block_Field class.
14 *
15 * @since 2.8.0
16 */
17 class Block_Field extends Field {
18
19 /**
20 * {@inheritdoc}
21 */
22 protected static $type = 'block-field';
23
24 /**
25 * Get list of block args used for each field type.
26 *
27 * @since 2.8.0
28 *
29 * @return array[] List of block args used for each field type.
30 */
31 protected function get_block_arg_mapping() {
32 return [
33 'text' => [
34 'type' => 'TextControl',
35 'fieldOptions' => [
36 'className' => 'text__container',
37 'type' => 'text',
38 ],
39 'attributeOptions' => [
40 'type' => 'string',
41 ],
42 ],
43 'paragraph' => [
44 'type' => 'TextareaControl',
45 'fieldOptions' => [
46 'className' => 'textarea__container',
47 'auto_p' => true,
48 ],
49 'attributeOptions' => [
50 'type' => 'string',
51 ],
52 ],
53 // @todo Add support for RichText at a later time.
54 // [
55 // 'type' => 'RichText',
56 // 'name' => 'richTextField',
57 // 'fieldOptions' => [
58 // 'tagName' => 'p',
59 // 'className' => 'custom__container',
60 // 'label' => 'Label for the richtext field',
61 // ],
62 // 'attributeOptions' => [
63 // 'type' => 'string',
64 // ],
65 // ],
66 'datetime' => [
67 'type' => 'DateTimePicker',
68 'fieldOptions' => [
69 'is12Hour' => true,
70 ],
71 'attributeOptions' => [
72 'type' => 'string',
73 ],
74 ],
75 'number' => [
76 'type' => 'NumberControl',
77 'fieldOptions' => [
78 'isShiftStepEnabled' => false,
79 'shiftStep' => false,
80 'step' => 1,
81 ],
82 'attributeOptions' => [
83 'type' => 'number',
84 ],
85 ],
86 'file' => [
87 'type' => 'MediaUpload',
88 'fieldOptions' => [],
89 'attributeOptions' => [
90 'type' => 'object',
91 ],
92 ],
93 'color' => [
94 'type' => 'ColorPicker',
95 'fieldOptions' => [],
96 'attributeOptions' => [
97 'type' => 'string',
98 ],
99 ],
100 'html' => [
101 'type' => 'html',
102 'fieldOptions' => [
103 'className' => 'text__container',
104 'type' => 'text',
105 ],
106 'attributeOptions' => [
107 'type' => 'string',
108 ],
109 ],
110 ];
111 }
112
113 /**
114 * Get list of Block API arguments to use.
115 *
116 * @since 2.8.0
117 *
118 * @return array|null List of Block API arguments or null if not valid.
119 */
120 public function get_block_args() {
121 $field_mapping = $this->get_block_arg_mapping();
122
123 $type = $this->get_arg( 'type' );
124
125 if ( 'pick' === $type ) {
126 $field_mapping[ $type ] = $this->get_pick_block_args();
127 } elseif ( 'boolean' === $type ) {
128 $field_mapping[ $type ] = $this->get_boolean_block_args();
129 }
130
131 if ( ! isset( $field_mapping[ $type ] ) ) {
132 return null;
133 }
134
135 if ( 'file' === $type && 'multi' === $this->get_arg( 'file_format_type' ) ) {
136 return null;
137 }
138
139 if (
140 isset( $field_mapping['data'] )
141 && (
142 is_string( $field_mapping['data'] )
143 || is_object( $field_mapping['data'] )
144 || (
145 is_array( $field_mapping['data'] )
146 && 2 === count( $field_mapping['data'] )
147 && isset( $field_mapping['data'][0], $field_mapping['data'][1] )
148 && is_object( $field_mapping['data'][0] )
149 && is_string( $field_mapping['data'][1] )
150 )
151 )
152 && is_callable( $field_mapping['data'] )
153 ) {
154 $field_mapping['data'] = call_user_func( $field_mapping['data'] );
155 }
156
157 $block_args = $field_mapping[ $type ];
158
159 // Handle setting name/label/help.
160 $name = $this->get_arg( 'name' );
161
162 $block_args['name'] = $name;
163
164 $block_args['fieldOptions']['help'] = $this->get_arg( 'description' );
165
166 if ( 'html' === $type ) {
167 $block_args['fieldOptions']['html_content'] = $this->get_arg( 'html_content' );
168 }
169
170 if ( 'boolean' !== $type ) {
171 $block_args['fieldOptions']['label'] = $this->get_arg( 'label' );
172
173 $default_value = $this->get_arg( 'default' );
174
175 if ( 'pick' !== $type && ! in_array( $default_value, [ '', null ], true ) ) {
176 $block_args['attributeOptions']['default'] = $default_value;
177 }
178 }
179
180 return $block_args;
181 }
182
183 /**
184 * Get block args for a pick field type.
185 *
186 * @return array Block args.
187 */
188 public function get_pick_block_args() {
189 $format_type = $this->get_arg( 'pick_format_type', 'single' );
190 $format_single = $this->get_arg( 'pick_format_single', 'dropdown' );
191 $format_multi = $this->get_arg( 'pick_format_multi', 'checkbox' );
192
193 // Support raw data for now.
194 $raw_data = $this->get_arg( 'data', [] );
195 $data = [];
196
197 if ( ! is_array( $raw_data ) ) {
198 // Support string callables.
199 if ( is_callable( $raw_data ) ) {
200 $raw_data = $raw_data();
201 } else {
202 $raw_data = [];
203 }
204 } elseif ( isset( $raw_data[0] ) && is_object( $raw_data[0] ) && is_callable( $raw_data ) ) {
205 // Support array callables if first item is an object.
206 $raw_data = $raw_data();
207 }
208
209 foreach ( $raw_data as $key => $item ) {
210 if ( ! is_array( $item ) ) {
211 $item = [
212 'label' => $item,
213 'value' => $key,
214 ];
215 }
216
217 if ( ! isset( $item['label'], $item['value'] ) ) {
218 continue;
219 }
220
221 $data[] = $item;
222 }
223
224 $label = $this->get_arg( 'label' );
225 $default = $this->get_arg( 'default', '' );
226
227 if ( 'single' === $format_type ) {
228 if ( 'radio' === $format_single ) {
229 return [
230 'type' => 'RadioControl',
231 'fieldOptions' => [
232 'heading' => $label,
233 'options' => $data,
234 ],
235 'attributeOptions' => [
236 'type' => 'string',
237 'default' => $default,
238 ],
239 ];
240 }
241
242 foreach ( $data as $data_value ) {
243 if ( $default === $data_value['value'] ) {
244 $default = $data_value;
245
246 break;
247 }
248 }
249
250 return [
251 'type' => 'SelectControl',
252 'fieldOptions' => [
253 'heading' => $label,
254 'options' => $data,
255 ],
256 'attributeOptions' => [
257 'type' => 'object',
258 'default' => $default,
259 ],
260 ];
261 }
262
263 if ( in_array( $format_multi, [ 'multiselect', 'autocomplete' ], true ) ) {
264 return [
265 'type' => 'SelectControl',
266 'fieldOptions' => [
267 'multiple' => true,
268 'heading' => $label,
269 'options' => $data,
270 ],
271 'attributeOptions' => [
272 'type' => 'array',
273 ],
274 ];
275 }
276
277 return [
278 'type' => 'CheckboxGroup',
279 'name' => 'checkboxGroup',
280 'fieldOptions' => [
281 'heading' => $label,
282 'options' => $data,
283 ],
284 'attributeOptions' => [
285 'type' => 'array',
286 ],
287 ];
288 }
289
290 /**
291 * Get block args for a boolean field type.
292 *
293 * @return array Block args.
294 */
295 public function get_boolean_block_args() {
296 $format_type = $this->get_arg( 'boolean_format_type', 'checkbox' );
297
298 $data = [
299 [
300 'label' => $this->get_arg( 'boolean_yes_label', __( 'Yes', 'pods' ) ),
301 'value' => 1,
302 ],
303 [
304 'label' => $this->get_arg( 'boolean_no_label', __( 'No', 'pods' ) ),
305 'value' => 0,
306 ],
307 ];
308
309 $label = $this->get_arg( 'label' );
310 $default = (boolean) $this->get_arg( 'default', 0 );
311
312 if ( 'radio' === $format_type ) {
313 return [
314 'type' => 'RadioControl',
315 'fieldOptions' => [
316 'heading' => $label,
317 'options' => $data,
318 ],
319 'attributeOptions' => [
320 'type' => 'string',
321 'default' => $default,
322 ],
323 ];
324 }
325
326 if ( 'dropdown' === $format_type ) {
327 return [
328 'type' => 'SelectControl',
329 'fieldOptions' => [
330 'heading' => $label,
331 'options' => $data,
332 ],
333 'attributeOptions' => [
334 'type' => 'object',
335 'default' => $default,
336 ],
337 ];
338 }
339
340 return [
341 'type' => 'CheckboxControl',
342 'fieldOptions' => [
343 'heading' => $label,
344 'label' => $data[0]['label'],
345 ],
346 'attributeOptions' => [
347 'type' => 'boolean',
348 'default' => $default,
349 ],
350 ];
351 }
352
353 /**
354 * {@inheritdoc}
355 */
356 public function get_table_info() {
357 return [];
358 }
359
360 /**
361 * {@inheritdoc}
362 */
363 public function get_arg( $arg, $default = null, $strict = false, $raw = false ) {
364 if ( 'block' === $arg ) {
365 return $this->get_parent_name();
366 }
367
368 return Whatsit::get_arg( $arg, $default, $strict, $raw );
369 }
370
371 /**
372 * {@inheritdoc}
373 */
374 public function get_related_object_type() {
375 return null;
376 }
377
378 /**
379 * {@inheritdoc}
380 */
381 public function get_related_object_name() {
382 return null;
383 }
384 }
385