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
This commit is contained in:
parent
000f4f98b4
commit
7add279793
|
@ -243,14 +243,17 @@ define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEAR
|
|||
* activated and deactivated.
|
||||
*
|
||||
* @since 2.1.0
|
||||
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
|
||||
*
|
||||
* @see WP_Rewrite::add_endpoint()
|
||||
* @global object $wp_rewrite
|
||||
*
|
||||
* @param string $name Name of the endpoint.
|
||||
* @param int $places Endpoint mask describing the places the endpoint should be added.
|
||||
* @param string $query_var Name of the corresponding query variable. Defaults to $name.
|
||||
* @param string $name Name of the endpoint.
|
||||
* @param int $places Endpoint mask describing the places the endpoint should be added.
|
||||
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
|
||||
* for this endpoint. Defaults to the value of `$name`.
|
||||
*/
|
||||
function add_rewrite_endpoint( $name, $places, $query_var = null ) {
|
||||
function add_rewrite_endpoint( $name, $places, $query_var = true ) {
|
||||
global $wp_rewrite;
|
||||
$wp_rewrite->add_endpoint( $name, $places, $query_var );
|
||||
}
|
||||
|
@ -1959,22 +1962,29 @@ class WP_Rewrite {
|
|||
*
|
||||
* @since 2.1.0
|
||||
* @since 3.9.0 $query_var parameter added.
|
||||
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
|
||||
* @access public
|
||||
*
|
||||
* @see add_rewrite_endpoint() for full documentation.
|
||||
* @uses WP::add_query_var()
|
||||
*
|
||||
* @param string $name Name of the endpoint.
|
||||
* @param int $places Endpoint mask describing the places the endpoint should be added.
|
||||
* @param string $query_var Name of the corresponding query variable. Default is value of $name.
|
||||
* @param string $name Name of the endpoint.
|
||||
* @param int $places Endpoint mask describing the places the endpoint should be added.
|
||||
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering
|
||||
* a query_var for this endpoint. Defaults to the value of `$name`.
|
||||
*/
|
||||
public function add_endpoint( $name, $places, $query_var = null ) {
|
||||
public function add_endpoint( $name, $places, $query_var = true ) {
|
||||
global $wp;
|
||||
if ( null === $query_var ) {
|
||||
|
||||
// For backward compatibility, if `null` has explicitly been passed as `$query_var`, assume `true`.
|
||||
if ( true === $query_var || null === func_get_arg( 2 ) ) {
|
||||
$query_var = $name;
|
||||
}
|
||||
$this->endpoints[] = array( $places, $name, $query_var );
|
||||
$wp->add_query_var( $query_var );
|
||||
|
||||
if ( $query_var ) {
|
||||
$wp->add_query_var( $query_var );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -121,4 +121,22 @@ class Tests_Rewrite extends WP_UnitTestCase {
|
|||
|
||||
restore_current_blog();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25143
|
||||
*/
|
||||
public function test_is_home_should_be_false_when_visiting_custom_endpoint_without_a_registered_query_var_and_page_on_front_is_set() {
|
||||
|
||||
$page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
|
||||
update_option( 'show_on_front', 'page' );
|
||||
update_option( 'page_on_front', $page_id );
|
||||
|
||||
add_rewrite_endpoint( 'test', EP_ALL, false );
|
||||
flush_rewrite_rules();
|
||||
|
||||
$this->go_to( home_url( '/test/1' ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_front_page', 'is_page', 'is_singular' );
|
||||
$this->assertFalse( is_home() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @group rewrite
|
||||
*/
|
||||
class Tests_Rewrite_AddRewriteEndpoint extends WP_UnitTestCase {
|
||||
private $qvs;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->qvs = $GLOBALS['wp']->public_query_vars;
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
$GLOBALS['wp']->public_query_vars = $this->qvs;
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_should_register_query_using_name_param_by_default() {
|
||||
add_rewrite_endpoint( 'foo', EP_ALL );
|
||||
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
|
||||
}
|
||||
|
||||
public function test_should_register_query_using_name_param_if_null_is_passed_as_query_var() {
|
||||
add_rewrite_endpoint( 'foo', EP_ALL, null );
|
||||
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
|
||||
}
|
||||
|
||||
public function test_should_register_query_using_query_var_param_if_not_null() {
|
||||
add_rewrite_endpoint( 'foo', EP_ALL, 'bar' );
|
||||
$this->assertContains( 'bar', $GLOBALS['wp']->public_query_vars );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25143
|
||||
*/
|
||||
public function test_should_register_query_var_using_name_param_if_true_is_passed_as_query_var() {
|
||||
add_rewrite_endpoint( 'foo', EP_ALL, true );
|
||||
$this->assertContains( 'foo', $GLOBALS['wp']->public_query_vars );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25143
|
||||
*/
|
||||
public function test_should_not_register_query_var_if_query_var_param_is_false() {
|
||||
$qvs = $GLOBALS['wp']->public_query_vars;
|
||||
add_rewrite_endpoint( 'foo', EP_ALL, false );
|
||||
$this->assertSame( $qvs, $GLOBALS['wp']->public_query_vars );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue