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.


git-svn-id: https://develop.svn.wordpress.org/trunk@31157 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2015-01-12 02:23:43 +00:00
parent 331262b544
commit d759c0ef23
2 changed files with 19 additions and 3 deletions

View File

@ -33,11 +33,11 @@ function wpmu_update_blogs_date() {
* @since MU * @since MU
* *
* @param int $blog_id Blog ID * @param int $blog_id Blog ID
* @return string * @return string Full URL of the blog if found. Empty string if not.
*/ */
function get_blogaddress_by_id( $blog_id ) { function get_blogaddress_by_id( $blog_id ) {
$bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details! $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ); return isset( $bloginfo->domain ) && isset( $bloginfo->path ) ? esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) : '';
} }
/** /**

View File

@ -1461,6 +1461,22 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 ); remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 );
} }
/**
* Tests returning an address for a given valid id.
*/
function test_get_blogaddress_by_id_with_valid_id() {
$blogaddress = get_blogaddress_by_id( 1 );
$this->assertEquals( 'http://example.org/', $blogaddress );
}
/**
* Tests returning the appropriate response for a invalid id given.
*/
function test_get_blogaddress_by_id_with_invalid_id() {
$blogaddress = get_blogaddress_by_id( 42 );
$this->assertEquals( '', $blogaddress );
}
} }
endif; endif;