OSDN Git Service

move utilities for validating user ids/logins to FetcherUser class
authorscribu <mail@scribu.net>
Sat, 9 Nov 2013 21:54:22 +0000 (23:54 +0200)
committerscribu <mail@scribu.net>
Sat, 9 Nov 2013 21:54:22 +0000 (23:54 +0200)
php/WP_CLI/Fetcher.php [new file with mode: 0644]
php/WP_CLI/FetcherUser.php [new file with mode: 0644]
php/commands/user.php

diff --git a/php/WP_CLI/Fetcher.php b/php/WP_CLI/Fetcher.php
new file mode 100644 (file)
index 0000000..f1de345
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+namespace WP_CLI;
+
+interface Fetcher {
+
+       // Returns the object if found; otherwise returns false
+       function get( $id );
+
+       // Returns the object if found; otherwise calls WP_CLI::error()
+       function get_check( $id );
+
+       // Returns the list of found objects
+       function get_many( $ids );
+}
+
diff --git a/php/WP_CLI/FetcherUser.php b/php/WP_CLI/FetcherUser.php
new file mode 100644 (file)
index 0000000..1ed29bf
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace WP_CLI;
+
+class FetcherUser implements Fetcher {
+
+       public function get( $id_or_login ) {
+               if ( is_numeric( $id_or_login ) )
+                       $user = get_user_by( 'id', $id_or_login );
+               else
+                       $user = get_user_by( 'login', $id_or_login );
+
+               return $user;
+       }
+
+       public function get_check( $id_or_login ) {
+               $user = $this->get( $id_or_login );
+
+               if ( ! $user ) {
+                       \WP_CLI::error( "Invalid user ID or login: $id_or_login" );
+               }
+
+               return $user;
+       }
+
+       public function get_many( $ids_or_logins ) {
+               $users = array();
+
+               foreach ( $ids_or_logins as $id_or_login ) {
+                       $user = $this->get( $id_or_login );
+                       if ( $user ) {
+                               $users[] = $user;
+                       } else {
+                               \WP_CLI::warning( "Invalid user ID or login: $id_or_login" );
+                       }
+               }
+
+               return $users;
+       }
+}
+
index 95c4d05..cf18fcb 100644 (file)
@@ -17,6 +17,10 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
                'roles'
        );
 
+       public function __construct() {
+               $this->fetcher = new \WP_CLI\FetcherUser;
+       }
+
        /**
         * List users.
         *
@@ -95,7 +99,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         *     wp user get bob --format=json > bob.json
         */
        public function get( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
 
                if ( method_exists( $user, 'to_array' ) ) {
                        $user_data = $user->to_array();
@@ -129,11 +133,11 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
                        'reassign' => null
                ) );
 
-               foreach ( $args as $key => $arg ) {
-                       $args[ $key ] = self::get_user( $arg )->ID;
-               }
+               $users = $this->fetcher->get_many( $args );
+
+               parent::_delete( $users, $assoc_args, function ( $user, $assoc_args ) {
+                       $user_id = $user->ID;
 
-               parent::_delete( $args, $assoc_args, function ( $user_id, $assoc_args ) {
                        if ( is_multisite() ) {
                                $r = wpmu_delete_user( $user_id );
                        } else {
@@ -243,11 +247,13 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         *     wp user update mary --user_pass=marypass
         */
        public function update( $args, $assoc_args ) {
-               foreach ( $args as $key => $arg ) {
-                       $args[ $key ] = self::get_user( $arg )->ID;
+               $user_ids = array();
+
+               foreach ( $this->fetcher->get_many( $args ) as $user ) {
+                       $user_ids[] = $user->ID;
                }
 
-               parent::_update( $args, $assoc_args, 'wp_update_user' );
+               parent::_update( $user_ids, $assoc_args, 'wp_update_user' );
        }
 
        /**
@@ -321,7 +327,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand set-role
         */
        public function set_role( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
 
                $role = isset( $args[1] ) ? $args[1] : get_option( 'default_role' );
 
@@ -353,7 +359,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand add-role
         */
        public function add_role( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
 
                $role = $args[1];
 
@@ -381,7 +387,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand remove-role
         */
        public function remove_role( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
 
                if ( isset( $args[1] ) ) {
                        $role = $args[1];
@@ -419,7 +425,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand add-cap
         */
        public function add_cap( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
                if ( $user ) {
                        $cap  = $args[1];
                        $user->add_cap( $cap );
@@ -447,7 +453,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand remove-cap
         */
        public function remove_cap( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
                if ( $user ) {
                        $cap = $args[1];
                        $user->remove_cap( $cap );
@@ -472,7 +478,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
         * @subcommand list-caps
         */
        public function list_caps( $args, $assoc_args ) {
-               $user = self::get_user( $args[0] );
+               $user = $this->fetcher->get_check( $args[0] );
 
                if ( $user ) {
                        $user->get_role_caps();
@@ -488,19 +494,6 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
                }
        }
 
-       private static function get_user( $id_or_login ) {
-               if ( is_numeric( $id_or_login ) )
-                       $user = get_user_by( 'id', $id_or_login );
-               else
-                       $user = get_user_by( 'login', $id_or_login );
-
-               if ( ! $user ) {
-                       WP_CLI::warning( "Invalid user ID or login: $id_or_login" );
-               }
-
-               return $user;
-       }
-
        /**
         * Import users from a CSV file.
         *