* REST API Onboarding Themes Controller
* Handles requests to install and activate themes.
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
* Onboarding Themes Controller.
* @extends WC_REST_Data_Controller
class OnboardingThemes extends \WC_REST_Data_Controller {
protected $namespace = 'wc-admin';
protected $rest_base = 'onboarding/themes';
public function register_routes() {
'/' . $this->rest_base . '/install',
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install_theme' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'schema' => array( $this, 'get_item_schema' ),
'/' . $this->rest_base . '/activate',
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_theme' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'schema' => array( $this, 'get_item_schema' ),
* Check if a given request has access to manage themes.
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
public function update_item_permissions_check( $request ) {
if ( ! current_user_can( 'switch_themes' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage themes.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
* Installs the requested theme.
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Theme installation status.
public function install_theme( $request ) {
$theme = sanitize_text_field( $request['theme'] );
$installed_themes = wp_get_themes();
if ( in_array( $theme, array_keys( $installed_themes ), true ) ) {
'name' => $installed_themes[ $theme ]->get( 'Name' ),
include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/theme-install.php';
include_once ABSPATH . '/wp-admin/includes/theme.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . '/wp-admin/includes/class-theme-upgrader.php';
if ( is_wp_error( $api ) ) {
'woocommerce_rest_theme_install',
/* translators: %s: theme slug (example: woocommerce-services) */
__( 'The requested theme `%s` could not be installed. Theme API call failed.', 'woocommerce' ),
$upgrader = new \Theme_Upgrader( new \Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );
if ( is_wp_error( $result ) || is_null( $result ) ) {
'woocommerce_rest_theme_install',
/* translators: %s: theme slug (example: woocommerce-services) */
__( 'The requested theme `%s` could not be installed.', 'woocommerce' ),
* Activate the requested theme.
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Theme activation status.
public function activate_theme( $request ) {
$theme = sanitize_text_field( $request['theme'] );
require_once ABSPATH . 'wp-admin/includes/theme.php';
$installed_themes = wp_get_themes();
if ( ! in_array( $theme, array_keys( $installed_themes ), true ) ) {
/* translators: %s: theme slug (example: woocommerce-services) */
return new \WP_Error( 'woocommerce_rest_invalid_theme', sprintf( __( 'Invalid theme %s.', 'woocommerce' ), $theme ), 404 );
$result = switch_theme( $theme );
if ( ! is_null( $result ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_theme', sprintf( __( 'The requested theme could not be activated.', 'woocommerce' ), $theme ), 500 );
'name' => $installed_themes[ $theme ]->get( 'Name' ),
* Get the schema, conforming to JSON Schema.
public function get_item_schema() {
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'onboarding_theme',
'description' => __( 'Theme slug.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
'description' => __( 'Theme name.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
'description' => __( 'Theme status.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
return $this->add_additional_fields_schema( $schema );