Skip to content

Commit

Permalink
Skip visibility if no rules match
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd committed Jan 5, 2020
1 parent 8fd6cfc commit 69066d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/UriRuleMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -114,6 +114,6 @@ public function match_path( $path ) {
return ! $match_inverted;
}

return false;
return null;
}
}
20 changes: 13 additions & 7 deletions src/WidgetContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}


Expand Down
5 changes: 0 additions & 5 deletions tests/php/WidgetContextTargetByUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 69066d1

Please sign in to comment.