PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.0.10.4
Pods – Custom Content Types and Fields v3.0.10.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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 weeks ago Block.php 2 weeks ago Block_Collection.php 2 weeks ago Block_Field.php 2 weeks ago Field.php 2 weeks ago Group.php 2 weeks ago Legacy_Object.php 2 weeks ago Object_Field.php 2 weeks ago Page.php 2 weeks ago Pod.php 2 weeks ago Storage.php 2 weeks ago Store.php 2 weeks ago Template.php 2 weeks ago
Block_Field.php
366 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 ];
96 }
97
98 /**
99 * Get list of Block API arguments to use.
100 *
101 * @since 2.8.0
102 *
103 * @return array|null List of Block API arguments or null if not valid.
104 */
105 public function get_block_args() {
106 $field_mapping = $this->get_block_arg_mapping();
107
108 $type = $this->get_arg( 'type' );
109
110 if ( 'pick' === $type ) {
111 $field_mapping[ $type ] = $this->get_pick_block_args();
112 } elseif ( 'boolean' === $type ) {
113 $field_mapping[ $type ] = $this->get_boolean_block_args();
114 }
115
116 if ( ! isset( $field_mapping[ $type ] ) ) {
117 return null;
118 }
119
120 if ( 'file' === $type && 'multi' === $this->get_arg( 'file_format_type' ) ) {
121 return null;
122 }
123
124 if (
125 isset( $field_mapping['data'] )
126 && (
127 is_string( $field_mapping['data'] )
128 || is_object( $field_mapping['data'] )
129 || (
130 is_array( $field_mapping['data'] )
131 && 2 === count( $field_mapping['data'] )
132 && isset( $field_mapping['data'][0], $field_mapping['data'][1] )
133 && is_object( $field_mapping['data'][0] )
134 && is_string( $field_mapping['data'][1] )
135 )
136 )
137 && is_callable( $field_mapping['data'] )
138 ) {
139 $field_mapping['data'] = call_user_func( $field_mapping['data'] );
140 }
141
142 $block_args = $field_mapping[ $type ];
143
144 // Handle setting name/label/help.
145 $name = $this->get_arg( 'name' );
146
147 $block_args['name'] = $name;
148
149 $block_args['fieldOptions']['help'] = wp_kses_post( $this->get_arg( 'description' ) );
150
151 if ( 'boolean' !== $type ) {
152 $block_args['fieldOptions']['label'] = $this->get_arg( 'label' );
153
154 $default_value = $this->get_arg( 'default' );
155
156 if ( 'pick' !== $type && ! in_array( $default_value, [ '', null ], true ) ) {
157 $block_args['attributeOptions']['default'] = $default_value;
158 }
159 }
160
161 return $block_args;
162 }
163
164 /**
165 * Get block args for a pick field type.
166 *
167 * @return array Block args.
168 */
169 public function get_pick_block_args() {
170 $format_type = $this->get_arg( 'pick_format_type', 'single' );
171 $format_single = $this->get_arg( 'pick_format_single', 'dropdown' );
172 $format_multi = $this->get_arg( 'pick_format_multi', 'checkbox' );
173
174 // Support raw data for now.
175 $raw_data = $this->get_arg( 'data', [] );
176 $data = [];
177
178 if ( ! is_array( $raw_data ) ) {
179 // Support string callables.
180 if ( is_callable( $raw_data ) ) {
181 $raw_data = $raw_data();
182 } else {
183 $raw_data = [];
184 }
185 } elseif ( isset( $raw_data[0] ) && is_object( $raw_data[0] ) && is_callable( $raw_data ) ) {
186 // Support array callables if first item is an object.
187 $raw_data = $raw_data();
188 }
189
190 foreach ( $raw_data as $key => $item ) {
191 if ( ! is_array( $item ) ) {
192 $item = [
193 'label' => $item,
194 'value' => $key,
195 ];
196 }
197
198 if ( ! isset( $item['label'], $item['value'] ) ) {
199 continue;
200 }
201
202 $data[] = $item;
203 }
204
205 $label = $this->get_arg( 'label' );
206 $default = $this->get_arg( 'default', '' );
207
208 if ( 'single' === $format_type ) {
209 if ( 'radio' === $format_single ) {
210 return [
211 'type' => 'RadioControl',
212 'fieldOptions' => [
213 'heading' => $label,
214 'options' => $data,
215 ],
216 'attributeOptions' => [
217 'type' => 'string',
218 'default' => $default,
219 ],
220 ];
221 }
222
223 foreach ( $data as $data_value ) {
224 if ( $default === $data_value['value'] ) {
225 $default = $data_value;
226
227 break;
228 }
229 }
230
231 return [
232 'type' => 'SelectControl',
233 'fieldOptions' => [
234 'heading' => $label,
235 'options' => $data,
236 ],
237 'attributeOptions' => [
238 'type' => 'object',
239 'default' => $default,
240 ],
241 ];
242 }
243
244 if ( in_array( $format_multi, [ 'multiselect', 'autocomplete' ], true ) ) {
245 return [
246 'type' => 'SelectControl',
247 'fieldOptions' => [
248 'multiple' => true,
249 'heading' => $label,
250 'options' => $data,
251 ],
252 'attributeOptions' => [
253 'type' => 'array',
254 ],
255 ];
256 }
257
258 return [
259 'type' => 'CheckboxGroup',
260 'name' => 'checkboxGroup',
261 'fieldOptions' => [
262 'heading' => $label,
263 'options' => $data,
264 ],
265 'attributeOptions' => [
266 'type' => 'array',
267 ],
268 ];
269 }
270
271 /**
272 * Get block args for a boolean field type.
273 *
274 * @return array Block args.
275 */
276 public function get_boolean_block_args() {
277 $format_type = $this->get_arg( 'boolean_format_type', 'checkbox' );
278
279 $data = [
280 [
281 'label' => $this->get_arg( 'boolean_yes_label', __( 'Yes', 'pods' ) ),
282 'value' => 1,
283 ],
284 [
285 'label' => $this->get_arg( 'boolean_no_label', __( 'No', 'pods' ) ),
286 'value' => 0,
287 ],
288 ];
289
290 $label = $this->get_arg( 'label' );
291 $default = (boolean) $this->get_arg( 'default', 0 );
292
293 if ( 'radio' === $format_type ) {
294 return [
295 'type' => 'RadioControl',
296 'fieldOptions' => [
297 'heading' => $label,
298 'options' => $data,
299 ],
300 'attributeOptions' => [
301 'type' => 'string',
302 'default' => $default,
303 ],
304 ];
305 }
306
307 if ( 'dropdown' === $format_type ) {
308 return [
309 'type' => 'SelectControl',
310 'fieldOptions' => [
311 'heading' => $label,
312 'options' => $data,
313 ],
314 'attributeOptions' => [
315 'type' => 'object',
316 'default' => $default,
317 ],
318 ];
319 }
320
321 return [
322 'type' => 'CheckboxControl',
323 'fieldOptions' => [
324 'heading' => $label,
325 'label' => $data[0]['label'],
326 ],
327 'attributeOptions' => [
328 'type' => 'boolean',
329 'default' => $default,
330 ],
331 ];
332 }
333
334 /**
335 * {@inheritdoc}
336 */
337 public function get_table_info() {
338 return [];
339 }
340
341 /**
342 * {@inheritdoc}
343 */
344 public function get_arg( $arg, $default = null, $strict = false, $raw = false ) {
345 if ( 'block' === $arg ) {
346 return $this->get_parent_name();
347 }
348
349 return Whatsit::get_arg( $arg, $default, $strict, $raw );
350 }
351
352 /**
353 * {@inheritdoc}
354 */
355 public function get_related_object_type() {
356 return null;
357 }
358
359 /**
360 * {@inheritdoc}
361 */
362 public function get_related_object_name() {
363 return null;
364 }
365 }
366