WP_CLI\CommandWithUpgrade::update_many PHP Method

update_many() protected method

protected update_many ( $args, $assoc_args )
    protected function update_many($args, $assoc_args)
    {
        call_user_func($this->upgrade_refresh);
        if (!empty($assoc_args['format']) && in_array($assoc_args['format'], array('json', 'csv'))) {
            $logger = new \WP_CLI\Loggers\Quiet();
            \WP_CLI::set_logger($logger);
        }
        if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'all') && empty($args)) {
            \WP_CLI::error("Please specify one or more {$this->item_type}s, or use --all.");
        }
        $items = $this->get_item_list();
        if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'all')) {
            $items = $this->filter_item_list($items, $args);
        }
        $items_to_update = wp_list_filter($items, array('update' => true));
        if (\WP_CLI\Utils\get_flag_value($assoc_args, 'dry-run')) {
            if (empty($items_to_update)) {
                \WP_CLI::line("No {$this->item_type} updates available.");
                return;
            }
            if (!empty($assoc_args['format']) && in_array($assoc_args['format'], array('json', 'csv'))) {
                \WP_CLI\Utils\format_items($assoc_args['format'], $items_to_update, array('name', 'status', 'version', 'update_version'));
            } else {
                if (!empty($assoc_args['format']) && 'summary' === $assoc_args['format']) {
                    \WP_CLI::line("Available {$this->item_type} updates:");
                    foreach ($items_to_update as $item_to_update => $info) {
                        \WP_CLI::log("{$info['title']} update from version {$info['version']} to version {$info['update_version']}");
                    }
                } else {
                    \WP_CLI::line("Available {$this->item_type} updates:");
                    \WP_CLI\Utils\format_items('table', $items_to_update, array('name', 'status', 'version', 'update_version'));
                }
            }
            return;
        }
        $result = array();
        // Only attempt to update if there is something to update
        if (!empty($items_to_update)) {
            $cache_manager = \WP_CLI::get_http_cache_manager();
            foreach ($items_to_update as $item) {
                $cache_manager->whitelist_package($item['update_package'], $this->item_type, $item['name'], $item['update_version']);
            }
            $upgrader = $this->get_upgrader($assoc_args);
            $result = $upgrader->bulk_upgrade(wp_list_pluck($items_to_update, 'update_id'));
        }
        // Let the user know the results.
        $num_to_update = count($items_to_update);
        $num_updated = count(array_filter($result));
        if ($num_to_update > 0) {
            if (!empty($assoc_args['format']) && 'summary' === $assoc_args['format']) {
                foreach ($items_to_update as $item_to_update => $info) {
                    $message = $result[$info['update_id']] !== null ? 'updated successfully' : 'did not update';
                    \WP_CLI::log("{$info['title']} {$message} from version {$info['version']} to version {$info['update_version']}");
                }
            } else {
                $status = array();
                foreach ($items_to_update as $item_to_update => $info) {
                    $status[$item_to_update] = array('name' => $info['name'], 'old_version' => $info['version'], 'new_version' => $info['update_version'], 'status' => $result[$info['update_id']] !== null ? 'Updated' : 'Error');
                }
                $format = 'table';
                if (!empty($assoc_args['format']) && in_array($assoc_args['format'], array('json', 'csv'))) {
                    $format = $assoc_args['format'];
                }
                \WP_CLI\Utils\format_items($format, $status, array('name', 'old_version', 'new_version', 'status'));
            }
        }
        Utils\report_batch_operation_results($this->item_type, 'update', $num_to_update, $num_updated, $num_to_update - $num_updated);
    }

Usage Example

Example #1
0
 /**
  * Update one or more plugins.
  *
  * ## OPTIONS
  *
  * [<plugin>...]
  * : One or more plugins to update.
  *
  * [--all]
  * : If set, all plugins that have updates will be updated.
  *
  * [--version=<version>]
  * : If set, the plugin will be updated to the specified version.
  *
  * [--dry-run]
  * : Preview which plugins would be updated.
  *
  * ## EXAMPLES
  *
  *     wp plugin update bbpress --version=dev
  *
  *     wp plugin update --all
  */
 function update($args, $assoc_args)
 {
     if (isset($assoc_args['version'])) {
         foreach ($this->fetcher->get_many($args) as $plugin) {
             $this->_delete($plugin);
             $assoc_args['force'] = 1;
             $this->install(array($plugin->name), $assoc_args);
         }
     } else {
         parent::update_many($args, $assoc_args);
     }
 }
All Usage Examples Of WP_CLI\CommandWithUpgrade::update_many