Make WordPress Core

Changeset 24066


Ignore:
Timestamp:
04/22/2013 10:11:42 PM (13 years ago)
Author:
markjaquith
Message:

Multiple improvements to image post format insertion and display.

  • get_tag_regex() altered based on Unit Tests.
  • Changes to post-formats.js to provide size and link context during image selection.
  • Captions are now output in the_post_format_image() when present.
  • The meta value for url is respected for the image post format when the HTML in the image meta doesn't include a link

props wonderboymusic. fixes #23965, #23964. see #24147, #24046.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/post-formats.js

    r24039 r24066  
    9494                        lastMimeType = mime;
    9595
    96                         // Create the media frame.
    97                         mediaFrame = wp.media.frames.formatMedia = wp.media({
    98                                 // Set the title of the modal.
    99                                 title: $el.data('choose'),
    100 
    101                                 // Tell the modal to show only items matching the current mime type.
    102                                 library: {
    103                                         type: mime
     96                        mediaFrame = wp.media.frames.formatMedia = wp.media( {
     97                                button: {
     98                                        text: $el.data('update')
    10499                                },
    105 
    106                                 // Customize the submit button.
    107                                 button: {
    108                                         // Set the text of the button.
    109                                         text: $el.data('update')
    110                                 }
    111                         });
     100                                states: [
     101                                        new wp.media.controller.Library({
     102                                                library: wp.media.query( { type: mime } ),
     103                                                title: $el.data('choose'),
     104                                                displaySettings: 'image' === mime
     105                                        })
     106                                ]
     107                        } );
    112108
    113109                        mediaPreview = function(attachment) {
    … …  
    156152                                        mediaPreview(attachment);
    157153                                } else {
    158                                         html = wp.media.string.image({}, attachment);
     154                                        html = wp.media.string.image({
     155                                                align : getUserSetting('align'),
     156                                                size : getUserSetting('imgsize'),
     157                                                link : getUserSetting('urlbutton')
     158                                        }, attachment);
    159159                                        // set the hidden input's value
    160160                                        $field.val(html);
  • trunk/wp-includes/functions.php

    r23922 r24066  
    39863986        if ( empty( $tag ) )
    39873987                return;
    3988 
    3989         return sprintf( '(<%1$s[^>]*(?:/?>$|>[\s\S]*?</%1$s>))', tag_escape( $tag ) );
    3990 }
     3988        return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
     3989}
  • trunk/wp-includes/media.php

    r24021 r24066  
    19671967        foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) {
    19681968                if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
    1969                         $html[] = $matches[1];
     1969                        $html[] = $matches[0];
    19701970                        if ( $remove )
    19711971                                $content = str_replace( $matches[0], '', $content );
    … …  
    22102210        $captions = array();
    22112211
    2212         if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     2212        if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
    22132213                foreach ( $matches as $shortcode ) {
    2214                         if ( 'caption' === $shortcode[2] )
     2214                        if ( 'caption' === $shortcode[2] ) {
    22152215                                $captions[] = $shortcode[0];
    2216                 }
    2217         }
    2218 
    2219         if ( preg_match_all( '#<img[^>]+/?>#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
    2220                 foreach ( $matches as $tag ) {
    2221                         $count = 1;
    2222                         if ( $remove ) {
     2216                                if ( $html )
     2217                                        $tags[] = do_shortcode( $shortcode[0] );
     2218                        }
     2219
     2220                        if ( $limit > 0 && count( $tags ) >= $limit )
     2221                                break;
     2222                }
     2223        }
     2224
     2225        foreach ( array( 'a', 'img' ) as $tag ) {
     2226                if ( preg_match_all( '#' . get_tag_regex( $tag ) .  '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     2227                        foreach ( $matches as $node ) {
     2228                                if ( ! strstr( $node[0], '<img ' ) )
     2229                                        continue;
     2230
     2231                                $count = 1;
     2232                                $found = false;
     2233
    22232234                                foreach ( $captions as $caption ) {
    2224                                         if ( strstr( $caption, $tag[0] ) ) {
    2225                                                 $content = str_replace( $caption, '', $content, $count );
     2235                                        if ( strstr( $caption, $node[0] ) ) {
     2236                                                $found = true;
     2237                                                if ( $remove )
     2238                                                        $content = str_replace( $caption, '', $content, $count );
    22262239                                        }
    22272240                                }
    22282241
    2229                                 $content = str_replace( $tag[0], '', $content, $count );
     2242                                if ( $remove )
     2243                                        $content = str_replace( $node[0], '', $content, $count );
     2244
     2245                                if ( ! $found )
     2246                                        $tags[] = $node[0];
     2247
     2248                                if ( $limit > 0 && count( $tags ) >= $limit )
     2249                                        break 2;
    22302250                        }
    2231 
    2232                         $tags[] = $tag[0];
    2233 
    2234                         if ( $limit > 0 && count( $tags ) >= $limit )
    2235                                 break;
    22362251                }
    22372252        }
    … …  
    24012416                return $post->format_content;
    24022417
     2418        $matched = false;
    24032419        $meta = get_post_format_meta( $post->ID );
    24042420
    … …  
    24082424
    24092425        if ( ! empty( $meta['image'] ) ) {
    2410                 if ( is_numeric( $meta['image'] ) )
     2426                if ( is_numeric( $meta['image'] ) ) {
    24112427                        $image = wp_get_attachment_image( absint( $meta['image'] ), $attached_size );
    2412                 elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['image'] ) )
     2428                        // wrap image in <a>
     2429                        if ( ! empty( $meta['url'] ) )
     2430                                $image = sprint( $link_fmt, $image );
     2431                } elseif ( has_shortcode( $meta['image'], 'gallery' ) ) {
     2432                        // wrap <img> in <a>
     2433                        if ( ! empty( $meta['url'] ) && false === strpos( $meta['image'], '<a ' ) ) {
     2434                                $meta['image'] = preg_replace(
     2435                                        '#(<img[^>]+>)#',
     2436                                        sprintf( '<a href="%s">$1</a>', esc_url( $meta['url'] ) ),
     2437                                        $meta['image']
     2438                                );
     2439                        }
    24132440                        $image = do_shortcode( $meta['image'] );
    2414                 elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) )
     2441                } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) {
     2442                        // not HTML, assume URL
    24152443                        $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
     2444                } else {
     2445                        // assume HTML
     2446                        $image = $meta['image'];
     2447                }
     2448
     2449                if ( false === strpos( $image, '<a ' ) )
     2450                        $post->format_content = sprintf( $link_fmt, $image );
    24162451                else
    2417                         $image = $meta['image'];
    2418 
    2419                 $post->format_content = sprintf( $link_fmt, $image );
     2452                        $post->format_content = $image;
    24202453                return $post->format_content;
    24212454        }
    … …  
    24452478                                if ( 'caption' === $shortcode[2] ) {
    24462479                                        foreach ( $urls as $url ) {
    2447                                                 if ( strstr( $shortcode[0], $url ) )
     2480                                                if ( strstr( $shortcode[0], $url ) ) {
     2481                                                        if ( ! $matched )
     2482                                                                $matched = do_shortcode( $shortcode[0] );
    24482483                                                        $content = str_replace( $shortcode[0], '', $content, $count );
     2484                                                }
    24492485                                        }
    24502486                                }
    … …  
    24562492                                foreach ( $matches as $match ) {
    24572493                                        foreach ( $urls as $url ) {
    2458                                                 if ( strstr( $match[0], $url ) )
     2494                                                if ( strstr( $match[0], $url ) ) {
     2495                                                        if ( ! $matched )
     2496                                                                $matched = $match[0];
    24592497                                                        $content = str_replace( $match[0], '', $content, $count );
     2498                                                }
    24602499                                        }
    24612500                                }
    … …  
    24642503
    24652504                $post->split_content = $content;
    2466                 $image = wp_get_attachment_image( $media->ID, $attached_size );
    2467                 $post->format_content = sprintf( $link_fmt, $image );
     2505                if ( ! $matched ) {
     2506                        $image = wp_get_attachment_image( $media->ID, $attached_size );
     2507                        $post->format_content = sprintf( $link_fmt, $image );
     2508                } else {
     2509                        $post->format_content = $matched;
     2510                        if ( ! empty( $meta['url'] ) && false === stripos( $matched, '<a ' ) )
     2511                                $post->format_content = sprintf( $link_fmt, $matched );
     2512                }
    24682513                return $post->format_content;
    24692514        }
Note: See TracChangeset for help on using the changeset viewer.