`; * rewriting them here saves the round trip through site-editor.php that the * redirect below would otherwise take. The active theme's entry is left * alone: it carries no `wp_theme_preview` parameter and links to the site * editor as a customize action. * * @param array $prepared_themes Themes prepared for the themes screen. * @return array Themes with rewritten live preview links. */ function gutenberg_use_theme_preview_page_for_live_preview_links( $prepared_themes ) { if ( ! current_user_can( 'switch_themes' ) ) { return $prepared_themes; } $current_stylesheet = get_stylesheet(); foreach ( $prepared_themes as $stylesheet => $theme_data ) { if ( $stylesheet === $current_stylesheet || empty( $theme_data['blockTheme'] ) || empty( $theme_data['actions']['customize'] ) ) { continue; } $prepared_themes[ $stylesheet ]['actions']['customize'] = esc_url( gutenberg_get_theme_preview_url( $stylesheet ) ); } return $prepared_themes; } /** * Redirects block theme previews from the site editor to the theme preview page. * * The links the filter above rewrites no longer reach site-editor.php; this * catches the remaining entry points — bookmarks, hand-typed URLs, and the * live preview link the theme installer renders after installing a theme. * For users the preview filters ignore, site-editor.php behaves as before. */ function gutenberg_redirect_theme_preview_to_theme_preview_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the same unauthenticated query arg Core's theme-previews.php reads to decide whether a preview is requested. $stylesheet = isset( $_GET['wp_theme_preview'] ) ? sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ) : ''; if ( '' === $stylesheet ) { return; } // Core ignores the preview for users without `switch_themes`; leave them // on the site editor, which keeps showing the active theme. if ( ! current_user_can( 'switch_themes' ) ) { return; } wp_safe_redirect( gutenberg_get_theme_preview_url( $stylesheet ) ); exit; } // The render callback is generated into `build/pages`, which lib/load.php // requires before this file: a build that predates the page has nothing to // register, link, or redirect to. if ( function_exists( 'gutenberg_theme_preview_wp_admin_render_page' ) ) { add_action( 'admin_menu', 'gutenberg_register_theme_preview_admin_page' ); add_filter( 'wp_prepare_themes_for_js', 'gutenberg_use_theme_preview_page_for_live_preview_links' ); add_action( 'load-site-editor.php', 'gutenberg_redirect_theme_preview_to_theme_preview_page' ); }