OSDN Git Service

replace parse_name() with validate_plugin_names().
authorscribu <mail@scribu.net>
Fri, 30 Aug 2013 15:12:53 +0000 (18:12 +0300)
committerscribu <mail@scribu.net>
Fri, 30 Aug 2013 16:02:37 +0000 (19:02 +0300)
features/plugin.feature
php/WP_CLI/CommandWithUpgrade.php
php/commands/plugin.php

index 299a116..26bd999 100644 (file)
@@ -44,10 +44,9 @@ Feature: Manage WordPress plugins
       | zombieland | active | none   | 0.1-alpha |
 
     When I try `wp plugin uninstall zombieland`
-    Then the return code should be 1
-    And STDERR should contain:
+    Then STDERR should contain:
       """
-      The plugin is active.
+      The 'zombieland' plugin is active.
       """
 
     When I run `wp plugin deactivate zombieland`
@@ -61,10 +60,9 @@ Feature: Manage WordPress plugins
     And the {PLUGIN_DIR}/zombieland file should not exist
 
     When I try the previous command again
-    Then the return code should be 1
-    And STDERR should contain:
+    Then STDERR should contain:
       """
-      The plugin 'zombieland' could not be found.
+      The 'zombieland' plugin could not be found.
       """
 
   Scenario: Install a plugin, activate, then force install an older version of the plugin
