From e989c8cf733d5bbea88ea429c2522c141280297c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 10:29:58 +0100 Subject: [PATCH 1/8] Add command to update a meta field for a user --- src/php/wp-cli/commands/internals/user.php | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/php/wp-cli/commands/internals/user.php b/src/php/wp-cli/commands/internals/user.php index 7515979d4e..34df9cc24b 100644 --- a/src/php/wp-cli/commands/internals/user.php +++ b/src/php/wp-cli/commands/internals/user.php @@ -156,4 +156,32 @@ public function update( $args, $assoc_args ) { WP_CLI::success( "Updated user $updated_id." ); } } + + /** + * Update meta field for a user + * + * @param array $args + * @param array $assoc_args + **/ + public function update_meta( $args, $assoc_args ) { + $user_id = $args[0]; + $meta_key = $args[1]; + $meta_value = $args[2]; + + if ( ! is_numeric($user_id) ) { + WP_CLI::error( "User ID required (see 'wp user help')." ); + } + + if ( ! $meta_key || ! $meta_value ) { + WP_CLI:error( "meta_key and meta_value required (see 'wp user help')."); + } + + $success = update_user_meta( $user_id, $meta_key, $meta_value ); + + if ( $success ) { + WP_CLI::success( "Updated user $user_id." ); + } else { + WP_CLI::error( "Failed to update meta field" ); + } + } } From c3a8bbd5d7cdf95de9a3ae69c9ef7c02258d631c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 10:33:48 +0100 Subject: [PATCH 2/8] Factor out duplicated error message string --- src/php/wp-cli/commands/internals/user.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/php/wp-cli/commands/internals/user.php b/src/php/wp-cli/commands/internals/user.php index 34df9cc24b..2c70241dd9 100644 --- a/src/php/wp-cli/commands/internals/user.php +++ b/src/php/wp-cli/commands/internals/user.php @@ -63,7 +63,7 @@ public function delete( $args, $assoc_args ) { $user_id = $args[0]; if ( ! is_numeric($user_id) ) { - WP_CLI::error("User ID required (see 'wp user help')"); + self::error_see_help( "User ID required" ); } $defaults = array( 'reassign' => NULL ); @@ -90,7 +90,7 @@ public function create( $args, $assoc_args ) { $user_email = $args[1]; if ( ! $user_login || ! $user_email ) { - WP_CLI::error("Login and email required (see 'wp user help')."); + self::error_see_help( "Login and email required" ); } $defaults = array( @@ -139,7 +139,7 @@ public function update( $args, $assoc_args ) { $user_id = $args[0]; if ( ! is_numeric($user_id) ) { - WP_CLI::error( "User ID required (see 'wp user help')." ); + self::error_see_help( "User ID required" ); } if ( empty( $assoc_args ) ) { @@ -169,11 +169,11 @@ public function update_meta( $args, $assoc_args ) { $meta_value = $args[2]; if ( ! is_numeric($user_id) ) { - WP_CLI::error( "User ID required (see 'wp user help')." ); + self::error_see_help( "User ID required" ); } if ( ! $meta_key || ! $meta_value ) { - WP_CLI:error( "meta_key and meta_value required (see 'wp user help')."); + self::error_see_help( "meta_key and meta_value required"); } $success = update_user_meta( $user_id, $meta_key, $meta_value ); @@ -184,4 +184,8 @@ public function update_meta( $args, $assoc_args ) { WP_CLI::error( "Failed to update meta field" ); } } + + private function error_see_help( $message ) { + WP_CLI::error( "$message (see 'wp user help')."); + } } From 534d7b922d6ec93aa5f442d1e3d2160ffd27b4d5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 10:39:49 +0100 Subject: [PATCH 3/8] Factor out code to retrieve args in update_meta --- src/php/wp-cli/commands/internals/user.php | 30 ++++++++++++++-------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/php/wp-cli/commands/internals/user.php b/src/php/wp-cli/commands/internals/user.php index 2c70241dd9..2c124b0e61 100644 --- a/src/php/wp-cli/commands/internals/user.php +++ b/src/php/wp-cli/commands/internals/user.php @@ -164,17 +164,9 @@ public function update( $args, $assoc_args ) { * @param array $assoc_args **/ public function update_meta( $args, $assoc_args ) { - $user_id = $args[0]; - $meta_key = $args[1]; - $meta_value = $args[2]; - - if ( ! is_numeric($user_id) ) { - self::error_see_help( "User ID required" ); - } - - if ( ! $meta_key || ! $meta_value ) { - self::error_see_help( "meta_key and meta_value required"); - } + $user_id = self::get_numeric_arg_or_error($args, 0, "User ID"); + $meta_key = self::get_arg_or_error($args, 1, "meta_key");; + $meta_value = self::get_arg_or_error($args, 2, "meta_value");; $success = update_user_meta( $user_id, $meta_key, $meta_value ); @@ -185,6 +177,22 @@ public function update_meta( $args, $assoc_args ) { } } + private function get_numeric_arg_or_error( $args, $index, $name ) { + $value = self::get_arg_or_error( $args, $index, $name ); + if ( ! is_numeric( $value ) ) { + self::error_see_help( "$name must be numeric" ); + } + return $value; + } + + private function get_arg_or_error( $args, $index, $name ) { + $value = $args[$index]; + if ( ! $value ) { + self::error_see_help( "$name required" ); + } + return $value; + } + private function error_see_help( $message ) { WP_CLI::error( "$message (see 'wp user help')."); } From 487f5c7ffcfb4d5313e42b1b63a4b29ff59e7bd4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 10:41:20 +0100 Subject: [PATCH 4/8] Factor out fetching user ID from $args --- src/php/wp-cli/commands/internals/user.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/php/wp-cli/commands/internals/user.php b/src/php/wp-cli/commands/internals/user.php index 2c124b0e61..1b408dc36e 100644 --- a/src/php/wp-cli/commands/internals/user.php +++ b/src/php/wp-cli/commands/internals/user.php @@ -59,12 +59,8 @@ public function _list( $args, $assoc_args ) { **/ public function delete( $args, $assoc_args ) { global $blog_id; - - $user_id = $args[0]; - - if ( ! is_numeric($user_id) ) { - self::error_see_help( "User ID required" ); - } + + $user_id = self::get_numeric_arg_or_error($args, 0, "User ID"); $defaults = array( 'reassign' => NULL ); @@ -136,11 +132,7 @@ public function create( $args, $assoc_args ) { * @param array $assoc_args **/ public function update( $args, $assoc_args ) { - $user_id = $args[0]; - - if ( ! is_numeric($user_id) ) { - self::error_see_help( "User ID required" ); - } + $user_id = self::get_numeric_arg_or_error($args, 0, "User ID"); if ( empty( $assoc_args ) ) { WP_CLI::error( "Need some fields to update." ); From 900f62515fc772a92f640b4bb64885d7af24db43 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 10:53:56 +0100 Subject: [PATCH 5/8] Add man page for user update_meta --- src/doc/user-update_meta.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/doc/user-update_meta.txt diff --git a/src/doc/user-update_meta.txt b/src/doc/user-update_meta.txt new file mode 100644 index 0000000000..74616807d8 --- /dev/null +++ b/src/doc/user-update_meta.txt @@ -0,0 +1,24 @@ +wp-user-update_meta(1) -- Update a meta field for a WordPress user. +==== + +## SYNOPSIS + +`wp user update_meta` + +## OPTIONS + +* ``: + + The ID of the user to update. + +* meta_key: + + The meta_key to be updated + +* meta_value: + + The new desired value of the meta_key + +## EXAMPLES + + wp user update_meta 123 description "Mary is a WordPress developer" From cd96aabb2882d02bc6ddb9e876f0829aae96017b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 11:37:28 +0100 Subject: [PATCH 6/8] Use isset to check if an argument exists --- src/php/wp-cli/commands/internals/user.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/php/wp-cli/commands/internals/user.php b/src/php/wp-cli/commands/internals/user.php index 1b408dc36e..2b67398211 100644 --- a/src/php/wp-cli/commands/internals/user.php +++ b/src/php/wp-cli/commands/internals/user.php @@ -178,11 +178,10 @@ private function get_numeric_arg_or_error( $args, $index, $name ) { } private function get_arg_or_error( $args, $index, $name ) { - $value = $args[$index]; - if ( ! $value ) { + if ( ! isset( $args[$index] ) ) { self::error_see_help( "$name required" ); } - return $value; + return $args[$index]; } private function error_see_help( $message ) { From a06cb5dcb872d5bc3888bce47b09b533139a1167 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 May 2012 13:11:47 +0100 Subject: [PATCH 7/8] Change "wp user update_meta" to "wp user-meta set" --- src/doc/user-meta-set.txt | 24 +++++++++ src/doc/user-update_meta.txt | 24 --------- .../wp-cli/commands/internals/user-meta.php | 51 +++++++++++++++++++ src/php/wp-cli/commands/internals/user.php | 20 -------- 4 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 src/doc/user-meta-set.txt delete mode 100644 src/doc/user-update_meta.txt create mode 100644 src/php/wp-cli/commands/internals/user-meta.php diff --git a/src/doc/user-meta-set.txt b/src/doc/user-meta-set.txt new file mode 100644 index 0000000000..b40cc0cc4b --- /dev/null +++ b/src/doc/user-meta-set.txt @@ -0,0 +1,24 @@ +wp-user-meta-set(1) -- Update a meta field for a WordPress user. +==== + +## SYNOPSIS + +`wp user-meta set` + +## OPTIONS + +* ``: + + The ID of the user to update. + +* meta_key: + + The meta_key to be updated + +* meta_value: + + The new desired value of the meta_key + +## EXAMPLES + + wp user-meta set 123 description "Mary is a WordPress developer" diff --git a/src/doc/user-update_meta.txt b/src/doc/user-update_meta.txt deleted file mode 100644 index 74616807d8..0000000000 --- a/src/doc/user-update_meta.txt +++ /dev/null @@ -1,24 +0,0 @@ -wp-user-update_meta(1) -- Update a meta field for a WordPress user. -==== - -## SYNOPSIS - -`wp user update_meta` - -## OPTIONS - -* ``: - - The ID of the user to update. - -* meta_key: - - The meta_key to be updated - -* meta_value: - - The new desired value of the meta_key - -## EXAMPLES - - wp user update_meta 123 description "Mary is a WordPress developer" diff --git a/src/php/wp-cli/commands/internals/user-meta.php b/src/php/wp-cli/commands/internals/user-meta.php new file mode 100644 index 0000000000..7636eff275 --- /dev/null +++ b/src/php/wp-cli/commands/internals/user-meta.php @@ -0,0 +1,51 @@ + Date: Thu, 31 May 2012 13:20:40 +0100 Subject: [PATCH 8/8] Check for --url before it's unset by WP_CLI::_set_url --- src/php/wp-cli/wp-cli.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/php/wp-cli/wp-cli.php b/src/php/wp-cli/wp-cli.php index ed2a2da563..a34d4a9d3b 100755 --- a/src/php/wp-cli/wp-cli.php +++ b/src/php/wp-cli/wp-cli.php @@ -52,9 +52,6 @@ define( 'WP_ROOT', $_SERVER['PWD'] . '/' ); } -// Handle --url and --blog parameters -WP_CLI::_set_url( $assoc_args ); - if ( array( 'core', 'download' ) == $arguments ) { WP_CLI::run_command( $arguments, $assoc_args ); exit; @@ -88,6 +85,9 @@ define( 'WP_INSTALLING', true ); } +// Handle --url and --blog parameters +WP_CLI::_set_url( $assoc_args ); + // Load WordPress require WP_ROOT . 'wp-load.php'; require ABSPATH . 'wp-admin/includes/admin.php';