From 92e6cfa2d32a530a9e6069c4d80926c31f95963d Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Thu, 27 Jun 2024 02:18:15 +0000 Subject: [PATCH] Expect that the REST API endpoint will exist on the site, and as a result, always be provided to the JS. --- .../src/theme-previewer/render.php | 8 ++-- .../src/theme-previewer/view.js | 41 ++++++------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/render.php b/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/render.php index e5b306b..e8ffe52 100644 --- a/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/render.php +++ b/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/render.php @@ -7,11 +7,11 @@ return; } -$is_playground = (bool) ( $_REQUEST['playground-preview'] ?? false ); - // Manually enqueue this script, so that it's available for the interactivity view script. wp_enqueue_script( 'wp-a11y' ); +$is_playground = (bool) ( $_REQUEST['playground-preview'] ?? false ); + $theme_post = get_post( $block->context['postId'] ); $theme = wporg_themes_theme_information( $theme_post->post_name ); @@ -21,8 +21,8 @@ $blueprint = ''; if ( $is_playground ) { - $permalink = add_query_arg( [ 'playground-preview' => 1 ], $permalink ); - $blueprint = wp_json_encode( + $permalink = add_query_arg( [ 'playground-preview' => 1 ], $permalink ); + $blueprint = wp_json_encode( rest_get_server()->response_to_data( rest_do_request( new WP_REST_Request( diff --git a/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/view.js b/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/view.js index 62d289f..ffeb45e 100644 --- a/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/view.js +++ b/source/wp-content/themes/wporg-themes-2024/src/theme-previewer/view.js @@ -71,36 +71,19 @@ store( 'wporg/themes/preview', { // The loading overlay doesn't play nicely with the low latency of playground. context.isLoaded = true; - (new Promise( (resolve) => { - if ( context.blueprint ) { - return resolve(); - } - - // If the blueprint wasn't preloaded, fetch it. - // TODO: wp.apiFetch. not enqueued. - fetch( 'https://wordpress.org/themes/wp-json/themes/v1/preview-blueprint/' + context.theme ).then( - ( response ) => response.text() - ).then( - ( response ) => { - context.blueprint = response; - resolve() - } - ) - } ) ).then( ( blueprint ) => { - startPlaygroundWeb({ - iframe: ref, - remoteUrl: 'https://playground.wordpress.net/remote.html', - blueprint: JSON.parse( context.blueprint ) - }).then( ( playground ) => { - playgroundClient = playground; + startPlaygroundWeb({ + iframe: ref, + remoteUrl: 'https://playground.wordpress.net/remote.html', + blueprint: JSON.parse( context.blueprint ) + }).then( ( playground ) => { + playgroundClient = playground; - // If the preview URL has any query params, head straight there. - const currentPreviewURL = new URL( context.url ); - if ( currentPreviewURL.searchParams.size ) { - playgroundClient.goTo( '/?' + currentPreviewURL.searchParams.toString() ); - } - }); - } ); + // If the preview URL has any query params, head straight there. + const currentPreviewURL = new URL( context.url ); + if ( currentPreviewURL.searchParams.size ) { + playgroundClient.goTo( '/?' + currentPreviewURL.searchParams.toString() ); + } + }); }, }, } );