WordPress.org

Make WordPress Core

Changeset 31157


Ignore:
Timestamp:
01/12/2015 02:23:43 AM (5 years ago)
Author:
jeremyfelt
Message:

Check for existence of data from get_blogaddress_by_id() before returning a URL

  • Prevent a notice when an invalid ID is used with get_blogaddres_by_id().
  • Return a falsy empty string rather than the previous "http://".
  • Add unit tests for get_blogaddress_by_id().

Props nerrad.

Fixes #30566.

Location:
trunk
Files:
2 edited

Legend:

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

    r31155 r31157  
    3434 *
    3535 * @param int $blog_id Blog ID
    36  * @return string
     36 * @return string Full URL of the blog if found. Empty string if not.
    3737 */
    3838function get_blogaddress_by_id( $blog_id ) {
    3939    $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
    40     return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
     40    return isset( $bloginfo->domain ) && isset( $bloginfo->path ) ?  esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) : '';
    4141}
    4242
  • trunk/tests/phpunit/tests/multisite/site.php

    r31155 r31157  
    333333        $this->assertFalse( $result );
    334334    }
    335    
     335
    336336    function test_update_blog_details() {
    337337        $blog_id = $this->factory->blog->create();
     
    14621462        remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
    14631463    }
     1464
     1465    /**
     1466     * Tests returning an address for a given valid id.
     1467     */
     1468    function test_get_blogaddress_by_id_with_valid_id() {
     1469        $blogaddress = get_blogaddress_by_id( 1 );
     1470        $this->assertEquals( 'http://example.org/', $blogaddress );
     1471    }
     1472
     1473    /**
     1474     * Tests returning the appropriate response for a invalid id given.
     1475     */
     1476    function test_get_blogaddress_by_id_with_invalid_id() {
     1477        $blogaddress = get_blogaddress_by_id( 42 );
     1478        $this->assertEquals( '', $blogaddress );
     1479    }
    14641480}
    14651481
Note: See TracChangeset for help on using the changeset viewer.