| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( '_GUTENBERG_VERSION_MIGRATION' ) ) { |
| 9 | 9 | // It's necessary to update this version every time a new migration is needed. |
| 10 | - define( '_GUTENBERG_VERSION_MIGRATION', '23.8.0' ); | |
| 10 | + define( '_GUTENBERG_VERSION_MIGRATION', '23.9.0' ); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * Migrate Gutenberg's database on upgrade. |
| @@ -28,8 +28,12 @@ | ||
| 28 | 28 | if ( version_compare( $gutenberg_installed_version, '23.8.0', '<' ) ) { |
| 29 | 29 | _gutenberg_migrate_remove_legacy_collaboration_options(); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | + if ( version_compare( $gutenberg_installed_version, '23.9.0', '<' ) ) { | |
| 33 | + _gutenberg_migrate_active_templates(); | |
| 34 | + } | |
| 35 | + | |
| 32 | 36 | update_option( 'gutenberg_version_migration', _GUTENBERG_VERSION_MIGRATION ); |
| 33 | 37 | } |
| 34 | 38 | } |
| 35 | 39 | |
| @@ -75,8 +79,73 @@ | ||
| 75 | 79 | function _gutenberg_migrate_remove_legacy_collaboration_options() { |
| 76 | 80 | delete_option( 'enable_real_time_collaboration' ); |
| 77 | 81 | delete_option( 'wp_enable_real_time_collaboration' ); |
| 78 | 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' ); | |
| 79 | 148 | } |
| 80 | 149 | |
| 81 | 150 | // Deletion of the `_wp_file_based` term (in _gutenberg_migrate_remove_fse_drafts) must happen |
| 82 | 151 | // after its taxonomy (`wp_theme`) is registered. This happens in `gutenberg_register_wp_theme_taxonomy`, |