From 69066d1b09cbb50d0a3df3e553c3fbc23d7a767b Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 5 Jan 2020 11:13:21 +0200 Subject: [PATCH] Skip visibility if no rules match --- src/UriRuleMatcher.php | 4 ++-- src/WidgetContext.php | 20 +++++++++++++------- tests/php/WidgetContextTargetByUrlTest.php | 5 ----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/UriRuleMatcher.php b/src/UriRuleMatcher.php index 5123aa8..5840e41 100644 --- a/src/UriRuleMatcher.php +++ b/src/UriRuleMatcher.php @@ -89,7 +89,7 @@ function ( $rule ) { * * @param string $path URI path to check. * - * @return bool + * @return bool|null */ public function match_path( $path ) { $match_positive = null; @@ -114,6 +114,6 @@ public function match_path( $path ) { return ! $match_inverted; } - return false; + return null; } } diff --git a/src/WidgetContext.php b/src/WidgetContext.php index ec52f98..62e46da 100644 --- a/src/WidgetContext.php +++ b/src/WidgetContext.php @@ -415,8 +415,10 @@ function context_check_url( $check, $settings ) { $path = $this->get_request_path( $_SERVER['REQUEST_URI'] ); } - if ( $this->match_path( $path, $urls ) ) { - return true; + $matched = $this->match_path( $path, $urls ); + + if ( null !== $matched ) { + return $matched; } return $check; @@ -467,14 +469,18 @@ function( $pattern ) { $patterns ); - $matcher = new UriRuleMatcher( new UriRules( array_filter( $patterns ) ) ); + $uri_rules = new UriRules( array_filter( $patterns ) ); + $matcher = new UriRuleMatcher( $uri_rules ); - // Match against the path with and without the query string. - if ( $matcher->match_path( $path ) || $matcher->match_path( $path_only ) ) { - return true; + /** + * Ignore query parameters in path unless any of the rules actually use them. + * Defaults to matching paths with any query parameters. + */ + if ( $uri_rules->has_rules_with_query_strings() ) { + return $matcher->match_path( $path ); } - return false; + return $matcher->match_path( $path_only ); } diff --git a/tests/php/WidgetContextTargetByUrlTest.php b/tests/php/WidgetContextTargetByUrlTest.php index 1c54f58..39b1187 100644 --- a/tests/php/WidgetContextTargetByUrlTest.php +++ b/tests/php/WidgetContextTargetByUrlTest.php @@ -121,11 +121,6 @@ public function testUrlSpecial() { $this->plugin->match_path( 'campaigns?cc=automotive', 'campaigns/?some=other' ), 'Respect query string if differen used' ); - - $this->assertFalse( - $this->plugin->match_path( 'campaigns?cc=automotive', 'campaigns/?has=query' ), - 'Ignore query string because no rules use it' - ); } public function testPathResolverAbsolute() {