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