Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#23570 closed enhancement (fixed)

Add functions to extract a URL from an arbitrary string, HTML, or the current $post

Reported by: wonderboymusic's profile wonderboymusic Owned by:
Milestone: 3.6 Priority: normal
Severity: normal Version:
Component: Post Formats Keywords: has-unit-tests needs-codex
Focuses: Cc:

Description (last modified by wonderboymusic)

Scenarios

My post has a format of "link"

  • My post has format meta for URL
  • OR - My post content is a URL
  • OR - My post contains an <a> tag with an href
  • OR - My post content is a URL followed by new line(s) and commentary

An arbitrary string might be only a URL, a URL with some commentary, or a blob of HTML that might contain an <a> with an href attribute

Introduce the_link() and get_the_link( $id = 0 ) which will arbitrarily extract a URL from a post's content.

Introduce get_content_link( &$content, $remove = false ) to find a URL in an arbitrary string meeting the above criteria and optionally remove the found URL if it wasn't embedded in HTML content.

Attachments (8)

23570.diff (4.4 KB) - added by wonderboymusic 12 years ago.
23570.2.diff (3.5 KB) - added by wonderboymusic 12 years ago.
23570.3.patch (3.4 KB) - added by Frank Klein 12 years ago.
23570-tests.diff (7.9 KB) - added by wonderboymusic 12 years ago.
23570.3.diff (3.5 KB) - added by wonderboymusic 12 years ago.
23570.4.diff (3.5 KB) - added by wonderboymusic 12 years ago.
23570.5.diff (1.1 KB) - added by helen 12 years ago.
23570.6.diff (577 bytes) - added by lancewillett 12 years ago.
Fix parameter order of in_array() call

Download all attachments as: .zip

Change History (33)

#1 @helen
12 years ago

  • Component changed from General to Post Formats

Sort of Template, sort of Post Formats. Putting it in Post Formats for easy tracking of the feature dev.

#2 @lancewillett
12 years ago

  • Cc lancewillett added

#3 @lancewillett
12 years ago

For support of new functionality in default themes, see:

  • Twenty Eleven: link post format - #23618
  • Twenty Thirteen: link post format - #23619

#4 @wonderboymusic
12 years ago

  • Description modified (diff)

attachment:23570.2.diff​ is patch refreshed against trunk and tested compatibility for these 6 scenarios:

  • Post with the link as the content
  • Post with the link as the content, followed by commentary
  • Post with the link as the content AND has link meta value
  • Post with the link as the content AND has link meta value, followed by commentary
  • Post with the link as metadata, content is empty
  • Post with the link as metadata, followed by commentary

If the theme supports structured-post-formats, nothing happens to the content.
The themer can use: the_link(), get_the_link(), and get_content_link( $content ) - all return a URL. get_content_link( $content ) only works if the link is in the post's body.

If the post has a post format of link, the compat runs whether the theme supports it or not.

#5 @Frank Klein
12 years ago

I tested the 23570.2 patch as part of #23619 and there was a very small issue with the get_content_link when the content is HTML. The regular expression for detecting the link was incorrect (it's (.+?) instead of (.?)).

So here's the .2 patch with this very small change.

#6 @Frank Klein
12 years ago

  • Cc contact@… added

#7 @nacin
12 years ago

  • Keywords needs-unit-tests added

get_content_link() really super needs unit tests for all of the scenarios covered. This is a textbook case where tests can drive development.

#8 @wonderboymusic
12 years ago

Updated patch to always return empty string, makes all attached UNIT TESTS pass

#9 @DrewAPicture
12 years ago

  • Cc xoodrew@… added

#10 follow-up: @Mamaduka
12 years ago

I think we should use esc_url() instead of esc_url_raw() in get_content_link(), because second function is not safe to use for displaying the URL.

http://codex.wordpress.org/Function_Reference/esc_url_raw

#11 in reply to: ↑ 10 @helen
12 years ago

Replying to Mamaduka:

I think we should use esc_url() instead of esc_url_raw() in get_content_link(), because second function is not safe to use for displaying the URL.

Function could be used for purposes other than display. the_link() could probably stand to do esc_url(), though.

#12 @wonderboymusic
12 years ago

Updated to use *_url instead of *_link func names

#13 @markjaquith
12 years ago

In 23773:

Add functions to extract a URL from an arbitrary string, HTML, or the current $post

props wonderboymusic. see #23570

#14 @markjaquith
12 years ago

What about filters? What if I have an existing postmeta key that stores URLs. Would be nice to be able to filter get_the_url() and have that old data considered.

#15 @helen
12 years ago

  • Keywords needs-patch has-unit-tests added; has-patch needs-unit-tests removed

Yeah, let's definitely get some filters in.

@helen
12 years ago

#16 @helen
12 years ago

23570.5.diff adds a filter to get_the_url(), corrects a little doc, and checks for the quote format as well as link. Do we need anything in get_content_url()? I guess I could see somebody wanting that.

#17 @helen
12 years ago

In 23775:

Add a filter for get_the_url(). Make sure get_the_url() also checks for the quote post format, as it currently has a URL field. see #23570.

#18 @helen
12 years ago

Leaving open - have a suspicion some things might shift when we actually start practical integration into Twenty Thirteen.

#19 @helen
12 years ago

  • Keywords needs-patch removed

@lancewillett
12 years ago

Fix parameter order of in_array() call

#20 @lancewillett
12 years ago

Quick patch to fix order of in_array() parameters: 23570.6.diff

#21 @SergeyBiryukov
12 years ago

In 23789:

Fix order of in_array() parameters. props lancewillett. see #23570.

#22 @wonderboymusic
12 years ago

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

#23 @DrewAPicture
12 years ago

  • Keywords needs-codex added

#24 @wonderboymusic
12 years ago

In 1261/tests:

Add initial tests for . See #23570

#25 @ryan
12 years ago

In 1330/tests:

Update test_get_url_in_content() to reflect changes in get_url_in_content().

see #23570

Note: See TracTickets for help on using tickets.