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