Make WordPress Core

Ticket #39373: 39373.diff

File 39373.diff, 1.5 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/rewrite.php

    diff --git src/wp-includes/rewrite.php src/wp-includes/rewrite.php
    index 705e5f2bc8..1a1f31847a 100644
    function url_to_postid( $url ) { 
    471471         */
    472472        $url = apply_filters( 'url_to_postid', $url );
    473473
     474        // Bail early when URL doesn't belong to this site.
     475        $host = parse_url( home_url(), PHP_URL_HOST );
     476        if ( false === strpos( $url, $host ) ) {
     477                //return 0;
     478        }
     479
    474480        // First, check to see if there is a 'p=N' or 'page_id=N' to match against
    475481        if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) )       {
    476482                $id = absint($values[2]);
  • tests/phpunit/tests/rewrite.php

    diff --git tests/phpunit/tests/rewrite.php tests/phpunit/tests/rewrite.php
    index 5f7514915e..014a6c1e76 100644
    class Tests_Rewrite extends WP_UnitTestCase { 
    389389                $this->assertInternalType( 'array', $rewrite_rules );
    390390                $this->assertNotEmpty( $rewrite_rules );
    391391        }
     392
     393        /**
     394         * @ticket 39373
     395         */
     396        public function test_url_to_postid_should_bail_when_host_does_not_match() {
     397                $this->set_permalink_structure( '/%postname%/' );
     398
     399                $post_id = self::factory()->post->create( array( 'post_name' => 'foo-bar-baz' ) );
     400                $permalink = get_permalink( $post_id );
     401                $url = str_replace( home_url(), 'http://some-other-domain.com/', get_permalink( $post_id ) );
     402
     403                $this->assertSame( $post_id, url_to_postid( $permalink ) );
     404                $this->assertSame( 0, url_to_postid( $url ) );
     405        }
    392406}