Automattic\WooCommerce\Admin\API

Themes::upload_theme()publicWC 1.0

Upload and install a theme.

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

Хуки из метода

Возвращает

WP_Error|WP_REST_Response.

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

$Themes = new Themes();
$Themes->upload_theme( $request );
$request(WP_REST_Request) (обязательный)
Request data.

Код Themes::upload_theme() WC 9.3.3

public function upload_theme( $request ) {
	if (
		! isset( $_FILES['pluginzip'] ) || ! isset( $_FILES['pluginzip']['tmp_name'] ) || ! is_uploaded_file( $_FILES['pluginzip']['tmp_name'] ) || ! is_file( $_FILES['pluginzip']['tmp_name'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,  WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		return new \WP_Error( 'woocommerce_rest_invalid_file', __( 'Specified file failed upload test.', 'woocommerce' ) );
	}

	include_once ABSPATH . 'wp-admin/includes/file.php';
	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';

	$_GET['package'] = true;
	$file_upload     = new \File_Upload_Upgrader( 'pluginzip', 'package' );
	$upgrader        = new ThemeUpgrader( new ThemeUpgraderSkin() );
	$install         = $upgrader->install( $file_upload->package );

	if ( $install || is_wp_error( $install ) ) {
		$file_upload->cleanup();
	}

	if ( ! is_wp_error( $install ) && isset( $install['destination_name'] ) ) {
		$theme  = $install['destination_name'];
		$result = array(
			'status'  => 'success',
			'message' => $upgrader->strings['process_success'],
			'theme'   => $theme,
		);

		/**
		 * Fires when a theme is successfully installed.
		 *
		 * @param string $theme The theme name.
		 */
		do_action( 'woocommerce_theme_installed', $theme );
	} else {
		if ( is_wp_error( $install ) && $install->get_error_code() ) {
			$error_message = isset( $upgrader->strings[ $install->get_error_code() ] ) ? $upgrader->strings[ $install->get_error_code() ] : $install->get_error_data();
		} else {
			$error_message = $upgrader->strings['process_failed'];
		}

		$result = array(
			'status'  => 'error',
			'message' => $error_message,
		);
	}

	$response = $this->prepare_item_for_response( $result, $request );
	$data     = $this->prepare_response_for_collection( $response );

	return rest_ensure_response( $data );
}