From 951e70737c9ff392b1c15e4ff50293e07fd2e55b Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 14 Nov 2022 15:17:19 -0800 Subject: [PATCH 1/3] Support full URLs for `wp rewrite list --match=` --- features/rewrite.feature | 16 ++++++++++++++++ src/Rewrite_Command.php | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/features/rewrite.feature b/features/rewrite.feature index 807b75de8..b47aa8094 100644 --- a/features/rewrite.feature +++ b/features/rewrite.feature @@ -124,3 +124,19 @@ Feature: Manage WordPress rewrites Warning: Some rewrite rules may be missing """ And the return code should be 0 + + Scenario: Match as expected when full URL is provided + Given a WP install + And I run `wp rewrite structure /%year%/%monthnum%/%day%/%postname%/` + + When I run `wp rewrite list --match=/2022/11/13/hello-world/ --format=csv` + Then STDOUT should be CSV containing: + | match | query | source | + | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$ | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | + | (.?.+?)(?:/([0-9]+))?/?$ | index.php?pagename=$matches[1]&page=$matches[2] | page | + + When I run `wp rewrite list --match=https://example.com/2022/11/13/hello-world/ --format=csv` + Then STDOUT should be CSV containing: + | match | query | source | + | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$ | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | + | (.?.+?)(?:/([0-9]+))?/?$ | index.php?pagename=$matches[1]&page=$matches[2] | page | diff --git a/src/Rewrite_Command.php b/src/Rewrite_Command.php index bef6d7e4c..5002593c5 100644 --- a/src/Rewrite_Command.php +++ b/src/Rewrite_Command.php @@ -234,6 +234,15 @@ public function list_( $args, $assoc_args ) { ]; $assoc_args = array_merge( $defaults, $assoc_args ); + if ( ! empty( $assoc_args['match'] ) ) { + if ( 0 === stripos( $assoc_args['match'], 'http://' ) + || 0 === stripos( $assoc_args['match'], 'https://' ) ) { + $bits = WP_CLI\Utils\parse_url( $assoc_args['match'] ); + $assoc_args['match'] = ( isset( $bits['path'] ) ? $bits['path'] : '' ) + . ( isset( $bits['query'] ) ? '?' . $bits['query'] : '' ); + } + } + $rewrite_rules_by_source = []; $rewrite_rules_by_source['post'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->permalink_structure, EP_PERMALINK ); $rewrite_rules_by_source['date'] = $wp_rewrite->generate_rewrite_rules( $wp_rewrite->get_date_permastruct(), EP_DATE ); From a69a1b1845f85fa6f55e1b3fddf35f2a78835b01 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 14 Nov 2022 15:20:22 -0800 Subject: [PATCH 2/3] Fix PHPCS issue --- src/Rewrite_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rewrite_Command.php b/src/Rewrite_Command.php index 5002593c5..424f50779 100644 --- a/src/Rewrite_Command.php +++ b/src/Rewrite_Command.php @@ -237,7 +237,7 @@ public function list_( $args, $assoc_args ) { if ( ! empty( $assoc_args['match'] ) ) { if ( 0 === stripos( $assoc_args['match'], 'http://' ) || 0 === stripos( $assoc_args['match'], 'https://' ) ) { - $bits = WP_CLI\Utils\parse_url( $assoc_args['match'] ); + $bits = WP_CLI\Utils\parse_url( $assoc_args['match'] ); $assoc_args['match'] = ( isset( $bits['path'] ) ? $bits['path'] : '' ) . ( isset( $bits['query'] ) ? '?' . $bits['query'] : '' ); } From 582012e373026fbbd47bac11260a2376ccbb73f5 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 15 Nov 2022 03:50:00 -0800 Subject: [PATCH 3/3] Limit test for compatibility with earlier WP versions --- features/rewrite.feature | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/features/rewrite.feature b/features/rewrite.feature index b47aa8094..7fd024f71 100644 --- a/features/rewrite.feature +++ b/features/rewrite.feature @@ -129,14 +129,14 @@ Feature: Manage WordPress rewrites Given a WP install And I run `wp rewrite structure /%year%/%monthnum%/%day%/%postname%/` - When I run `wp rewrite list --match=/2022/11/13/hello-world/ --format=csv` + When I run `wp rewrite list --match=/2022/11/13/hello-world/ --format=csv --fields=query,source` Then STDOUT should be CSV containing: - | match | query | source | - | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$ | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | - | (.?.+?)(?:/([0-9]+))?/?$ | index.php?pagename=$matches[1]&page=$matches[2] | page | + | query | source | + | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | + | index.php?pagename=$matches[1]&page=$matches[2] | page | - When I run `wp rewrite list --match=https://example.com/2022/11/13/hello-world/ --format=csv` + When I run `wp rewrite list --match=https://example.com/2022/11/13/hello-world/ --format=csv --fields=query,source` Then STDOUT should be CSV containing: - | match | query | source | - | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$ | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | - | (.?.+?)(?:/([0-9]+))?/?$ | index.php?pagename=$matches[1]&page=$matches[2] | page | + | query | source | + | index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5] | post | + | index.php?pagename=$matches[1]&page=$matches[2] | page |