return $colors[ $status ];
}
+
+ /**
+ * Search wordpress.org plugin repo
+ *
+ * @param object $api data from WP plugin/theme API
+ * @param array $fields Data fields to display in table.
+ * @param string $data_type Plugin or Theme api endpoint
+ */
+ public function search( $api, $fields, $data_type = 'plugin' ) {
+
+ // Sanitize to 1 of 2 types
+ $data_type = 'plugin' === $data_type ? 'plugin' : 'theme';
+ $plural = $data_type . 's';
+ $data = $api->$plural;
+ $count = isset( $api->info['results'] ) ? $api->info['results'] : count( $data );
+
+ if ( is_wp_error( $api ) )
+ \WP_CLI::error( $api->get_error_message() . __( ' Try again' ) );
+
+ if ( ! isset( $data ) )
+ \WP_CLI::error( __( 'API error. Try Again.' ) );
+
+ \WP_CLI::success( $count .' '. $plural .' Found. \'search=$key\' in place of slug available for '. $data_type .' commands.' );
+
+ foreach ( $data as $key => $item ) {
+ $item->key = $key;
+ $data[$key] = $item;
+ }
+
+ $set = set_site_transient( 'wpcli-$data_type-search-data', $data, 60*60 );
+
+ \WP_CLI\Utils\format_items( 'table', $data, array_merge( array( 'key' ), $fields ) );
+
+ }
+
+ /**
+ * Parse the name of a plugin to check if 'search=' exists, and check search transient for the key
+ *
+ * @param string name
+ * @return string
+ */
+ public function parse_search_key( $name, $data_type = 'plugin' ) {
+
+ // Sanitize to 1 of 2 types
+ $data_type = 'plugin' === $data_type ? 'plugin' : 'theme';
+
+ if ( false !== strpos( $name, 'search=' ) ) {
+ $search_key = (int) str_replace( 'search=', '', $name );
+ if ( ( $trans = get_site_transient( 'wpcli-$data_type-search-data' ) ) && isset( $trans[$search_key] ) )
+ $name = $trans[$search_key]->slug;
+ else
+ \WP_CLI::error( 'There is no recent search with that key.' );
+ }
+ return $name;
+ }
+
}
$per_page = isset( $assoc_args['per_page'] ) ? (int) $assoc_args['per_page'] : 10;
$fields = isset( $assoc_args['fields'] ) ? $assoc_args['fields'] : array( 'name', 'slug', 'author_profile', 'rating' );
- if ( $term ) {
- $api = plugins_api( 'query_plugins', array(
- 'per_page' => $per_page,
- 'search' => $term,
- ) );
+ $api = plugins_api( 'query_plugins', array(
+ 'per_page' => $per_page,
+ 'search' => $term,
+ ) );
- if ( is_wp_error( $api ) )
- WP_CLI::error( $api->get_error_message() . __( ' Try again' ) );
-
- if ( ! isset( $api->plugins ) )
- WP_CLI::error( $api->get_error_message() . __( 'API error. Try Again.' ) );
-
- WP_CLI::success( count( $api->plugins ). ' Plugins Found. Use slug for other plugin methods.' );
-
- WP_CLI\Utils\format_items( 'table', $api->plugins, $fields );
- }
+ parent::search( $api, $fields, 'plugin' );
}
protected function status_single( $args ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
$details = $this->get_details( $file );
* @synopsis <plugin> [--network]
*/
function activate( $args, $assoc_args = array() ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
$network_wide = isset( $assoc_args['network'] );
* @synopsis <plugin> [--network]
*/
function deactivate( $args, $assoc_args = array() ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
$network_wide = isset( $assoc_args['network'] );
* @synopsis <plugin> [--network]
*/
function toggle( $args, $assoc_args = array() ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
$network_wide = isset( $assoc_args['network'] );
* @synopsis <plugin> [--version=<version>]
*/
function update( $args, $assoc_args ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$basename = $this->parse_name( $name );
if ( isset( $assoc_args['version'] ) && 'dev' == $assoc_args['version'] ) {
* @synopsis <plugin|zip|url> [--version=<version>] [--force] [--activate]
*/
function install( $args, $assoc_args ) {
+ $args[0] = $this->parse_search_key( $args[0] );
parent::install( $args, $assoc_args );
}
* @synopsis <plugin> [--no-delete]
*/
function uninstall( $args, $assoc_args = array() ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
if ( is_plugin_active( $file ) ) {
* @synopsis <plugin>
*/
function delete( $args, $assoc_args = array() ) {
- $name = $args[0];
+ $name = $this->parse_search_key( $args[0] );
$file = $this->parse_name( $name );
if ( $this->_delete( $file ) ) {
}
/**
+ * Parse the name of a plugin to check if 'search=' exists, and check search transient for the key
+ *
+ * @param string name
+ * @return string
+ */
+ public function parse_search_key( $name ) {
+ return parent::parse_search_key( $name, 'plugin' );
+ }
+
+ /**
* Converts a plugin basename back into a friendly slug.
*/
private function get_name( $file ) {
parent::status( $args );
}
+ /**
+ * Search wordpress.org theme repo
+ *
+ * ## OPTIONS
+ *
+ * <theme>
+ * : A particular theme to search for.
+ *
+ * --per_page
+ * : Optional number of results to display. Defaults to 10.
+ *
+ * --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
+ *
+ * ## EXAMPLES
+ *
+ * wp theme search automattic --per_page=20
+ *
+ * wp theme search automattic --fields=name,version,slug,rating,num_ratings,description
+ *
+ * @synopsis <theme> [--per_page=<per_page>] [--fields=<fields>]
+ */
+ public function search( $args, $assoc_args = array() ) {
+ $term = $args[0];
+ $per_page = isset( $assoc_args['per_page'] ) ? (int) $assoc_args['per_page'] : 10;
+ $fields = isset( $assoc_args['fields'] ) ? $assoc_args['fields'] : array( 'name', 'slug', 'author', 'rating' );
+
+ $api = themes_api( 'query_themes', array(
+ 'per_page' => $per_page,
+ 'search' => $term,
+ ) );
+
+ parent::search( $api, $fields, 'theme' );
+
+ }
+
protected function status_single( $args ) {
$theme = $this->parse_name( $args[0] );
* @synopsis <theme|zip|url> [--version=<version>] [--force] [--activate]
*/
function install( $args, $assoc_args ) {
+ $args[0] = $this->parse_search_key( $args[0] );
parent::install( $args, $assoc_args );
}
* @return object
*/
private function parse_name( $name ) {
- $theme = wp_get_theme( $name );
+ $theme = wp_get_theme( $this->parse_search_key( $name ) );
if ( !$theme->exists() ) {
WP_CLI::error( "The theme '$name' could not be found." );
return $theme;
}
+
+ /**
+ * Parse the name of a theme to check if 'search=' exists, and check search transient for the key
+ *
+ * @param string name
+ * @return string
+ */
+ public function parse_search_key( $name ) {
+ return parent::parse_search_key( $name, 'theme' );
+ }
}
WP_CLI::add_command( 'theme', 'Theme_Command' );