Theme_Upgrader::upgrade()

最后更新于:2021-11-26 04:20:23

Theme_Upgrader::upgrade( string$theme, array$args=array())

Upgrade a theme.

参数

$theme

(string) (Required) The theme slug.

$args

(array) (Optional) Other arguments for upgrading a theme.

  • ‘clear_update_cache’
    (bool) Whether to clear the update cache if successful. Default true.

Default value: array()

响应

(bool|GC_Error) True if the upgrade was successful, false or a GC_Error object otherwise.

源文件

文件: gc-admin/includes/class-theme-upgrader.php

	public function upgrade( $theme, $args = array() ) {
		$defaults    = array(
			'clear_update_cache' => true,
		);
		$parsed_args = gc_parse_args( $args, $defaults );

		$this->init();
		$this->upgrade_strings();

		// Is an update available?
		$current = get_site_transient( 'update_themes' );
		if ( ! isset( $current->response[ $theme ] ) ) {
			$this->skin->before();
			$this->skin->set_result( false );
			$this->skin->error( 'up_to_date' );
			$this->skin->after();
			return false;
		}

		$r = $current->response[ $theme ];

		add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
		add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
		if ( $parsed_args['clear_update_cache'] ) {
			// Clear cache so gc_update_themes() knows about the new theme.
			add_action( 'upgrader_process_complete', 'gc_clean_themes_cache', 9, 0 );
		}

		$this->run(
			array(
				'package'           => $r['package'],
				'destination'       => get_theme_root( $theme ),
				'clear_destination' => true,
				'clear_working'     => true,
				'hook_extra'        => array(
					'theme'  => $theme,
					'type'   => 'theme',
					'action' => 'update',
				),
			)
		);

		remove_action( 'upgrader_process_complete', 'gc_clean_themes_cache', 9 );
		remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
		remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );

		if ( ! $this->result || is_gc_error( $this->result ) ) {
			return $this->result;
		}

		gc_clean_themes_cache( $parsed_args['clear_update_cache'] );

		// Ensure any future auto-update failures trigger a failure email by removing
		// the last failure notification from the list when themes update successfully.
		$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );

		if ( isset( $past_failure_emails[ $theme ] ) ) {
			unset( $past_failure_emails[ $theme ] );
			update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
		}

		return true;
	}