Skip to content

Commit

Permalink
Expect that the REST API endpoint will exist on the site, and as a re…
Browse files Browse the repository at this point in the history
…sult, always be provided to the JS.
  • Loading branch information
dd32 committed Jun 27, 2024
1 parent 9c6cf2a commit 92e6cfa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
});
},
},
} );

0 comments on commit 92e6cfa

Please sign in to comment.