Ticket #24484: 24484.diff
File 24484.diff, 16.8 KB (added by , 12 years ago) |
---|
-
wp-includes/media.php
function get_attached_video( $post_id = 0 ) { 1901 1901 * @param string $type Type of media: audio or video 1902 1902 * @param string $content A string which might contain media data. 1903 1903 * @param boolean $html Whether to return HTML or URLs 1904 * @param boolean $remove Whether to remove the found URL from the passed content.1905 1904 * @param int $limit Optional. The number of medias to return 1906 1905 * @return array A list of parsed shortcodes or extracted srcs 1907 1906 */ 1908 function get_content_media( $type, &$content, $html = true, $remove = false, $limit = 0 ) {1907 function get_content_media( $type, $content, $html = true, $limit = 0 ) { 1909 1908 $items = array(); 1910 1909 1911 1910 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 1912 1911 foreach ( $matches as $shortcode ) { 1913 1912 if ( $type === $shortcode[2] ) { 1914 1913 $count = 1; 1915 if ( $remove )1916 $content =& str_replace( $shortcode[0], '', $content, $count );1917 1914 1918 1915 $items[] = do_shortcode_tag( $shortcode ); 1919 1916 if ( $limit > 0 && count( $items ) >= $limit ) … … function get_content_media( $type, &$content, $html = true, $remove = false, $li 1949 1946 * 1950 1947 * @param string $type Type of media: audio or video 1951 1948 * @param string $content A string which might contain media data. 1952 * @param boolean $remove Whether to remove the found URL from the passed content.1953 1949 * @param int $limit Optional. The number of galleries to return 1954 1950 * @return array A list of found HTML media embeds and possibly a URL by itself 1955 1951 */ 1956 function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) {1952 function get_embedded_media( $type, $content, $limit = 0 ) { 1957 1953 $html = array(); 1958 1954 1959 1955 foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) { 1960 1956 if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { 1961 1957 $html[] = $matches[0]; 1962 if ( $remove )1963 $content = str_replace( $matches[0], '', $content );1964 1958 1965 1959 if ( $limit > 0 && count( $html ) >= $limit ) 1966 1960 break; … … function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) { 1973 1967 $lines = explode( "\n", trim( $content ) ); 1974 1968 $line = trim( array_shift( $lines ) ); 1975 1969 if ( 0 === stripos( $line, 'http' ) ) { 1976 if ( $remove )1977 $content = join( "\n", $lines );1978 1979 1970 $html[] = $line; 1980 1971 } 1981 1972 return $html; … … function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) { 1988 1979 * 1989 1980 * @param string $content A string which might contain audio data. 1990 1981 * @param boolean $html Whether to return HTML or URLs 1991 * @param boolean $remove Whether to remove the found URL from the passed content.1992 1982 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 1993 1983 * to an [audio]'s HTML or primary src and specified fallbacks 1994 1984 */ 1995 function get_content_audio( &$content, $html = true, $remove = false ) {1996 return get_content_media( 'audio', $content, $html , $remove);1985 function get_content_audio( $content, $html = true ) { 1986 return get_content_media( 'audio', $content, $html ); 1997 1987 } 1998 1988 1999 1989 /** … … function get_content_audio( &$content, $html = true, $remove = false ) { 2003 1993 * @since 3.6.0 2004 1994 * 2005 1995 * @param string $content A string which might contain audio data. 2006 * @param boolean $remove Whether to remove the found URL from the passed content.2007 1996 * @return array A list of found HTML audio embeds and possibly a URL by itself 2008 1997 */ 2009 function get_embedded_audio( &$content, $remove = false) {2010 return get_embedded_media( 'audio', $content , $remove);1998 function get_embedded_audio( $content ) { 1999 return get_embedded_media( 'audio', $content ); 2011 2000 } 2012 2001 2013 2002 /** … … function get_embedded_audio( &$content, $remove = false ) { 2017 2006 * 2018 2007 * @param string $content A string which might contain video data. 2019 2008 * @param boolean $html Whether to return HTML or URLs 2020 * @param boolean $remove Whether to remove the found URL from the passed content.2021 2009 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 2022 2010 * to a [video]'s HTML or primary src and specified fallbacks 2023 2011 */ 2024 function get_content_video( &$content, $html = true, $remove = false ) {2025 return get_content_media( 'video', $content, $html , $remove);2012 function get_content_video( $content, $html = true ) { 2013 return get_content_media( 'video', $content, $html ); 2026 2014 } 2027 2015 2028 2016 /** … … function get_content_video( &$content, $html = true, $remove = false ) { 2032 2020 * @since 3.6.0 2033 2021 * 2034 2022 * @param string $content A string which might contain video data. 2035 * @param boolean $remove Whether to remove the found URL from the passed content.2036 2023 * @return array A list of found HTML video embeds and possibly a URL by itself 2037 2024 */ 2038 function get_embedded_video( &$content, $remove = false) {2039 return get_embedded_media( 'video', $content , $remove);2025 function get_embedded_video( $content ) { 2026 return get_embedded_media( 'video', $content ); 2040 2027 } 2041 2028 2042 2029 /** 2043 2030 * 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 field2045 2031 * 2046 2032 * @since 3.6.0 2047 2033 * 2048 2034 * @param string $type Required. 'audio' or 'video' 2049 2035 * @param WP_Post $post Optional. Used instead of global $post when passed. 2050 * @param int $limit Optional. The number of medias to removeif content is scanned.2036 * @param int $limit Optional. The number of medias to extract if content is scanned. 2051 2037 * @return string HTML for the media. Blank string if no media is found. 2052 2038 */ 2053 2039 function get_the_post_format_media( $type, &$post = null, $limit = 0 ) { … … function get_the_post_format_media( $type, &$post = null, $limit = 0 ) { 2069 2055 2070 2056 $count = 1; 2071 2057 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 2101 2058 // these functions expect a reference, so we should make a copy of post content to avoid changing it 2102 2059 $content = $post->post_content; 2103 2060 2104 $htmls = get_content_media( $type, $content, true, true,$limit );2061 $htmls = get_content_media( $type, $content, true, $limit ); 2105 2062 if ( ! empty( $htmls ) ) { 2106 2063 $html = reset( $htmls ); 2107 $post->split_content = $content;2108 2064 $post->format_content[ $cache_key ] = $html; 2109 2065 return $post->format_content[ $cache_key ]; 2110 2066 } 2111 2067 2112 $embeds = get_embedded_media( $type, $content, true,1 );2068 $embeds = get_embedded_media( $type, $content, 1 ); 2113 2069 if ( ! empty( $embeds ) ) { 2114 2070 $embed = reset( $embeds ); 2115 $post->split_content = $content;2116 2071 if ( 0 === strpos( $embed, 'http' ) ) { 2117 2072 if ( strstr( $embed, home_url() ) ) { 2118 2073 $post->format_content[ $cache_key ] = do_shortcode( sprintf( '[%s src="%s"]', $type, $embed ) ); … … function get_attached_image_srcs( $post_id = 0 ) { 2197 2152 * 2198 2153 * @param string $content A string which might contain image data. 2199 2154 * @param boolean $html Whether to return HTML or URLs 2200 * @param boolean $remove Whether to remove the found data from the passed content.2201 2155 * @param int $limit Optional. The number of image srcs to return 2202 2156 * @return array The found images or srcs 2203 2157 */ 2204 function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) {2158 function get_content_images( $content, $html = true, $limit = 0 ) { 2205 2159 $tags = array(); 2206 2160 $captions = array(); 2207 2161 … … function get_content_images( &$content, $html = true, $remove = false, $limit = 2230 2184 foreach ( $captions as $caption ) { 2231 2185 if ( strstr( $caption, $node[0] ) ) { 2232 2186 $found = true; 2233 if ( $remove )2234 $content = str_replace( $caption, '', $content, $count );2235 2187 } 2236 2188 } 2237 2189 2238 if ( $remove )2239 $content = str_replace( $node[0], '', $content, $count );2240 2241 2190 if ( ! $found ) 2242 2191 $tags[] = $node[0]; 2243 2192 … … function get_content_images( &$content, $html = true, $remove = false, $limit = 2271 2220 * 2272 2221 * @param string $content A string which might contain image data. 2273 2222 * @param boolean $html Whether to return HTML or URLs 2274 * @param boolean $remove Whether to remove the found data from the passed content.2275 2223 * @return string The found data 2276 2224 */ 2277 function get_content_image( &$content, $html = true, $remove = false ) {2278 $srcs = get_content_images( $content, $html, $remove,1 );2225 function get_content_image( $content, $html = true ) { 2226 $srcs = get_content_images( $content, $html, 1 ); 2279 2227 if ( empty( $srcs ) ) 2280 2228 return ''; 2281 2229 … … function get_content_image( &$content, $html = true, $remove = false ) { 2289 2237 * 2290 2238 * @param string $content A string which might contain image data. 2291 2239 * @param boolean $html Whether to return HTML or data 2292 * @param boolean $remove Optional. Whether to remove the found data from the passed content.2293 2240 * @param int $limit Optional. The number of galleries to return 2294 2241 * @return array A list of galleries, which in turn are a list of their srcs in order 2295 2242 */ 2296 function get_content_galleries( &$content, $html = true, $remove = false, $limit = 0 ) {2243 function get_content_galleries( $content, $html = true, $limit = 0 ) { 2297 2244 $galleries = array(); 2298 2245 2299 2246 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { … … function get_content_galleries( &$content, $html = true, $remove = false, $limit 2301 2248 if ( 'gallery' === $shortcode[2] ) { 2302 2249 $srcs = array(); 2303 2250 $count = 1; 2304 if ( $remove )2305 $content = str_replace( $shortcode[0], '', $content, $count );2306 2251 2307 2252 $data = shortcode_parse_atts( $shortcode[3] ); 2308 2253 $gallery = do_shortcode_tag( $shortcode ); … … function get_the_post_format_image( $attached_size = 'full', &$post = null ) { 2438 2383 $post->format_content = array(); 2439 2384 2440 2385 $matched = false; 2441 $meta = get_post_format_meta( $post->ID );2442 2443 2386 $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 URL2478 $attachment_id = attachment_url_to_postid( $meta['image'] );2479 if ( $attachment_id )2480 $image = wp_get_attachment_image( $attachment_id, $attached_size );2481 else2482 $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );2483 } else {2484 // assume HTML2485 $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 else2494 $post->format_content[ $cache_key ] = $image;2495 return $post->format_content[ $cache_key ];2496 }2497 2387 2498 2388 $medias = get_attached_images( $post->ID ); 2499 2389 if ( ! empty( $medias ) ) { … … function get_the_post_format_image( $attached_size = 'full', &$post = null ) { 2521 2411 if ( strstr( $shortcode[0], $url ) ) { 2522 2412 if ( ! $matched ) 2523 2413 $matched = do_shortcode( $shortcode[0] ); 2524 $content = str_replace( $shortcode[0], '', $content, $count );2414 // $content = str_replace( $shortcode[0], '', $content, $count ); 2525 2415 } 2526 2416 } 2527 2417 } … … function get_the_post_format_image( $attached_size = 'full', &$post = null ) { 2535 2425 if ( strstr( $match[0], $url ) ) { 2536 2426 if ( ! $matched ) 2537 2427 $matched = $match[0]; 2538 $content = str_replace( $match[0], '', $content, $count );2428 // $content = str_replace( $match[0], '', $content, $count ); 2539 2429 } 2540 2430 } 2541 2431 } 2542 2432 } 2543 2433 } 2544 2434 2545 $post->split_content = $content;2546 2435 if ( ! $matched ) { 2547 2436 $image = wp_get_attachment_image( $media->ID, $attached_size ); 2548 2437 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $image ); … … function get_the_post_format_image( $attached_size = 'full', &$post = null ) { 2555 2444 } 2556 2445 2557 2446 $content = $post->post_content; 2558 $htmls = get_content_images( $content, true, true,1 );2447 $htmls = get_content_images( $content, true, 1 ); 2559 2448 if ( ! empty( $htmls ) ) { 2560 2449 $html = reset( $htmls ); 2561 2450 … … function get_the_post_format_image( $attached_size = 'full', &$post = null ) { 2563 2452 if ( $attachment_id && $matched_html ) 2564 2453 $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html ); 2565 2454 2566 $post->split_content = $content;2567 2455 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html ); 2568 2456 return $post->format_content[ $cache_key ]; 2569 2457 } -
wp-includes/post.php
final class WP_Post { 577 577 */ 578 578 public $format_content; 579 579 580 /**581 * Private variable used by post formats to cache parsed content.582 *583 * @since 3.6.0584 *585 * @var string586 * @access private587 */588 public $split_content;589 580 590 581 public static function get_instance( $post_id ) { 591 582 global $wpdb; … … function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache 4990 4981 * @uses paginate_content() 4991 4982 * 4992 4983 * @param object $post The post object. 4993 * @param bool $remaining Whether to parse post formats from the content. Defaults to false.4994 4984 * @return array An array of values used for paginating the parsed content. 4995 4985 */ 4996 function wp_parse_post_content( $post , $remaining = false) {4986 function wp_parse_post_content( $post ) { 4997 4987 $numpages = 1; 4998 4988 4999 if ( $remaining ) {5000 $format = get_post_format( $post );5001 if ( $format && in_array( $format, array( 'image', 'audio', 'video', 'quote' ) ) ) {5002 // Call get_the_post_format_*() to set $post->split_content5003 switch ( $format ) {5004 case 'image':5005 get_the_post_format_image( 'full', $post );5006 break;5007 case 'audio':5008 get_the_post_format_media( 'audio', $post, 1 );5009 break;5010 case 'video':5011 get_the_post_format_media( 'video', $post, 1 );5012 break;5013 case 'quote':5014 get_the_post_format_quote( $post );5015 break;5016 }5017 }5018 }5019 5020 4989 if ( strpos( $post->post_content, '<!--nextpage-->' ) ) { 5021 4990 $multipage = 1; 5022 if ( $remaining && isset( $post->split_content ) ) 5023 $pages = paginate_content( $post->split_content ); 5024 else 5025 $pages = paginate_content( $post->post_content ); 4991 $pages = paginate_content( $post->post_content ); 5026 4992 $numpages = count( $pages ); 5027 4993 } else { 5028 if ( $remaining && isset( $post->split_content ) ) 5029 $pages = array( $post->split_content ); 5030 else 5031 $pages = array( $post->post_content ); 4994 $pages = array( $post->post_content ); 5032 4995 $multipage = 0; 5033 4996 } 5034 4997