| 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 |
// location |
| 38 |
add_filter('acf/location/validate_rule/type=post_taxonomy', array($this, 'validate_post_taxonomy_location_rule'), 20, 1); |
| 39 |
add_filter('acf/location/validate_rule/type=post_category', array($this, 'validate_post_taxonomy_location_rule'), 20, 1); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* validate_field |
| 44 |
* |
| 45 |
* Adds compatibility with deprecated settings |
| 46 |
* |
| 47 |
* @date 23/04/2014 |
| 48 |
* @since 5.0.0 |
| 49 |
* |
| 50 |
* @param array $field The field array. |
| 51 |
* @return array $field |
| 52 |
*/ |
| 53 |
function validate_field( $field ) { |
| 54 |
|
| 55 |
// conditional logic data structure changed to groups in version 5.0.0 |
| 56 |
// convert previous data (status, rules, allorany) into groups |
| 57 |
if( isset($field['conditional_logic']['status']) ) { |
| 58 |
|
| 59 |
// check status |
| 60 |
if( $field['conditional_logic']['status'] ) { |
| 61 |
$field['conditional_logic'] = acf_convert_rules_to_groups($field['conditional_logic']['rules'], $field['conditional_logic']['allorany']); |
| 62 |
} else { |
| 63 |
$field['conditional_logic'] = 0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// return |
| 68 |
return $field; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* validate_textarea_field |
| 73 |
* |
| 74 |
* Adds compatibility with deprecated settings |
| 75 |
* |
| 76 |
* @date 23/04/2014 |
| 77 |
* @since 5.0.0 |
| 78 |
* |
| 79 |
* @param array $field The field array. |
| 80 |
* @return array $field |
| 81 |
*/ |
| 82 |
function validate_textarea_field( $field ) { |
| 83 |
|
| 84 |
// formatting has been removed |
| 85 |
$formatting = acf_extract_var( $field, 'formatting' ); |
| 86 |
if( $formatting === 'br' ) { |
| 87 |
$field['new_lines'] = 'br'; |
| 88 |
} |
| 89 |
|
| 90 |
// return |
| 91 |
return $field; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* validate_relationship_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_relationship_field( $field ) { |
| 106 |
|
| 107 |
// remove 'all' from post_type |
| 108 |
if( acf_in_array('all', $field['post_type']) ) { |
| 109 |
$field['post_type'] = array(); |
| 110 |
} |
| 111 |
|
| 112 |
// remove 'all' from taxonomy |
| 113 |
if( acf_in_array('all', $field['taxonomy']) ) { |
| 114 |
$field['taxonomy'] = array(); |
| 115 |
} |
| 116 |
|
| 117 |
// result_elements is now elements |
| 118 |
if( isset($field['result_elements']) ) { |
| 119 |
$field['elements'] = acf_extract_var( $field, 'result_elements' ); |
| 120 |
} |
| 121 |
|
| 122 |
// return |
| 123 |
return $field; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* validate_image_field |
| 128 |
* |
| 129 |
* Adds compatibility with deprecated settings |
| 130 |
* |
| 131 |
* @date 23/04/2014 |
| 132 |
* @since 5.0.0 |
| 133 |
* |
| 134 |
* @param array $field The field array. |
| 135 |
* @return array $field |
| 136 |
*/ |
| 137 |
function validate_image_field( $field ) { |
| 138 |
|
| 139 |
// save_format is now return_format |
| 140 |
if( isset($field['save_format']) ) { |
| 141 |
$field['return_format'] = acf_extract_var( $field, 'save_format' ); |
| 142 |
} |
| 143 |
|
| 144 |
// object is now array |
| 145 |
if( $field['return_format'] == 'object' ) { |
| 146 |
$field['return_format'] = 'array'; |
| 147 |
} |
| 148 |
|
| 149 |
// return |
| 150 |
return $field; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* validate_wysiwyg_field |
| 155 |
* |
| 156 |
* Adds compatibility with deprecated settings |
| 157 |
* |
| 158 |
* @date 23/04/2014 |
| 159 |
* @since 5.0.0 |
| 160 |
* |
| 161 |
* @param array $field The field array. |
| 162 |
* @return array $field |
| 163 |
*/ |
| 164 |
function validate_wysiwyg_field( $field ) { |
| 165 |
|
| 166 |
// media_upload is now numeric |
| 167 |
if( $field['media_upload'] === 'yes' ) { |
| 168 |
$field['media_upload'] = 1; |
| 169 |
} elseif( $field['media_upload'] === 'no' ) { |
| 170 |
$field['media_upload'] = 0; |
| 171 |
} |
| 172 |
|
| 173 |
// return |
| 174 |
return $field; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* validate_date_picker_field |
| 179 |
* |
| 180 |
* Adds compatibility with deprecated settings |
| 181 |
* |
| 182 |
* @date 23/04/2014 |
| 183 |
* @since 5.0.0 |
| 184 |
* |
| 185 |
* @param array $field The field array. |
| 186 |
* @return array $field |
| 187 |
*/ |
| 188 |
function validate_date_picker_field( $field ) { |
| 189 |
|
| 190 |
// date_format has changed to display_format |
| 191 |
if( isset($field['date_format']) ) { |
| 192 |
|
| 193 |
// extract vars |
| 194 |
$date_format = $field['date_format']; |
| 195 |
$display_format = $field['display_format']; |
| 196 |
|
| 197 |
// convert from js to php |
| 198 |
$display_format = acf_convert_date_to_php( $display_format ); |
| 199 |
|
| 200 |
// append settings |
| 201 |
$field['display_format'] = $display_format; |
| 202 |
$field['save_format'] = $date_format; |
| 203 |
|
| 204 |
// clean up |
| 205 |
unset($field['date_format']); |
| 206 |
} |
| 207 |
|
| 208 |
// return |
| 209 |
return $field; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* validate_taxonomy_field |
| 214 |
* |
| 215 |
* Adds compatibility with deprecated settings |
| 216 |
* |
| 217 |
* @date 23/04/2014 |
| 218 |
* @since 5.2.7 |
| 219 |
* |
| 220 |
* @param array $field The field array. |
| 221 |
* @return array $field |
| 222 |
*/ |
| 223 |
function validate_taxonomy_field( $field ) { |
| 224 |
|
| 225 |
// load_save_terms deprecated in favour of separate save_terms |
| 226 |
if( isset($field['load_save_terms']) ) { |
| 227 |
$field['save_terms'] = acf_extract_var( $field, 'load_save_terms' ); |
| 228 |
} |
| 229 |
|
| 230 |
// return |
| 231 |
return $field; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* validate_date_time_picker_field |
| 236 |
* |
| 237 |
* Adds compatibility with deprecated settings |
| 238 |
* |
| 239 |
* @date 23/04/2014 |
| 240 |
* @since 5.2.7 |
| 241 |
* |
| 242 |
* @param array $field The field array. |
| 243 |
* @return array $field |
| 244 |
*/ |
| 245 |
function validate_date_time_picker_field( $field ) { |
| 246 |
|
| 247 |
// 3rd party date time picker |
| 248 |
// https://github.com/soderlind/acf-field-date-time-picker |
| 249 |
if( !empty($field['time_format']) ) { |
| 250 |
|
| 251 |
// extract vars |
| 252 |
$time_format = acf_extract_var( $field, 'time_format' ); |
| 253 |
$date_format = acf_extract_var( $field, 'date_format' ); |
| 254 |
$get_as_timestamp = acf_extract_var( $field, 'get_as_timestamp' ); |
| 255 |
|
| 256 |
// convert from js to php |
| 257 |
$time_format = acf_convert_time_to_php( $time_format ); |
| 258 |
$date_format = acf_convert_date_to_php( $date_format ); |
| 259 |
|
| 260 |
// append settings |
| 261 |
$field['return_format'] = $date_format . ' ' . $time_format; |
| 262 |
$field['display_format'] = $date_format . ' ' . $time_format; |
| 263 |
|
| 264 |
// timestamp |
| 265 |
if( $get_as_timestamp === 'true' ) { |
| 266 |
$field['return_format'] = 'U'; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// return |
| 271 |
return $field; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* validate_user_field |
| 276 |
* |
| 277 |
* Adds compatibility with deprecated settings |
| 278 |
* |
| 279 |
* @date 23/04/2014 |
| 280 |
* @since 5.2.7 |
| 281 |
* |
| 282 |
* @param array $field The field array. |
| 283 |
* @return array $field |
| 284 |
*/ |
| 285 |
function validate_user_field( $field ) { |
| 286 |
|
| 287 |
// remove 'all' from roles |
| 288 |
if( acf_in_array('all', $field['role']) ) { |
| 289 |
$field['role'] = ''; |
| 290 |
} |
| 291 |
|
| 292 |
// field_type removed in favour of multiple |
| 293 |
if( isset($field['field_type']) ) { |
| 294 |
|
| 295 |
// extract vars |
| 296 |
$field_type = acf_extract_var( $field, 'field_type' ); |
| 297 |
|
| 298 |
// multiple |
| 299 |
if( $field_type === 'multi_select' ) { |
| 300 |
$field['multiple'] = true; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// return |
| 305 |
return $field; |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
* validate_field_group |
| 310 |
* |
| 311 |
* This function will provide compatibility with ACF4 field groups |
| 312 |
* |
| 313 |
* @type function |
| 314 |
* @date 23/04/2014 |
| 315 |
* @since 5.0.0 |
| 316 |
* |
| 317 |
* @param $field_group (array) |
| 318 |
* @return $field_group |
| 319 |
*/ |
| 320 |
function validate_field_group( $field_group ) { |
| 321 |
|
| 322 |
// vars |
| 323 |
$version = 5; |
| 324 |
|
| 325 |
// field group key was added in version 5.0.0 |
| 326 |
// detect ACF4 data and generate key |
| 327 |
if( !$field_group['key'] ) { |
| 328 |
$version = 4; |
| 329 |
$field_group['key'] = uniqid('group_'); |
| 330 |
} |
| 331 |
|
| 332 |
// prior to version 5.0.0, settings were saved in an 'options' array |
| 333 |
// extract and merge options into the field group |
| 334 |
if( isset($field_group['options']) ) { |
| 335 |
$options = acf_extract_var($field_group, 'options'); |
| 336 |
$field_group = array_merge($field_group, $options); |
| 337 |
} |
| 338 |
|
| 339 |
// location data structure changed to groups in version 4.1.0 |
| 340 |
// convert previous data (rules, allorany) into groups |
| 341 |
if( isset($field_group['location']['rules']) ) { |
| 342 |
$field_group['location'] = acf_convert_rules_to_groups($field_group['location']['rules'], $field_group['location']['allorany']); |
| 343 |
} |
| 344 |
|
| 345 |
// some location rule names have changed in version 5.0.0 |
| 346 |
// loop over location data and modify rules |
| 347 |
$replace = array( |
| 348 |
'taxonomy' => 'post_taxonomy', |
| 349 |
'ef_media' => 'attachment', |
| 350 |
'ef_taxonomy' => 'taxonomy', |
| 351 |
'ef_user' => 'user_role', |
| 352 |
'user_type' => 'current_user_role' // 5.2.0 |
| 353 |
); |
| 354 |
|
| 355 |
// only replace 'taxonomy' rule if is an ACF4 field group |
| 356 |
if( $version > 4 ) { |
| 357 |
unset($replace['taxonomy']); |
| 358 |
} |
| 359 |
|
| 360 |
// loop over location groups |
| 361 |
if( $field_group['location'] ) { |
| 362 |
foreach( $field_group['location'] as $i => $group ) { |
| 363 |
|
| 364 |
// loop over group rules |
| 365 |
if( $group ) { |
| 366 |
foreach( $group as $j => $rule ) { |
| 367 |
|
| 368 |
// migrate param |
| 369 |
if( isset($replace[ $rule['param'] ]) ) { |
| 370 |
$field_group['location'][ $i ][ $j ]['param'] = $replace[ $rule['param'] ]; |
| 371 |
} |
| 372 |
}} |
| 373 |
}} |
| 374 |
|
| 375 |
// change layout to style (v5.0.0) |
| 376 |
if( isset($field_group['layout']) ) { |
| 377 |
$field_group['style'] = acf_extract_var($field_group, 'layout'); |
| 378 |
} |
| 379 |
|
| 380 |
// change no_box to seamless (v5.0.0) |
| 381 |
if( $field_group['style'] === 'no_box' ) { |
| 382 |
$field_group['style'] = 'seamless'; |
| 383 |
} |
| 384 |
|
| 385 |
//return |
| 386 |
return $field_group; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* validate_post_taxonomy_location_rule |
| 391 |
* |
| 392 |
* description |
| 393 |
* |
| 394 |
* @date 27/8/18 |
| 395 |
* @since 5.7.4 |
| 396 |
* |
| 397 |
* @param type $var Description. Default. |
| 398 |
* @return type Description. |
| 399 |
*/ |
| 400 |
function validate_post_taxonomy_location_rule( $rule ) { |
| 401 |
|
| 402 |
// previous versions of ACF (v4.4.12) saved value as term_id |
| 403 |
// convert term_id into "taxonomy:slug" string |
| 404 |
if( is_numeric($rule['value']) ) { |
| 405 |
$term = acf_get_term( $rule['value'] ); |
| 406 |
if( $term ) { |
| 407 |
$rule['value'] = acf_encode_term($term); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// return |
| 412 |
return $rule; |
| 413 |
} |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
acf_new_instance('ACF_Compatibility'); |
| 418 |
|
| 419 |
endif; // class_exists check |
| 420 |
|
| 421 |
?> |