WP_CLI\CommandWithUpgrade::_search PHP Method

    protected function _search($args, $assoc_args)
    {
        $term = $args[0];
        $defaults = array('per-page' => 10, 'page' => 1, 'fields' => implode(',', array('name', 'slug', 'rating')));
        $assoc_args = array_merge($defaults, $assoc_args);
        $fields = array();
        foreach (explode(',', $assoc_args['fields']) as $field) {
            $fields[$field] = true;
        }
        $formatter = $this->get_formatter($assoc_args);
        $api_args = array('per_page' => (int) $assoc_args['per-page'], 'page' => (int) $assoc_args['page'], 'search' => $term, 'fields' => $fields);
        if ('plugin' == $this->item_type) {
            $api = plugins_api('query_plugins', $api_args);
        } else {
            $api = themes_api('query_themes', $api_args);
        }
        if (is_wp_error($api)) {
            \WP_CLI::error($api->get_error_message() . __(' Try again'));
        }
        $plural = $this->item_type . 's';
        if (!isset($api->{$plural})) {
            \WP_CLI::error(__('API error. Try Again.'));
        }
        $items = $api->{$plural};
        $count = \WP_CLI\Utils\get_flag_value($api->info, 'results', 'unknown');
        \WP_CLI::success(sprintf('Showing %s of %s %s.', count($items), $count, $plural));
        $formatter->display_items($items);
    }

Usage Example

Example #1
0
 /**
  * Search the wordpress.org theme repository.
  *
  * ## OPTIONS
  *
  * <search>
  * : The string to search for.
  *
  * [--per-page=<per-page>]
  * : Optional number of results to display. Defaults to 10.
  *
  * [--field=<field>]
  * : Prints the value of a single field for each plugin.
  *
  * [--fields=<fields>]
  * : Ask for specific fields from the API. Defaults to name,slug,author,rating. Acceptable values:
  *
  *     **name**: Theme Name
  *     **slug**: Theme Slug
  *     **version**: Current Version Number
  *     **author**: Theme Author
  *     **preview_url**: Theme Preview URL
  *     **screenshot_url**: Theme Screenshot URL
  *     **rating**: Theme Rating
  *     **num_ratings**: Number of Theme Ratings
  *     **homepage**: Theme Author's Homepage
  *     **description**: Theme Description
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * ## EXAMPLES
  *
  *     wp theme search automattic --per-page=20
  *
  *     wp theme search automattic --fields=name,version,slug,rating,num_ratings,description
  */
 public function search($args, $assoc_args)
 {
     parent::_search($args, $assoc_args);
 }