<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13851] sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory: Theme Directory: Add an API endpoint that returns a blueprint for a theme preview.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/13851">13851</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/13851","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-06-21 03:44:30 +0000 (Fri, 21 Jun 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Theme Directory: Add an API endpoint that returns a blueprint for a theme preview.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryrestapiphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryrestapiclassthemepreviewphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryrestapiclassthemepreviewphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php 2024-06-21 03:44:30 UTC (rev 13851)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,236 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Theme_Directory\Rest_API;
+use WP_REST_Server;
+use WP_Error;
+use WPORG_Themes_Repo_Package;
+
+class Theme_Preview {
+
+ function __construct() {
+ register_rest_route( 'themes/v1', 'preview-blueprint/(?<slug>[^/]+)', array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'preview' ),
+ 'permission_callback' => '__return_true',
+ ) );
+ }
+
+ /**
+ * Generate a Blueprint for a theme preview.
+ */
+ function preview( $request ) {
+ $theme_data = wporg_themes_query_api(
+ 'theme_information',
+ [ 'slug' => $request->get_param( 'slug' ) ]
+ );
+
+ if ( ! empty( $theme_data->error ) ) {
+ return new WP_Error( 'error', $theme_data->error );
+ }
+
+ return $this->build_blueprint( $theme_data );
+ }
+
+ /**
+ * Generate a blueprint for previewing a theme.
+ */
+ function build_blueprint( $theme_data ) {
+ $repo_package = new WPORG_Themes_Repo_Package( $theme_data->slug );
+ $theme_blueprint = $repo_package->blueprint;
+
+ // Base blueprint.
+ $blueprint = [
+ 'preferredVersions' => [
+ 'php' => '8.0',
+ 'wp' => 'latest'
+ ],
+ // Sadly many themes require the full kitchen-sink.
+ 'phpExtensionBundles' => [
+ 'kitchen-sink'
+ ],
+ // Networking is useful for testing a theme, to install extra plugins.
+ 'features' => [
+ 'networking' => true
+ ],
+ // Do not specify steps here, see below for adding steps.
+ 'steps' => []
+ ];
+
+ // Merge in the theme's blueprint.
+ if ( $theme_blueprint ) {
+ $blueprint = array_merge( $blueprint, $theme_blueprint );
+ }
+
+ // The various steps will be in these variables, and added at the end.
+ $requred_steps = [];
+ $theme_steps = $blueprint['steps'] ?? [];
+ $final_steps = [];
+
+ // Must run the latest version of WordPress.
+ if ( 'latest' !== $blueprint['preferredVersions']['wp'] ?? '' ) {
+ $blueprint['preferredVersions']['wp'] = 'latest';
+ }
+
+ // Make sure the landing page isn't an admin page.
+ if ( str_contains( $blueprint['landingPage'] ?? '', 'wp-admin/' ) ) {
+ unset( $blueprint['landingPage'] );
+ }
+
+ // These steps will always run for the previews.
+ $required_steps = [
+ // Remove any existing themes.
+ [
+ 'step' => 'rmdir',
+ 'path' => '/wordpress/wp-content/themes/'
+ ],
+ [
+ 'step' => 'mkdir',
+ 'path' => '/wordpress/wp-content/themes/'
+ ],
+ // Login as admin.
+ [
+ 'step' => 'login',
+ 'username' => 'admin',
+ 'password' => 'password'
+ ],
+ // Set the default site details. The theme blueprint may replace this.
+ [
+ 'step' => 'setSiteOptions',
+ 'options' => [
+ 'blogname' => $theme_data->name,
+ 'blogdescription' => preg_replace( '![.].+$!', '.', $theme_data->sections['description'] ), // First sentence only.
+ ]
+ ],
+ // Install parent theme.
+ empty( $theme_data->template ) ? false : [
+ 'step' => 'installTheme',
+ 'themeZipFile' => [
+ 'resource' => 'wordpress.org/themes',
+ 'slug' => $theme_data->template
+ ],
+ 'options' => [
+ 'activate' => false
+ ]
+ ],
+ // Install the theme.
+ [
+ 'step' => 'installTheme',
+ 'themeZipFile' => [
+ 'resource' => 'wordpress.org/themes',
+ 'slug' => $theme_data->slug
+ ]
+ ]
+ ];
+
+ // Filter out any theme provided steps we don't want.
+ $theme_steps = array_filter(
+ $theme_steps,
+ static function( $step ) use( $invalid_steps ) {
+ // Don't install assets from URLs.
+ if (
+ ! empty( $step['themeZipFile']['url'] ) ||
+ ! empty( $step['pluginZipFile']['url'] )
+ ) {
+ return false;
+ }
+
+ // Don't need to install the theme again.
+ if ( 'installTheme' === $step['step'] ) {
+ if ( $theme_data->slug === ( $step['themeZipFile']['slug'] ?? '' ) ) {
+ return false;
+ }
+ if ( ! empty( $theme_data->template ) && $theme_data->template === ( $step['themeZipFile']['slug'] ?? '' ) ) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+ );
+
+ // Fill in a theme starter content step if specified.
+ // See: `installThemeStarterContent`. https://github.com/WordPress/wordpress-playground/pull/1521
+ foreach ( $theme_steps as $i => $step ) {
+ if ( 'installThemeStarterContent' === $step['step'] ) {
+ $theme_steps[ $i ] = $this->get_install_starter_content_step();
+ }
+ }
+
+ // If the theme didn't provide a blueprint, we'll also install the Starter Content. This must be done last.
+ // See also: `installThemeStarterContent`. https://github.com/WordPress/wordpress-playground/pull/1521
+ if ( ! $theme_blueprint ) {
+ $final_steps[] = $this->get_install_starter_content_step();
+ }
+
+ /*
+ * TODO: These artifacts need to be hosted somewhere better.
+ */
+ $final_steps[] = [
+ 'step' => 'installPlugin',
+ 'pluginZipFile' => [
+ 'resource' => 'url',
+ 'url' => 'https://raw.githubusercontent.com/WordPress/wordpress.org/trunk/wp-themes.com/public_html/wp-content/plugins/pattern-page.zip',
+ ],
+ ];
+ $final_steps[] = [
+ 'step' => 'installPlugin',
+ 'pluginZipFile' => [
+ 'resource' => 'url',
+ 'url' => 'https://raw.githubusercontent.com/WordPress/wordpress.org/trunk/wp-themes.com/public_html/wp-content/plugins/style-variations.zip',
+ ],
+ ];
+
+ // Set the steps.
+ $blueprint['steps'] = array_merge(
+ array_filter( $required_steps ),
+ array_values( $theme_steps ),
+ $final_steps
+ );
+
+ return $blueprint;
+ }
+
+ /**
+ * Returns a formed step to install the starter content.
+ */
+ function get_install_starter_content_step() {
+ return [
+ 'step' => 'runPHP',
+ 'code' => '<?php
+ playground_add_filter( "plugins_loaded", "importThemeStarterContent_plugins_loaded", 0 );
+ function importThemeStarterContent_plugins_loaded() {
+ /* Set as the admin user, this ensures we can customize the site. */
+ wp_set_current_user(
+ get_users( [ "role" => "Administrator" ] )[0]
+ );
+
+ /*
+ * Simulate this request as a ajax customizer save, with the current theme in preview mode.
+ *
+ * See _wp_customize_include()
+ */
+ add_filter( "wp_doing_ajax", "__return_true" );
+ $_REQUEST["action"] = "customize_save";
+ $_REQUEST["wp_customize"] = "on";
+ $_REQUEST["customize_theme"] = get_stylesheet();
+ $_GET = $_REQUEST;
+
+ /* Force the site to be fresh, although it should already be, some themes require this. */
+ add_filter( "pre_option_fresh_site", "__return_true" );
+ }
+
+ require "/wordpress/wp-load.php";
+
+ if ( ! get_theme_starter_content() ) {
+ return;
+ }
+
+ /* Import the Starter Content. */
+ $wp_customize->import_theme_starter_content();
+
+ /* Publish the changeset, which publishes the starter content. */
+ wp_publish_post( $wp_customize->changeset_post_id() );
+ '
+ ];
+ }
+}
+new Theme_Preview();
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api.php 2024-06-21 03:40:26 UTC (rev 13850)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api.php 2024-06-21 03:44:30 UTC (rev 13851)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -26,7 +26,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'rest_api_init', function() {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( preg_match( '!^/themes/(\d\.\d)/!', $wp->query_vars['rest_route'], $m ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( preg_match( '!^/themes/(\d\.\d)/!', $wp->query_vars['rest_route'] ?? '', $m ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> define( 'THEMES_API_VERSION', $m[1] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -55,4 +55,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/rest-api/class-themes-auto-review.php';
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/rest-api/class-theme-categorization.php';
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/rest-api/class-theme-review-stats.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ include __DIR__ . '/rest-api/class-theme-preview.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> } );
</span></span></pre>
</div>
</div>
</body>
</html>