WordPress.org

Make WordPress Core

Changeset 36750


Ignore:
Timestamp:
02/28/2016 02:12:13 AM (6 years ago)
Author:
johnbillion
Message:

Rewrite Rules: Ensure url_to_postid() operates as expected when it's used in the context of another site within a Multisite network that uses mixed URL schemes.

Fixes #35531

Location:
trunk
Files:
2 edited

Legend:

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

    r36307 r36750  
    488488
    489489    // Set the correct URL scheme.
    490     $url = set_url_scheme( $url );
     490    $scheme = parse_url( home_url(), PHP_URL_SCHEME );
     491    $url = set_url_scheme( $url, $scheme );
    491492
    492493    // Add 'www.' if it is absent and should be there
  • trunk/tests/phpunit/tests/rewrite.php

    r36721 r36750  
    118118    }
    119119
     120    /**
     121     * @ticket 35531
     122     * @group multisite
     123     */
     124    function test_url_to_postid_of_http_site_when_current_site_uses_https() {
     125        if ( ! is_multisite() ) {
     126            $this->markTestSkipped( 'This test requires multisite' );
     127        }
     128
     129        $_SERVER['HTTPS'] = 'on';
     130
     131        $network_home = home_url();
     132        $this->blog_id_35531 = self::factory()->blog->create();
     133
     134        add_filter( 'home_url', array( $this, '_filter_http_home_url' ), 10, 4 );
     135
     136        switch_to_blog( $this->blog_id_35531 );
     137
     138        $post_id       = self::factory()->post->create();
     139        $permalink     = get_permalink( $post_id );
     140        $url_to_postid = url_to_postid( $permalink );
     141
     142        restore_current_blog();
     143
     144        // Cleanup.
     145        remove_filter( 'home_url', array( $this, '_filter_http_home_url' ), 10 );
     146
     147        // Test the tests:
     148        $this->assertSame( 'http', parse_url( $permalink, PHP_URL_SCHEME ) );
     149        $this->assertSame( 'https', parse_url( $network_home, PHP_URL_SCHEME ) );
     150
     151        // Test that the url_to_postid() call matched:
     152        $this->assertEquals( $post_id, $url_to_postid );
     153    }
     154
     155    /**
     156     * Enforce an `http` scheme for our target site.
     157     *
     158     * @param string      $url         The complete home URL including scheme and path.
     159     * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
     160     * @param string|null $orig_scheme Scheme to give the home URL context.
     161     * @param int|null    $blog_id     Site ID, or null for the current site.
     162     * @return string                  The complete home URL including scheme and path.
     163     */
     164    function _filter_http_home_url( $url, $path, $orig_scheme, $_blog_id ) {
     165        global $blog_id;
     166
     167        if ( $this->blog_id_35531 === $blog_id ) {
     168            return set_url_scheme( $url, 'http' );
     169        }
     170
     171        return $url;
     172    }
     173
    120174    function test_url_to_postid_custom_post_type() {
    121175        delete_option( 'rewrite_rules' );
Note: See TracChangeset for help on using the changeset viewer.