Make WordPress Core


Ignore:
Timestamp:
03/27/2013 06:34:59 PM (14 years ago)
Author:
markjaquith
Message:

Give themers tangible, user-friendly template functions to take full advantage of structured post formats.

  • the_audio()
  • the_video()
  • the_image()
  • get_the_media()

Also introduces:

  • get_the_extra_content()
  • the_extra_content()

Those two functions are like their non-extra versions, except that they
will have any post-format bits extracted. e.g. It's an image post, for
which the_image() will extract an <img /> tag. the_extra_content() will
output the content *without* that image.

props wonderboymusic. Herculean effort. fixes #23572

File:
1 edited

Legend:

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

    r23776 r23819  
    888888
    889889        $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
    890         if ( 'mediaelement' === $library ) {
     890        if ( 'mediaelement' === $library && did_action( 'init' ) ) {
    891891                wp_enqueue_style( 'wp-mediaelement' );
    892892                wp_enqueue_script( 'wp-mediaelement' );
    … …  
    995995
    996996        $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' );
    997         if ( 'mediaelement' === $library ) {
     997        if ( 'mediaelement' === $library && did_action( 'init' ) ) {
    998998                wp_enqueue_style( 'wp-mediaelement' );
    999999                wp_enqueue_script( 'wp-mediaelement' );
    … …  
    18291829
    18301830/**
    1831  * Extract the srcs from the post's [{media type}] <source>s
    1832  *
    1833  * @since 3.6.0
    1834  *
     1831 * Extract and parse {media type} shortcodes or srcs from the passed content
     1832 *
     1833 * @since 3.6.0
     1834 *
     1835 * @param string $type Type of media: audio or video
    18351836 * @param string $content A string which might contain media data.
     1837 * @param boolean $html Whether to return HTML or URLs
    18361838 * @param boolean $remove Whether to remove the found URL from the passed content.
    1837  * @return array A list of lists. Each item has a list of sources corresponding
    1838  *              to a [{media type}]'s primary src and specified fallbacks
    1839  */
    1840 function get_content_media( $type, &$content, $remove = false ) {
    1841         $src = '';
     1839 * @return array A list of parsed shortcodes or extracted srcs
     1840 */
     1841function get_content_media( $type, &$content, $html = true, $remove = false ) {
    18421842        $items = array();
    18431843        $matches = array();
    … …  
    18461846                foreach ( $matches as $shortcode ) {
    18471847                        if ( $type === $shortcode[2] ) {
    1848                                 $srcs = array();
    18491848                                $count = 1;
    18501849                                if ( $remove )
    1851                                         $content = str_replace( $shortcode[0], '', $content, $count );
    1852 
    1853                                 $item = do_shortcode_tag( $shortcode );
    1854                                 preg_match_all( '#src=[\'"](.+?)[\'"]#is', $item, $src, PREG_SET_ORDER );
    1855                                 if ( ! empty( $src ) ) {
    1856                                         foreach ( $src as $s )
    1857                                                 $srcs[] = $s[1];
    1858 
    1859                                         $items[] = array_values( array_unique( $srcs ) );
    1860                                 }
     1850                                        $content =& str_replace( $shortcode[0], '', $content, $count );
     1851
     1852                                $items[] = do_shortcode_tag( $shortcode );
    18611853                        }
    18621854                }
    18631855        }
    1864         return $items;
     1856
     1857        if ( $html )
     1858                return $items;
     1859
     1860        $src = '';
     1861        $data = array();
     1862
     1863        foreach ( $items as $item ) {
     1864                preg_match_all( '#src=[\'"](.+?)[\'"]#is', $item, $src, PREG_SET_ORDER );
     1865                if ( ! empty( $src ) ) {
     1866                        $srcs = array();
     1867                        foreach ( $src as $s )
     1868                                $srcs[] = $s[1];
     1869
     1870                        $data[] = array_values( array_unique( $srcs ) );
     1871                }
     1872        }
     1873
     1874        return $data;
    18651875}
    18661876
    … …  
    18711881 * @since 3.6.0
    18721882 *
     1883 * @param string $type Type of media: audio or video
    18731884 * @param string $content A string which might contain media data.
    18741885 * @param boolean $remove Whether to remove the found URL from the passed content.
     1886 * @param int $limit Optional. The number of galleries to return
    18751887 * @return array A list of found HTML media embeds and possibly a URL by itself
    18761888 */
    1877 function get_embedded_media( $type, &$content, $remove = false ) {
     1889function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) {
    18781890        $html = array();
    18791891        $matches = '';
    … …  
    18851897                                $content = str_replace( $matches[0], '', $content );
    18861898
    1887                         return $html;
    1888                 }
    1889         }
     1899                        if ( $limit > 0 && count( $html ) >= $limit )
     1900                                break;
     1901                }
     1902        }
     1903
     1904        if ( ! empty( $html ) && count( $html ) >= $limit )
     1905                return $html;
    18901906
    18911907        $lines = explode( "\n", trim( $content ) );
    18921908        $line = trim( array_shift( $lines  ) );
    1893 
    18941909        if ( 0 === stripos( $line, 'http' ) ) {
    18951910                if ( $remove )
    … …  
    19021917
    19031918/**
    1904  * Extract the srcs from the post's [audio] <source>s
     1919 * Extract the HTML or <source> srcs from the content's [audio]
    19051920 *
    19061921 * @since 3.6.0
    19071922 *
    19081923 * @param string $content A string which might contain audio data.
     1924 * @param boolean $html Whether to return HTML or URLs
    19091925 * @param boolean $remove Whether to remove the found URL from the passed content.
    1910  * @return array A list of lists. Each item has a list of sources corresponding
    1911  *              to a [audio]'s primary src and specified fallbacks
    1912  */
    1913 function get_content_audio( &$content, $remove = false ) {
    1914         return get_content_media( 'audio', $content, $remove );
     1926 * @return array A list of lists. Each item has a list of HTML or srcs corresponding
     1927 *              to an [audio]'s HTML or primary src and specified fallbacks
     1928 */
     1929function get_content_audio( &$content, $html = true, $remove = false ) {
     1930        return get_content_media( 'audio', $content, $html, $remove );
    19151931}
    19161932
    … …  
    19301946
    19311947/**
    1932  * Extract the srcs from the post's [video] <source>s
     1948 * Extract the HTML or <source> srcs from the content's [video]
    19331949 *
    19341950 * @since 3.6.0
    19351951 *
    19361952 * @param string $content A string which might contain video data.
     1953 * @param boolean $html Whether to return HTML or URLs
    19371954 * @param boolean $remove Whether to remove the found URL from the passed content.
    1938  * @return array A list of lists. Each item has a list of sources corresponding
    1939  *              to a [video]'s primary src and specified fallbacks
    1940  */
    1941 function get_content_video( &$content, $remove = false ) {
    1942         return get_content_media( 'video', $content, $remove );
     1955 * @return array A list of lists. Each item has a list of HTML or srcs corresponding
     1956 *              to a [video]'s HTML or primary src and specified fallbacks
     1957 */
     1958function get_content_video( &$content, $html = true, $remove = false ) {
     1959        return get_content_media( 'video', $content, $html, $remove );
    19431960}
    19441961
    … …  
    20022019
    20032020/**
     2021 * Return suitable HTML code for output based on the content related to the global $post
     2022 * If found, remove the content from the @global $post's post_content field
     2023 *
     2024 * @since 3.6.0
     2025 *
     2026 * @param string $type Required. 'audio' or 'video'
     2027 * @param WP_Post $post Optional. Used instead of global $post when passed.
     2028 * @return string
     2029 */
     2030function get_the_media( $type, &$post = null ) {
     2031        global $wp_embed;
     2032
     2033        if ( empty( $post ) )
     2034                $post =& get_post();
     2035
     2036        if ( empty( $post ) )
     2037                return '';
     2038
     2039        if ( isset( $post->format_content ) )
     2040                return $post->format_content;
     2041
     2042        $count = 1;
     2043
     2044        if ( has_post_format( $type ) ) {
     2045                $meta = get_post_format_meta( $post->ID );
     2046                if ( ! empty( $meta['media'] ) ) {
     2047                        if ( is_numeric( $meta['media'] ) ) {
     2048                                $url = wp_get_attachment_url( $meta['media'] );
     2049                                $shortcode = sprintf( '[%s src="%s"]', $type, $url );
     2050                        } elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['media'] ) ) {
     2051                                $shortcode = $meta['media'];
     2052                        } elseif ( preg_match( '#<[^>]+>#', $meta['media'] ) ) {
     2053                                $post->format_content = $meta['media'];
     2054                                return $post->format_content;
     2055                        } elseif ( 0 === strpos( $meta['media'], 'http' ) ) {
     2056                                $post->split_content = str_replace( $meta['media'], '', $post->post_content, $count );
     2057                                if ( strstr( $meta['media'], home_url() ) ) {
     2058                                        $shortcode = sprintf( '[%s src="%s"]', $type, $meta['media'] );
     2059                                } else {
     2060                                        $post->format_content = $wp_embed->autoembed( $meta['media'] );
     2061                                        return $post->format_content;
     2062                                }
     2063                        }
     2064
     2065                        if ( ! empty( $shortcode ) ) {
     2066                                $post->format_content = do_shortcode( $shortcode );
     2067                                return $post->format_content;
     2068                        }
     2069                }
     2070        }
     2071
     2072        $medias = call_user_func( 'get_attached_' . $type );
     2073        if ( ! empty( $medias ) ) {
     2074                $media = reset( $medias );
     2075                $url = wp_get_attachment_url( $media->ID );
     2076                $shortcode = sprintf( '[%s src="%s"]', $type, $url );
     2077                $post->format_content = do_shortcode( $shortcode );
     2078                return $post->format_content;
     2079        }
     2080
     2081        // these functions expected a reference, not a value
     2082        $_content = $post->post_content;
     2083        $content =& $_content;
     2084
     2085        $htmls = get_content_media( $type, $content, true, true );
     2086        if ( ! empty( $htmls ) ) {
     2087                $html = reset( $htmls );
     2088                $post->split_content = $content;
     2089                $post->format_content = $html;
     2090                return $post->format_content;
     2091        }
     2092
     2093        $embeds = get_embedded_media( $type, $content, true, 1 );
     2094        if ( ! empty( $embeds ) ) {
     2095                $embed = reset( $embeds );
     2096                $post->split_content = $content;
     2097                if ( 0 === strpos( $embed, 'http' ) ) {
     2098                        if ( strstr( $embed, home_url() ) ) {
     2099                                $post->format_content = do_shortcode( sprintf( '[%s src="%s"]', $type, $embed ) );
     2100                        } else {
     2101                                $post->format_content = $wp_embed->autoembed( $embed );
     2102                        }
     2103                } else {
     2104                        $post->format_content = $embed;
     2105                }
     2106                return $post->format_content;
     2107        }
     2108
     2109        return '';
     2110}
     2111
     2112/**
     2113 * Output the first video in the current (@global) post's content
     2114 *
     2115 * @since 3.6.0
     2116 *
     2117 */
     2118function the_video() {
     2119        echo get_the_media( 'video' );
     2120}
     2121/**
     2122 * Output the first audio  in the current (@global) post's content
     2123 *
     2124 * @since 3.6.0
     2125 *
     2126 */
     2127function the_audio() {
     2128        echo get_the_media( 'audio' );
     2129}
     2130
     2131/**
    20042132 * Retrieve images attached to the passed post
    20052133 *
    … …  
    20342162
    20352163/**
    2036  * Check the content blob for image srcs
     2164 * Check the content blob for images or image srcs
    20372165 *
    20382166 * @since 3.6.0
    20392167 *
    20402168 * @param string $content A string which might contain image data.
     2169 * @param boolean $html Whether to return HTML or URLs
    20412170 * @param boolean $remove Whether to remove the found data from the passed content.
    20422171 * @param int $limit Optional. The number of image srcs to return
    2043  * @return array The found image srcs
    2044  */
    2045 function get_content_images( &$content, $remove = false, $limit = 0 ) {
    2046         $src = '';
    2047         $srcs = array();
     2172 * @return array The found images or srcs
     2173 */
     2174function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) {
    20482175        $matches = array();
     2176        $tags = array();
     2177        $captions = array();
    20492178
    20502179        if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
    2051                 $captions = array();
    20522180                foreach ( $matches as $shortcode ) {
    20532181                        if ( 'caption' === $shortcode[2] )
    … …  
    20692197                        }
    20702198
    2071                         preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[0], $src );
    2072                         if ( ! empty( $src[1] ) ) {
    2073                                 $srcs[] = $src[1];
    2074                                 if ( $limit > 0 && count( $srcs ) >= $limit )
    2075                                         break;
    2076                         }
     2199                        $tags[] = $tag[0];
     2200
     2201                        if ( $limit > 0 && count( $tags ) >= $limit )
     2202                                break;
     2203                }
     2204        }
     2205
     2206        if ( $html )
     2207                return $tags;
     2208
     2209        $src = '';
     2210        $srcs = array();
     2211
     2212        foreach ( $tags as $tag ) {
     2213                preg_match( '#src=[\'"](.+?)[\'"]#is', $tag, $src );
     2214                if ( ! empty( $src[1] ) ) {
     2215                        $srcs[] = $src[1];
     2216                        if ( $limit > 0 && count( $srcs ) >= $limit )
     2217                                break;
    20772218                }
    20782219        }
    … …  
    20822223
    20832224/**
    2084  * Check the content blob for image srcs and return the first
     2225 * Check the content blob for images or srcs and return the first
    20852226 *
    20862227 * @since 3.6.0
    20872228 *
    20882229 * @param string $content A string which might contain image data.
     2230 * @param boolean $html Whether to return HTML or URLs
    20892231 * @param boolean $remove Whether to remove the found data from the passed content.
    20902232 * @return string The found data
    20912233 */
    2092 function get_content_image( &$content, $remove = false ) {
    2093         $srcs = get_content_images( $content, $remove, 1 );
     2234function get_content_image( &$content, $html = true, $remove = false ) {
     2235        $srcs = get_content_images( $content, $html, $remove, 1 );
    20942236        if ( empty( $srcs ) )
    20952237                return '';
    … …  
    22072349        return $gallery['src'];
    22082350}
     2351
     2352/**
     2353 * Return the first image in the current (@global) post's content
     2354 *
     2355 * @since 3.6.0
     2356 *
     2357 * @param string $attached_size If an attached image is found, the size to display it.
     2358 * @param WP_Post $post Optional. Used instead of global $post when passed.
     2359 */
     2360function get_the_image( $attached_size = 'full', &$post = null ) {
     2361        if ( empty( $post ) )
     2362                $post = get_post();
     2363
     2364        if ( empty( $post ) )
     2365                return '';
     2366
     2367        if ( isset( $post->format_content ) )
     2368                return $post->format_content;
     2369
     2370        $medias = get_attached_images();
     2371        if ( ! empty( $medias ) ) {
     2372                $media = reset( $medias );
     2373                $sizes = get_intermediate_image_sizes();
     2374
     2375                $urls = array();
     2376                foreach ( $sizes as $size ) {
     2377                        $urls[] = reset( wp_get_attachment_image_src( $media->ID, $size ) );
     2378                        $urls[] = get_attachment_link( $media->ID );
     2379                }
     2380
     2381                $count = 1;
     2382                $matches = array();
     2383                $content =& $post->post_content;
     2384
     2385                if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     2386                        foreach ( $matches as $shortcode ) {
     2387                                if ( 'caption' === $shortcode[2] ) {
     2388                                        foreach ( $urls as $url ) {
     2389                                                if ( strstr( $shortcode[0], $url ) )
     2390                                                        $content = str_replace( $shortcode[0], '', $content, $count );
     2391                                        }
     2392                                }
     2393                        }
     2394                }
     2395
     2396                foreach ( array( 'a', 'img' ) as $tag ) {
     2397                        if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     2398                                foreach ( $matches as $match ) {
     2399                                        foreach ( $urls as $url ) {
     2400                                                if ( strstr( $match[0], $url ) )
     2401                                                        $content = str_replace( $match[0], '', $content, $count );
     2402                                        }
     2403                                }
     2404                        }
     2405                }
     2406
     2407                $post->split_content = $content;
     2408                $post->format_content = wp_get_attachment_image( $media->ID, $attached_size );
     2409                return $post->format_content;
     2410        }
     2411
     2412        $content =& $post->post_content;
     2413        $htmls = get_content_images( $content, true, true, 1 );
     2414        if ( ! empty( $htmls ) ) {
     2415                $html = reset( $htmls );
     2416                $post->split_content = $content;
     2417                $post->format_content = $html;
     2418                return $post->format_content;
     2419        }
     2420}
     2421
     2422/**
     2423 * Output the first image in the current (@global) post's content
     2424 *
     2425 * @since 3.6.0
     2426 *
     2427 * @param string $attached_size If an attached image is found, the size to display it.
     2428 */
     2429function the_image( $attached_size = 'full' ) {
     2430        echo get_the_image( $attached_size );
     2431}
Note: See TracChangeset for help on using the changeset viewer.