Make WordPress Core

Changeset 23773


Ignore:
Timestamp:
03/22/2013 05:25:20 AM (11 years ago)
Author:
markjaquith
Message:

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

props wonderboymusic. see #23570

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post-formats.php

    r23753 r23773  
    312312        case 'link':
    313313            $compat['tag'] = '';
     314            $compat['position'] = 'before';
    314315
    315316            if ( ! empty( $meta['url'] ) ) {
    316317                $esc_url = preg_quote( $meta['url'], '#' );
    317318                // Make sure the same URL isn't in the post (modified/extended versions allowed)
    318                 if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) {
    319                     $format_output .= sprintf(
    320                         '<a %shref="%s">%s</a>',
    321                         empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ),
    322                         esc_url( $meta['url'] ),
    323                         empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title, $post->ID )
    324                     );
     319                if ( ! preg_match( '#' . $esc_url . '[^/&\?]?#', $content ) ) {
     320                    $url = $meta['url'];
     321                } else {
     322                    $url = get_content_url( $content, true );
    325323                }
     324            } else {
     325                $content_before = $content;
     326                $url = get_content_url( $content, true );
     327                if ( $content_before == $content )
     328                    $url = '';
    326329            }
     330
     331            if ( ! empty( $url ) ) {
     332                $format_output .= sprintf(
     333                    '<a %shref="%s">%s</a>',
     334                    empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ),
     335                    esc_url( $url ),
     336                    empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title )
     337                );
     338            }
    327339            break;
    328340
     
    379391    return $output;
    380392}
     393
     394/**
     395 * Extract a URL from passed content, if possible
     396 * Checks for a URL on the first line of the content or the first encountered href attribute.
     397 *
     398 * @since 3.6.0
     399 *
     400 * @param string $content A string which might contain a URL.
     401 * @param boolean $remove Whether the remove the found URL from the passed content.
     402 * @return string The found URL.
     403 */
     404function get_content_url( &$content, $remove = false ) {
     405    if ( empty( $content ) )
     406        return '';
     407
     408    $matches = array();
     409
     410    // the content is a URL
     411    $trimmed = trim( $content );
     412    if ( 0 === stripos( $trimmed, 'http' ) && ! preg_match( '#\s#', $trimmed ) ) {
     413        if ( $remove )
     414            $content = '';
     415
     416        return $trimmed;
     417    // the content is HTML so we grab the first href
     418    } elseif ( preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', $content, $matches ) ) {
     419        return esc_url_raw( $matches[1] );
     420    }
     421
     422    $lines = explode( "\n", $trimmed );
     423    $line = trim( array_shift( $lines ) );
     424
     425    // the content is a URL followed by content
     426    if ( 0 === stripos( $line, 'http' ) ) {
     427        if ( $remove )
     428            $content = trim( join( "\n", $lines ) );
     429
     430        return esc_url_raw( $line );
     431    }
     432
     433    return '';
     434}
     435
     436/**
     437 * Attempt to retrieve a URL from a post's content
     438 *
     439 * @since 3.6.0
     440 *
     441 * @param int $id Optional. Post ID.
     442 * @return string A URL, if found.
     443 */
     444function get_the_url( $id = 0 ) {
     445    $post = empty( $id ) ? get_post() : get_post( $id );
     446    if ( empty( $post ) )
     447        return '';
     448
     449    if ( has_post_format( 'link', $post ) ) {
     450        $meta = get_post_format_meta( $post->ID );
     451        if ( ! empty( $meta['url'] ) )
     452            return esc_url_raw( $meta['url'] );
     453    }
     454
     455    if ( ! empty( $post->post_content ) )
     456        return get_content_url( $post->post_content );
     457}
     458
     459/**
     460 * Attempt to output a URL from a post's content
     461 *
     462 * @since 3.6.0
     463 *.
     464 */
     465function the_url() {
     466    echo esc_url( get_the_url() );
     467}
Note: See TracChangeset for help on using the changeset viewer.