fields.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('acf_fields') ) : |
| 6 | |
| 7 | class acf_fields { |
| 8 | |
| 9 | /** @var array Contains an array of field type instances */ |
| 10 | var $types = array(); |
| 11 | |
| 12 | |
| 13 | /* |
| 14 | * __construct |
| 15 | * |
| 16 | * This function will setup the class functionality |
| 17 | * |
| 18 | * @type function |
| 19 | * @date 5/03/2014 |
| 20 | * @since 5.4.0 |
| 21 | * |
| 22 | * @param n/a |
| 23 | * @return n/a |
| 24 | */ |
| 25 | |
| 26 | function __construct() { |
| 27 | /* do nothing */ |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * register_field_type |
| 33 | * |
| 34 | * This function will register a field type instance |
| 35 | * |
| 36 | * @type function |
| 37 | * @date 6/07/2016 |
| 38 | * @since 5.4.0 |
| 39 | * |
| 40 | * @param $class (string) |
| 41 | * @return n/a |
| 42 | */ |
| 43 | |
| 44 | function register_field_type( $class ) { |
| 45 | |
| 46 | // allow instance |
| 47 | if( $class instanceOf acf_field ) { |
| 48 | $this->types[ $class->name ] = $class; |
| 49 | |
| 50 | // allow class name |
| 51 | } else { |
| 52 | $instance = new $class(); |
| 53 | $this->types[ $instance->name ] = $instance; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * get_field_type |
| 60 | * |
| 61 | * This function will return a field type instance |
| 62 | * |
| 63 | * @type function |
| 64 | * @date 6/07/2016 |
| 65 | * @since 5.4.0 |
| 66 | * |
| 67 | * @param $name (string) |
| 68 | * @return (mixed) |
| 69 | */ |
| 70 | |
| 71 | function get_field_type( $name ) { |
| 72 | return isset( $this->types[$name] ) ? $this->types[$name] : null; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * is_field_type |
| 78 | * |
| 79 | * This function will return true if a field type exists |
| 80 | * |
| 81 | * @type function |
| 82 | * @date 6/07/2016 |
| 83 | * @since 5.4.0 |
| 84 | * |
| 85 | * @param $name (string) |
| 86 | * @return (mixed) |
| 87 | */ |
| 88 | |
| 89 | function is_field_type( $name ) { |
| 90 | return isset( $this->types[$name] ); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /* |
| 95 | * register_field_type_info |
| 96 | * |
| 97 | * This function will store a basic array of info about the field type |
| 98 | * to later be overriden by the above register_field_type function |
| 99 | * |
| 100 | * @type function |
| 101 | * @date 29/5/17 |
| 102 | * @since 5.6.0 |
| 103 | * |
| 104 | * @param $info (array) |
| 105 | * @return n/a |
| 106 | */ |
| 107 | |
| 108 | function register_field_type_info( $info ) { |
| 109 | |
| 110 | // convert to object |
| 111 | $instance = (object) $info; |
| 112 | $this->types[ $instance->name ] = $instance; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | * get_field_types |
| 118 | * |
| 119 | * This function will return an array of all field types |
| 120 | * |
| 121 | * @type function |
| 122 | * @date 6/07/2016 |
| 123 | * @since 5.4.0 |
| 124 | * |
| 125 | * @param $name (string) |
| 126 | * @return (mixed) |
| 127 | */ |
| 128 | |
| 129 | function get_field_types() { |
| 130 | return $this->types; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // initialize |
| 136 | acf()->fields = new acf_fields(); |
| 137 | |
| 138 | endif; // class_exists check |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * acf_register_field_type |
| 143 | * |
| 144 | * alias of acf()->fields->register_field_type() |
| 145 | * |
| 146 | * @type function |
| 147 | * @date 31/5/17 |
| 148 | * @since 5.6.0 |
| 149 | * |
| 150 | * @param n/a |
| 151 | * @return n/a |
| 152 | */ |
| 153 | |
| 154 | function acf_register_field_type( $class ) { |
| 155 | return acf()->fields->register_field_type( $class ); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | * acf_register_field_type_info |
| 161 | * |
| 162 | * alias of acf()->fields->register_field_type_info() |
| 163 | * |
| 164 | * @type function |
| 165 | * @date 31/5/17 |
| 166 | * @since 5.6.0 |
| 167 | * |
| 168 | * @param n/a |
| 169 | * @return n/a |
| 170 | */ |
| 171 | |
| 172 | function acf_register_field_type_info( $info ) { |
| 173 | return acf()->fields->register_field_type_info( $info ); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | * acf_get_field_type |
| 179 | * |
| 180 | * alias of acf()->fields->get_field_type() |
| 181 | * |
| 182 | * @type function |
| 183 | * @date 31/5/17 |
| 184 | * @since 5.6.0 |
| 185 | * |
| 186 | * @param n/a |
| 187 | * @return n/a |
| 188 | */ |
| 189 | |
| 190 | function acf_get_field_type( $name ) { |
| 191 | return acf()->fields->get_field_type( $name ); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * acf_get_field_types |
| 197 | * |
| 198 | * alias of acf()->fields->get_field_types() |
| 199 | * |
| 200 | * @type function |
| 201 | * @date 31/5/17 |
| 202 | * @since 5.6.0 |
| 203 | * |
| 204 | * @param n/a |
| 205 | * @return n/a |
| 206 | */ |
| 207 | |
| 208 | function acf_get_field_types( $args = array() ) { |
| 209 | |
| 210 | // default |
| 211 | $args = wp_parse_args($args, array( |
| 212 | 'public' => true, // true, false |
| 213 | )); |
| 214 | |
| 215 | // get field types |
| 216 | $field_types = acf()->fields->get_field_types(); |
| 217 | |
| 218 | // filter |
| 219 | return wp_filter_object_list( $field_types, $args ); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * acf_get_field_types_info |
| 225 | * |
| 226 | * Returns an array containing information about each field type |
| 227 | * |
| 228 | * @date 18/6/18 |
| 229 | * @since 5.6.9 |
| 230 | * |
| 231 | * @param type $var Description. Default. |
| 232 | * @return type Description. |
| 233 | */ |
| 234 | |
| 235 | function acf_get_field_types_info( $args = array() ) { |
| 236 | |
| 237 | // vars |
| 238 | $data = array(); |
| 239 | $field_types = acf_get_field_types(); |
| 240 | |
| 241 | // loop |
| 242 | foreach( $field_types as $type ) { |
| 243 | $data[ $type->name ] = array( |
| 244 | 'label' => $type->label, |
| 245 | 'name' => $type->name, |
| 246 | 'category' => $type->category, |
| 247 | 'public' => $type->public |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | // return |
| 252 | return $data; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* |
| 257 | * acf_is_field_type |
| 258 | * |
| 259 | * alias of acf()->fields->is_field_type() |
| 260 | * |
| 261 | * @type function |
| 262 | * @date 31/5/17 |
| 263 | * @since 5.6.0 |
| 264 | * |
| 265 | * @param n/a |
| 266 | * @return n/a |
| 267 | */ |
| 268 | |
| 269 | function acf_is_field_type( $name = '' ) { |
| 270 | return acf()->fields->is_field_type( $name ); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /* |
| 275 | * acf_get_field_type_prop |
| 276 | * |
| 277 | * This function will return a field type's property |
| 278 | * |
| 279 | * @type function |
| 280 | * @date 1/10/13 |
| 281 | * @since 5.0.0 |
| 282 | * |
| 283 | * @param n/a |
| 284 | * @return (array) |
| 285 | */ |
| 286 | |
| 287 | function acf_get_field_type_prop( $name = '', $prop = '' ) { |
| 288 | $type = acf_get_field_type( $name ); |
| 289 | return ($type && isset($type->$prop)) ? $type->$prop : null; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /* |
| 294 | * acf_get_field_type_label |
| 295 | * |
| 296 | * This function will return the label of a field type |
| 297 | * |
| 298 | * @type function |
| 299 | * @date 1/10/13 |
| 300 | * @since 5.0.0 |
| 301 | * |
| 302 | * @param n/a |
| 303 | * @return (array) |
| 304 | */ |
| 305 | |
| 306 | function acf_get_field_type_label( $name = '' ) { |
| 307 | $label = acf_get_field_type_prop( $name, 'label' ); |
| 308 | return $label ? $label : '<span class="acf-tooltip-js" title="'.__('Field type does not exist', 'acf').'">'.__('Unknown', 'acf').'</span>'; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* |
| 313 | * acf_field_type_exists (deprecated) |
| 314 | * |
| 315 | * deprecated in favour of acf_is_field_type() |
| 316 | * |
| 317 | * @type function |
| 318 | * @date 1/10/13 |
| 319 | * @since 5.0.0 |
| 320 | * |
| 321 | * @param $type (string) |
| 322 | * @return (boolean) |
| 323 | */ |
| 324 | |
| 325 | function acf_field_type_exists( $type = '' ) { |
| 326 | return acf_is_field_type( $type ); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /* |
| 331 | * acf_get_grouped_field_types |
| 332 | * |
| 333 | * Returns an multi-dimentional array of field types "name => label" grouped by category |
| 334 | * |
| 335 | * @type function |
| 336 | * @date 1/10/13 |
| 337 | * @since 5.0.0 |
| 338 | * |
| 339 | * @param n/a |
| 340 | * @return (array) |
| 341 | */ |
| 342 | |
| 343 | function acf_get_grouped_field_types() { |
| 344 | |
| 345 | // vars |
| 346 | $types = acf_get_field_types(); |
| 347 | $groups = array(); |
| 348 | $l10n = array( |
| 349 | 'basic' => __('Basic', 'acf'), |
| 350 | 'content' => __('Content', 'acf'), |
| 351 | 'choice' => __('Choice', 'acf'), |
| 352 | 'relational' => __('Relational', 'acf'), |
| 353 | 'jquery' => __('jQuery', 'acf'), |
| 354 | 'layout' => __('Layout', 'acf'), |
| 355 | ); |
| 356 | |
| 357 | |
| 358 | // loop |
| 359 | foreach( $types as $type ) { |
| 360 | |
| 361 | // translate |
| 362 | $cat = $type->category; |
| 363 | $cat = isset( $l10n[$cat] ) ? $l10n[$cat] : $cat; |
| 364 | |
| 365 | // append |
| 366 | $groups[ $cat ][ $type->name ] = $type->label; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | // filter |
| 371 | $groups = apply_filters('acf/get_field_types', $groups); |
| 372 | |
| 373 | |
| 374 | // return |
| 375 | return $groups; |
| 376 | } |
| 377 | |
| 378 | ?> |