Changeset 24555
- Timestamp:
- 07/04/2013 06:57:00 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r24554 r24555 2024 2024 2025 2025 /** 2026 * Return suitable HTML code for output based on the content related to the global $post2027 *2028 * @since 3.6.02029 *2030 * @param string $type Required. 'audio' or 'video'2031 * @param WP_Post $post Optional. Used instead of global $post when passed.2032 * @param int $limit Optional. The number of medias to extract if content is scanned.2033 * @return string HTML for the media. Blank string if no media is found.2034 */2035 function get_the_post_format_media( $type, &$post = null, $limit = 0 ) {2036 global $wp_embed;2037 2038 if ( empty( $post ) )2039 $post = get_post();2040 2041 if ( empty( $post ) )2042 return '';2043 2044 $cache_key = "media:{$type}";2045 2046 if ( isset( $post->format_content[ $cache_key ] ) )2047 return $post->format_content[ $cache_key ];2048 2049 if ( ! isset( $post->format_content ) )2050 $post->format_content = array();2051 2052 $count = 1;2053 2054 // these functions expect a reference, so we should make a copy of post content to avoid changing it2055 $content = $post->post_content;2056 2057 $htmls = get_content_media( $type, $content, true, $limit );2058 if ( ! empty( $htmls ) ) {2059 $html = reset( $htmls );2060 $post->format_content[ $cache_key ] = $html;2061 return $post->format_content[ $cache_key ];2062 }2063 2064 $embeds = get_embedded_media( $type, $content, 1 );2065 if ( ! empty( $embeds ) ) {2066 $embed = reset( $embeds );2067 if ( 0 === strpos( $embed, 'http' ) ) {2068 if ( strstr( $embed, home_url() ) ) {2069 2070 $format_content = '';2071 $attr = array( 'src' => $embed );2072 2073 if ( 'audio' == $type )2074 $format_content = wp_audio_shortcode( $attr );2075 elseif ( 'video' == $type )2076 $format_content = wp_video_shortcode( $attr );2077 2078 $post->format_content[ $cache_key ] = $format_content;2079 } else {2080 $post->format_content[ $cache_key ] = $wp_embed->autoembed( $embed );2081 }2082 } else {2083 $post->format_content[ $cache_key ] = $embed;2084 }2085 return $post->format_content[ $cache_key ];2086 }2087 2088 $medias = call_user_func( 'get_attached_' . $type, $post->ID );2089 if ( ! empty( $medias ) ) {2090 $media = reset( $medias );2091 $url = wp_get_attachment_url( $media->ID );2092 2093 $format_content = '';2094 $attr = array( 'src' => $url );2095 2096 if ( 'audio' == $type )2097 $format_content = wp_audio_shortcode( $attr );2098 elseif ( 'video' == $type )2099 $format_content = wp_video_shortcode( $attr );2100 2101 $post->format_content[ $cache_key ] = $format_content;2102 return $post->format_content[ $cache_key ];2103 }2104 2105 return '';2106 }2107 2108 /**2109 * Output the first video in the current (@global) post's content2110 *2111 * @since 3.6.02112 *2113 */2114 function the_post_format_video() {2115 $null = null;2116 echo get_the_post_format_media( 'video', $null, 1 );2117 }2118 /**2119 * Output the first audio in the current (@global) post's content2120 *2121 * @since 3.6.02122 *2123 */2124 function the_post_format_audio() {2125 $null = null;2126 echo get_the_post_format_media( 'audio', $null, 1 );2127 }2128 2129 /**2130 2026 * Retrieve images attached to the passed post 2131 2027 * … … 2373 2269 2374 2270 /** 2375 * Return the first image in the current (@global) post's content2376 *2377 * @since 3.6.02378 *2379 * @param string $attached_size If an attached image is found, the size to display it.2380 * @param WP_Post $post Optional. Used instead of global $post when passed.2381 * @return string HTML for the image. Blank string if no image is found.2382 */2383 function get_the_post_format_image( $attached_size = 'full', &$post = null ) {2384 if ( empty( $post ) )2385 $post = get_post();2386 2387 if ( empty( $post ) )2388 return '';2389 2390 $cache_key = "image:{$attached_size}";2391 2392 if ( isset( $post->format_content[ $cache_key ] ) )2393 return $post->format_content[ $cache_key ];2394 2395 if ( ! isset( $post->format_content ) )2396 $post->format_content = array();2397 2398 $matched = false;2399 $link_fmt = '%s';2400 2401 $medias = get_attached_images( $post->ID );2402 if ( ! empty( $medias ) ) {2403 $media = reset( $medias );2404 $sizes = get_intermediate_image_sizes();2405 $sizes[] = 'full'; // Add original image source.2406 2407 $urls = array();2408 foreach ( $sizes as $size ) {2409 $image = wp_get_attachment_image_src( $media->ID, $size );2410 if ( $image )2411 $urls[] = reset( $image );2412 }2413 2414 // Add media permalink.2415 $urls[] = get_attachment_link( $media->ID );2416 2417 $count = 1;2418 $content = $post->post_content;2419 2420 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {2421 foreach ( $matches as $shortcode ) {2422 if ( 'caption' === $shortcode[2] ) {2423 foreach ( $urls as $url ) {2424 if ( strstr( $shortcode[0], $url ) ) {2425 if ( ! $matched )2426 $matched = do_shortcode_tag( $shortcode );2427 }2428 }2429 }2430 }2431 }2432 2433 foreach ( array( 'a', 'img' ) as $tag ) {2434 if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {2435 foreach ( $matches as $match ) {2436 foreach ( $urls as $url ) {2437 if ( strstr( $match[0], $url ) ) {2438 if ( ! $matched )2439 $matched = $match[0];2440 }2441 }2442 }2443 }2444 }2445 2446 if ( ! $matched ) {2447 $image = wp_get_attachment_image( $media->ID, $attached_size );2448 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $image );2449 } else {2450 $post->format_content[ $cache_key ] = $matched;2451 if ( ! empty( $meta['url'] ) && false === stripos( $matched, '<a ' ) )2452 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $matched );2453 }2454 return $post->format_content[ $cache_key ];2455 }2456 2457 $content = $post->post_content;2458 $htmls = get_content_images( $content, true, 1 );2459 if ( ! empty( $htmls ) ) {2460 $html = reset( $htmls );2461 2462 $attachment_id = img_html_to_post_id( $html, $matched_html );2463 if ( $attachment_id && $matched_html )2464 $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html );2465 2466 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html );2467 return $post->format_content[ $cache_key ];2468 }2469 2470 return '';2471 }2472 2473 /**2474 * Output the first image in the current (@global) post's content2475 *2476 * @since 3.6.02477 *2478 * @param string $attached_size If an attached image is found, the size to display it.2479 */2480 function the_post_format_image( $attached_size = 'full' ) {2481 echo get_the_post_format_image( $attached_size );2482 }2483 2484 /**2485 2271 * Retrieve the attachment post id from HTML containing an image. 2486 2272 *
Note: See TracChangeset
for help on using the changeset viewer.