index d1a327c..04d365d 100644 (file)
@@ -11,7 +11,14 @@ abstract class CommandWithUpgrade extends \WP_CLI_Command {
        abstract protected function get_upgrader_class( $force );
 
        abstract protected function get_item_list();
+
+       /**
+        * @param array List of update candidates
+        * @param array List of item names
+        * @return array List of update candidates
+        */
        abstract protected function filter_item_list( $items, $args );
+
        abstract protected function get_all_items();
 
        abstract protected function get_status( $file );
index 0cc5914..5aeeed7 100644 (file)
@@ -97,20 +97,23 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function status_single( $args ) {
-               $name = $args[0];
-               $file = $this->parse_name( $name );
+               $plugins = $this->validate_plugin_names( $args );
+               if ( empty( $plugins ) )
+                       exit(1);
 
-               $details = $this->get_details( $file );
+               list( $plugin ) = $plugins;
 
-               $status = $this->format_status( $this->get_status( $file ), 'long' );
+               $details = $this->get_details( $plugin->file );
 
-               $version = $details[ 'Version' ];
+               $status = $this->format_status( $this->get_status( $plugin->file ), 'long' );
 
-               if ( $this->has_update( $file ) )
+               $version = $details['Version'];
+
+               if ( $this->has_update( $plugin->file ) )
                        $version .= ' (%gUpdate available%n)';
 
                echo WP_CLI::colorize( \WP_CLI\Utils\mustache_render( 'plugin-status.mustache', array(
-                       'slug' => $name,
+                       'slug' => $plugin->name,
                        'status' => $status,
                        'version' => $version,
                        'name' => $details['Name'],
@@ -149,20 +152,16 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function activate( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $args as $name ) {
-                       $file = $this->parse_name( $name );
-                       if ( !$file )
-                               continue;
-
-                       activate_plugin( $file, '', $network_wide );
+               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       activate_plugin( $plugin->file, '', $network_wide );
 
-                       if ( $this->check_active( $file, $network_wide ) ) {
+                       if ( $this->check_active( $plugin->file, $network_wide ) ) {
                                if ( $network_wide )
-                                       WP_CLI::success( "Plugin '$name' network activated." );
+                                       WP_CLI::success( "Plugin '{$plugin->name}' network activated." );
                                else
-                                       WP_CLI::success( "Plugin '$name' activated." );
+                                       WP_CLI::success( "Plugin '{$plugin->name}' activated." );
                        } else {
-                               WP_CLI::warning( 'Could not activate plugin: ' . $name );
+                               WP_CLI::warning( "Could not activate the '{$plugin->name}' plugin." );
                        }
                }
        }
@@ -183,20 +182,16 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function deactivate( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $args as $name ) {
-                       $file = $this->parse_name( $name );
-                       if ( !$file )
-                               continue;
-
-                       deactivate_plugins( $file, false, $network_wide );
+               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       deactivate_plugins( $plugin->file, false, $network_wide );
 
-                       if ( ! $this->check_active( $file, $network_wide ) ) {
+                       if ( ! $this->check_active( $plugin->file, $network_wide ) ) {
                                if ( $network_wide )
-                                       WP_CLI::success( "Plugin '$name' network deactivated." );
+                                       WP_CLI::success( "Plugin '{$plugin->name}' network deactivated." );
                                else
-                                       WP_CLI::success( "Plugin '$name' deactivated." );
+                                       WP_CLI::success( "Plugin '{$plugin->name}' deactivated." );
                        } else {
-                               WP_CLI::warning( 'Could not deactivate plugin: ' . $name );
+                               WP_CLI::warning( "Could not deactivate the '{$plugin->name}' plugin." );
                        }
                }
        }
@@ -217,15 +212,11 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        function toggle( $args, $assoc_args = array() ) {
                $network_wide = isset( $assoc_args['network'] );
 
-               foreach ( $args as $name ) {
-                       $file = $this->parse_name( $name );
-                       if ( !$file )
-                               continue;
-
-                       if ( $this->check_active( $file, $network_wide ) ) {
-                               $this->deactivate( array( $name ), $assoc_args );
+               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       if ( $this->check_active( $plugin->file, $network_wide ) ) {
+                               $this->deactivate( array( $plugin->name ), $assoc_args );
                        } else {
-                               $this->activate( array( $name ), $assoc_args );
+                               $this->activate( array( $plugin->name ), $assoc_args );
                        }
                }
        }
@@ -253,11 +244,13 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                $path = untrailingslashit( WP_PLUGIN_DIR );
 
                if ( !empty( $args ) ) {
-                       $file = $this->parse_name( $args[0] );
-                       if ( !$file )
+                       $plugins = $this->validate_plugin_names( $args );
+                       if ( empty( $plugins ) )
                                return;
 
-                       $path .= '/' . $file;
+                       list( $plugin ) = $plugins;
+
+                       $path .= '/' . $plugin->file;
 
                        if ( isset( $assoc_args['dir'] ) )
                                $path = dirname( $path );
@@ -318,9 +311,9 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         */
        function update( $args, $assoc_args ) {
                if ( isset( $assoc_args['version'] ) && 'dev' == $assoc_args['version'] ) {
-                       foreach ( $args as $arg ) {
-                               $this->_delete( $this->parse_name( $arg ) );
-                               $this->install( array( $arg ), $assoc_args );
+                       foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                               $this->_delete( $plugin->file );
+                               $this->install( array( $plugin->name ), $assoc_args );
                        }
                } else {
                        parent::update_many( $args, $assoc_args );
@@ -344,7 +337,7 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        protected function filter_item_list( $items, $args ) {
-               $basenames = array_map( array( $this, 'parse_name' ), $args );
+               $basenames = wp_list_pluck( $this->validate_plugin_names( $args ), 'file' );
                return \WP_CLI\Utils\pick_fields( $items, $basenames );
        }
 
@@ -403,27 +396,23 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *
         *     wp plugin uninstall hello
         *
-        * @synopsis <plugin> [--no-delete]
+        * @synopsis <plugin>... [--no-delete]
         */
        function uninstall( $args, $assoc_args = array() ) {
-               $name = $args[0];
-               $file = $this->parse_name( $name );
-
-               if ( is_plugin_active( $file ) ) {
-                       WP_CLI::error( 'The plugin is active.' );
-               }
-
-               uninstall_plugin( $file );
+               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       if ( is_plugin_active( $plugin->file ) ) {
+                               WP_CLI::warning( "The '{$plugin->name}' plugin is active." );
+                               continue;
+                       }
 
-               if ( isset( $assoc_args['no-delete'] ) )
-                       return;
+                       uninstall_plugin( $plugin->file );
 
-               if ( $this->_delete( $file ) ) {
-                       WP_CLI::success( sprintf( "Uninstalled '%s' plugin.", $name ) );
+                       if ( !isset( $assoc_args['no-delete'] ) && $this->_delete( $plugin->file ) ) {
+                               WP_CLI::success( "Uninstalled '$plugin->name' plugin." );
+                       }
                }
        }
 
-
        /**
         * Check if the plugin is installed
         *
@@ -459,14 +448,13 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
         *
         *     wp plugin delete hello
         *
-        * @synopsis <plugin>
+        * @synopsis <plugin>...
         */
        function delete( $args, $assoc_args = array() ) {
-               $name = $args[0];
-               $file = $this->parse_name( $name );
-
-               if ( $this->_delete( $file ) ) {
-                       WP_CLI::success( sprintf( "Deleted '%s' plugin.", $name ) );
+               foreach ( $this->validate_plugin_names( $args ) as $plugin ) {
+                       if ( $this->_delete( $plugin->file ) ) {
+                               WP_CLI::success( "Deleted '{$plugin->name}' plugin." );
+                       }
                }
        }
 
@@ -546,13 +534,22 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
                return $file;
        }
 
-       private function parse_name( $name ) {
-               if ( $file = $this->_parse_name( $name ) ) {
-                       return $file;
-               } else {
-                       WP_CLI::warning( "The plugin '$name' could not be found." );
-                       return false;
+       private function validate_plugin_names( $args ) {
+               $plugins = array();
+
+               foreach ( $args as $name ) {
+                       $file = $this->_parse_name( $name );
+                       if ( $file ) {
+                               $plugins[] = (object) array(
+                                       'name' => $name,
+                                       'file' => $file
+                               );
+                       } else {
+                               WP_CLI::warning( "The '$name' plugin could not be found." );
+                       }
                }
+
+               return $plugins;
        }
 
        /**