From 0a49442e3532b068ebab495682697bef4264d19d Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Mon, 22 Apr 2013 22:11:42 +0000 Subject: [PATCH] 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. git-svn-id: http://core.svn.wordpress.org/trunk@24066 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/post-formats.js | 30 +++++------ wp-includes/functions.php | 3 +- wp-includes/media.php | 103 ++++++++++++++++++++++++++---------- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/wp-admin/js/post-formats.js b/wp-admin/js/post-formats.js index 649f4b48f0..69729ad1fd 100644 --- a/wp-admin/js/post-formats.js +++ b/wp-admin/js/post-formats.js @@ -93,22 +93,18 @@ window.wp = window.wp || {}; lastMimeType = mime; - // Create the media frame. - mediaFrame = wp.media.frames.formatMedia = wp.media({ - // Set the title of the modal. - title: $el.data('choose'), - - // Tell the modal to show only items matching the current mime type. - library: { - type: mime - }, - - // Customize the submit button. + mediaFrame = wp.media.frames.formatMedia = wp.media( { button: { - // Set the text of the button. text: $el.data('update') - } - }); + }, + states: [ + new wp.media.controller.Library({ + library: wp.media.query( { type: mime } ), + title: $el.data('choose'), + displaySettings: 'image' === mime + }) + ] + } ); mediaPreview = function(attachment) { var w, h, dimensions = '', url = attachment.url, mime = attachment.mime, format = attachment.type; @@ -155,7 +151,11 @@ window.wp = window.wp || {}; // show one preview at a time mediaPreview(attachment); } else { - html = wp.media.string.image({}, attachment); + html = wp.media.string.image({ + align : getUserSetting('align'), + size : getUserSetting('imgsize'), + link : getUserSetting('urlbutton') + }, attachment); // set the hidden input's value $field.val(html); $('#image-preview').remove(); diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 0afa50b6d3..c10709c167 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3985,6 +3985,5 @@ function wp_auth_check( $response, $data ) { function get_tag_regex( $tag ) { if ( empty( $tag ) ) return; - - return sprintf( '(<%1$s[^>]*(?:/?>$|>[\s\S]*?))', tag_escape( $tag ) ); + return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) ); } diff --git a/wp-includes/media.php b/wp-includes/media.php index bd5286763c..09e06308c8 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1966,7 +1966,7 @@ function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) { foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) { if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { - $html[] = $matches[1]; + $html[] = $matches[0]; if ( $remove ) $content = str_replace( $matches[0], '', $content ); @@ -2209,33 +2209,48 @@ function get_content_images( &$content, $html = true, $remove = false, $limit = $tags = array(); $captions = array(); - if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { + if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { foreach ( $matches as $shortcode ) { - if ( 'caption' === $shortcode[2] ) + if ( 'caption' === $shortcode[2] ) { $captions[] = $shortcode[0]; - } - } - - if ( preg_match_all( '#]+/?>#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { - foreach ( $matches as $tag ) { - $count = 1; - if ( $remove ) { - foreach ( $captions as $caption ) { - if ( strstr( $caption, $tag[0] ) ) { - $content = str_replace( $caption, '', $content, $count ); - } - } - - $content = str_replace( $tag[0], '', $content, $count ); + if ( $html ) + $tags[] = do_shortcode( $shortcode[0] ); } - $tags[] = $tag[0]; - if ( $limit > 0 && count( $tags ) >= $limit ) break; } } + foreach ( array( 'a', 'img' ) as $tag ) { + if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { + foreach ( $matches as $node ) { + if ( ! strstr( $node[0], ' 0 && count( $tags ) >= $limit ) + break 2; + } + } + } + if ( $html ) return $tags; @@ -2400,6 +2415,7 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) { if ( isset( $post->format_content ) ) return $post->format_content; + $matched = false; $meta = get_post_format_meta( $post->ID ); $link_fmt = '%s'; @@ -2407,16 +2423,33 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) { $link_fmt = '%s'; if ( ! empty( $meta['image'] ) ) { - if ( is_numeric( $meta['image'] ) ) + if ( is_numeric( $meta['image'] ) ) { $image = wp_get_attachment_image( absint( $meta['image'] ), $attached_size ); - elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['image'] ) ) + // wrap image in + if ( ! empty( $meta['url'] ) ) + $image = sprint( $link_fmt, $image ); + } elseif ( has_shortcode( $meta['image'], 'gallery' ) ) { + // wrap in + if ( ! empty( $meta['url'] ) && false === strpos( $meta['image'], ']+>)#', + sprintf( '$1', esc_url( $meta['url'] ) ), + $meta['image'] + ); + } $image = do_shortcode( $meta['image'] ); - elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) + } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) { + // not HTML, assume URL $image = sprintf( '', esc_url( $meta['image'] ) ); - else + } else { + // assume HTML $image = $meta['image']; + } - $post->format_content = sprintf( $link_fmt, $image ); + if ( false === strpos( $image, 'format_content = sprintf( $link_fmt, $image ); + else + $post->format_content = $image; return $post->format_content; } @@ -2444,8 +2477,11 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) { foreach ( $matches as $shortcode ) { if ( 'caption' === $shortcode[2] ) { foreach ( $urls as $url ) { - if ( strstr( $shortcode[0], $url ) ) + if ( strstr( $shortcode[0], $url ) ) { + if ( ! $matched ) + $matched = do_shortcode( $shortcode[0] ); $content = str_replace( $shortcode[0], '', $content, $count ); + } } } } @@ -2455,16 +2491,25 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) { if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { foreach ( $matches as $match ) { foreach ( $urls as $url ) { - if ( strstr( $match[0], $url ) ) + if ( strstr( $match[0], $url ) ) { + if ( ! $matched ) + $matched = $match[0]; $content = str_replace( $match[0], '', $content, $count ); + } } } } } $post->split_content = $content; - $image = wp_get_attachment_image( $media->ID, $attached_size ); - $post->format_content = sprintf( $link_fmt, $image ); + if ( ! $matched ) { + $image = wp_get_attachment_image( $media->ID, $attached_size ); + $post->format_content = sprintf( $link_fmt, $image ); + } else { + $post->format_content = $matched; + if ( ! empty( $meta['url'] ) && false === stripos( $matched, 'format_content = sprintf( $link_fmt, $matched ); + } return $post->format_content; } @@ -2508,4 +2553,4 @@ function attachment_url_to_postid( $url ) { } return 0; -} \ No newline at end of file +}