Skip to content

Commit 7b9d4cf

Browse files
committed
General: Correct path replacement regex in wp_guess_url.
In `wp_guess_url`, the regex to check for wp-login.php in the URL is slightly too permissive, not escaping `.` in "wp-login.php". `.` is a token in regex that matches any character. This change simply escapes the `.` and adds unit test coverage for `wp_guess_url`. Props cfinke, ocean90, jrf, voldemortensen, jdgrimes, curdin, netweb, petitphp, SergeyBiryukov, costdev. Fixes #36827. git-svn-id: https://develop.svn.wordpress.org/trunk@54146 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9bde2e9 commit 7b9d4cf

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/wp-includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6033,7 +6033,7 @@ function wp_guess_url() {
60336033

60346034
// The request is for the admin.
60356035
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
6036-
$path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
6036+
$path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] );
60376037

60386038
// The request is for a file in ABSPATH.
60396039
} elseif ( $script_filename_dir . '/' === $abspath_fix ) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Test wp_guess_url().
5+
*
6+
* @group functions.php
7+
* @covers ::wp_guess_url
8+
*/
9+
class Tests_Functions_wpGuessUrl extends WP_UnitTestCase {
10+
11+
/**
12+
* @ticket 36827
13+
*
14+
* @dataProvider data_guess_url_should_return_site_url
15+
*
16+
* @param string $url The URL to navigate to, relative to `site_url()`.
17+
*/
18+
public function test_guess_url_should_return_site_url( $url ) {
19+
$siteurl = site_url();
20+
$this->go_to( site_url( $url ) );
21+
$this->assertSame( $siteurl, wp_guess_url() );
22+
}
23+
24+
/**
25+
* Data provider.
26+
*
27+
* @return array
28+
*/
29+
function data_guess_url_should_return_site_url() {
30+
return array(
31+
'no trailing slash' => array( 'url' => 'wp-admin' ),
32+
'trailing slash' => array( 'url' => 'wp-admin/' ),
33+
'trailing slash, query var' => array( 'url' => 'wp-admin/?foo=bar' ),
34+
'file extension, no trailing slash' => array( 'url' => 'wp-login.php' ),
35+
'file extension, query var, no trailing slash' => array( 'url' => 'wp-login.php?foo=bar' ),
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)