Repository navigation
Backport: Block theme previews #4627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3eef498
fa4b2e5
fce6400
2c48f38
7658703
ea870d3
841c25e
5dc273c
aabf5dc
ebf6909
9aa8b1f
0d4a6f4
7c78729
c0376f7
1270e3e
8339d07
bf1eafa
ffbcbb0
596545c
86be126
4a6581d
e6182be
88c7f9d
4c32603
f260cd5
391e296
a217191
9971a7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
| /** | ||
| * Theme previews using the Site Editor for block themes. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Filters the blog option to return the path for the previewed theme. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @param string $current_stylesheet The current theme's stylesheet or template path. | ||
| * @return string The previewed theme's stylesheet or template path. | ||
| */ | ||
| function wp_get_theme_preview_path( $current_stylesheet = null ) { | ||
| if ( ! current_user_can( 'switch_themes' ) ) { | ||
| return $current_stylesheet; | ||
| } | ||
|
|
||
| $preview_stylesheet = ! empty( $_GET['wp_theme_preview'] ) ? sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ) : null; | ||
| $wp_theme = wp_get_theme( $preview_stylesheet ); | ||
| if ( ! is_wp_error( $wp_theme->errors() ) ) { | ||
| if ( current_filter() === 'template' ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is happening here? Is this detecting if the theme has a parent? If so, I remember using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for looking at this Johnny. This function ( |
||
| $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. | ||
|
MaggieCabrera marked this conversation as resolved.
Resolved
|
||
| * 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' | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.