Skip to content

Commit c80b673

Browse files
SergeyBiryukovjrfnl
authored andcommitted
Check parse_url() result in url_to_postid()
1 parent 0f2b27b commit c80b673

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/wp-includes/rewrite.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,19 @@ function url_to_postid( $url ) {
500500
*/
501501
$url = apply_filters( 'url_to_postid', $url );
502502

503-
$url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
504-
$home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
503+
$url_host = parse_url( $url, PHP_URL_HOST );
504+
if ( is_string( $url_host ) ) {
505+
$url_host = str_replace( 'www.', '', $url_host );
506+
} else {
507+
return 0;
508+
}
509+
510+
$home_url_host = parse_url( home_url(), PHP_URL_HOST );
511+
if ( is_string( $home_url_host ) ) {
512+
$home_url_host = str_replace( 'www.', '', $home_url_host );
513+
} else {
514+
return 0;
515+
}
505516

506517
// Bail early if the URL does not belong to this site.
507518
if ( $url_host && $url_host !== $home_url_host ) {

tests/phpunit/tests/rewrite.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ public function test_url_to_postid_hierarchical_with_matching_leaves() {
255255
$this->assertSame( $grandchild_id_2, url_to_postid( get_permalink( $grandchild_id_2 ) ) );
256256
}
257257

258-
public function test_url_to_postid_home_has_path() {
258+
public function test_url_to_postid_url_has_only_path() {
259+
260+
$this->assertSame( 0, url_to_postid( '/example/' ) );
261+
}
262+
263+
public function test_url_to_postid_home_has_only_path() {
259264

260265
update_option( 'home', home_url( '/example/' ) );
261266

0 commit comments

Comments
 (0)