+ /**
+ * Upgrade several themes at once.
+ *
+ * @since 3.0.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
+ *
+ * @param array $themes The theme slugs.
+ * @param array $args {
+ * Optional. Other arguments for upgrading several themes at once. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the update cache if successful.
+ * Default true.
+ * }
+ * @return array[]|false An array of results, or false if unable to connect to the filesystem.
+ */
+ public function bulk_upgrade( $themes, $args = array() ) {
+
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->bulk = true;
+ $this->upgrade_strings();
+
+ $current = get_site_transient( 'update_themes' );
+
+ 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);
+
+ $this->skin->header();
+
+ // Connect to the Filesystem first.
+ $res = $this->fs_connect( array(WP_CONTENT_DIR) );
+ if ( ! $res ) {
+ $this->skin->footer();
+ return false;
+ }
+
+ $this->skin->bulk_header();
+
+ // Only start maintenance mode if:
+ // - running Multisite and there are one or more themes specified, OR
+ // - a theme with an update available is currently in use.
+ // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
+ $maintenance = ( is_multisite() && ! empty( $themes ) );
+ foreach ( $themes as $theme )
+ $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
+ if ( $maintenance )
+ $this->maintenance_mode(true);
+
+ $results = array();
+
+ $this->update_count = count($themes);
+ $this->update_current = 0;
+ foreach ( $themes as $theme ) {
+ $this->update_current++;
+
+ $this->skin->theme_info = $this->theme_info($theme);
+
+ if ( !isset( $current->response[ $theme ] ) ) {
+ $this->skin->set_result(true);
+ $this->skin->before();
+ $this->skin->feedback( 'up_to_date' );
+ $this->skin->after();
+ $results[$theme] = true;
+ continue;
+ }
+
+ // Get the URL to the zip file
+ $r = $current->response[ $theme ];
+
+ $result = $this->run( array(
+ 'package' => $r['package'],
+ 'destination' => get_theme_root( $theme ),
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'is_multi' => true,
+ 'hook_extra' => array(
+ 'theme' => $theme
+ ),
+ ) );
+
+ $results[$theme] = $this->result;
+
+ // Prevent credentials auth screen from displaying multiple times
+ if ( false === $result )
+ break;
+ } //end foreach $plugins
+
+ $this->maintenance_mode(false);
+
+ /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
+ do_action( 'upgrader_process_complete', $this, array(
+ 'action' => 'update',
+ 'type' => 'theme',
+ 'bulk' => true,
+ 'themes' => $themes,
+ ) );
+
+ $this->skin->bulk_footer();
+
+ $this->skin->footer();
+
+ // Cleanup our hooks, in case something else does a upgrade on this connection.
+ 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'));
+
+ // Refresh the Theme Update information
+ wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
+
+ return $results;
+ }
+
+ /**
+ * Check that the package source contains a valid theme.
+ *
+ * Hooked to the {@see 'upgrader_source_selection'} filter by {@see Theme_Upgrader::install()}.
+ * It will return an error if the theme doesn't have style.css or index.php
+ * files.
+ *
+ * @since 3.3.0
+ *
+ * @param string $source The full path to the package source.
+ * @return string|WP_Error The source or a WP_Error.
+ */
+ public function check_package( $source ) {
+ global $wp_filesystem;
+
+ if ( is_wp_error($source) )
+ return $source;
+
+ // Check the folder contains a valid theme
+ $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
+ if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
+ return $source;
+
+ // A proper archive should have a style.css file in the single subdirectory
+ if ( ! file_exists( $working_directory . 'style.css' ) )
+ return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>style.css</code> stylesheet.' ) );
+
+ $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
+
+ if ( empty( $info['Name'] ) )
+ return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'], __( "The <code>style.css</code> stylesheet doesn't contain a valid theme header." ) );
+
+ // If it's not a child theme, it must have at least an index.php to be legit.
+ if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) )
+ return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>index.php</code> file.' ) );
+
+ return $source;
+ }
+
+ /**
+ * Turn on maintenance mode before attempting to upgrade the current theme.
+ *
+ * Hooked to the {@see 'upgrader_pre_install'} filter by {@see Theme_Upgrader::upgrade()} and
+ * {@see Theme_Upgrader::bulk_upgrade()}.
+ *
+ * @since 2.8.0
+ */
+ public function current_before($return, $theme) {