Skip to content

Commit 7add279

Browse files
committed
Allow rewrite endpoints to be registered without also registering query vars.
Passing `false` to `add_rewrite_endpoint()` will now allow you to take advantage of the rewrite API without thereby modifying the way that WP sets up the main query from the request URI. This new functionality allows developers to work around certain edge-case bugs, such as when a proper endpoint request (such as `/test/1/`) would short- circuit `is_home()` calculation when a static front page is set. Props mordauk, boonebgorges. Fixes #25143. git-svn-id: https://develop.svn.wordpress.org/trunk@32293 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 000f4f9 commit 7add279

3 files changed

Lines changed: 88 additions & 10 deletions

File tree

src/wp-includes/rewrite.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,17 @@ function flush_rewrite_rules( $hard = true ) {
243243
* activated and deactivated.
244244
*
245245
* @since 2.1.0
246+
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
247+
*
246248
* @see WP_Rewrite::add_endpoint()
247249
* @global object $wp_rewrite
248250
*
249-
* @param string $name Name of the endpoint.
250-
* @param int $places Endpoint mask describing the places the endpoint should be added.
251-
* @param string $query_var Name of the corresponding query variable. Defaults to $name.
251+
* @param string $name Name of the endpoint.
252+
* @param int $places Endpoint mask describing the places the endpoint should be added.
253+
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
254+
* for this endpoint. Defaults to the value of `$name`.
252255
*/
253-
function add_rewrite_endpoint( $name, $places, $query_var = null ) {
256+
function add_rewrite_endpoint( $name, $places, $query_var = true ) {
254257
global $wp_rewrite;
255258
$wp_rewrite->add_endpoint( $name, $places, $query_var );
256259
}
@@ -1959,22 +1962,29 @@ public function add_external_rule($regex, $redirect) {
19591962
*
19601963
* @since 2.1.0
19611964
* @since 3.9.0 $query_var parameter added.
1965+
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
19621966
* @access public
19631967
*
19641968
* @see add_rewrite_endpoint() for full documentation.
19651969
* @uses WP::add_query_var()
19661970
*
1967-
* @param string $name Name of the endpoint.
1968-
* @param int $places Endpoint mask describing the places the endpoint should be added.
1969-
* @param string $query_var Name of the corresponding query variable. Default is value of $name.
1971+
* @param string $name Name of the endpoint.
1972+
* @param int $places Endpoint mask describing the places the endpoint should be added.
1973+
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering
1974+
* a query_var for this endpoint. Defaults to the value of `$name`.
19701975
*/
1971-
public function add_endpoint( $name, $places, $query_var = null ) {
1976+
public function add_endpoint( $name, $places, $query_var = true ) {
19721977
global $wp;
1973-
if ( null === $query_var ) {
1978+
1979+
// For backward compatibility, if `null` has explicitly been passed as `$query_var`, assume `true`.
1980+
if ( true === $query_var || null === func_get_arg( 2 ) ) {
19741981
$query_var = $name;
19751982
}
19761983
$this->endpoints[] = array( $places, $name, $query_var );
1977-
$wp->add_query_var( $query_var );
1984+
1985+
if ( $query_var ) {
1986+
$wp->add_query_var( $query_var );
1987+
}
19781988
}
19791989

19801990
/**

tests/phpunit/tests/rewrite.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,22 @@ function test_url_to_postid_ms_home_url_collision() {
121121

122122
restore_current_blog();
123123
}
124+
125+
/**
126+
* @ticket 25143
127+
*/
128+
public function test_is_home_should_be_false_when_visiting_custom_endpoint_without_a_registered_query_var_and_page_on_front_is_set() {
129+
130+
$page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
131+
update_option( 'show_on_front', 'page' );
132+
update_option( 'page_on_front', $page_id );
133+
134+
add_rewrite_endpoint( 'test', EP_ALL, false );
135+
flush_rewrite_rules();
136+
137+
$this->go_to( home_url( '/test/1' ) );
138+
139+
$this->assertQueryTrue( 'is_front_page', 'is_page', 'is_singular' );
140+
$this->assertFalse( is_home() );
141+
}
124142
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @group rewrite
5+
*/
6+
class Tests_Rewrite_AddRewriteEndpoint extends WP_UnitTestCase {
7+
private $qvs;
8+
9+
public function setUp() {
10+
parent::setUp();
11+
$this->qvs = $GLOBALS['wp']->public_query_vars;
12+
}
13+
14+
public function tearDown() {
15+
$GLOBALS['wp']->public_query_vars = $this->qvs;
16+
parent::tearDown();
17+
}
18+
19+
public function test_should_register_query_using_name_param_by_default() {
20+
add_rewrite_endpoint( 'foo', EP_ALL );
21+
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
22+
}
23+
24+
public function test_should_register_query_using_name_param_if_null_is_passed_as_query_var() {
25+
add_rewrite_endpoint( 'foo', EP_ALL, null );
26+
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
27+
}
28+
29+
public function test_should_register_query_using_query_var_param_if_not_null() {
30+
add_rewrite_endpoint( 'foo', EP_ALL, 'bar' );
31+
$this->assertContains( 'bar', $GLOBALS['wp']->public_query_vars );
32+
}
33+
34+
/**
35+
* @ticket 25143
36+
*/
37+
public function test_should_register_query_var_using_name_param_if_true_is_passed_as_query_var() {
38+
add_rewrite_endpoint( 'foo', EP_ALL, true );
39+
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
40+
}
41+
42+
/**
43+
* @ticket 25143
44+
*/
45+
public function test_should_not_register_query_var_if_query_var_param_is_false() {
46+
$qvs = $GLOBALS['wp']->public_query_vars;
47+
add_rewrite_endpoint( 'foo', EP_ALL, false );
48+
$this->assertSame( $qvs, $GLOBALS['wp']->public_query_vars );
49+
}
50+
}

0 commit comments

Comments
 (0)