From 143b874f761a46faca856c54293f6046ea2e8c39 Mon Sep 17 00:00:00 2001 From: Bunty Date: Wed, 26 Feb 2025 15:23:31 +0530 Subject: [PATCH 1/8] Add a new flag for getting post meta as a single value or not --- src/WP_CLI/CommandWithMeta.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index bee25719..b51bba71 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -142,6 +142,15 @@ function ( $a, $b ) use ( $orderby, $order ) { * * : The name of the meta field to get. * + * [--is-single=] + * : Whether to return a single value. This parameter has no effect if $key is not specified. + * --- + * default: 1 + * options: + * - 1 + * - 0 + * --- + * * [--format=] * : Get value in a particular format. * --- @@ -157,7 +166,7 @@ public function get( $args, $assoc_args ) { $object_id = $this->check_object_id( $object_id ); - $value = $this->get_metadata( $object_id, $meta_key, true ); + $value = $this->get_metadata( $object_id, $meta_key, $assoc_args['is-single'] ); if ( '' === $value ) { die( 1 ); From dcab6382477aa091b529823f9d1fed26ce8f63db Mon Sep 17 00:00:00 2001 From: Bunty Date: Wed, 26 Feb 2025 15:42:06 +0530 Subject: [PATCH 2/8] Fix Notice --- src/WP_CLI/CommandWithMeta.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index b51bba71..9a192038 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -165,8 +165,9 @@ public function get( $args, $assoc_args ) { list( $object_id, $meta_key ) = $args; $object_id = $this->check_object_id( $object_id ); + $is_single = isset( $assoc_args['is-single'] ) ? $assoc_args['is-single'] : 1; - $value = $this->get_metadata( $object_id, $meta_key, $assoc_args['is-single'] ); + $value = $this->get_metadata( $object_id, $meta_key, $is_single ); if ( '' === $value ) { die( 1 ); From b6fe339fad4dede5d85f70c38b465c2918a66020 Mon Sep 17 00:00:00 2001 From: Bunty Date: Wed, 26 Feb 2025 16:57:23 +0530 Subject: [PATCH 3/8] Improve code as per feedback --- src/WP_CLI/CommandWithMeta.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index 9a192038..83ea2450 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -142,14 +142,8 @@ function ( $a, $b ) use ( $orderby, $order ) { * * : The name of the meta field to get. * - * [--is-single=] + * [--single] * : Whether to return a single value. This parameter has no effect if $key is not specified. - * --- - * default: 1 - * options: - * - 1 - * - 0 - * --- * * [--format=] * : Get value in a particular format. @@ -165,9 +159,9 @@ public function get( $args, $assoc_args ) { list( $object_id, $meta_key ) = $args; $object_id = $this->check_object_id( $object_id ); - $is_single = isset( $assoc_args['is-single'] ) ? $assoc_args['is-single'] : 1; + $single = Utils\get_flag_value( $assoc_args, 'single', true ); - $value = $this->get_metadata( $object_id, $meta_key, $is_single ); + $value = $this->get_metadata( $object_id, $meta_key, $single ); if ( '' === $value ) { die( 1 ); From e1a0e901fa73586c1760fa3a31a5159b880441ae Mon Sep 17 00:00:00 2001 From: Bunty Date: Wed, 26 Feb 2025 20:29:23 +0530 Subject: [PATCH 4/8] Add tests for single flag --- features/post-meta.feature | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/features/post-meta.feature b/features/post-meta.feature index b75bd3d2..96fa4c0c 100644 --- a/features/post-meta.feature +++ b/features/post-meta.feature @@ -219,6 +219,39 @@ Feature: Manage post custom fields My\New\Meta """ +Scenario: List post meta with or without single flag + Given a WP install + + When I run `wp post meta add 1 apple banana` + And I run `wp post meta add 1 apple banana` + Then STDOUT should not be empty + + When I run `wp post meta get 1 apple` + Then STDOUT should be: + """ + banana + """ + + When I run `wp post meta get 1 apple --single` + Then STDOUT should be: + """ + banana + """ + + When I run `wp post meta get 1 apple --no-single` + Then STDOUT should be: + """ + array ( + 0 => 'banana', + 1 => 'banana', + ) + """ + When I run `wp post meta get 1 apple --no-single --format=json` + Then STDOUT should be: + """ + ["banana","banana"] + """ + @pluck Scenario: Nested values can be retrieved. Given a WP install From 8ecd2f66fa799ed2940a3540f017222053d90848 Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 27 Feb 2025 11:40:18 +0530 Subject: [PATCH 5/8] Update documentation --- src/WP_CLI/CommandWithMeta.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index 83ea2450..78b45dd4 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -143,7 +143,7 @@ function ( $a, $b ) use ( $orderby, $order ) { * : The name of the meta field to get. * * [--single] - * : Whether to return a single value. This parameter has no effect if $key is not specified. + * : Whether to return a single value. * * [--format=] * : Get value in a particular format. @@ -159,7 +159,7 @@ public function get( $args, $assoc_args ) { list( $object_id, $meta_key ) = $args; $object_id = $this->check_object_id( $object_id ); - $single = Utils\get_flag_value( $assoc_args, 'single', true ); + $single = Utils\get_flag_value($assoc_args, 'single', true ); $value = $this->get_metadata( $object_id, $meta_key, $single ); From 440660eb6cb36269a26515ebec7a00203b8ab70d Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 27 Feb 2025 11:48:06 +0530 Subject: [PATCH 6/8] Fix phpcs --- src/WP_CLI/CommandWithMeta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index 78b45dd4..984f66c1 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -159,7 +159,7 @@ public function get( $args, $assoc_args ) { list( $object_id, $meta_key ) = $args; $object_id = $this->check_object_id( $object_id ); - $single = Utils\get_flag_value($assoc_args, 'single', true ); + $single = Utils\get_flag_value( $assoc_args, 'single', true ); $value = $this->get_metadata( $object_id, $meta_key, $single ); From 1365235ef3f1f34681d78c31d8f96d33a5fb5bef Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 27 Feb 2025 11:49:32 +0530 Subject: [PATCH 7/8] Improve feature file for psot meta --- features/post-meta.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/post-meta.feature b/features/post-meta.feature index 96fa4c0c..783ba258 100644 --- a/features/post-meta.feature +++ b/features/post-meta.feature @@ -223,7 +223,7 @@ Scenario: List post meta with or without single flag Given a WP install When I run `wp post meta add 1 apple banana` - And I run `wp post meta add 1 apple banana` + And I run the previous command again Then STDOUT should not be empty When I run `wp post meta get 1 apple` From 97d52fa66ed94ead61e7d49c7e9ecc8cbb4c6e45 Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 27 Feb 2025 12:46:20 +0530 Subject: [PATCH 8/8] Improve feature file for psot meta --- features/post-meta.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/post-meta.feature b/features/post-meta.feature index 783ba258..237f99a2 100644 --- a/features/post-meta.feature +++ b/features/post-meta.feature @@ -223,7 +223,7 @@ Scenario: List post meta with or without single flag Given a WP install When I run `wp post meta add 1 apple banana` - And I run the previous command again + And I run `wp post meta add 1 apple mango` Then STDOUT should not be empty When I run `wp post meta get 1 apple` @@ -243,13 +243,13 @@ Scenario: List post meta with or without single flag """ array ( 0 => 'banana', - 1 => 'banana', + 1 => 'mango', ) """ When I run `wp post meta get 1 apple --no-single --format=json` Then STDOUT should be: """ - ["banana","banana"] + ["banana","mango"] """ @pluck