Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30373 closed defect (bug) (fixed)

wp_make_link_relative() isn't handling already relative urls

Reported by: dd32 Owned by: boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 4.1
Component: General Keywords: has-patch
Focuses: Cc:


#30372 added a unit test to show wp_make_link_relative() working, so I took a look and spotted a few problems with the function:

  • It doesn't handle schemeless url's
  • It doesn't handle relative URL's with a URL in a parameter.

This example shows both failings:
var_dump( wp_make_link_relative( "//something.com/blah/blah?blah=http://example.com/ex" ) ); resulting in //something.com/blah/blah?blah=/ex

Attachments (3)

30373.diff (1.5 KB) - added by voldemortensen 6 years ago.
30373.2.diff (2.0 KB) - added by voldemortensen 6 years ago.
30373.patch (2.0 KB) - added by boonebgorges 6 years ago.

Download all attachments as: .zip

Change History (15)

6 years ago

#1 @voldemortensen
6 years ago

  • Keywords has-patch added

30373.diff fixes all these issues. It also fixes #30372 and includes more comprehensive unit tests.

#2 @voldemortensen
6 years ago

#30372 was marked as a duplicate.

#3 @boonebgorges
6 years ago

  • Keywords needs-patch added; has-patch removed

voldemortensen - Thanks for the patch. Nitpick: can you separate the tests into three separate methods with more descriptive names, and put a @group 30373 annotation on the schemeless test, since that's the one actually fixed by this patch?

It looks like you are resolving the URL parameter issue with the ^ flag to mark the beginning of the string - is that right? Could you write a test for it?

#4 @voldemortensen
6 years ago

  • Keywords has-patch added; needs-patch removed

boonebgorges - made a patch with your suggestions.

6 years ago

#5 @boonebgorges
6 years ago

30373.patch fixes the annotations (sorry, I meant @ticket :) )

This looks good to me, but I'll let dd32 make the final call.

This ticket was mentioned in Slack in #core by voldemortensen. View the logs.

6 years ago

#7 @DrewAPicture
6 years ago

#30372 was marked as a duplicate.

#8 @dd32
6 years ago

  • Summary changed from wp_make_link_relative() isn't to wp_make_link_relative() isn't handling already relative urls

#9 @dd32
6 years ago

Just curious why the regex was changed from // to \/\/? Other than looking more like a W, / isn't a special character in regex (As long as it's not also the delimiter, which in this case is |)

Other than that, looks good to me.

#10 @boonebgorges
6 years ago

  • Milestone changed from Awaiting Review to 4.1

I wondered the same thing about the unnecessary escaping. I love *W*ordPress as much as the next guy, but....

#11 @boonebgorges
6 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 30383:

Improvements to wp_make_link_relative().

  • Support relative URL input.
  • When the URL being made relative has another URL as a parameter, don't make the second URL relative.

Props voldemortensen.
Fixes #30373.

#12 @DrewAPicture
6 years ago

In 30496:

Add an entry to the changelog for wp_make_link_relative() noting that intentional support was added for relative URLs.

See #30373.

Note: See TracTickets for help on using tickets.