WC_Install::theme_background_installer()public staticWC 3.1.0

Install a theme from .org in the background via a cron job (used by installer - opt in).

Method of the class: WC_Install{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_Install::theme_background_installer( $theme_slug );
$theme_slug(string) (required)
Theme slug.

Changelog

Since 3.1.0 Introduced.

WC_Install::theme_background_installer() code WC 9.4.2

public static function theme_background_installer( $theme_slug ) {
	// Explicitly clear the event.
	$args = func_get_args();

	if ( ! empty( $theme_slug ) ) {
		// Suppress feedback.
		ob_start();

		try {
			$theme = wp_get_theme( $theme_slug );

			if ( ! $theme->exists() ) {
				require_once ABSPATH . 'wp-admin/includes/file.php';
				include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
				include_once ABSPATH . 'wp-admin/includes/theme.php';

				WP_Filesystem();

				$skin     = new Automatic_Upgrader_Skin();
				$upgrader = new Theme_Upgrader( $skin );
				$api      = themes_api(
					'theme_information',
					array(
						'slug'   => $theme_slug,
						'fields' => array( 'sections' => false ),
					)
				);
				$result   = $upgrader->install( $api->download_link );

				if ( is_wp_error( $result ) ) {
					throw new Exception( $result->get_error_message() );
				} elseif ( is_wp_error( $skin->result ) ) {
					throw new Exception( $skin->result->get_error_message() );
				} elseif ( is_null( $result ) ) {
					throw new Exception( 'Unable to connect to the filesystem. Please confirm your credentials.' );
				}
			}

			switch_theme( $theme_slug );
		} catch ( Exception $e ) {
			WC_Admin_Notices::add_custom_notice(
				$theme_slug . '_install_error',
				sprintf(
					// translators: 1: theme slug, 2: error message, 3: URL to install theme manually.
					__( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ),
					$theme_slug,
					$e->getMessage(),
					esc_url( admin_url( 'update.php?action=install-theme&theme=' . $theme_slug . '&_wpnonce=' . wp_create_nonce( 'install-theme_' . $theme_slug ) ) )
				)
			);
		}

		// Discard feedback.
		ob_end_clean();
	}
}