| 1 |
<?php |
| 2 |
/** |
| 3 |
* Upgrading Gutenberg's database. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( '_GUTENBERG_VERSION_MIGRATION' ) ) { |
| 9 |
// It's necessary to update this version every time a new migration is needed. |
| 10 |
define( '_GUTENBERG_VERSION_MIGRATION', '23.9.0' ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Migrate Gutenberg's database on upgrade. |
| 15 |
* |
| 16 |
* @access private |
| 17 |
* @internal |
| 18 |
*/ |
| 19 |
function _gutenberg_migrate_database() { |
| 20 |
// The default value used here is the first version before migrations were added. |
| 21 |
$gutenberg_installed_version = get_option( 'gutenberg_version_migration', '9.7.0' ); |
| 22 |
|
| 23 |
if ( _GUTENBERG_VERSION_MIGRATION !== $gutenberg_installed_version ) { |
| 24 |
if ( version_compare( $gutenberg_installed_version, '9.8.0', '<' ) ) { |
| 25 |
_gutenberg_migrate_remove_fse_drafts(); |
| 26 |
} |
| 27 |
|
| 28 |
if ( version_compare( $gutenberg_installed_version, '23.8.0', '<' ) ) { |
| 29 |
_gutenberg_migrate_remove_legacy_collaboration_options(); |
| 30 |
} |
| 31 |
|
| 32 |
if ( version_compare( $gutenberg_installed_version, '23.9.0', '<' ) ) { |
| 33 |
_gutenberg_migrate_active_templates(); |
| 34 |
} |
| 35 |
|
| 36 |
update_option( 'gutenberg_version_migration', _GUTENBERG_VERSION_MIGRATION ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Remove FSE auto drafts and associated terms. |
| 42 |
* |
| 43 |
* @access private |
| 44 |
* @internal |
| 45 |
*/ |
| 46 |
function _gutenberg_migrate_remove_fse_drafts() { |
| 47 |
// Delete auto-draft templates and template parts. |
| 48 |
$delete_query = new WP_Query( |
| 49 |
array( |
| 50 |
'post_status' => array( 'auto-draft' ), |
| 51 |
'post_type' => array( 'wp_template', 'wp_template_part' ), |
| 52 |
'posts_per_page' => -1, |
| 53 |
) |
| 54 |
); |
| 55 |
foreach ( $delete_query->posts as $post ) { |
| 56 |
wp_delete_post( $post->ID, true ); |
| 57 |
} |
| 58 |
|
| 59 |
// Delete _wp_file_based term. |
| 60 |
$term = get_term_by( 'name', '_wp_file_based', 'wp_theme' ); |
| 61 |
if ( $term ) { |
| 62 |
wp_delete_term( $term->term_id, 'wp_theme' ); |
| 63 |
} |
| 64 |
|
| 65 |
// Delete useless options. |
| 66 |
delete_option( 'gutenberg_last_synchronize_theme_template_checks' ); |
| 67 |
delete_option( 'gutenberg_last_synchronize_theme_template-part_checks' ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Removes collaboration options replaced by the Real-Time Collaboration experiment. |
| 72 |
* |
| 73 |
* The previous values are intentionally not migrated to the experiment. Real-time |
| 74 |
* collaboration is now opt-in, so existing sites must explicitly enable the |
| 75 |
* experiment instead of being opted in by a legacy setting. |
| 76 |
* |
| 77 |
* @since 23.8.0 |
| 78 |
*/ |
| 79 |
function _gutenberg_migrate_remove_legacy_collaboration_options() { |
| 80 |
delete_option( 'enable_real_time_collaboration' ); |
| 81 |
delete_option( 'wp_enable_real_time_collaboration' ); |
| 82 |
delete_option( 'wp_collaboration_enabled' ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Migrates templates created by the removed template activation experiment. |
| 87 |
* |
| 88 |
* The experiment allowed multiple templates with the same slug, with the |
| 89 |
* `active_templates` option deciding which one renders. Without the |
| 90 |
* experiment, WordPress expects at most one template per slug, so an inactive |
| 91 |
* duplicate could take over rendering. Move every template the experiment did |
| 92 |
* not treat as active out of the template hierarchy by renaming it to a |
| 93 |
* `custom-{slug}` slug; it remains available as a custom template. |
| 94 |
* |
| 95 |
* Only runs on sites where the `active_templates` option exists, which means |
| 96 |
* the experiment was enabled at some point. |
| 97 |
* |
| 98 |
* @since 23.9.0 |
| 99 |
* |
| 100 |
* @access private |
| 101 |
* @internal |
| 102 |
*/ |
| 103 |
function _gutenberg_migrate_active_templates() { |
| 104 |
$active_templates = get_option( 'active_templates' ); |
| 105 |
|
| 106 |
if ( ! is_array( $active_templates ) ) { |
| 107 |
// The template activation experiment was never enabled on this site. |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$template_query = new WP_Query( |
| 112 |
array( |
| 113 |
'post_type' => 'wp_template', |
| 114 |
'post_status' => array( 'publish', 'draft', 'auto-draft' ), |
| 115 |
'posts_per_page' => -1, |
| 116 |
'no_found_rows' => true, |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
foreach ( $template_query->posts as $post ) { |
| 121 |
$slug = $post->post_name; |
| 122 |
|
| 123 |
// The active template keeps its slug, so it continues to override the |
| 124 |
// theme's template as it did under the experiment. |
| 125 |
if ( isset( $active_templates[ $slug ] ) && absint( $active_templates[ $slug ] ) === $post->ID ) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
|
| 129 |
$template = _build_block_template_result_from_post( $post ); |
| 130 |
|
| 131 |
// Custom templates never needed activation; leave them untouched. |
| 132 |
if ( is_wp_error( $template ) || $template->is_custom ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
wp_update_post( |
| 137 |
array( |
| 138 |
'ID' => $post->ID, |
| 139 |
'post_name' => 'custom-' . $slug, |
| 140 |
) |
| 141 |
); |
| 142 |
// The template is a plain custom template from now on. |
| 143 |
delete_post_meta( $post->ID, 'is_wp_suggestion' ); |
| 144 |
delete_post_meta( $post->ID, 'is_inactive_by_default' ); |
| 145 |
} |
| 146 |
|
| 147 |
delete_option( 'active_templates' ); |
| 148 |
} |
| 149 |
|
| 150 |
// Deletion of the `_wp_file_based` term (in _gutenberg_migrate_remove_fse_drafts) must happen |
| 151 |
// after its taxonomy (`wp_theme`) is registered. This happens in `gutenberg_register_wp_theme_taxonomy`, |
| 152 |
// which is hooked into `init` (default priority, i.e. 10). |
| 153 |
add_action( 'init', '_gutenberg_migrate_database', 20 ); |
| 154 |
|