Make WordPress Core

Changeset 32293


Ignore:
Timestamp:
04/24/2015 04:37:12 PM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rewrite.php

    r31964 r32293  
    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.
    252  */
    253 function add_rewrite_endpoint( $name, $places, $query_var = null ) {
     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`.
     255 */
     256function add_rewrite_endpoint( $name, $places, $query_var = true ) {
    254257    global $wp_rewrite;
    255258    $wp_rewrite->add_endpoint( $name, $places, $query_var );
     
    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     *
     
    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.
    1970      */
    1971     public function add_endpoint( $name, $places, $query_var = null ) {
     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`.
     1975     */
     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
  • trunk/tests/phpunit/tests/rewrite.php

    r31623 r32293  
    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}
Note: See TracChangeset for help on using the changeset viewer.