From d01393e3910b1bdf7130db182c6d0e9f4b2d1e55 Mon Sep 17 00:00:00 2001 From: scruffian Date: Wed, 5 Jul 2023 11:17:19 +0100 Subject: [PATCH 1/7] Block Theme Previews: Add a nonce for activation, and ensure that we always load the filters even outside of wp-admin --- src/wp-admin/includes/admin-filters.php | 7 ------ src/wp-admin/includes/admin.php | 1 - .../theme-previews.php | 25 +++++++++++++++++++ src/wp-settings.php | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) rename src/{wp-admin/includes => wp-includes}/theme-previews.php (71%) diff --git a/src/wp-admin/includes/admin-filters.php b/src/wp-admin/includes/admin-filters.php index 8f364360fd8a9..33354cb073e32 100644 --- a/src/wp-admin/includes/admin-filters.php +++ b/src/wp-admin/includes/admin-filters.php @@ -168,10 +168,3 @@ // Append '(Draft)' to draft page titles in the privacy page dropdown. add_filter( 'list_pages', '_wp_privacy_settings_filter_draft_page_titles', 10, 2 ); - -// Attaches filters to enable theme previews in the Site Editor. -if ( ! empty( $_GET['wp_theme_preview'] ) ) { - add_filter( 'stylesheet', 'wp_get_theme_preview_path' ); - add_filter( 'template', 'wp_get_theme_preview_path' ); - add_action( 'init', 'wp_attach_theme_preview_middleware' ); -} diff --git a/src/wp-admin/includes/admin.php b/src/wp-admin/includes/admin.php index 4930e92b71ed1..ce2ec0c68b855 100644 --- a/src/wp-admin/includes/admin.php +++ b/src/wp-admin/includes/admin.php @@ -71,7 +71,6 @@ /** WordPress Theme Administration API */ require_once ABSPATH . 'wp-admin/includes/theme.php'; -require_once ABSPATH . 'wp-admin/includes/theme-previews.php'; /** WordPress Privacy Functions */ require_once ABSPATH . 'wp-admin/includes/privacy-tools.php'; diff --git a/src/wp-admin/includes/theme-previews.php b/src/wp-includes/theme-previews.php similarity index 71% rename from src/wp-admin/includes/theme-previews.php rename to src/wp-includes/theme-previews.php index 55a3679096dd4..865b224d03b61 100644 --- a/src/wp-admin/includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -54,3 +54,28 @@ function wp_attach_theme_preview_middleware() { 'after' ); } + + +// Attaches filters to enable theme previews in the Site Editor. +if ( ! empty( $_GET['wp_theme_preview'] ) ) { + add_filter( 'stylesheet', 'wp_get_theme_preview_path' ); + add_filter( 'template', 'wp_get_theme_preview_path' ); + add_action( 'init', 'wp_attach_theme_preview_middleware' ); + add_action( 'admin_head', 'block_theme_activate_nonce' ); +} + + + +/** + * Adds a nonce for the theme activation link. + */ +function block_theme_activate_nonce() { + $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path(); + ?> + + Date: Wed, 5 Jul 2023 11:22:27 +0100 Subject: [PATCH 2/7] fix formatting --- src/wp-includes/theme-previews.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/theme-previews.php b/src/wp-includes/theme-previews.php index 865b224d03b61..0a80ed3851a76 100644 --- a/src/wp-includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -55,17 +55,6 @@ function wp_attach_theme_preview_middleware() { ); } - -// Attaches filters to enable theme previews in the Site Editor. -if ( ! empty( $_GET['wp_theme_preview'] ) ) { - add_filter( 'stylesheet', 'wp_get_theme_preview_path' ); - add_filter( 'template', 'wp_get_theme_preview_path' ); - add_action( 'init', 'wp_attach_theme_preview_middleware' ); - add_action( 'admin_head', 'block_theme_activate_nonce' ); -} - - - /** * Adds a nonce for the theme activation link. */ @@ -78,4 +67,10 @@ function block_theme_activate_nonce() { Date: Wed, 5 Jul 2023 11:26:10 +0100 Subject: [PATCH 3/7] update comment --- src/wp-includes/theme-previews.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/theme-previews.php b/src/wp-includes/theme-previews.php index 0a80ed3851a76..cb4a3a2aec512 100644 --- a/src/wp-includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -57,6 +57,10 @@ function wp_attach_theme_preview_middleware() { /** * Adds a nonce for the theme activation link. + * This is needed until we have a way to activate the theme via the REST API. + * See https://github.com/WordPress/gutenberg/pull/41836. + * + * @since 6.3.0 */ function block_theme_activate_nonce() { $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path(); From 0cdfdc964f58a5152f4ddbdb7668e7cd23362d47 Mon Sep 17 00:00:00 2001 From: scruffian Date: Wed, 5 Jul 2023 12:48:52 +0100 Subject: [PATCH 4/7] prefix function --- src/wp-includes/theme-previews.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/theme-previews.php b/src/wp-includes/theme-previews.php index cb4a3a2aec512..fa0f542c1fbdf 100644 --- a/src/wp-includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -62,7 +62,7 @@ function wp_attach_theme_preview_middleware() { * * @since 6.3.0 */ -function block_theme_activate_nonce() { +function wp_block_theme_activate_nonce() { $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path(); ?> + Date: Tue, 11 Jul 2023 13:31:03 +1000 Subject: [PATCH 7/7] Add private note Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- src/wp-includes/theme-previews.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/theme-previews.php b/src/wp-includes/theme-previews.php index bdf0e4e2d4d7b..d9325ddbf36de 100644 --- a/src/wp-includes/theme-previews.php +++ b/src/wp-includes/theme-previews.php @@ -64,6 +64,7 @@ function wp_attach_theme_preview_middleware() { * @see https://github.com/WordPress/gutenberg/pull/41836. * * @since 6.3.0 + * @private */ function wp_block_theme_activate_nonce() { $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path();