PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.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 3 weeks ago Block.php 3 weeks ago Block_Collection.php 3 weeks ago Block_Field.php 3 weeks ago Field.php 3 weeks ago Group.php 3 weeks ago Legacy_Object.php 3 weeks ago Object_Field.php 3 weeks ago Page.php 3 weeks ago Pod.php 3 weeks ago Storage.php 3 weeks ago Store.php 3 weeks ago Template.php 3 weeks ago
Block_Field.php
336 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 $block_args = $field_mapping[ $type ];
125
126 // Handle setting name/label/help.
127 $name = $this->get_arg( 'name' );
128
129 $block_args['name'] = $name;
130
131 $block_args['fieldOptions']['help'] = wp_kses_post( $this->get_arg( 'description' ) );
132
133 if ( 'boolean' !== $type ) {
134 $block_args['fieldOptions']['label'] = $this->get_arg( 'label' );
135
136 $default_value = $this->get_arg( 'default' );
137
138 if ( 'pick' !== $type && ! in_array( $default_value, [ '', null ], true ) ) {
139 $block_args['attributeOptions']['default'] = $default_value;
140 }
141 }
142
143 return $block_args;
144 }
145
146 /**
147 * Get block args for a pick field type.
148 *
149 * @return array Block args.
150 */
151 public function get_pick_block_args() {
152 $format_type = $this->get_arg( 'pick_format_type', 'single' );
153 $format_single = $this->get_arg( 'pick_format_single', 'dropdown' );
154 $format_multi = $this->get_arg( 'pick_format_multi', 'checkbox' );
155
156 // Support raw data for now.
157 $raw_data = (array) $this->get_arg( 'data', [] );
158 $data = [];
159
160 foreach ( $raw_data as $key => $item ) {
161 if ( ! is_array( $item ) ) {
162 $item = [
163 'label' => $item,
164 'value' => $key,
165 ];
166 }
167
168 if ( ! isset( $item['label'], $item['value'] ) ) {
169 continue;
170 }
171
172 $data[] = $item;
173 }
174
175 $label = $this->get_arg( 'label' );
176 $default = $this->get_arg( 'default', '' );
177
178 if ( 'single' === $format_type ) {
179 if ( 'radio' === $format_single ) {
180 return [
181 'type' => 'RadioControl',
182 'fieldOptions' => [
183 'heading' => $label,
184 'options' => $data,
185 ],
186 'attributeOptions' => [
187 'type' => 'string',
188 'default' => $default,
189 ],
190 ];
191 }
192
193 foreach ( $data as $data_value ) {
194 if ( $default === $data_value['value'] ) {
195 $default = $data_value;
196
197 break;
198 }
199 }
200
201 return [
202 'type' => 'SelectControl',
203 'fieldOptions' => [
204 'heading' => $label,
205 'options' => $data,
206 ],
207 'attributeOptions' => [
208 'type' => 'object',
209 'default' => $default,
210 ],
211 ];
212 }
213
214 if ( in_array( $format_multi, [ 'multiselect', 'autocomplete' ], true ) ) {
215 return [
216 'type' => 'SelectControl',
217 'fieldOptions' => [
218 'multiple' => true,
219 'heading' => $label,
220 'options' => $data,
221 ],
222 'attributeOptions' => [
223 'type' => 'array',
224 ],
225 ];
226 }
227
228 return [
229 'type' => 'CheckboxGroup',
230 'name' => 'checkboxGroup',
231 'fieldOptions' => [
232 'heading' => $label,
233 'options' => $data,
234 ],
235 'attributeOptions' => [
236 'type' => 'array',
237 ],
238 ];
239 }
240
241 /**
242 * Get block args for a boolean field type.
243 *
244 * @return array Block args.
245 */
246 public function get_boolean_block_args() {
247 $format_type = $this->get_arg( 'boolean_format_type', 'checkbox' );
248
249 $data = [
250 [
251 'label' => $this->get_arg( 'boolean_yes_label', __( 'Yes', 'pods' ) ),
252 'value' => 1,
253 ],
254 [
255 'label' => $this->get_arg( 'boolean_no_label', __( 'No', 'pods' ) ),
256 'value' => 0,
257 ],
258 ];
259
260 $label = $this->get_arg( 'label' );
261 $default = (boolean) $this->get_arg( 'default', 0 );
262
263 if ( 'radio' === $format_type ) {
264 return [
265 'type' => 'RadioControl',
266 'fieldOptions' => [
267 'heading' => $label,
268 'options' => $data,
269 ],
270 'attributeOptions' => [
271 'type' => 'string',
272 'default' => $default,
273 ],
274 ];
275 }
276
277 if ( 'dropdown' === $format_type ) {
278 return [
279 'type' => 'SelectControl',
280 'fieldOptions' => [
281 'heading' => $label,
282 'options' => $data,
283 ],
284 'attributeOptions' => [
285 'type' => 'object',
286 'default' => $default,
287 ],
288 ];
289 }
290
291 return [
292 'type' => 'CheckboxControl',
293 'fieldOptions' => [
294 'heading' => $label,
295 'label' => $data[0]['label'],
296 ],
297 'attributeOptions' => [
298 'type' => 'boolean',
299 'default' => $default,
300 ],
301 ];
302 }
303
304 /**
305 * {@inheritdoc}
306 */
307 public function get_table_info() {
308 return [];
309 }
310
311 /**
312 * {@inheritdoc}
313 */
314 public function get_arg( $arg, $default = null, $strict = false ) {
315 if ( 'block' === $arg ) {
316 return $this->get_parent_name();
317 }
318
319 return Whatsit::get_arg( $arg, $default );
320 }
321
322 /**
323 * {@inheritdoc}
324 */
325 public function get_related_object_type() {
326 return null;
327 }
328
329 /**
330 * {@inheritdoc}
331 */
332 public function get_related_object_name() {
333 return null;
334 }
335 }
336