compatibility.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('ACF_Compatibility') ) : |
| 6 | |
| 7 | class ACF_Compatibility { |
| 8 | |
| 9 | /** |
| 10 | * __construct |
| 11 | * |
| 12 | * Sets up the class functionality. |
| 13 | * |
| 14 | * @date 30/04/2014 |
| 15 | * @since 5.0.0 |
| 16 | * |
| 17 | * @param void |
| 18 | * @return void |
| 19 | */ |
| 20 | function __construct() { |
| 21 | |
| 22 | // actions |
| 23 | add_filter('acf/validate_field', array($this, 'validate_field'), 20, 1); |
| 24 | add_filter('acf/validate_field/type=textarea', array($this, 'validate_textarea_field'), 20, 1); |
| 25 | add_filter('acf/validate_field/type=relationship', array($this, 'validate_relationship_field'), 20, 1); |
| 26 | add_filter('acf/validate_field/type=post_object', array($this, 'validate_relationship_field'), 20, 1); |
| 27 | add_filter('acf/validate_field/type=page_link', array($this, 'validate_relationship_field'), 20, 1); |
| 28 | add_filter('acf/validate_field/type=image', array($this, 'validate_image_field'), 20, 1); |
| 29 | add_filter('acf/validate_field/type=file', array($this, 'validate_image_field'), 20, 1); |
| 30 | add_filter('acf/validate_field/type=wysiwyg', array($this, 'validate_wysiwyg_field'), 20, 1); |
| 31 | add_filter('acf/validate_field/type=date_picker', array($this, 'validate_date_picker_field'), 20, 1); |
| 32 | add_filter('acf/validate_field/type=taxonomy', array($this, 'validate_taxonomy_field'), 20, 1); |
| 33 | add_filter('acf/validate_field/type=date_time_picker', array($this, 'validate_date_time_picker_field'), 20, 1); |
| 34 | add_filter('acf/validate_field/type=user', array($this, 'validate_user_field'), 20, 1); |
| 35 | add_filter('acf/validate_field_group', array($this, 'validate_field_group'), 20, 1); |
| 36 | |
| 37 | // Modify field wrapper attributes |
| 38 | add_filter('acf/field_wrapper_attributes', array($this, 'field_wrapper_attributes'), 20, 2); |
| 39 | |
| 40 | // location |
| 41 | add_filter('acf/location/validate_rule/type=post_taxonomy', array($this, 'validate_post_taxonomy_location_rule'), 20, 1); |
| 42 | add_filter('acf/location/validate_rule/type=post_category', array($this, 'validate_post_taxonomy_location_rule'), 20, 1); |
| 43 | |
| 44 | // Update settings |
| 45 | add_action('acf/init', array($this, 'init')); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * init |
| 50 | * |
| 51 | * Adds compatibility for deprecated settings. |
| 52 | * |
| 53 | * @date 10/6/19 |
| 54 | * @since 5.8.1 |
| 55 | * |
| 56 | * @param void |
| 57 | * @return void |
| 58 | */ |
| 59 | function init() { |
| 60 | |
| 61 | // Update "show_admin" setting based on defined constant. |
| 62 | if( defined('ACF_LITE') && ACF_LITE ) { |
| 63 | acf_update_setting( 'show_admin', false ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * field_wrapper_attributes |
| 69 | * |
| 70 | * Adds compatibility with deprecated field wrap attributes. |
| 71 | * |
| 72 | * @date 21/1/19 |
| 73 | * @since 5.7.10 |
| 74 | * |
| 75 | * @param array $wrapper The wrapper attributes array. |
| 76 | * @param array $field The field array. |
| 77 | */ |
| 78 | function field_wrapper_attributes( $wrapper, $field ) { |
| 79 | |
| 80 | // Check compatibility setting. |
| 81 | if( acf_get_compatibility('field_wrapper_class') ) { |
| 82 | $wrapper['class'] .= " field_type-{$field['type']}"; |
| 83 | if( $field['key'] ) { |
| 84 | $wrapper['class'] .= " field_key-{$field['key']}"; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Return wrapper. |
| 89 | return $wrapper; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * validate_field |
| 94 | * |
| 95 | * Adds compatibility with deprecated settings |
| 96 | * |
| 97 | * @date 23/04/2014 |
| 98 | * @since 5.0.0 |
| 99 | * |
| 100 | * @param array $field The field array. |
| 101 | * @return array $field |
| 102 | */ |
| 103 | function validate_field( $field ) { |
| 104 | |
| 105 | // conditional logic data structure changed to groups in version 5.0.0 |
| 106 | // convert previous data (status, rules, allorany) into groups |
| 107 | if( isset($field['conditional_logic']['status']) ) { |
| 108 | |
| 109 | // check status |
| 110 | if( $field['conditional_logic']['status'] ) { |
| 111 | $field['conditional_logic'] = acf_convert_rules_to_groups($field['conditional_logic']['rules'], $field['conditional_logic']['allorany']); |
| 112 | } else { |
| 113 | $field['conditional_logic'] = 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // return |
| 118 | return $field; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * validate_textarea_field |
| 123 | * |
| 124 | * Adds compatibility with deprecated settings |
| 125 | * |
| 126 | * @date 23/04/2014 |
| 127 | * @since 5.0.0 |
| 128 | * |
| 129 | * @param array $field The field array. |
| 130 | * @return array $field |
| 131 | */ |
| 132 | function validate_textarea_field( $field ) { |
| 133 | |
| 134 | // formatting has been removed |
| 135 | $formatting = acf_extract_var( $field, 'formatting' ); |
| 136 | if( $formatting === 'br' ) { |
| 137 | $field['new_lines'] = 'br'; |
| 138 | } |
| 139 | |
| 140 | // return |
| 141 | return $field; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * validate_relationship_field |
| 146 | * |
| 147 | * Adds compatibility with deprecated settings |
| 148 | * |
| 149 | * @date 23/04/2014 |
| 150 | * @since 5.0.0 |
| 151 | * |
| 152 | * @param array $field The field array. |
| 153 | * @return array $field |
| 154 | */ |
| 155 | function validate_relationship_field( $field ) { |
| 156 | |
| 157 | // remove 'all' from post_type |
| 158 | if( acf_in_array('all', $field['post_type']) ) { |
| 159 | $field['post_type'] = array(); |
| 160 | } |
| 161 | |
| 162 | // remove 'all' from taxonomy |
| 163 | if( acf_in_array('all', $field['taxonomy']) ) { |
| 164 | $field['taxonomy'] = array(); |
| 165 | } |
| 166 | |
| 167 | // result_elements is now elements |
| 168 | if( isset($field['result_elements']) ) { |
| 169 | $field['elements'] = acf_extract_var( $field, 'result_elements' ); |
| 170 | } |
| 171 | |
| 172 | // return |
| 173 | return $field; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * validate_image_field |
| 178 | * |
| 179 | * Adds compatibility with deprecated settings |
| 180 | * |
| 181 | * @date 23/04/2014 |
| 182 | * @since 5.0.0 |
| 183 | * |
| 184 | * @param array $field The field array. |
| 185 | * @return array $field |
| 186 | */ |
| 187 | function validate_image_field( $field ) { |
| 188 | |
| 189 | // save_format is now return_format |
| 190 | if( isset($field['save_format']) ) { |
| 191 | $field['return_format'] = acf_extract_var( $field, 'save_format' ); |
| 192 | } |
| 193 | |
| 194 | // object is now array |
| 195 | if( $field['return_format'] == 'object' ) { |
| 196 | $field['return_format'] = 'array'; |
| 197 | } |
| 198 | |
| 199 | // return |
| 200 | return $field; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * validate_wysiwyg_field |
| 205 | * |
| 206 | * Adds compatibility with deprecated settings |
| 207 | * |
| 208 | * @date 23/04/2014 |
| 209 | * @since 5.0.0 |
| 210 | * |
| 211 | * @param array $field The field array. |
| 212 | * @return array $field |
| 213 | */ |
| 214 | function validate_wysiwyg_field( $field ) { |
| 215 | |
| 216 | // media_upload is now numeric |
| 217 | if( $field['media_upload'] === 'yes' ) { |
| 218 | $field['media_upload'] = 1; |
| 219 | } elseif( $field['media_upload'] === 'no' ) { |
| 220 | $field['media_upload'] = 0; |
| 221 | } |
| 222 | |
| 223 | // return |
| 224 | return $field; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * validate_date_picker_field |
| 229 | * |
| 230 | * Adds compatibility with deprecated settings |
| 231 | * |
| 232 | * @date 23/04/2014 |
| 233 | * @since 5.0.0 |
| 234 | * |
| 235 | * @param array $field The field array. |
| 236 | * @return array $field |
| 237 | */ |
| 238 | function validate_date_picker_field( $field ) { |
| 239 | |
| 240 | // date_format has changed to display_format |
| 241 | if( isset($field['date_format']) ) { |
| 242 | |
| 243 | // extract vars |
| 244 | $date_format = $field['date_format']; |
| 245 | $display_format = $field['display_format']; |
| 246 | |
| 247 | // convert from js to php |
| 248 | $display_format = acf_convert_date_to_php( $display_format ); |
| 249 | |
| 250 | // append settings |
| 251 | $field['display_format'] = $display_format; |
| 252 | $field['save_format'] = $date_format; |
| 253 | |
| 254 | // clean up |
| 255 | unset($field['date_format']); |
| 256 | } |
| 257 | |
| 258 | // return |
| 259 | return $field; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * validate_taxonomy_field |
| 264 | * |
| 265 | * Adds compatibility with deprecated settings |
| 266 | * |
| 267 | * @date 23/04/2014 |
| 268 | * @since 5.2.7 |
| 269 | * |
| 270 | * @param array $field The field array. |
| 271 | * @return array $field |
| 272 | */ |
| 273 | function validate_taxonomy_field( $field ) { |
| 274 | |
| 275 | // load_save_terms deprecated in favour of separate save_terms |
| 276 | if( isset($field['load_save_terms']) ) { |
| 277 | $field['save_terms'] = acf_extract_var( $field, 'load_save_terms' ); |
| 278 | } |
| 279 | |
| 280 | // return |
| 281 | return $field; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * validate_date_time_picker_field |
| 286 | * |
| 287 | * Adds compatibility with deprecated settings |
| 288 | * |
| 289 | * @date 23/04/2014 |
| 290 | * @since 5.2.7 |
| 291 | * |
| 292 | * @param array $field The field array. |
| 293 | * @return array $field |
| 294 | */ |
| 295 | function validate_date_time_picker_field( $field ) { |
| 296 | |
| 297 | // 3rd party date time picker |
| 298 | // https://github.com/soderlind/acf-field-date-time-picker |
| 299 | if( !empty($field['time_format']) ) { |
| 300 | |
| 301 | // extract vars |
| 302 | $time_format = acf_extract_var( $field, 'time_format' ); |
| 303 | $date_format = acf_extract_var( $field, 'date_format' ); |
| 304 | $get_as_timestamp = acf_extract_var( $field, 'get_as_timestamp' ); |
| 305 | |
| 306 | // convert from js to php |
| 307 | $time_format = acf_convert_time_to_php( $time_format ); |
| 308 | $date_format = acf_convert_date_to_php( $date_format ); |
| 309 | |
| 310 | // append settings |
| 311 | $field['return_format'] = $date_format . ' ' . $time_format; |
| 312 | $field['display_format'] = $date_format . ' ' . $time_format; |
| 313 | |
| 314 | // timestamp |
| 315 | if( $get_as_timestamp === 'true' ) { |
| 316 | $field['return_format'] = 'U'; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // return |
| 321 | return $field; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * validate_user_field |
| 326 | * |
| 327 | * Adds compatibility with deprecated settings |
| 328 | * |
| 329 | * @date 23/04/2014 |
| 330 | * @since 5.2.7 |
| 331 | * |
| 332 | * @param array $field The field array. |
| 333 | * @return array $field |
| 334 | */ |
| 335 | function validate_user_field( $field ) { |
| 336 | |
| 337 | // remove 'all' from roles |
| 338 | if( acf_in_array('all', $field['role']) ) { |
| 339 | $field['role'] = ''; |
| 340 | } |
| 341 | |
| 342 | // field_type removed in favour of multiple |
| 343 | if( isset($field['field_type']) ) { |
| 344 | |
| 345 | // extract vars |
| 346 | $field_type = acf_extract_var( $field, 'field_type' ); |
| 347 | |
| 348 | // multiple |
| 349 | if( $field_type === 'multi_select' ) { |
| 350 | $field['multiple'] = true; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // return |
| 355 | return $field; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * validate_field_group |
| 360 | * |
| 361 | * This function will provide compatibility with ACF4 field groups |
| 362 | * |
| 363 | * @type function |
| 364 | * @date 23/04/2014 |
| 365 | * @since 5.0.0 |
| 366 | * |
| 367 | * @param $field_group (array) |
| 368 | * @return $field_group |
| 369 | */ |
| 370 | function validate_field_group( $field_group ) { |
| 371 | |
| 372 | // vars |
| 373 | $version = 5; |
| 374 | |
| 375 | // field group key was added in version 5.0.0 |
| 376 | // detect ACF4 data and generate key |
| 377 | if( !$field_group['key'] ) { |
| 378 | $version = 4; |
| 379 | $field_group['key'] = isset($field_group['id']) ? "group_{$field_group['id']}" : uniqid('group_'); |
| 380 | } |
| 381 | |
| 382 | // prior to version 5.0.0, settings were saved in an 'options' array |
| 383 | // extract and merge options into the field group |
| 384 | if( isset($field_group['options']) ) { |
| 385 | $options = acf_extract_var($field_group, 'options'); |
| 386 | $field_group = array_merge($field_group, $options); |
| 387 | } |
| 388 | |
| 389 | // location data structure changed to groups in version 4.1.0 |
| 390 | // convert previous data (rules, allorany) into groups |
| 391 | if( isset($field_group['location']['rules']) ) { |
| 392 | $field_group['location'] = acf_convert_rules_to_groups($field_group['location']['rules'], $field_group['location']['allorany']); |
| 393 | } |
| 394 | |
| 395 | // some location rule names have changed in version 5.0.0 |
| 396 | // loop over location data and modify rules |
| 397 | $replace = array( |
| 398 | 'taxonomy' => 'post_taxonomy', |
| 399 | 'ef_media' => 'attachment', |
| 400 | 'ef_taxonomy' => 'taxonomy', |
| 401 | 'ef_user' => 'user_role', |
| 402 | 'user_type' => 'current_user_role' // 5.2.0 |
| 403 | ); |
| 404 | |
| 405 | // only replace 'taxonomy' rule if is an ACF4 field group |
| 406 | if( $version > 4 ) { |
| 407 | unset($replace['taxonomy']); |
| 408 | } |
| 409 | |
| 410 | // loop over location groups |
| 411 | if( $field_group['location'] ) { |
| 412 | foreach( $field_group['location'] as $i => $group ) { |
| 413 | |
| 414 | // loop over group rules |
| 415 | if( $group ) { |
| 416 | foreach( $group as $j => $rule ) { |
| 417 | |
| 418 | // migrate param |
| 419 | if( isset($replace[ $rule['param'] ]) ) { |
| 420 | $field_group['location'][ $i ][ $j ]['param'] = $replace[ $rule['param'] ]; |
| 421 | } |
| 422 | }} |
| 423 | }} |
| 424 | |
| 425 | // change layout to style (v5.0.0) |
| 426 | if( isset($field_group['layout']) ) { |
| 427 | $field_group['style'] = acf_extract_var($field_group, 'layout'); |
| 428 | } |
| 429 | |
| 430 | // change no_box to seamless (v5.0.0) |
| 431 | if( $field_group['style'] === 'no_box' ) { |
| 432 | $field_group['style'] = 'seamless'; |
| 433 | } |
| 434 | |
| 435 | //return |
| 436 | return $field_group; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * validate_post_taxonomy_location_rule |
| 441 | * |
| 442 | * description |
| 443 | * |
| 444 | * @date 27/8/18 |
| 445 | * @since 5.7.4 |
| 446 | * |
| 447 | * @param type $var Description. Default. |
| 448 | * @return type Description. |
| 449 | */ |
| 450 | function validate_post_taxonomy_location_rule( $rule ) { |
| 451 | |
| 452 | // previous versions of ACF (v4.4.12) saved value as term_id |
| 453 | // convert term_id into "taxonomy:slug" string |
| 454 | if( is_numeric($rule['value']) ) { |
| 455 | $term = acf_get_term( $rule['value'] ); |
| 456 | if( $term ) { |
| 457 | $rule['value'] = acf_encode_term($term); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // return |
| 462 | return $rule; |
| 463 | } |
| 464 | |
| 465 | } |
| 466 | |
| 467 | acf_new_instance('ACF_Compatibility'); |
| 468 | |
| 469 | endif; // class_exists check |
| 470 | |
| 471 | /* |
| 472 | * acf_get_compatibility |
| 473 | * |
| 474 | * Returns true if compatibility is enabled for the given component. |
| 475 | * |
| 476 | * @date 20/1/15 |
| 477 | * @since 5.1.5 |
| 478 | * |
| 479 | * @param string $name The name of the component to check. |
| 480 | * @return bool |
| 481 | */ |
| 482 | function acf_get_compatibility( $name ) { |
| 483 | return apply_filters( "acf/compatibility/{$name}", false ); |
| 484 | } |