WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#23570 closed enhancement (fixed)

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

Reported by: 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 2 years ago.
23570.2.diff (3.5 KB) - added by wonderboymusic 2 years ago.
23570.3.patch (3.4 KB) - added by Frank Klein 2 years ago.
23570-tests.diff (7.9 KB) - added by wonderboymusic 2 years ago.
23570.3.diff (3.5 KB) - added by wonderboymusic 2 years ago.
23570.4.diff (3.5 KB) - added by wonderboymusic 2 years ago.
23570.5.diff (1.1 KB) - added by helen 2 years ago.
23570.6.diff (577 bytes) - added by lancewillett 2 years ago.
Fix parameter order of in_array() call

Download all attachments as: .zip

Change History (33)

@wonderboymusic2 years ago

comment:1 @helen2 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.

comment:2 @lancewillett2 years ago

  • Cc lancewillett added

comment:3 @lancewillett2 years ago

For support of new functionality in default themes, see:

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

@wonderboymusic2 years ago

comment:4 @wonderboymusic2 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.

@Frank Klein2 years ago

comment:5 @Frank Klein2 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.

comment:6 @Frank Klein2 years ago

  • Cc contact@… added

comment:7 @nacin2 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.

@wonderboymusic2 years ago

@wonderboymusic2 years ago

comment:8 @wonderboymusic2 years ago

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

comment:9 @DrewAPicture2 years ago

  • Cc xoodrew@… added

comment:10 follow-up: @Mamaduka2 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

comment:11 in reply to: ↑ 10 @helen2 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.

@wonderboymusic2 years ago

comment:12 @wonderboymusic2 years ago

Updated to use *_url instead of *_link func names

comment:13 @markjaquith2 years ago

In 23773:

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

props wonderboymusic. see #23570

comment:14 @markjaquith2 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.

comment:15 @helen2 years ago

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

Yeah, let's definitely get some filters in.

@helen2 years ago

comment:16 @helen2 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.

comment:17 @helen2 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.

comment:18 @helen2 years ago

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

comment:19 @helen2 years ago

  • Keywords needs-patch removed

@lancewillett2 years ago

Fix parameter order of in_array() call

comment:20 @lancewillett2 years ago

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

comment:21 @SergeyBiryukov2 years ago

In 23789:

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

comment:22 @wonderboymusic2 years ago

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

comment:23 @DrewAPicture2 years ago

  • Keywords needs-codex added

comment:24 @wonderboymusic2 years ago

In 1261/tests:

Add initial tests for . See #23570

comment:25 @ryan2 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.