OSDN Git Service

1) Move search method to CommandWithUpgrade parent class. 2) Add search method for...
authorjtsternberg <me@jtsternberg.com>
Mon, 5 Aug 2013 02:53:10 +0000 (22:53 -0400)
committerjtsternberg <me@jtsternberg.com>
Mon, 5 Aug 2013 02:53:10 +0000 (22:53 -0400)
php/WP_CLI/CommandWithUpgrade.php
php/commands/plugin.php
php/commands/theme.php

index aaac62e..0a6e901 100644 (file)
@@ -308,4 +308,60 @@ abstract class CommandWithUpgrade extends \WP_CLI_Command {
 
                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;
+       }
+
 }
index f3d6059..6e58e67 100644 (file)
@@ -85,27 +85,17 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                $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 );
@@ -155,7 +145,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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'] );
@@ -186,7 +176,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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'] );
@@ -217,7 +207,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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'] );
@@ -308,7 +298,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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'] ) {
@@ -405,6 +395,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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 );
        }
 
@@ -427,7 +418,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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 ) ) {
@@ -459,7 +450,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         * @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 ) ) {
@@ -545,6 +536,16 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        /**
+        * 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 ) {
index 33d8041..b5852c4 100644 (file)
@@ -36,6 +36,53 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
                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] );
 
@@ -199,6 +246,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
         * @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 );
        }
 
@@ -305,7 +353,7 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
         * @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." );
@@ -314,6 +362,16 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
 
                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' );