Blocks
1 year ago
admin
1 year ago
ajax
1 year ago
api
1 year ago
fields
1 year ago
forms
1 year ago
legacy
1 year ago
locations
1 year ago
post-types
1 year ago
rest-api
1 year ago
walkers
1 year ago
acf-bidirectional-functions.php
1 year ago
acf-field-functions.php
1 year ago
acf-field-group-functions.php
1 year ago
acf-form-functions.php
1 year ago
acf-helper-functions.php
1 year ago
acf-hook-functions.php
1 year ago
acf-input-functions.php
1 year ago
acf-internal-post-type-functions.php
1 year ago
acf-meta-functions.php
1 year ago
acf-post-functions.php
1 year ago
acf-post-type-functions.php
1 year ago
acf-taxonomy-functions.php
1 year ago
acf-user-functions.php
1 year ago
acf-utility-functions.php
1 year ago
acf-value-functions.php
1 year ago
acf-wp-functions.php
1 year ago
assets.php
1 year ago
blocks.php
1 year ago
class-acf-data.php
1 year ago
class-acf-internal-post-type.php
1 year ago
class-acf-options-page.php
1 year ago
class-acf-site-health.php
1 year ago
compatibility.php
1 year ago
deprecated.php
1 year ago
fields.php
1 year ago
index.php
1 year ago
l10n.php
1 year ago
local-fields.php
1 year ago
local-json.php
1 year ago
local-meta.php
1 year ago
locations.php
1 year ago
loop.php
1 year ago
media.php
1 year ago
rest-api.php
1 year ago
revisions.php
1 year ago
scf-ui-options-page-functions.php
1 year ago
third-party.php
1 year ago
upgrades.php
1 year ago
validation.php
1 year ago
wpml.php
1 year ago
upgrades.php
534 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * acf_has_upgrade |
| 5 | * |
| 6 | * Returns true if this site has an upgrade avaialble. |
| 7 | * |
| 8 | * @date 24/8/18 |
| 9 | * @since ACF 5.7.4 |
| 10 | * |
| 11 | * @return boolean |
| 12 | */ |
| 13 | function acf_has_upgrade() { |
| 14 | $db_version = acf_get_db_version(); |
| 15 | |
| 16 | if ( $db_version && acf_version_compare( $db_version, '<', ACF_UPGRADE_VERSION ) ) { |
| 17 | return true; |
| 18 | } |
| 19 | |
| 20 | if ( $db_version !== ACF_VERSION ) { |
| 21 | acf_update_db_version( ACF_VERSION ); |
| 22 | } |
| 23 | |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Runs upgrade routines if this site has an upgrade available. |
| 29 | * |
| 30 | * @date 24/8/18 |
| 31 | * @since ACF 5.7.4 |
| 32 | */ |
| 33 | function acf_upgrade_all() { |
| 34 | // Increase time limit if possible. |
| 35 | if ( function_exists( 'set_time_limit' ) ) { |
| 36 | set_time_limit( 600 ); |
| 37 | } |
| 38 | |
| 39 | // start timer |
| 40 | timer_start(); |
| 41 | |
| 42 | // log |
| 43 | acf_dev_log( 'ACF Upgrade Begin.' ); |
| 44 | |
| 45 | // vars |
| 46 | $db_version = acf_get_db_version(); |
| 47 | |
| 48 | // 5.0.0 |
| 49 | if ( acf_version_compare( $db_version, '<', '5.0.0' ) ) { |
| 50 | acf_upgrade_500(); |
| 51 | } |
| 52 | |
| 53 | // 5.5.0 |
| 54 | if ( acf_version_compare( $db_version, '<', '5.5.0' ) ) { |
| 55 | acf_upgrade_550(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * When adding new upgrade routines here, increment the ACF_UPGRADE_VERSION |
| 60 | * constant in `acf.php` to the new highest upgrade version. |
| 61 | */ |
| 62 | |
| 63 | // upgrade DB version once all updates are complete |
| 64 | acf_update_db_version( ACF_VERSION ); |
| 65 | |
| 66 | if ( is_multisite() ) { |
| 67 | // Clears the network upgrade notification banner after site upgrades. |
| 68 | delete_site_transient( 'acf_network_upgrade_needed_' . ACF_UPGRADE_VERSION ); |
| 69 | } |
| 70 | |
| 71 | // log |
| 72 | global $wpdb; |
| 73 | acf_dev_log( 'ACF Upgrade Complete.', $wpdb->num_queries, timer_stop( 0 ) ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * acf_get_db_version |
| 78 | * |
| 79 | * Returns the ACF DB version. |
| 80 | * |
| 81 | * @date 10/09/2016 |
| 82 | * @since ACF 5.4.0 |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | function acf_get_db_version() { |
| 87 | return get_option( 'acf_version' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Updates the ACF DB version. |
| 92 | * |
| 93 | * @date 10/09/2016 |
| 94 | * @since ACF 5.4.0 |
| 95 | * |
| 96 | * @param string $version The new version. |
| 97 | * @return void |
| 98 | */ |
| 99 | function acf_update_db_version( $version = '' ) { |
| 100 | update_option( 'acf_version', $version ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * acf_upgrade_500 |
| 105 | * |
| 106 | * Version 5 introduces new post types for field groups and fields. |
| 107 | * |
| 108 | * @date 23/8/18 |
| 109 | * @since ACF 5.7.4 |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | function acf_upgrade_500() { |
| 114 | |
| 115 | // log |
| 116 | acf_dev_log( 'ACF Upgrade 5.0.0.' ); |
| 117 | |
| 118 | // action |
| 119 | do_action( 'acf/upgrade_500' ); |
| 120 | |
| 121 | // do tasks |
| 122 | acf_upgrade_500_field_groups(); |
| 123 | |
| 124 | // update version |
| 125 | acf_update_db_version( '5.0.0' ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * acf_upgrade_500_field_groups |
| 130 | * |
| 131 | * Upgrades all ACF4 field groups to ACF5 |
| 132 | * |
| 133 | * @date 23/8/18 |
| 134 | * @since ACF 5.7.4 |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | function acf_upgrade_500_field_groups() { |
| 139 | |
| 140 | // log |
| 141 | acf_dev_log( 'ACF Upgrade 5.0.0 Field Groups.' ); |
| 142 | |
| 143 | // get old field groups |
| 144 | $ofgs = get_posts( |
| 145 | array( |
| 146 | 'numberposts' => -1, |
| 147 | 'post_type' => 'acf', |
| 148 | 'orderby' => 'menu_order title', |
| 149 | 'order' => 'asc', |
| 150 | 'suppress_filters' => true, |
| 151 | ) |
| 152 | ); |
| 153 | |
| 154 | // loop |
| 155 | if ( $ofgs ) { |
| 156 | foreach ( $ofgs as $ofg ) { |
| 157 | acf_upgrade_500_field_group( $ofg ); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * acf_upgrade_500_field_group |
| 164 | * |
| 165 | * Upgrades a ACF4 field group to ACF5 |
| 166 | * |
| 167 | * @date 23/8/18 |
| 168 | * @since ACF 5.7.4 |
| 169 | * |
| 170 | * @param object $ofg The old field group post object. |
| 171 | * @return array $nfg The new field group array. |
| 172 | */ |
| 173 | function acf_upgrade_500_field_group( $ofg ) { |
| 174 | |
| 175 | // log |
| 176 | acf_dev_log( 'ACF Upgrade 5.0.0 Field Group.', $ofg ); |
| 177 | |
| 178 | // vars |
| 179 | $nfg = array( |
| 180 | 'ID' => 0, |
| 181 | 'title' => $ofg->post_title, |
| 182 | 'menu_order' => $ofg->menu_order, |
| 183 | ); |
| 184 | |
| 185 | // construct the location rules |
| 186 | $rules = get_post_meta( $ofg->ID, 'rule', false ); |
| 187 | $anyorall = get_post_meta( $ofg->ID, 'allorany', true ); |
| 188 | if ( is_array( $rules ) ) { |
| 189 | |
| 190 | // if field group was duplicated, rules may be a serialized string! |
| 191 | $rules = array_map( 'acf_maybe_unserialize', $rules ); |
| 192 | |
| 193 | // convert rules to groups |
| 194 | $nfg['location'] = acf_convert_rules_to_groups( $rules, $anyorall ); |
| 195 | } |
| 196 | |
| 197 | // settings |
| 198 | if ( $position = get_post_meta( $ofg->ID, 'position', true ) ) { |
| 199 | $nfg['position'] = $position; |
| 200 | } |
| 201 | |
| 202 | if ( $layout = get_post_meta( $ofg->ID, 'layout', true ) ) { |
| 203 | $nfg['layout'] = $layout; |
| 204 | } |
| 205 | |
| 206 | if ( $hide_on_screen = get_post_meta( $ofg->ID, 'hide_on_screen', true ) ) { |
| 207 | $nfg['hide_on_screen'] = acf_maybe_unserialize( $hide_on_screen ); |
| 208 | } |
| 209 | |
| 210 | // save field group |
| 211 | // acf_upgrade_field_group will call the acf_get_valid_field_group function and apply 'compatibility' changes |
| 212 | $nfg = acf_update_field_group( $nfg ); |
| 213 | |
| 214 | // log |
| 215 | acf_dev_log( '> Complete.', $nfg ); |
| 216 | |
| 217 | // action for 3rd party |
| 218 | do_action( 'acf/upgrade_500_field_group', $nfg, $ofg ); |
| 219 | |
| 220 | // upgrade fields |
| 221 | acf_upgrade_500_fields( $ofg, $nfg ); |
| 222 | |
| 223 | // trash? |
| 224 | if ( $ofg->post_status == 'trash' ) { |
| 225 | acf_trash_field_group( $nfg['ID'] ); |
| 226 | } |
| 227 | |
| 228 | // return |
| 229 | return $nfg; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * acf_upgrade_500_fields |
| 234 | * |
| 235 | * Upgrades all ACF4 fields to ACF5 from a specific field group |
| 236 | * |
| 237 | * @date 23/8/18 |
| 238 | * @since ACF 5.7.4 |
| 239 | * |
| 240 | * @param object $ofg The old field group post object. |
| 241 | * @param array $nfg The new field group array. |
| 242 | * @return void |
| 243 | */ |
| 244 | function acf_upgrade_500_fields( $ofg, $nfg ) { |
| 245 | |
| 246 | // log |
| 247 | acf_dev_log( 'ACF Upgrade 5.0.0 Fields.' ); |
| 248 | |
| 249 | // global |
| 250 | global $wpdb; |
| 251 | |
| 252 | // get field from postmeta |
| 253 | $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key LIKE %s", $ofg->ID, 'field_%' ), ARRAY_A ); |
| 254 | |
| 255 | // check |
| 256 | if ( $rows ) { |
| 257 | |
| 258 | // vars |
| 259 | $checked = array(); |
| 260 | |
| 261 | // loop |
| 262 | foreach ( $rows as $row ) { |
| 263 | |
| 264 | // vars |
| 265 | $field = $row['meta_value']; |
| 266 | $field = acf_maybe_unserialize( $field ); |
| 267 | $field = acf_maybe_unserialize( $field ); // run again for WPML |
| 268 | |
| 269 | // bail early if key already migrated (potential duplicates in DB) |
| 270 | if ( isset( $checked[ $field['key'] ] ) ) { |
| 271 | continue; |
| 272 | } |
| 273 | $checked[ $field['key'] ] = 1; |
| 274 | |
| 275 | // add parent |
| 276 | $field['parent'] = $nfg['ID']; |
| 277 | |
| 278 | // migrate field |
| 279 | $field = acf_upgrade_500_field( $field ); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * acf_upgrade_500_field |
| 286 | * |
| 287 | * Upgrades a ACF4 field to ACF5 |
| 288 | * |
| 289 | * @date 23/8/18 |
| 290 | * @since ACF 5.7.4 |
| 291 | * |
| 292 | * @param array $field The old field. |
| 293 | * @return array $field The new field. |
| 294 | */ |
| 295 | function acf_upgrade_500_field( $field ) { |
| 296 | |
| 297 | // log |
| 298 | acf_dev_log( 'ACF Upgrade 5.0.0 Field.', $field ); |
| 299 | |
| 300 | // order_no is now menu_order |
| 301 | $field['menu_order'] = acf_extract_var( $field, 'order_no', 0 ); |
| 302 | |
| 303 | // correct very old field keys (field2 => field_2) |
| 304 | if ( substr( $field['key'], 0, 6 ) !== 'field_' ) { |
| 305 | $field['key'] = 'field_' . str_replace( 'field', '', $field['key'] ); |
| 306 | } |
| 307 | |
| 308 | // extract sub fields |
| 309 | $sub_fields = array(); |
| 310 | if ( $field['type'] == 'repeater' ) { |
| 311 | |
| 312 | // loop over sub fields |
| 313 | if ( ! empty( $field['sub_fields'] ) ) { |
| 314 | foreach ( $field['sub_fields'] as $sub_field ) { |
| 315 | $sub_fields[] = $sub_field; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // remove sub fields from field |
| 320 | unset( $field['sub_fields'] ); |
| 321 | } elseif ( $field['type'] == 'flexible_content' ) { |
| 322 | |
| 323 | // loop over layouts |
| 324 | if ( is_array( $field['layouts'] ) ) { |
| 325 | foreach ( $field['layouts'] as $i => $layout ) { |
| 326 | |
| 327 | // generate key |
| 328 | $layout['key'] = uniqid( 'layout_' ); |
| 329 | |
| 330 | // loop over sub fields |
| 331 | if ( ! empty( $layout['sub_fields'] ) ) { |
| 332 | foreach ( $layout['sub_fields'] as $sub_field ) { |
| 333 | $sub_field['parent_layout'] = $layout['key']; |
| 334 | $sub_fields[] = $sub_field; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // remove sub fields from layout |
| 339 | unset( $layout['sub_fields'] ); |
| 340 | |
| 341 | // update |
| 342 | $field['layouts'][ $i ] = $layout; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // save field |
| 348 | $field = acf_update_field( $field ); |
| 349 | |
| 350 | // log |
| 351 | acf_dev_log( '> Complete.', $field ); |
| 352 | |
| 353 | // sub fields |
| 354 | if ( $sub_fields ) { |
| 355 | foreach ( $sub_fields as $sub_field ) { |
| 356 | $sub_field['parent'] = $field['ID']; |
| 357 | acf_upgrade_500_field( $sub_field ); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // action for 3rd party |
| 362 | do_action( 'acf/update_500_field', $field ); |
| 363 | |
| 364 | // return |
| 365 | return $field; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * acf_upgrade_550 |
| 370 | * |
| 371 | * Version 5.5 adds support for the wp_termmeta table added in WP 4.4. |
| 372 | * |
| 373 | * @date 23/8/18 |
| 374 | * @since ACF 5.7.4 |
| 375 | * |
| 376 | * @return void |
| 377 | */ |
| 378 | function acf_upgrade_550() { |
| 379 | |
| 380 | // log |
| 381 | acf_dev_log( 'ACF Upgrade 5.5.0.' ); |
| 382 | |
| 383 | // action |
| 384 | do_action( 'acf/upgrade_550' ); |
| 385 | |
| 386 | // do tasks |
| 387 | acf_upgrade_550_termmeta(); |
| 388 | |
| 389 | // update version |
| 390 | acf_update_db_version( '5.5.0' ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * acf_upgrade_550_termmeta |
| 395 | * |
| 396 | * Upgrades all ACF4 termmeta saved in wp_options to the wp_termmeta table. |
| 397 | * |
| 398 | * @date 23/8/18 |
| 399 | * @since ACF 5.7.4 |
| 400 | * |
| 401 | * @return void |
| 402 | */ |
| 403 | function acf_upgrade_550_termmeta() { |
| 404 | |
| 405 | // log |
| 406 | acf_dev_log( 'ACF Upgrade 5.5.0 Termmeta.' ); |
| 407 | |
| 408 | // bail early if no wp_termmeta table |
| 409 | if ( get_option( 'db_version' ) < 34370 ) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | // get all taxonomies |
| 414 | $taxonomies = get_taxonomies( false, 'objects' ); |
| 415 | |
| 416 | // loop |
| 417 | if ( $taxonomies ) { |
| 418 | foreach ( $taxonomies as $taxonomy ) { |
| 419 | acf_upgrade_550_taxonomy( $taxonomy->name ); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // action for 3rd party |
| 424 | do_action( 'acf/upgrade_550_termmeta' ); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * When the database is updated to support term meta, migrate ACF term meta data across. |
| 429 | * |
| 430 | * @date 23/8/18 |
| 431 | * @since ACF 5.7.4 |
| 432 | * |
| 433 | * @param string $wp_db_version The new $wp_db_version. |
| 434 | * @param string $wp_current_db_version The old (current) $wp_db_version. |
| 435 | * @return void |
| 436 | */ |
| 437 | function acf_wp_upgrade_550_termmeta( $wp_db_version, $wp_current_db_version ) { |
| 438 | if ( $wp_db_version >= 34370 && $wp_current_db_version < 34370 ) { |
| 439 | if ( acf_version_compare( acf_get_db_version(), '>', '5.5.0' ) ) { |
| 440 | acf_upgrade_550_termmeta(); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | add_action( 'wp_upgrade', 'acf_wp_upgrade_550_termmeta', 10, 2 ); |
| 445 | |
| 446 | /** |
| 447 | * acf_upgrade_550_taxonomy |
| 448 | * |
| 449 | * Upgrades all ACF4 termmeta for a specific taxonomy. |
| 450 | * |
| 451 | * @date 24/8/18 |
| 452 | * @since ACF 5.7.4 |
| 453 | * |
| 454 | * @param string $taxonomy The taxonomy name. |
| 455 | * @return void |
| 456 | */ |
| 457 | function acf_upgrade_550_taxonomy( $taxonomy ) { |
| 458 | |
| 459 | // log |
| 460 | acf_dev_log( 'ACF Upgrade 5.5.0 Taxonomy.', $taxonomy ); |
| 461 | |
| 462 | // global |
| 463 | global $wpdb; |
| 464 | |
| 465 | // vars |
| 466 | $search = $taxonomy . '_%'; |
| 467 | $_search = '_' . $search; |
| 468 | |
| 469 | // escape '_' |
| 470 | // http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server |
| 471 | $search = str_replace( '_', '\_', $search ); |
| 472 | $_search = str_replace( '_', '\_', $_search ); |
| 473 | |
| 474 | // search |
| 475 | // results show faster query times using 2 LIKE vs 2 wildcards |
| 476 | $rows = $wpdb->get_results( |
| 477 | $wpdb->prepare( |
| 478 | "SELECT * |
| 479 | FROM $wpdb->options |
| 480 | WHERE option_name LIKE %s |
| 481 | OR option_name LIKE %s", |
| 482 | $search, |
| 483 | $_search |
| 484 | ), |
| 485 | ARRAY_A |
| 486 | ); |
| 487 | |
| 488 | // loop |
| 489 | if ( $rows ) { |
| 490 | foreach ( $rows as $row ) { |
| 491 | |
| 492 | /** |
| 493 | * Use regex to find "(_)taxonomy_(term_id)_(field_name)" and populate $matches: |
| 494 | * Array |
| 495 | * ( |
| 496 | * [0] => _category_3_color |
| 497 | * [1] => _ |
| 498 | * [2] => 3 |
| 499 | * [3] => color |
| 500 | * ) |
| 501 | */ |
| 502 | if ( ! preg_match( "/^(_?){$taxonomy}_(\d+)_(.+)/", is_null( $row['option_name'] ) ? '' : $row['option_name'], $matches ) ) { |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | // vars |
| 507 | $term_id = $matches[2]; |
| 508 | $meta_key = $matches[1] . $matches[3]; |
| 509 | $meta_value = $row['option_value']; |
| 510 | |
| 511 | // update |
| 512 | // memory usage reduced by 50% by using a manual insert vs update_metadata() function. |
| 513 | // update_metadata( 'term', $term_id, $meta_name, $meta_value ); |
| 514 | $wpdb->insert( |
| 515 | $wpdb->termmeta, |
| 516 | array( |
| 517 | 'term_id' => $term_id, |
| 518 | 'meta_key' => $meta_key, |
| 519 | 'meta_value' => $meta_value, |
| 520 | ) |
| 521 | ); |
| 522 | |
| 523 | // log |
| 524 | acf_dev_log( 'ACF Upgrade 5.5.0 Term.', $term_id, $meta_key ); |
| 525 | |
| 526 | // action |
| 527 | do_action( 'acf/upgrade_550_taxonomy_term', $term_id ); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // action for 3rd party |
| 532 | do_action( 'acf/upgrade_550_taxonomy', $taxonomy ); |
| 533 | } |
| 534 |