From 71bc41e65c5533e08727194763d539b07b57e73e Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Mon, 24 Jun 2024 06:49:23 +0000 Subject: [PATCH] Theme Directory: Add a rest api endpoint to set the blueprint for previews. See r13851. See https://github.com/WordPress/wporg-theme-directory/pull/118. git-svn-id: https://meta.svn.wordpress.org/sites/trunk@13854 74240141-8908-4e6f-9713-ba540dce6ec7 --- .../rest-api/class-theme-preview.php | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php b/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php index 5834113940..17c2f3b1bf 100644 --- a/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php +++ b/wordpress.org/public_html/wp-content/plugins/theme-directory/rest-api/class-theme-preview.php @@ -12,16 +12,31 @@ function __construct() { 'callback' => array( $this, 'preview' ), 'permission_callback' => '__return_true', ) ); + + register_rest_route( 'themes/v1', 'preview-blueprint/(?[^/]+)', array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'set_blueprint' ), + 'permission_callback' => function( $request ) { + $theme_data = wporg_themes_theme_information( $request['slug'] ); + $theme_package = new WPORG_Themes_Repo_Package( $theme_data->slug ); + + if ( ! empty( $theme_data->error ) || ! $theme_package->post_author ) { + return false; + } + + return ( + get_current_user_id() === $theme_package->post_author || + current_user_can( 'edit_post', $theme_package->ID ) + ); + }, + ) ); } /** * Generate a Blueprint for a theme preview. */ function preview( $request ) { - $theme_data = wporg_themes_query_api( - 'theme_information', - [ 'slug' => $request->get_param( 'slug' ) ] - ); + $theme_data = wporg_themes_theme_information( $request->get_param( 'slug' ) ); if ( ! empty( $theme_data->error ) ) { return new WP_Error( 'error', $theme_data->error ); @@ -30,13 +45,40 @@ function preview( $request ) { return $this->build_blueprint( $theme_data ); } + /** + * Set a Blueprint for a theme preview. + */ + function set_blueprint( $request ) { + $theme_data = wporg_themes_theme_information( $request['slug'] ); + $theme_package = new WPORG_Themes_Repo_Package( $theme_data->slug ); + + if ( ! empty( $theme_data->error ) ) { + return new WP_Error( 'error', $theme_data->error ); + } + + // Validate the blueprint, TODO expand upon this. + if ( + empty( $request['blueprint'] ) || + ! is_string( $request['blueprint'] ) || + ! ( $decoded_blueprint = json_decode( $request['blueprint'], true) ) || + empty( $decoded_blueprint['steps'] ) + ) { + return new WP_Error( 'error', 'Invalid Blueprint provided, verify the JSON validates.' ); + } + + // Save the blueprint. + update_post_meta( $theme_package->ID, 'preview_blueprint', $decoded_blueprint ); + + return $this->build_blueprint( $theme_data ); + } + /** * Generate a blueprint for previewing a theme. */ function build_blueprint( $theme_data ) { $theme_package = new WPORG_Themes_Repo_Package( $theme_data->slug ); $parent_package = $theme_data->template ? new WPORG_Themes_Repo_Package( $theme_data->template ) : false; - $theme_blueprint = $theme_package->blueprint; + $theme_blueprint = $theme_package->preview_blueprint; // Base blueprint. $blueprint = [