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