Skip to content

Commit

Permalink
Filter query vars in slugs
Browse files Browse the repository at this point in the history
Remove the query vars that were part of the matched query before slugs.
Keep the new query vars with the matched query found by slugs.
  • Loading branch information
herrvigg committed Jun 5, 2022
1 parent 5acf4e8 commit 9036c6a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions modules/slugs/src/slugs-class-slugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,24 @@ public function query_vars( $query_vars ) {
}

if ( isset( $wp->matched_rule ) && isset( $query ) && isset( $matches ) ) {
// Store the query vars before update with slugs
parse_str( $wp->matched_query, $query_vars_before_slugs_match );
// Trim the query of everything up to the '?'.
$query = preg_replace( "!^.+\?!", '', $query );
// Substitute the substring matches into the query.
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
$wp->matched_query = $query;
// Parse the query.
parse_str( $query, $perma_query_vars );
// If we're processing a 404 request, clear the error var
// since we found something.
// If we're processing a 404 request, clear the error var since we found something.
unset( $_GET['error'] );
unset( $error );
// Remove all query vars previously set in the matched query, as they may not be consistent with the slugs request.
$to_remove = array_keys( $query_vars_before_slugs_match );
$to_keep = array_keys( $perma_query_vars );
$query_vars = array_filter( $query_vars, function ( $item ) use ( $to_remove, $to_keep ) {
return ! in_array( $item, $to_remove ) || in_array( $item, $to_keep );
} );
}

// If req_uri is empty or if it is a request for ourself, unset error.
Expand All @@ -326,8 +333,7 @@ public function query_vars( $query_vars ) {
}
}

// TODO check this call, looks bug-prone
return count( array_diff( $query_vars, $wp->public_query_vars ) ) > 0 ? $query_vars : $wp->public_query_vars;
return $query_vars;
}

/**
Expand Down

0 comments on commit 9036c6a

Please sign in to comment.