From a415c29efee45520ae252d2aa28f1083a521cd7b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 09:56:49 +0200 Subject: Adding upstream version 6.4.3+dfsg1. Signed-off-by: Daniel Baumann --- wp-includes/theme-previews.php | 93 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 wp-includes/theme-previews.php (limited to 'wp-includes/theme-previews.php') diff --git a/wp-includes/theme-previews.php b/wp-includes/theme-previews.php new file mode 100644 index 0000000..882f3e0 --- /dev/null +++ b/wp-includes/theme-previews.php @@ -0,0 +1,93 @@ +errors() ) ) { + if ( current_filter() === 'template' ) { + $theme_path = $wp_theme->get_template(); + } else { + $theme_path = $wp_theme->get_stylesheet(); + } + + return sanitize_text_field( $theme_path ); + } + + return $current_stylesheet; +} + +/** + * Adds a middleware to `apiFetch` to set the theme for the preview. + * This adds a `wp_theme_preview` URL parameter to API requests from the Site Editor, so they also respond as if the theme is set to the value of the parameter. + * + * @since 6.3.0 + */ +function wp_attach_theme_preview_middleware() { + // Don't allow non-admins to preview themes. + if ( ! current_user_can( 'switch_themes' ) ) { + return; + } + + wp_add_inline_script( + 'wp-api-fetch', + sprintf( + 'wp.apiFetch.use( wp.apiFetch.createThemePreviewMiddleware( %s ) );', + wp_json_encode( sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ) ) + ), + 'after' + ); +} + +/** + * Set a JavaScript constant for theme activation. + * + * Sets the JavaScript global WP_BLOCK_THEME_ACTIVATE_NONCE containing the nonce + * required to activate a theme. For use within the site editor. + * + * @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(); + ?> + +