wp/wp-admin/includes/class-theme-upgrader.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    36 	 * @var bool $bulk
    36 	 * @var bool $bulk
    37 	 */
    37 	 */
    38 	public $bulk = false;
    38 	public $bulk = false;
    39 
    39 
    40 	/**
    40 	/**
       
    41 	 * New theme info.
       
    42 	 *
       
    43 	 * @since 5.5.0
       
    44 	 * @var array $new_theme_data
       
    45 	 *
       
    46 	 * @see check_package()
       
    47 	 */
       
    48 	public $new_theme_data = array();
       
    49 
       
    50 	/**
    41 	 * Initialize the upgrade strings.
    51 	 * Initialize the upgrade strings.
    42 	 *
    52 	 *
    43 	 * @since 2.8.0
    53 	 * @since 2.8.0
    44 	 */
    54 	 */
    45 	public function upgrade_strings() {
    55 	public function upgrade_strings() {
    46 		$this->strings['up_to_date'] = __( 'The theme is at the latest version.' );
    56 		$this->strings['up_to_date'] = __( 'The theme is at the latest version.' );
    47 		$this->strings['no_package'] = __( 'Update package not available.' );
    57 		$this->strings['no_package'] = __( 'Update package not available.' );
    48 		/* translators: %s: package URL */
    58 		/* translators: %s: Package URL. */
    49 		$this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
    59 		$this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
    50 		$this->strings['unpack_package']      = __( 'Unpacking the update&#8230;' );
    60 		$this->strings['unpack_package']      = __( 'Unpacking the update&#8230;' );
    51 		$this->strings['remove_old']          = __( 'Removing the old version of the theme&#8230;' );
    61 		$this->strings['remove_old']          = __( 'Removing the old version of the theme&#8230;' );
    52 		$this->strings['remove_old_failed']   = __( 'Could not remove the old theme.' );
    62 		$this->strings['remove_old_failed']   = __( 'Could not remove the old theme.' );
    53 		$this->strings['process_failed']      = __( 'Theme update failed.' );
    63 		$this->strings['process_failed']      = __( 'Theme update failed.' );
    59 	 *
    69 	 *
    60 	 * @since 2.8.0
    70 	 * @since 2.8.0
    61 	 */
    71 	 */
    62 	public function install_strings() {
    72 	public function install_strings() {
    63 		$this->strings['no_package'] = __( 'Installation package not available.' );
    73 		$this->strings['no_package'] = __( 'Installation package not available.' );
    64 		/* translators: %s: package URL */
    74 		/* translators: %s: Package URL. */
    65 		$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
    75 		$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
    66 		$this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
    76 		$this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
    67 		$this->strings['installing_package']  = __( 'Installing the theme&#8230;' );
    77 		$this->strings['installing_package']  = __( 'Installing the theme&#8230;' );
       
    78 		$this->strings['remove_old']          = __( 'Removing the old version of the theme&#8230;' );
       
    79 		$this->strings['remove_old_failed']   = __( 'Could not remove the old theme.' );
    68 		$this->strings['no_files']            = __( 'The theme contains no files.' );
    80 		$this->strings['no_files']            = __( 'The theme contains no files.' );
    69 		$this->strings['process_failed']      = __( 'Theme installation failed.' );
    81 		$this->strings['process_failed']      = __( 'Theme installation failed.' );
    70 		$this->strings['process_success']     = __( 'Theme installed successfully.' );
    82 		$this->strings['process_success']     = __( 'Theme installed successfully.' );
    71 		/* translators: 1: theme name, 2: version */
    83 		/* translators: 1: Theme name, 2: Theme version. */
    72 		$this->strings['process_success_specific'] = __( 'Successfully installed the theme <strong>%1$s %2$s</strong>.' );
    84 		$this->strings['process_success_specific'] = __( 'Successfully installed the theme <strong>%1$s %2$s</strong>.' );
    73 		$this->strings['parent_theme_search']      = __( 'This theme requires a parent theme. Checking if it is installed&#8230;' );
    85 		$this->strings['parent_theme_search']      = __( 'This theme requires a parent theme. Checking if it is installed&#8230;' );
    74 		/* translators: 1: theme name, 2: version */
    86 		/* translators: 1: Theme name, 2: Theme version. */
    75 		$this->strings['parent_theme_prepare_install'] = __( 'Preparing to install <strong>%1$s %2$s</strong>&#8230;' );
    87 		$this->strings['parent_theme_prepare_install'] = __( 'Preparing to install <strong>%1$s %2$s</strong>&#8230;' );
    76 		/* translators: 1: theme name, 2: version */
    88 		/* translators: 1: Theme name, 2: Theme version. */
    77 		$this->strings['parent_theme_currently_installed'] = __( 'The parent theme, <strong>%1$s %2$s</strong>, is currently installed.' );
    89 		$this->strings['parent_theme_currently_installed'] = __( 'The parent theme, <strong>%1$s %2$s</strong>, is currently installed.' );
    78 		/* translators: 1: theme name, 2: version */
    90 		/* translators: 1: Theme name, 2: Theme version. */
    79 		$this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, <strong>%1$s %2$s</strong>.' );
    91 		$this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, <strong>%1$s %2$s</strong>.' );
    80 		/* translators: %s: theme name */
    92 		/* translators: %s: Theme name. */
    81 		$this->strings['parent_theme_not_found'] = sprintf( __( '<strong>The parent theme could not be found.</strong> You will need to install the parent theme, %s, before you can use this child theme.' ), '<strong>%s</strong>' );
    93 		$this->strings['parent_theme_not_found'] = sprintf( __( '<strong>The parent theme could not be found.</strong> You will need to install the parent theme, %s, before you can use this child theme.' ), '<strong>%s</strong>' );
       
    94 		/* translators: %s: Theme error. */
       
    95 		$this->strings['current_theme_has_errors'] = __( 'The current theme has the following error: "%s".' );
       
    96 
       
    97 		if ( ! empty( $this->skin->overwrite ) ) {
       
    98 			if ( 'update-theme' === $this->skin->overwrite ) {
       
    99 				$this->strings['installing_package'] = __( 'Updating the theme&#8230;' );
       
   100 				$this->strings['process_failed']     = __( 'Theme update failed.' );
       
   101 				$this->strings['process_success']    = __( 'Theme updated successfully.' );
       
   102 			}
       
   103 
       
   104 			if ( 'downgrade-theme' === $this->skin->overwrite ) {
       
   105 				$this->strings['installing_package'] = __( 'Downgrading the theme&#8230;' );
       
   106 				$this->strings['process_failed']     = __( 'Theme downgrade failed.' );
       
   107 				$this->strings['process_success']    = __( 'Theme downgraded successfully.' );
       
   108 			}
       
   109 		}
    82 	}
   110 	}
    83 
   111 
    84 	/**
   112 	/**
    85 	 * Check if a child theme is being installed and we need to install its parent.
   113 	 * Check if a child theme is being installed and we need to install its parent.
    86 	 *
   114 	 *
    89 	 * @since 3.4.0
   117 	 * @since 3.4.0
    90 	 *
   118 	 *
    91 	 * @param bool  $install_result
   119 	 * @param bool  $install_result
    92 	 * @param array $hook_extra
   120 	 * @param array $hook_extra
    93 	 * @param array $child_result
   121 	 * @param array $child_result
    94 	 * @return type
   122 	 * @return bool
    95 	 */
   123 	 */
    96 	public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
   124 	public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
    97 		// Check to see if we need to install a parent theme
   125 		// Check to see if we need to install a parent theme.
    98 		$theme_info = $this->theme_info();
   126 		$theme_info = $this->theme_info();
    99 
   127 
   100 		if ( ! $theme_info->parent() ) {
   128 		if ( ! $theme_info->parent() ) {
   101 			return $install_result;
   129 			return $install_result;
   102 		}
   130 		}
   117 				'fields' => array(
   145 				'fields' => array(
   118 					'sections' => false,
   146 					'sections' => false,
   119 					'tags'     => false,
   147 					'tags'     => false,
   120 				),
   148 				),
   121 			)
   149 			)
   122 		); //Save on a bit of bandwidth.
   150 		); // Save on a bit of bandwidth.
   123 
   151 
   124 		if ( ! $api || is_wp_error( $api ) ) {
   152 		if ( ! $api || is_wp_error( $api ) ) {
   125 			$this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
   153 			$this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
   126 			// Don't show activate or preview actions after installation
   154 			// Don't show activate or preview actions after installation.
   127 			add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
   155 			add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
   128 			return $install_result;
   156 			return $install_result;
   129 		}
   157 		}
   130 
   158 
   131 		// Backup required data we're going to override:
   159 		// Backup required data we're going to override:
   132 		$child_api             = $this->skin->api;
   160 		$child_api             = $this->skin->api;
   133 		$child_success_message = $this->strings['process_success'];
   161 		$child_success_message = $this->strings['process_success'];
   134 
   162 
   135 		// Override them
   163 		// Override them.
   136 		$this->skin->api                           = $api;
   164 		$this->skin->api = $api;
   137 		$this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
   165 
       
   166 		$this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];
   138 
   167 
   139 		$this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
   168 		$this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
   140 
   169 
   141 		add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
   170 		add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
   142 
   171 
   143 		// Install the parent theme
   172 		// Install the parent theme.
   144 		$parent_result = $this->run(
   173 		$parent_result = $this->run(
   145 			array(
   174 			array(
   146 				'package'           => $api->download_link,
   175 				'package'           => $api->download_link,
   147 				'destination'       => get_theme_root(),
   176 				'destination'       => get_theme_root(),
   148 				'clear_destination' => false, //Do not overwrite files.
   177 				'clear_destination' => false, // Do not overwrite files.
   149 				'clear_working'     => true,
   178 				'clear_working'     => true,
   150 			)
   179 			)
   151 		);
   180 		);
   152 
   181 
   153 		if ( is_wp_error( $parent_result ) ) {
   182 		if ( is_wp_error( $parent_result ) ) {
   154 			add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
   183 			add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
   155 		}
   184 		}
   156 
   185 
   157 		// Start cleaning up after the parents installation
   186 		// Start cleaning up after the parent's installation.
   158 		remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
   187 		remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
   159 
   188 
   160 		// Reset child's result and data
   189 		// Reset child's result and data.
   161 		$this->result                     = $child_result;
   190 		$this->result                     = $child_result;
   162 		$this->skin->api                  = $child_api;
   191 		$this->skin->api                  = $child_api;
   163 		$this->strings['process_success'] = $child_success_message;
   192 		$this->strings['process_success'] = $child_success_message;
   164 
   193 
   165 		return $install_result;
   194 		return $install_result;
   197 	 * }
   226 	 * }
   198 	 *
   227 	 *
   199 	 * @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
   228 	 * @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
   200 	 */
   229 	 */
   201 	public function install( $package, $args = array() ) {
   230 	public function install( $package, $args = array() ) {
   202 
       
   203 		$defaults    = array(
   231 		$defaults    = array(
   204 			'clear_update_cache' => true,
   232 			'clear_update_cache' => true,
       
   233 			'overwrite_package'  => false, // Do not overwrite files.
   205 		);
   234 		);
   206 		$parsed_args = wp_parse_args( $args, $defaults );
   235 		$parsed_args = wp_parse_args( $args, $defaults );
   207 
   236 
   208 		$this->init();
   237 		$this->init();
   209 		$this->install_strings();
   238 		$this->install_strings();
   210 
   239 
   211 		add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
   240 		add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
   212 		add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
   241 		add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
       
   242 
   213 		if ( $parsed_args['clear_update_cache'] ) {
   243 		if ( $parsed_args['clear_update_cache'] ) {
   214 			// Clear cache so wp_update_themes() knows about the new theme.
   244 			// Clear cache so wp_update_themes() knows about the new theme.
   215 			add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
   245 			add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
   216 		}
   246 		}
   217 
   247 
   218 		$this->run(
   248 		$this->run(
   219 			array(
   249 			array(
   220 				'package'           => $package,
   250 				'package'           => $package,
   221 				'destination'       => get_theme_root(),
   251 				'destination'       => get_theme_root(),
   222 				'clear_destination' => false, //Do not overwrite files.
   252 				'clear_destination' => $parsed_args['overwrite_package'],
   223 				'clear_working'     => true,
   253 				'clear_working'     => true,
   224 				'hook_extra'        => array(
   254 				'hook_extra'        => array(
   225 					'type'   => 'theme',
   255 					'type'   => 'theme',
   226 					'action' => 'install',
   256 					'action' => 'install',
   227 				),
   257 				),
   234 
   264 
   235 		if ( ! $this->result || is_wp_error( $this->result ) ) {
   265 		if ( ! $this->result || is_wp_error( $this->result ) ) {
   236 			return $this->result;
   266 			return $this->result;
   237 		}
   267 		}
   238 
   268 
   239 		// Refresh the Theme Update information
   269 		// Refresh the Theme Update information.
   240 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
   270 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
       
   271 
       
   272 		if ( $parsed_args['overwrite_package'] ) {
       
   273 			/**
       
   274 			 * Fires when the upgrader has successfully overwritten a currently installed
       
   275 			 * plugin or theme with an uploaded zip package.
       
   276 			 *
       
   277 			 * @since 5.5.0
       
   278 			 *
       
   279 			 * @param string  $package        The package file.
       
   280 			 * @param array   $new_theme_data The new theme data.
       
   281 			 * @param string  $package_type   The package type (plugin or theme).
       
   282 			 */
       
   283 			do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
       
   284 		}
   241 
   285 
   242 		return true;
   286 		return true;
   243 	}
   287 	}
   244 
   288 
   245 	/**
   289 	/**
   256 	 *                                    Default true.
   300 	 *                                    Default true.
   257 	 * }
   301 	 * }
   258 	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
   302 	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
   259 	 */
   303 	 */
   260 	public function upgrade( $theme, $args = array() ) {
   304 	public function upgrade( $theme, $args = array() ) {
   261 
       
   262 		$defaults    = array(
   305 		$defaults    = array(
   263 			'clear_update_cache' => true,
   306 			'clear_update_cache' => true,
   264 		);
   307 		);
   265 		$parsed_args = wp_parse_args( $args, $defaults );
   308 		$parsed_args = wp_parse_args( $args, $defaults );
   266 
   309 
   310 			return $this->result;
   353 			return $this->result;
   311 		}
   354 		}
   312 
   355 
   313 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
   356 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
   314 
   357 
       
   358 		// Ensure any future auto-update failures trigger a failure email by removing
       
   359 		// the last failure notification from the list when themes update successfully.
       
   360 		$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
       
   361 
       
   362 		if ( isset( $past_failure_emails[ $theme ] ) ) {
       
   363 			unset( $past_failure_emails[ $theme ] );
       
   364 			update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
       
   365 		}
       
   366 
   315 		return true;
   367 		return true;
   316 	}
   368 	}
   317 
   369 
   318 	/**
   370 	/**
   319 	 * Upgrade several themes at once.
   371 	 * Upgrade several themes at once.
   329 	 *                                    Default true.
   381 	 *                                    Default true.
   330 	 * }
   382 	 * }
   331 	 * @return array[]|false An array of results, or false if unable to connect to the filesystem.
   383 	 * @return array[]|false An array of results, or false if unable to connect to the filesystem.
   332 	 */
   384 	 */
   333 	public function bulk_upgrade( $themes, $args = array() ) {
   385 	public function bulk_upgrade( $themes, $args = array() ) {
   334 
       
   335 		$defaults    = array(
   386 		$defaults    = array(
   336 			'clear_update_cache' => true,
   387 			'clear_update_cache' => true,
   337 		);
   388 		);
   338 		$parsed_args = wp_parse_args( $args, $defaults );
   389 		$parsed_args = wp_parse_args( $args, $defaults );
   339 
   390 
   347 		add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
   398 		add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
   348 		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
   399 		add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
   349 
   400 
   350 		$this->skin->header();
   401 		$this->skin->header();
   351 
   402 
   352 		// Connect to the Filesystem first.
   403 		// Connect to the filesystem first.
   353 		$res = $this->fs_connect( array( WP_CONTENT_DIR ) );
   404 		$res = $this->fs_connect( array( WP_CONTENT_DIR ) );
   354 		if ( ! $res ) {
   405 		if ( ! $res ) {
   355 			$this->skin->footer();
   406 			$this->skin->footer();
   356 			return false;
   407 			return false;
   357 		}
   408 		}
   358 
   409 
   359 		$this->skin->bulk_header();
   410 		$this->skin->bulk_header();
   360 
   411 
   361 		// Only start maintenance mode if:
   412 		/*
   362 		// - running Multisite and there are one or more themes specified, OR
   413 		 * Only start maintenance mode if:
   363 		// - a theme with an update available is currently in use.
   414 		 * - running Multisite and there are one or more themes specified, OR
   364 		// @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
   415 		 * - a theme with an update available is currently in use.
       
   416 		 * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
       
   417 		 */
   365 		$maintenance = ( is_multisite() && ! empty( $themes ) );
   418 		$maintenance = ( is_multisite() && ! empty( $themes ) );
   366 		foreach ( $themes as $theme ) {
   419 		foreach ( $themes as $theme ) {
   367 			$maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
   420 			$maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
   368 		}
   421 		}
   369 		if ( $maintenance ) {
   422 		if ( $maintenance ) {
   370 			$this->maintenance_mode( true );
   423 			$this->maintenance_mode( true );
   371 		}
   424 		}
   372 
   425 
   386 				$this->skin->after();
   439 				$this->skin->after();
   387 				$results[ $theme ] = true;
   440 				$results[ $theme ] = true;
   388 				continue;
   441 				continue;
   389 			}
   442 			}
   390 
   443 
   391 			// Get the URL to the zip file
   444 			// Get the URL to the zip file.
   392 			$r = $current->response[ $theme ];
   445 			$r = $current->response[ $theme ];
   393 
   446 
   394 			$result = $this->run(
   447 			$result = $this->run(
   395 				array(
   448 				array(
   396 					'package'           => $r['package'],
   449 					'package'           => $r['package'],
   404 				)
   457 				)
   405 			);
   458 			);
   406 
   459 
   407 			$results[ $theme ] = $this->result;
   460 			$results[ $theme ] = $this->result;
   408 
   461 
   409 			// Prevent credentials auth screen from displaying multiple times
   462 			// Prevent credentials auth screen from displaying multiple times.
   410 			if ( false === $result ) {
   463 			if ( false === $result ) {
   411 				break;
   464 				break;
   412 			}
   465 			}
   413 		} //end foreach $plugins
   466 		} // End foreach $themes.
   414 
   467 
   415 		$this->maintenance_mode( false );
   468 		$this->maintenance_mode( false );
   416 
   469 
   417 		// Refresh the Theme Update information
   470 		// Refresh the Theme Update information.
   418 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
   471 		wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
   419 
   472 
   420 		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
   473 		/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
   421 		do_action(
   474 		do_action(
   422 			'upgrader_process_complete',
   475 			'upgrader_process_complete',
   436 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   489 		// Cleanup our hooks, in case something else does a upgrade on this connection.
   437 		remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
   490 		remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
   438 		remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
   491 		remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
   439 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
   492 		remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
   440 
   493 
       
   494 		// Ensure any future auto-update failures trigger a failure email by removing
       
   495 		// the last failure notification from the list when themes update successfully.
       
   496 		$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
       
   497 
       
   498 		foreach ( $results as $theme => $result ) {
       
   499 			// Maintain last failure notification when themes failed to update manually.
       
   500 			if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $theme ] ) ) {
       
   501 				continue;
       
   502 			}
       
   503 
       
   504 			unset( $past_failure_emails[ $theme ] );
       
   505 		}
       
   506 
       
   507 		update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
       
   508 
   441 		return $results;
   509 		return $results;
   442 	}
   510 	}
   443 
   511 
   444 	/**
   512 	/**
   445 	 * Check that the package source contains a valid theme.
   513 	 * Check that the package source contains a valid theme.
   456 	 * @return string|WP_Error The source or a WP_Error.
   524 	 * @return string|WP_Error The source or a WP_Error.
   457 	 */
   525 	 */
   458 	public function check_package( $source ) {
   526 	public function check_package( $source ) {
   459 		global $wp_filesystem;
   527 		global $wp_filesystem;
   460 
   528 
       
   529 		$this->new_theme_data = array();
       
   530 
   461 		if ( is_wp_error( $source ) ) {
   531 		if ( is_wp_error( $source ) ) {
   462 			return $source;
   532 			return $source;
   463 		}
   533 		}
   464 
   534 
   465 		// Check the folder contains a valid theme
   535 		// Check that the folder contains a valid theme.
   466 		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
   536 		$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
   467 		if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
   537 		if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
   468 			return $source;
   538 			return $source;
   469 		}
   539 		}
   470 
   540 
   471 		// A proper archive should have a style.css file in the single subdirectory
   541 		// A proper archive should have a style.css file in the single subdirectory.
   472 		if ( ! file_exists( $working_directory . 'style.css' ) ) {
   542 		if ( ! file_exists( $working_directory . 'style.css' ) ) {
   473 			return new WP_Error(
   543 			return new WP_Error(
   474 				'incompatible_archive_theme_no_style',
   544 				'incompatible_archive_theme_no_style',
   475 				$this->strings['incompatible_archive'],
   545 				$this->strings['incompatible_archive'],
   476 				sprintf(
   546 				sprintf(
   479 					'<code>style.css</code>'
   549 					'<code>style.css</code>'
   480 				)
   550 				)
   481 			);
   551 			);
   482 		}
   552 		}
   483 
   553 
       
   554 		// All these headers are needed on Theme_Installer_Skin::do_overwrite().
   484 		$info = get_file_data(
   555 		$info = get_file_data(
   485 			$working_directory . 'style.css',
   556 			$working_directory . 'style.css',
   486 			array(
   557 			array(
   487 				'Name'     => 'Theme Name',
   558 				'Name'        => 'Theme Name',
   488 				'Template' => 'Template',
   559 				'Version'     => 'Version',
       
   560 				'Author'      => 'Author',
       
   561 				'Template'    => 'Template',
       
   562 				'RequiresWP'  => 'Requires at least',
       
   563 				'RequiresPHP' => 'Requires PHP',
   489 			)
   564 			)
   490 		);
   565 		);
   491 
   566 
   492 		if ( empty( $info['Name'] ) ) {
   567 		if ( empty( $info['Name'] ) ) {
   493 			return new WP_Error(
   568 			return new WP_Error(
   512 					'<code>index.php</code>'
   587 					'<code>index.php</code>'
   513 				)
   588 				)
   514 			);
   589 			);
   515 		}
   590 		}
   516 
   591 
       
   592 		$requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
       
   593 		$requires_wp  = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
       
   594 
       
   595 		if ( ! is_php_version_compatible( $requires_php ) ) {
       
   596 			$error = sprintf(
       
   597 				/* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
       
   598 				__( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
       
   599 				phpversion(),
       
   600 				$requires_php
       
   601 			);
       
   602 
       
   603 			return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
       
   604 		}
       
   605 		if ( ! is_wp_version_compatible( $requires_wp ) ) {
       
   606 			$error = sprintf(
       
   607 				/* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
       
   608 				__( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
       
   609 				$GLOBALS['wp_version'],
       
   610 				$requires_wp
       
   611 			);
       
   612 
       
   613 			return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
       
   614 		}
       
   615 
       
   616 		$this->new_theme_data = $info;
       
   617 
   517 		return $source;
   618 		return $source;
   518 	}
   619 	}
   519 
   620 
   520 	/**
   621 	/**
   521 	 * Turn on maintenance mode before attempting to upgrade the current theme.
   622 	 * Turn on maintenance mode before attempting to upgrade the current theme.
   523 	 * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
   624 	 * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
   524 	 * Theme_Upgrader::bulk_upgrade().
   625 	 * Theme_Upgrader::bulk_upgrade().
   525 	 *
   626 	 *
   526 	 * @since 2.8.0
   627 	 * @since 2.8.0
   527 	 *
   628 	 *
   528 	 * @param bool|WP_Error  $return
   629 	 * @param bool|WP_Error $return Upgrade offer return.
   529 	 * @param array          $theme
   630 	 * @param array         $theme  Theme arguments.
   530 	 * @return bool|WP_Error
   631 	 * @return bool|WP_Error The passed in $return param or WP_Error.
   531 	 */
   632 	 */
   532 	public function current_before( $return, $theme ) {
   633 	public function current_before( $return, $theme ) {
   533 		if ( is_wp_error( $return ) ) {
   634 		if ( is_wp_error( $return ) ) {
   534 			return $return;
   635 			return $return;
   535 		}
   636 		}
   536 
   637 
   537 		$theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
   638 		$theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
   538 
   639 
   539 		if ( $theme != get_stylesheet() ) { //If not current
   640 		// Only run if current theme
       
   641 		if ( get_stylesheet() !== $theme ) {
   540 			return $return;
   642 			return $return;
   541 		}
   643 		}
   542 		//Change to maintenance mode now.
   644 
       
   645 		// Change to maintenance mode. Bulk edit handles this separately.
   543 		if ( ! $this->bulk ) {
   646 		if ( ! $this->bulk ) {
   544 			$this->maintenance_mode( true );
   647 			$this->maintenance_mode( true );
   545 		}
   648 		}
   546 
   649 
   547 		return $return;
   650 		return $return;
   553 	 * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
   656 	 * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
   554 	 * and Theme_Upgrader::bulk_upgrade().
   657 	 * and Theme_Upgrader::bulk_upgrade().
   555 	 *
   658 	 *
   556 	 * @since 2.8.0
   659 	 * @since 2.8.0
   557 	 *
   660 	 *
   558 	 * @param bool|WP_Error  $return
   661 	 * @param bool|WP_Error $return Upgrade offer return.
   559 	 * @param array          $theme
   662 	 * @param array         $theme  Theme arguments.
   560 	 * @return bool|WP_Error
   663 	 * @return bool|WP_Error The passed in $return param or WP_Error.
   561 	 */
   664 	 */
   562 	public function current_after( $return, $theme ) {
   665 	public function current_after( $return, $theme ) {
   563 		if ( is_wp_error( $return ) ) {
   666 		if ( is_wp_error( $return ) ) {
   564 			return $return;
   667 			return $return;
   565 		}
   668 		}
   566 
   669 
   567 		$theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
   670 		$theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
   568 
   671 
   569 		if ( $theme != get_stylesheet() ) { // If not current
   672 		// Only run if current theme.
       
   673 		if ( get_stylesheet() !== $theme ) {
   570 			return $return;
   674 			return $return;
   571 		}
   675 		}
   572 
   676 
   573 		// Ensure stylesheet name hasn't changed after the upgrade:
   677 		// Ensure stylesheet name hasn't changed after the upgrade:
   574 		if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
   678 		if ( get_stylesheet() === $theme && $theme !== $this->result['destination_name'] ) {
   575 			wp_clean_themes_cache();
   679 			wp_clean_themes_cache();
   576 			$stylesheet = $this->result['destination_name'];
   680 			$stylesheet = $this->result['destination_name'];
   577 			switch_theme( $stylesheet );
   681 			switch_theme( $stylesheet );
   578 		}
   682 		}
   579 
   683 
   580 		//Time to remove maintenance mode
   684 		// Time to remove maintenance mode. Bulk edit handles this separately.
   581 		if ( ! $this->bulk ) {
   685 		if ( ! $this->bulk ) {
   582 			$this->maintenance_mode( false );
   686 			$this->maintenance_mode( false );
   583 		}
   687 		}
   584 		return $return;
   688 		return $return;
   585 	}
   689 	}
   632 	 *                      the directory name from the last result will be used.
   736 	 *                      the directory name from the last result will be used.
   633 	 * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
   737 	 * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
   634 	 *                        and the last result isn't set.
   738 	 *                        and the last result isn't set.
   635 	 */
   739 	 */
   636 	public function theme_info( $theme = null ) {
   740 	public function theme_info( $theme = null ) {
   637 
       
   638 		if ( empty( $theme ) ) {
   741 		if ( empty( $theme ) ) {
   639 			if ( ! empty( $this->result['destination_name'] ) ) {
   742 			if ( ! empty( $this->result['destination_name'] ) ) {
   640 				$theme = $this->result['destination_name'];
   743 				$theme = $this->result['destination_name'];
   641 			} else {
   744 			} else {
   642 				return false;
   745 				return false;
   643 			}
   746 			}
   644 		}
   747 		}
   645 		return wp_get_theme( $theme );
   748 
       
   749 		$theme = wp_get_theme( $theme );
       
   750 		$theme->cache_delete();
       
   751 
       
   752 		return $theme;
   646 	}
   753 	}
   647 
   754 
   648 }
   755 }