| 2030 | | * Return suitable HTML code for output based on the content related to the global $post |
| 2031 | | * |
| 2032 | | * @since 3.6.0 |
| 2033 | | * |
| 2034 | | * @param string $type Required. 'audio' or 'video' |
| 2035 | | * @param WP_Post $post Optional. Used instead of global $post when passed. |
| 2036 | | * @param int $limit Optional. The number of medias to extract if content is scanned. |
| 2037 | | * @return string HTML for the media. Blank string if no media is found. |
| 2038 | | */ |
| 2039 | | function get_the_post_format_media( $type, &$post = null, $limit = 0 ) { |
| 2040 | | global $wp_embed; |
| 2041 | | |
| 2042 | | if ( empty( $post ) ) |
| 2043 | | $post = get_post(); |
| 2044 | | |
| 2045 | | if ( empty( $post ) ) |
| 2046 | | return ''; |
| 2047 | | |
| 2048 | | $cache_key = "media:{$type}"; |
| 2049 | | |
| 2050 | | if ( isset( $post->format_content[ $cache_key ] ) ) |
| 2051 | | return $post->format_content[ $cache_key ]; |
| 2052 | | |
| 2053 | | if ( ! isset( $post->format_content ) ) |
| 2054 | | $post->format_content = array(); |
| 2055 | | |
| 2056 | | $count = 1; |
| 2057 | | |
| 2058 | | // these functions expect a reference, so we should make a copy of post content to avoid changing it |
| 2059 | | $content = $post->post_content; |
| 2060 | | |
| 2061 | | $htmls = get_content_media( $type, $content, true, $limit ); |
| 2062 | | if ( ! empty( $htmls ) ) { |
| 2063 | | $html = reset( $htmls ); |
| 2064 | | $post->format_content[ $cache_key ] = $html; |
| 2065 | | return $post->format_content[ $cache_key ]; |
| 2066 | | } |
| 2067 | | |
| 2068 | | $embeds = get_embedded_media( $type, $content, 1 ); |
| 2069 | | if ( ! empty( $embeds ) ) { |
| 2070 | | $embed = reset( $embeds ); |
| 2071 | | if ( 0 === strpos( $embed, 'http' ) ) { |
| 2072 | | if ( strstr( $embed, home_url() ) ) { |
| 2073 | | $post->format_content[ $cache_key ] = do_shortcode( sprintf( '[%s src="%s"]', $type, $embed ) ); |
| 2074 | | } else { |
| 2075 | | $post->format_content[ $cache_key ] = $wp_embed->autoembed( $embed ); |
| 2076 | | } |
| 2077 | | } else { |
| 2078 | | $post->format_content[ $cache_key ] = $embed; |
| 2079 | | } |
| 2080 | | return $post->format_content[ $cache_key ]; |
| 2081 | | } |
| 2082 | | |
| 2083 | | $medias = call_user_func( 'get_attached_' . $type, $post->ID ); |
| 2084 | | if ( ! empty( $medias ) ) { |
| 2085 | | $media = reset( $medias ); |
| 2086 | | $url = wp_get_attachment_url( $media->ID ); |
| 2087 | | $shortcode = sprintf( '[%s src="%s"]', $type, $url ); |
| 2088 | | $post->format_content[ $cache_key ] = do_shortcode( $shortcode ); |
| 2089 | | return $post->format_content[ $cache_key ]; |
| 2090 | | } |
| 2091 | | |
| 2092 | | return ''; |
| 2093 | | } |
| 2094 | | |
| 2095 | | /** |
| 2096 | | * Output the first video in the current (@global) post's content |
| 2097 | | * |
| 2098 | | * @since 3.6.0 |
| 2099 | | * |
| 2100 | | */ |
| 2101 | | function the_post_format_video() { |
| 2102 | | $null = null; |
| 2103 | | echo get_the_post_format_media( 'video', $null, 1 ); |
| 2104 | | } |
| 2105 | | /** |
| 2106 | | * Output the first audio in the current (@global) post's content |
| 2107 | | * |
| 2108 | | * @since 3.6.0 |
| 2109 | | * |
| 2110 | | */ |
| 2111 | | function the_post_format_audio() { |
| 2112 | | $null = null; |
| 2113 | | echo get_the_post_format_media( 'audio', $null, 1 ); |
| 2114 | | } |
| 2115 | | |
| 2116 | | /** |
| 2362 | | * Return the first image in the current (@global) post's content |
| 2363 | | * |
| 2364 | | * @since 3.6.0 |
| 2365 | | * |
| 2366 | | * @param string $attached_size If an attached image is found, the size to display it. |
| 2367 | | * @param WP_Post $post Optional. Used instead of global $post when passed. |
| 2368 | | * @return string HTML for the image. Blank string if no image is found. |
| 2369 | | */ |
| 2370 | | function get_the_post_format_image( $attached_size = 'full', &$post = null ) { |
| 2371 | | if ( empty( $post ) ) |
| 2372 | | $post = get_post(); |
| 2373 | | |
| 2374 | | if ( empty( $post ) ) |
| 2375 | | return ''; |
| 2376 | | |
| 2377 | | $cache_key = "image:{$attached_size}"; |
| 2378 | | |
| 2379 | | if ( isset( $post->format_content[ $cache_key ] ) ) |
| 2380 | | return $post->format_content[ $cache_key ]; |
| 2381 | | |
| 2382 | | if ( ! isset( $post->format_content ) ) |
| 2383 | | $post->format_content = array(); |
| 2384 | | |
| 2385 | | $matched = false; |
| 2386 | | $link_fmt = '%s'; |
| 2387 | | |
| 2388 | | $medias = get_attached_images( $post->ID ); |
| 2389 | | if ( ! empty( $medias ) ) { |
| 2390 | | $media = reset( $medias ); |
| 2391 | | $sizes = get_intermediate_image_sizes(); |
| 2392 | | $sizes[] = 'full'; // Add original image source. |
| 2393 | | |
| 2394 | | $urls = array(); |
| 2395 | | foreach ( $sizes as $size ) { |
| 2396 | | $image = wp_get_attachment_image_src( $media->ID, $size ); |
| 2397 | | if ( $image ) |
| 2398 | | $urls[] = reset( $image ); |
| 2399 | | } |
| 2400 | | |
| 2401 | | // Add media permalink. |
| 2402 | | $urls[] = get_attachment_link( $media->ID ); |
| 2403 | | |
| 2404 | | $count = 1; |
| 2405 | | $content = $post->post_content; |
| 2406 | | |
| 2407 | | if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 2408 | | foreach ( $matches as $shortcode ) { |
| 2409 | | if ( 'caption' === $shortcode[2] ) { |
| 2410 | | foreach ( $urls as $url ) { |
| 2411 | | if ( strstr( $shortcode[0], $url ) ) { |
| 2412 | | if ( ! $matched ) |
| 2413 | | $matched = do_shortcode( $shortcode[0] ); |
| 2414 | | // $content = str_replace( $shortcode[0], '', $content, $count ); |
| 2415 | | } |
| 2416 | | } |
| 2417 | | } |
| 2418 | | } |
| 2419 | | } |
| 2420 | | |
| 2421 | | foreach ( array( 'a', 'img' ) as $tag ) { |
| 2422 | | if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 2423 | | foreach ( $matches as $match ) { |
| 2424 | | foreach ( $urls as $url ) { |
| 2425 | | if ( strstr( $match[0], $url ) ) { |
| 2426 | | if ( ! $matched ) |
| 2427 | | $matched = $match[0]; |
| 2428 | | // $content = str_replace( $match[0], '', $content, $count ); |
| 2429 | | } |
| 2430 | | } |
| 2431 | | } |
| 2432 | | } |
| 2433 | | } |
| 2434 | | |
| 2435 | | if ( ! $matched ) { |
| 2436 | | $image = wp_get_attachment_image( $media->ID, $attached_size ); |
| 2437 | | $post->format_content[ $cache_key ] = sprintf( $link_fmt, $image ); |
| 2438 | | } else { |
| 2439 | | $post->format_content[ $cache_key ] = $matched; |
| 2440 | | if ( ! empty( $meta['url'] ) && false === stripos( $matched, '<a ' ) ) |
| 2441 | | $post->format_content[ $cache_key ] = sprintf( $link_fmt, $matched ); |
| 2442 | | } |
| 2443 | | return $post->format_content[ $cache_key ]; |
| 2444 | | } |
| 2445 | | |
| 2446 | | $content = $post->post_content; |
| 2447 | | $htmls = get_content_images( $content, true, 1 ); |
| 2448 | | if ( ! empty( $htmls ) ) { |
| 2449 | | $html = reset( $htmls ); |
| 2450 | | |
| 2451 | | $attachment_id = img_html_to_post_id( $html, $matched_html ); |
| 2452 | | if ( $attachment_id && $matched_html ) |
| 2453 | | $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html ); |
| 2454 | | |
| 2455 | | $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html ); |
| 2456 | | return $post->format_content[ $cache_key ]; |
| 2457 | | } |
| 2458 | | |
| 2459 | | return ''; |
| 2460 | | } |
| 2461 | | |
| 2462 | | /** |
| 2463 | | * Output the first image in the current (@global) post's content |
| 2464 | | * |
| 2465 | | * @since 3.6.0 |
| 2466 | | * |
| 2467 | | * @param string $attached_size If an attached image is found, the size to display it. |
| 2468 | | */ |
| 2469 | | function the_post_format_image( $attached_size = 'full' ) { |
| 2470 | | echo get_the_post_format_image( $attached_size ); |
| 2471 | | } |
| 2472 | | |
| 2473 | | /** |