Make WordPress Core


Ignore:
Timestamp:
06/03/2013 07:27:58 PM (13 years ago)
Author:
markjaquith
Message:

Lose content removal and splitting from the media extraction functions.

see #24484

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r24376 r24400  
    19021902 * @param string $content A string which might contain media data.
    19031903 * @param boolean $html Whether to return HTML or URLs
    1904  * @param boolean $remove Whether to remove the found URL from the passed content.
    19051904 * @param int $limit Optional. The number of medias to return
    19061905 * @return array A list of parsed shortcodes or extracted srcs
    19071906 */
    1908 function get_content_media( $type, &$content, $html = true, $remove = false, $limit = 0 ) {
     1907function get_content_media( $type, $content, $html = true, $limit = 0 ) {
    19091908        $items = array();
    19101909
    … …  
    19131912                        if ( $type === $shortcode[2] ) {
    19141913                                $count = 1;
    1915                                 if ( $remove )
    1916                                         $content =& str_replace( $shortcode[0], '', $content, $count );
    19171914
    19181915                                $items[] = do_shortcode_tag( $shortcode );
    … …  
    19501947 * @param string $type Type of media: audio or video
    19511948 * @param string $content A string which might contain media data.
    1952  * @param boolean $remove Whether to remove the found URL from the passed content.
    19531949 * @param int $limit Optional. The number of galleries to return
    19541950 * @return array A list of found HTML media embeds and possibly a URL by itself
    19551951 */
    1956 function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) {
     1952function get_embedded_media( $type, $content, $limit = 0 ) {
    19571953        $html = array();
    19581954
    … …  
    19601956                if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
    19611957                        $html[] = $matches[0];
    1962                         if ( $remove )
    1963                                 $content = str_replace( $matches[0], '', $content );
    19641958
    19651959                        if ( $limit > 0 && count( $html ) >= $limit )
    … …  
    19741968        $line = trim( array_shift( $lines  ) );
    19751969        if ( 0 === stripos( $line, 'http' ) ) {
    1976                 if ( $remove )
    1977                         $content = join( "\n", $lines );
    1978 
    19791970                $html[] = $line;
    19801971        }
    … …  
    19891980 * @param string $content A string which might contain audio data.
    19901981 * @param boolean $html Whether to return HTML or URLs
    1991  * @param boolean $remove Whether to remove the found URL from the passed content.
    19921982 * @return array A list of lists. Each item has a list of HTML or srcs corresponding
    19931983 *              to an [audio]'s HTML or primary src and specified fallbacks
    19941984 */
    1995 function get_content_audio( &$content, $html = true, $remove = false ) {
    1996         return get_content_media( 'audio', $content, $html, $remove );
     1985function get_content_audio( $content, $html = true ) {
     1986        return get_content_media( 'audio', $content, $html );
    19971987}
    19981988
    … …  
    20041994 *
    20051995 * @param string $content A string which might contain audio data.
    2006  * @param boolean $remove Whether to remove the found URL from the passed content.
    20071996 * @return array A list of found HTML audio embeds and possibly a URL by itself
    20081997 */
    2009 function get_embedded_audio( &$content, $remove = false ) {
    2010         return get_embedded_media( 'audio', $content, $remove );
     1998function get_embedded_audio( $content ) {
     1999        return get_embedded_media( 'audio', $content );
    20112000}
    20122001
    … …  
    20182007 * @param string $content A string which might contain video data.
    20192008 * @param boolean $html Whether to return HTML or URLs
    2020  * @param boolean $remove Whether to remove the found URL from the passed content.
    20212009 * @return array A list of lists. Each item has a list of HTML or srcs corresponding
    20222010 *              to a [video]'s HTML or primary src and specified fallbacks
    20232011 */
    2024 function get_content_video( &$content, $html = true, $remove = false ) {
    2025         return get_content_media( 'video', $content, $html, $remove );
     2012function get_content_video( $content, $html = true ) {
     2013        return get_content_media( 'video', $content, $html );
    20262014}
    20272015
    … …  
    20332021 *
    20342022 * @param string $content A string which might contain video data.
    2035  * @param boolean $remove Whether to remove the found URL from the passed content.
    20362023 * @return array A list of found HTML video embeds and possibly a URL by itself
    20372024 */
    2038 function get_embedded_video( &$content, $remove = false ) {
    2039         return get_embedded_media( 'video', $content, $remove );
     2025function get_embedded_video( $content ) {
     2026        return get_embedded_media( 'video', $content );
    20402027}
    20412028
    20422029/**
    20432030 * Return suitable HTML code for output based on the content related to the global $post
    2044  * If found, remove the content from the @global $post's post_content field
    20452031 *
    20462032 * @since 3.6.0
    … …  
    20482034 * @param string $type Required. 'audio' or 'video'
    20492035 * @param WP_Post $post Optional. Used instead of global $post when passed.
    2050  * @param int $limit Optional. The number of medias to remove if content is scanned.
     2036 * @param int $limit Optional. The number of medias to extract if content is scanned.
    20512037 * @return string HTML for the media. Blank string if no media is found.
    20522038 */
    … …  
    20702056        $count = 1;
    20712057
    2072         if ( has_post_format( $type, $post ) ) {
    2073                 $meta = get_post_format_meta( $post->ID );
    2074                 if ( ! empty( $meta[$type . '_embed'] ) ) {
    2075                         $value = $meta[$type . '_embed'];
    2076                         if ( is_integer( $value ) ) {
    2077                                 $url = wp_get_attachment_url( $value );
    2078                                 $shortcode = sprintf( '[%s src="%s"]', $type, $url );
    2079                         } elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $value ) ) {
    2080                                 $shortcode = $value;
    2081                         } elseif ( preg_match( '#<[^>]+>#', $value ) ) {
    2082                                 $post->format_content[ $cache_key ] = $value;
    2083                                 return $post->format_content[ $cache_key ];
    2084                         } elseif ( 0 === strpos( $value, 'http' ) ) {
    2085                                 $post->split_content = str_replace( $value, '', $post->post_content, $count );
    2086                                 if ( strstr( $value, home_url() ) ) {
    2087                                         $shortcode = sprintf( '[%s src="%s"]', $type, $value );
    2088                                 } else {
    2089                                         $post->format_content[ $cache_key ] = $wp_embed->autoembed( $value );
    2090                                         return $post->format_content[ $cache_key ];
    2091                                 }
    2092                         }
    2093 
    2094                         if ( ! empty( $shortcode ) ) {
    2095                                 $post->format_content[ $cache_key ] = do_shortcode( $shortcode );
    2096                                 return $post->format_content[ $cache_key ];
    2097                         }
    2098                 }
    2099         }
    2100 
    21012058        // these functions expect a reference, so we should make a copy of post content to avoid changing it
    21022059        $content = $post->post_content;
    21032060
    2104         $htmls = get_content_media( $type, $content, true, true, $limit );
     2061        $htmls = get_content_media( $type, $content, true, $limit );
    21052062        if ( ! empty( $htmls ) ) {
    21062063                $html = reset( $htmls );
    2107                 $post->split_content = $content;
    21082064                $post->format_content[ $cache_key ] = $html;
    21092065                return $post->format_content[ $cache_key ];
    21102066        }
    21112067
    2112         $embeds = get_embedded_media( $type, $content, true, 1 );
     2068        $embeds = get_embedded_media( $type, $content, 1 );
    21132069        if ( ! empty( $embeds ) ) {
    21142070                $embed = reset( $embeds );
    2115                 $post->split_content = $content;
    21162071                if ( 0 === strpos( $embed, 'http' ) ) {
    21172072                        if ( strstr( $embed, home_url() ) ) {
    … …  
    21982153 * @param string $content A string which might contain image data.
    21992154 * @param boolean $html Whether to return HTML or URLs
    2200  * @param boolean $remove Whether to remove the found data from the passed content.
    22012155 * @param int $limit Optional. The number of image srcs to return
    22022156 * @return array The found images or srcs
    22032157 */
    2204 function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) {
     2158function get_content_images( $content, $html = true, $limit = 0 ) {
    22052159        $tags = array();
    22062160        $captions = array();
    … …  
    22312185                                        if ( strstr( $caption, $node[0] ) ) {
    22322186                                                $found = true;
    2233                                                 if ( $remove )
    2234                                                         $content = str_replace( $caption, '', $content, $count );
    22352187                                        }
    22362188                                }
    2237 
    2238                                 if ( $remove )
    2239                                         $content = str_replace( $node[0], '', $content, $count );
    22402189
    22412190                                if ( ! $found )
    … …  
    22722221 * @param string $content A string which might contain image data.
    22732222 * @param boolean $html Whether to return HTML or URLs
    2274  * @param boolean $remove Whether to remove the found data from the passed content.
    22752223 * @return string The found data
    22762224 */
    2277 function get_content_image( &$content, $html = true, $remove = false ) {
    2278         $srcs = get_content_images( $content, $html, $remove, 1 );
     2225function get_content_image( $content, $html = true ) {
     2226        $srcs = get_content_images( $content, $html, 1 );
    22792227        if ( empty( $srcs ) )
    22802228                return '';
    … …  
    22902238 * @param string $content A string which might contain image data.
    22912239 * @param boolean $html Whether to return HTML or data
    2292  * @param boolean $remove Optional. Whether to remove the found data from the passed content.
    22932240 * @param int $limit Optional. The number of galleries to return
    22942241 * @return array A list of galleries, which in turn are a list of their srcs in order
    22952242 */
    2296 function get_content_galleries( &$content, $html = true, $remove = false, $limit = 0 ) {
     2243function get_content_galleries( $content, $html = true, $limit = 0 ) {
    22972244        $galleries = array();
    22982245
    … …  
    23022249                                $srcs = array();
    23032250                                $count = 1;
    2304                                 if ( $remove )
    2305                                         $content = str_replace( $shortcode[0], '', $content, $count );
    23062251
    23072252                                $data = shortcode_parse_atts( $shortcode[3] );
    … …  
    24392384
    24402385        $matched = false;
    2441         $meta = get_post_format_meta( $post->ID );
    2442 
    24432386        $link_fmt = '%s';
    2444         if ( ! empty( $meta['url'] ) )
    2445                 $link_fmt = '<a href="' . esc_url( $meta['url'] ) . '">%s</a>';
    2446 
    2447         if ( ! empty( $meta['image'] ) ) {
    2448                 if ( is_numeric( $meta['image'] ) ) {
    2449                         $image = wp_get_attachment_image( absint( $meta['image'] ), $attached_size );
    2450                         // wrap image in <a>
    2451                         if ( ! empty( $meta['url'] ) )
    2452                                 $image = sprintf( $link_fmt, $image );
    2453                 } elseif ( has_shortcode( $meta['image'], 'caption' ) ) {
    2454                         // wrap <img> in <a>
    2455                         if ( ! empty( $meta['url'] ) && false === strpos( $meta['image'], '<a ' ) ) {
    2456                                 $meta['image'] = preg_replace(
    2457                                         '#(<img[^>]+>)#',
    2458                                         sprintf( '<a href="%s">$1</a>', esc_url( $meta['url'] ) ),
    2459                                         $meta['image']
    2460                                 );
    2461                         }
    2462 
    2463                         $attachment_id = img_html_to_post_id( $meta['image'], $matched_html );
    2464                         if ( $attachment_id && $matched_html ) {
    2465                                 $meta['image'] = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $meta['image'] );
    2466                                 $attachment = wp_get_attachment_image_src( $attachment_id, $attached_size );
    2467                                 $attachment_width = ( ! empty( $attachment[1] ) ) ? $attachment[1] : 0;
    2468 
    2469                                 if ( $attachment_width && preg_match_all( '#width=([\'"])(.+?)\1#is', $meta['image'], $matches ) && ! empty( $matches ) )
    2470                                         foreach ( $matches[2] as $width )
    2471                                                 if ( $width != $attachment_width )
    2472                                                         $meta['image'] = str_replace( $matches[0], sprintf( 'width="%d"', $attachment_width ), $meta['image'] );
    2473                         }
    2474 
    2475                         $image = do_shortcode( $meta['image'] );
    2476                 } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) {
    2477                         // not HTML, assume URL
    2478                         $attachment_id = attachment_url_to_postid( $meta['image'] );
    2479                         if ( $attachment_id )
    2480                                 $image = wp_get_attachment_image( $attachment_id, $attached_size );
    2481                         else
    2482                                 $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
    2483                 } else {
    2484                         // assume HTML
    2485                         $image = $meta['image'];
    2486                         $attachment_id = img_html_to_post_id( $image, $matched_html );
    2487                         if ( $attachment_id && $matched_html )
    2488                                 $image = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $image );
    2489                 }
    2490 
    2491                 if ( false === strpos( $image, '<a ' ) )
    2492                         $post->format_content[ $cache_key ] = sprintf( $link_fmt, $image );
    2493                 else
    2494                         $post->format_content[ $cache_key ] = $image;
    2495                 return $post->format_content[ $cache_key ];
    2496         }
    24972387
    24982388        $medias = get_attached_images( $post->ID );
    … …  
    25222412                                                        if ( ! $matched )
    25232413                                                                $matched = do_shortcode( $shortcode[0] );
    2524                                                         $content = str_replace( $shortcode[0], '', $content, $count );
     2414                                                        // $content = str_replace( $shortcode[0], '', $content, $count );
    25252415                                                }
    25262416                                        }
    … …  
    25362426                                                        if ( ! $matched )
    25372427                                                                $matched = $match[0];
    2538                                                         $content = str_replace( $match[0], '', $content, $count );
     2428                                                        // $content = str_replace( $match[0], '', $content, $count );
    25392429                                                }
    25402430                                        }
    … …  
    25432433                }
    25442434
    2545                 $post->split_content = $content;
    25462435                if ( ! $matched ) {
    25472436                        $image = wp_get_attachment_image( $media->ID, $attached_size );
    … …  
    25562445
    25572446        $content = $post->post_content;
    2558         $htmls = get_content_images( $content, true, true, 1 );
     2447        $htmls = get_content_images( $content, true, 1 );
    25592448        if ( ! empty( $htmls ) ) {
    25602449                $html = reset( $htmls );
    … …  
    25642453                        $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html );
    25652454
    2566                 $post->split_content = $content;
    25672455                $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html );
    25682456                return $post->format_content[ $cache_key ];
Note: See TracChangeset for help on using the changeset viewer.