Automattic\WooCommerce\Admin\API

OnboardingThemes::install_theme()publicWC 1.0

Installs the requested theme.

Метод класса: OnboardingThemes{}

Хуков нет.

Возвращает

WP_Error|Массив. Theme installation status.

Использование

$OnboardingThemes = new OnboardingThemes();
$OnboardingThemes->install_theme( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Код OnboardingThemes::install_theme() WC 8.7.0

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 ) ) {
		return( array(
			'slug'   => $theme,
			'name'   => $installed_themes[ $theme ]->get( 'Name' ),
			'status' => 'success',
		) );
	}

	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';

	$api = themes_api(
		'theme_information',
		array(
			'slug'   => $theme,
			'fields' => array(
				'sections' => false,
			),
		)
	);

	if ( is_wp_error( $api ) ) {
		return new \WP_Error(
			'woocommerce_rest_theme_install',
			sprintf(
			/* translators: %s: theme slug (example: woocommerce-services) */
				__( 'The requested theme `%s` could not be installed. Theme API call failed.', 'woocommerce' ),
				$theme
			),
			500
		);
	}

	$upgrader = new \Theme_Upgrader( new \Automatic_Upgrader_Skin() );
	$result   = $upgrader->install( $api->download_link );

	if ( is_wp_error( $result ) || is_null( $result ) ) {
		return new \WP_Error(
			'woocommerce_rest_theme_install',
			sprintf(
			/* translators: %s: theme slug (example: woocommerce-services) */
				__( 'The requested theme `%s` could not be installed.', 'woocommerce' ),
				$theme
			),
			500
		);
	}

	return array(
		'slug'   => $theme,
		'name'   => $api->name,
		'status' => 'success',
	);
}