From bb9bb6ca750801413db6387f935e6acca7721d61 Mon Sep 17 00:00:00 2001 From: Julian Bustamante Date: Thu, 16 Nov 2023 15:11:56 +0100 Subject: [PATCH 1/7] Add command to get the post ID by URL --- features/post.feature | 7 +++++++ src/Post_Command.php | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/features/post.feature b/features/post.feature index e1f4ec34..7808dc21 100644 --- a/features/post.feature +++ b/features/post.feature @@ -259,6 +259,13 @@ Feature: Manage WordPress posts """ https://example.com/?p=1 """ + And save STDOUT as {POST_URL} + + When I run `wp post url-to-id {POST_URL}` + Then STDOUT should contain: + """ + {POST_ID} + """ Scenario: Update a post from file or STDIN Given a content.html file: diff --git a/src/Post_Command.php b/src/Post_Command.php index 43429594..66fbc001 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -878,6 +878,31 @@ public function generate( $args, $assoc_args ) { } } + /** + * Gets post ID by URL. + * + * ## OPTIONS + * + * + * : The URL of the post to get. + * + * ## EXAMPLES + * + * # Get post ID by URL + * $ wp post url-to-id post-url + * + * @subcommand url-to-id + */ + public function url_to_id( $args, $assoc_args ) { + $post_id = url_to_postid( $args[0] ); + + if ( ! $value ) { + WP_CLI::error( "Could not get post with url '{$args[0]}'." ); + } + + WP_CLI::print_value( $post_id, $assoc_args ); + } + private function maybe_make_child() { // 50% chance of making child post. return ( wp_rand( 1, 2 ) === 1 ); From 163711810cc8a8ac4783e3186c6201456f5baad7 Mon Sep 17 00:00:00 2001 From: Julian Bustamante Date: Thu, 16 Nov 2023 16:07:50 +0100 Subject: [PATCH 2/7] Add test to check error case --- features/post.feature | 6 ++++++ src/Post_Command.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/features/post.feature b/features/post.feature index 7808dc21..127d3e07 100644 --- a/features/post.feature +++ b/features/post.feature @@ -267,6 +267,12 @@ Feature: Manage WordPress posts {POST_ID} """ + When I run `wp post url-to-id non-existent-url` + Then STDOUT should contain: + """ + Could not get post with url 'non-existent-url'. + """ + Scenario: Update a post from file or STDIN Given a content.html file: """ diff --git a/src/Post_Command.php b/src/Post_Command.php index 66fbc001..07c72eb1 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -897,7 +897,7 @@ public function url_to_id( $args, $assoc_args ) { $post_id = url_to_postid( $args[0] ); if ( ! $value ) { - WP_CLI::error( "Could not get post with url '{$args[0]}'." ); + WP_CLI::error( "Could not get post with url '$args[0]'." ); } WP_CLI::print_value( $post_id, $assoc_args ); From 80cacd23d895d4d51787df5b5ce51ceaa587993c Mon Sep 17 00:00:00 2001 From: Julian Bustamante Date: Thu, 16 Nov 2023 16:10:17 +0100 Subject: [PATCH 3/7] Fix undefined variable --- src/Post_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Post_Command.php b/src/Post_Command.php index 07c72eb1..7d01de47 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -896,7 +896,7 @@ public function generate( $args, $assoc_args ) { public function url_to_id( $args, $assoc_args ) { $post_id = url_to_postid( $args[0] ); - if ( ! $value ) { + if ( ! $post_id ) { WP_CLI::error( "Could not get post with url '$args[0]'." ); } From 291122b469f4411a2df8eaef3766bd57a12740f0 Mon Sep 17 00:00:00 2001 From: Julian Bustamante Date: Fri, 17 Nov 2023 10:51:10 +0100 Subject: [PATCH 4/7] Check if post exists --- features/post.feature | 8 ++++---- src/Post_Command.php | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/features/post.feature b/features/post.feature index 127d3e07..ca1f2aec 100644 --- a/features/post.feature +++ b/features/post.feature @@ -264,13 +264,13 @@ Feature: Manage WordPress posts When I run `wp post url-to-id {POST_URL}` Then STDOUT should contain: """ - {POST_ID} + 1 """ - When I run `wp post url-to-id non-existent-url` - Then STDOUT should contain: + When I try `wp post url-to-id 'https://example.com/?p=404'` + Then STDERR should contain: """ - Could not get post with url 'non-existent-url'. + Could not get post with url https://example.com/?p=404. """ Scenario: Update a post from file or STDIN diff --git a/src/Post_Command.php b/src/Post_Command.php index 7d01de47..730e6cd2 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -889,15 +889,17 @@ public function generate( $args, $assoc_args ) { * ## EXAMPLES * * # Get post ID by URL - * $ wp post url-to-id post-url + * $ wp post url-to-id https://example.com/?p=1 * * @subcommand url-to-id */ public function url_to_id( $args, $assoc_args ) { $post_id = url_to_postid( $args[0] ); - if ( ! $post_id ) { - WP_CLI::error( "Could not get post with url '$args[0]'." ); + $post = get_post( $post_id ); + + if ( null === $post ) { + WP_CLI::error( "Could not get post with url $args[0]." ); } WP_CLI::print_value( $post_id, $assoc_args ); From 764bd8f3d142ada97f31087a02a090fb3ec89808 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Sun, 19 Nov 2023 13:19:11 +0000 Subject: [PATCH 5/7] Add new command to README --- README.md | 21 +++++++++++++++++++++ composer.json | 1 + src/Post_Command.php | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a53377d..e497ca6a 100644 --- a/README.md +++ b/README.md @@ -3111,6 +3111,27 @@ wp post update ... [--post_author=] [--post_date=] [ +### wp post url-to-id + +Gets the post ID for a given URL. + +~~~ +wp post url-to-id +~~~ + +**OPTIONS** + + + The URL of the post to get. + +**EXAMPLES** + + # Get post ID by URL + $ wp post url-to-id https://example.com/?p=1 + 1 + + + ### wp post-type Retrieves details on the site's registered post types. diff --git a/composer.json b/composer.json index 40a75742..1bef351b 100644 --- a/composer.json +++ b/composer.json @@ -115,6 +115,7 @@ "post term remove", "post term set", "post update", + "post url-to-id", "post-type", "post-type get", "post-type list", diff --git a/src/Post_Command.php b/src/Post_Command.php index 730e6cd2..9d6e040a 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -879,7 +879,7 @@ public function generate( $args, $assoc_args ) { } /** - * Gets post ID by URL. + * Gets the post ID for a given URL. * * ## OPTIONS * @@ -890,6 +890,7 @@ public function generate( $args, $assoc_args ) { * * # Get post ID by URL * $ wp post url-to-id https://example.com/?p=1 + * 1 * * @subcommand url-to-id */ From a4fb7ea94b93339bfd402b450a44d69a69b7fde2 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Sun, 19 Nov 2023 13:20:23 +0000 Subject: [PATCH 6/7] Move `url-to-id` tests to a separate file --- features/post-url-to-id.feature | 25 +++++++++++++++++++++++++ features/post.feature | 13 ------------- 2 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 features/post-url-to-id.feature diff --git a/features/post-url-to-id.feature b/features/post-url-to-id.feature new file mode 100644 index 00000000..49b05e8c --- /dev/null +++ b/features/post-url-to-id.feature @@ -0,0 +1,25 @@ +Feature: Get the post ID for a given URL + + Background: + Given a WP install + + @daniel + Scenario: Get the post ID for a given URL + When I run `wp post get 1 --field=url` + Then STDOUT should be: + """ + https://example.com/?p=1 + """ + And save STDOUT as {POST_URL} + + When I run `wp post url-to-id {POST_URL}` + Then STDOUT should contain: + """ + 1 + """ + + When I try `wp post url-to-id 'https://example.com/?p=404'` + Then STDERR should contain: + """ + Could not get post with url https://example.com/?p=404. + """ \ No newline at end of file diff --git a/features/post.feature b/features/post.feature index ca1f2aec..e1f4ec34 100644 --- a/features/post.feature +++ b/features/post.feature @@ -259,19 +259,6 @@ Feature: Manage WordPress posts """ https://example.com/?p=1 """ - And save STDOUT as {POST_URL} - - When I run `wp post url-to-id {POST_URL}` - Then STDOUT should contain: - """ - 1 - """ - - When I try `wp post url-to-id 'https://example.com/?p=404'` - Then STDERR should contain: - """ - Could not get post with url https://example.com/?p=404. - """ Scenario: Update a post from file or STDIN Given a content.html file: From b5f922e8a67fe0f33da740b2f7ebfd1e7e96e9bb Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Sun, 19 Nov 2023 13:35:02 +0000 Subject: [PATCH 7/7] Remove tag, whoops --- features/post-url-to-id.feature | 1 - 1 file changed, 1 deletion(-) diff --git a/features/post-url-to-id.feature b/features/post-url-to-id.feature index 49b05e8c..cb9ddf9e 100644 --- a/features/post-url-to-id.feature +++ b/features/post-url-to-id.feature @@ -3,7 +3,6 @@ Feature: Get the post ID for a given URL Background: Given a WP install - @daniel Scenario: Get the post ID for a given URL When I run `wp post get 1 --field=url` Then STDOUT should be: