PluginProbe
Advanced Custom Fields (ACF®) / 6.5.1
Advanced Custom Fields (ACF®) v6.5.1
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / fields.php

fields.php in Advanced Custom Fields (ACF®) 6.5.1, at includes/fields.php

457 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'acf_fields' ) ) :
8 class acf_fields {
9
10 /**
11 * Contains an array of field type instances.
12 * @var array
13 */
14 public $types = array();
15
16 /**
17 * This function will setup the class functionality
18 *
19 * @type function
20 * @date 5/03/2014
21 * @since 5.4.0
22 *
23 * @param n/a
24 * @return n/a
25 */
26 function __construct() {
27 /* do nothing */
28 }
29
30 /**
31 * This function will register a field type instance based on a class name or instance.
32 * It will return the instance for further use.
33 *
34 * @since 5.4.0
35 *
36 * @param mixed $field_class Either a class name (string) or instance of acf_field.
37 * @return acf_field The instance of acf_field.
38 */
39 public function register_field_type( $field_class ) {
40 // Allow registering an instance.
41 if ( $field_class instanceof acf_field ) {
42 $this->types[ $field_class->name ] = $field_class;
43 return $field_class;
44 }
45
46 // Allow registering a loaded class name.
47 $instance = new $field_class();
48 $this->types[ $instance->name ] = $instance;
49 return $instance;
50 }
51
52 /**
53 * This function will return a field type instance
54 *
55 * @type function
56 * @date 6/07/2016
57 * @since 5.4.0
58 *
59 * @param $name (string)
60 * @return (mixed)
61 */
62 function get_field_type( $name ) {
63 return isset( $this->types[ $name ] ) ? $this->types[ $name ] : null;
64 }
65
66
67 /**
68 * This function will return true if a field type exists
69 *
70 * @type function
71 * @date 6/07/2016
72 * @since 5.4.0
73 *
74 * @param $name (string)
75 * @return (mixed)
76 */
77 function is_field_type( $name ) {
78 return isset( $this->types[ $name ] );
79 }
80
81
82 /**
83 * This function will store a basic array of info about the field type
84 * to later be overriden by the above register_field_type function
85 *
86 * @type function
87 * @date 29/5/17
88 * @since 5.6.0
89 *
90 * @param $info (array)
91 * @return n/a
92 */
93 function register_field_type_info( $info ) {
94
95 // convert to object
96 $instance = (object) $info;
97 $this->types[ $instance->name ] = $instance;
98 }
99
100
101 /**
102 * This function will return an array of all field types
103 *
104 * @type function
105 * @date 6/07/2016
106 * @since 5.4.0
107 *
108 * @param $name (string)
109 * @return (mixed)
110 */
111 function get_field_types() {
112 return $this->types;
113 }
114 }
115
116
117 // initialize
118 acf()->fields = new acf_fields();
119 endif; // class_exists check
120
121
122 /**
123 * alias of acf()->fields->register_field_type()
124 *
125 * @type function
126 * @date 31/5/17
127 * @since 5.6.0
128 *
129 * @param n/a
130 * @return n/a
131 */
132 function acf_register_field_type( $class ) {
133 return acf()->fields->register_field_type( $class );
134 }
135
136
137 /**
138 * alias of acf()->fields->register_field_type_info()
139 *
140 * @type function
141 * @date 31/5/17
142 * @since 5.6.0
143 *
144 * @param n/a
145 * @return n/a
146 */
147 function acf_register_field_type_info( $info ) {
148 return acf()->fields->register_field_type_info( $info );
149 }
150
151
152 /**
153 * alias of acf()->fields->get_field_type()
154 *
155 * @type function
156 * @date 31/5/17
157 * @since 5.6.0
158 *
159 * @param n/a
160 * @return n/a
161 */
162 function acf_get_field_type( $name ) {
163 return acf()->fields->get_field_type( $name );
164 }
165
166
167 /**
168 * alias of acf()->fields->get_field_types()
169 *
170 * @type function
171 * @date 31/5/17
172 * @since 5.6.0
173 *
174 * @param n/a
175 * @return n/a
176 */
177 function acf_get_field_types( $args = array() ) {
178
179 // default
180 $args = wp_parse_args(
181 $args,
182 array(
183 'public' => true, // true, false
184 )
185 );
186
187 // get field types
188 $field_types = acf()->fields->get_field_types();
189
190 // filter
191 return wp_filter_object_list( $field_types, $args );
192 }
193
194
195 /**
196 * acf_get_field_types_info
197 *
198 * Returns an array containing information about each field type
199 *
200 * @date 18/6/18
201 * @since 5.6.9
202 *
203 * @param type $var Description. Default.
204 * @return type Description.
205 */
206 function acf_get_field_types_info( $args = array() ) {
207
208 // vars
209 $data = array();
210 $field_types = acf_get_field_types();
211
212 // loop
213 foreach ( $field_types as $type ) {
214 $data[ $type->name ] = array_filter(
215 array(
216 'label' => $type->label,
217 'name' => $type->name,
218 'description' => $type->description,
219 'category' => $type->category,
220 'public' => $type->public,
221 'doc_url' => $type->doc_url,
222 'tutorial_url' => $type->tutorial_url,
223 'preview_image' => $type->preview_image,
224 'pro' => $type->pro,
225 )
226 );
227 }
228
229 // return
230 return $data;
231 }
232
233
234 /**
235 * alias of acf()->fields->is_field_type()
236 *
237 * @type function
238 * @date 31/5/17
239 * @since 5.6.0
240 *
241 * @param n/a
242 * @return n/a
243 */
244 function acf_is_field_type( $name = '' ) {
245 return acf()->fields->is_field_type( $name );
246 }
247
248
249 /**
250 * This function will return a field type's property
251 *
252 * @type function
253 * @date 1/10/13
254 * @since 5.0.0
255 *
256 * @param n/a
257 * @return (array)
258 */
259 function acf_get_field_type_prop( $name = '', $prop = '' ) {
260 $type = acf_get_field_type( $name );
261 return ( $type && isset( $type->$prop ) ) ? $type->$prop : null;
262 }
263
264
265 /**
266 * This function will return the label of a field type
267 *
268 * @type function
269 * @date 1/10/13
270 * @since 5.0.0
271 *
272 * @param n/a
273 * @return (array)
274 */
275 function acf_get_field_type_label( $name = '' ) {
276 $label = acf_get_field_type_prop( $name, 'label' );
277 return $label ? $label : '<span class="acf-js-tooltip" title="' . __( 'Field type does not exist', 'acf' ) . '">' . __( 'Unknown', 'acf' ) . '</span>';
278 }
279
280 /**
281 * Returns the value of a field type "supports" property.
282 *
283 * @since 6.2.5
284 *
285 * @param string $name The name of the field type.
286 * @param string $prop The name of the supports property.
287 * @param mixed $default The default value if the property is not set.
288 *
289 * @return mixed The value of the supports property which may be false, or $default on failure.
290 */
291 function acf_field_type_supports( $name = '', $prop = '', $default = false ) {
292 $supports = acf_get_field_type_prop( $name, 'supports' );
293 if ( ! is_array( $supports ) ) {
294 return $default;
295 }
296 return isset( $supports[ $prop ] ) ? $supports[ $prop ] : $default;
297 }
298
299
300 /**
301 *
302 * @deprecated
303 * @see acf_is_field_type()
304 *
305 * @type function
306 * @date 1/10/13
307 * @since 5.0.0
308 *
309 * @param $type (string)
310 * @return (boolean)
311 */
312 function acf_field_type_exists( $type = '' ) {
313 return acf_is_field_type( $type );
314 }
315
316 /**
317 * Returns an array of localised field categories.
318 *
319 * @since 6.1
320 *
321 * @return array
322 */
323 function acf_get_field_categories_i18n() {
324
325 $categories_i18n = array(
326 'basic' => __( 'Basic', 'acf' ),
327 'content' => __( 'Content', 'acf' ),
328 'choice' => __( 'Choice', 'acf' ),
329 'relational' => __( 'Relational', 'acf' ),
330 'advanced' => __( 'Advanced', 'acf' ),
331 'layout' => __( 'Layout', 'acf' ),
332 'pro' => __( 'PRO', 'acf' ),
333 );
334
335 return apply_filters( 'acf/localized_field_categories', $categories_i18n );
336 }
337
338
339 /**
340 * Returns an multi-dimentional array of field types "name => label" grouped by category
341 *
342 * @since 5.0.0
343 *
344 * @return array
345 */
346 function acf_get_grouped_field_types() {
347
348 // vars
349 $types = acf_get_field_types();
350 $groups = array();
351 $l10n = acf_get_field_categories_i18n();
352
353 // loop
354 foreach ( $types as $type ) {
355
356 // translate
357 $cat = $type->category;
358 $cat = isset( $l10n[ $cat ] ) ? $l10n[ $cat ] : $cat;
359
360 // append
361 $groups[ $cat ][ $type->name ] = $type->label;
362 }
363
364 // filter
365 $groups = apply_filters( 'acf/get_field_types', $groups );
366
367 // return
368 return $groups;
369 }
370
371 /**
372 * Returns an array of tabs for a field type.
373 * We combine a list of default tabs with filtered tabs.
374 * I.E. Default tabs should be static and should not be changed by the
375 * filtered tabs.
376 *
377 * @since 6.1
378 *
379 * @return array Key/value array of the default settings tabs for field type settings.
380 */
381 function acf_get_combined_field_type_settings_tabs() {
382 $default_field_type_settings_tabs = array(
383 'general' => __( 'General', 'acf' ),
384 'validation' => __( 'Validation', 'acf' ),
385 'presentation' => __( 'Presentation', 'acf' ),
386 'conditional_logic' => __( 'Conditional Logic', 'acf' ),
387 'advanced' => __( 'Advanced', 'acf' ),
388 );
389
390 $field_type_settings_tabs = (array) apply_filters( 'acf/field_group/additional_field_settings_tabs', array() );
391
392 // remove any default tab values from filter tabs.
393 foreach ( $field_type_settings_tabs as $key => $tab ) {
394 if ( isset( $default_field_type_settings_tabs[ $key ] ) ) {
395 unset( $field_type_settings_tabs[ $key ] );
396 }
397 }
398
399 $combined_field_type_settings_tabs = array_merge( $default_field_type_settings_tabs, $field_type_settings_tabs );
400
401 return $combined_field_type_settings_tabs;
402 }
403
404
405
406 /**
407 * Get the PRO only fields and their core metadata.
408 *
409 * @since 6.1
410 *
411 * @return array An array of all the pro field types and their field type selection required meta data.
412 */
413 function acf_get_pro_field_types() {
414 return array(
415 'clone' => array(
416 'name' => 'clone',
417 'label' => _x( 'Clone', 'noun', 'acf' ),
418 'doc_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/clone/', 'docs', 'field-type-selection' ),
419 'preview_image' => acf_get_url() . '/assets/images/field-type-previews/field-preview-clone.png',
420 'description' => __( 'This allows you to select and display existing fields. It does not duplicate any fields in the database, but loads and displays the selected fields at run-time. The Clone field can either replace itself with the selected fields or display the selected fields as a group of subfields.', 'acf' ),
421 'tutorial_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/how-to-use-the-clone-field/', 'docs', 'field-type-selection' ),
422 'category' => 'layout',
423 'pro' => true,
424 ),
425 'flexible_content' => array(
426 'name' => 'flexible_content',
427 'label' => __( 'Flexible Content', 'acf' ),
428 'doc_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/flexible-content/', 'docs', 'field-type-selection' ),
429 'preview_image' => acf_get_url() . '/assets/images/field-type-previews/field-preview-flexible-content.png',
430 'description' => __( 'This provides a simple, structured, layout-based editor. The Flexible Content field allows you to define, create and manage content with total control by using layouts and subfields to design the available blocks.', 'acf' ),
431 'tutorial_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/building-layouts-with-the-flexible-content-field-in-a-theme/', 'docs', 'field-type-selection' ),
432 'category' => 'layout',
433 'pro' => true,
434 ),
435 'gallery' => array(
436 'name' => 'gallery',
437 'label' => __( 'Gallery', 'acf' ),
438 'doc_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/gallery/', 'docs', 'field-type-selection' ),
439 'preview_image' => acf_get_url() . '/assets/images/field-type-previews/field-preview-gallery.png',
440 'description' => __( 'This provides an interactive interface for managing a collection of attachments. Most settings are similar to the Image field type. Additional settings allow you to specify where new attachments are added in the gallery and the minimum/maximum number of attachments allowed.', 'acf' ),
441 'tutorial_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/how-to-use-the-gallery-field/', 'docs', 'field-type-selection' ),
442 'category' => 'content',
443 'pro' => true,
444 ),
445 'repeater' => array(
446 'name' => 'repeater',
447 'label' => __( 'Repeater', 'acf' ),
448 'doc_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/repeater/', 'docs', 'field-type-selection' ),
449 'preview_image' => acf_get_url() . '/assets/images/field-type-previews/field-preview-repeater.png',
450 'description' => __( 'This provides a solution for repeating content such as slides, team members, and call-to-action tiles, by acting as a parent to a set of subfields which can be repeated again and again.', 'acf' ),
451 'tutorial_url' => acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/repeater/how-to-use-the-repeater-field/', 'docs', 'field-type-selection' ),
452 'category' => 'layout',
453 'pro' => true,
454 ),
455 );
456 }
457