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