Make WordPress Core


Ignore:
Timestamp:
07/12/2013 07:34:59 PM (11 years ago)
Author:
nacin
Message:

Simplify and reduce the new media/content extraction functions.

The URL extraction function is now get_url_in_content(). For more, see #24202.

Also adds filters to get_post_galleries() and get_post_gallery(). fixes #24309.

File:
1 edited

Legend:

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

    r24585 r24682  
    858858    $default_types = wp_get_audio_extensions();
    859859    $defaults_atts = array( 'src' => '' );
    860     foreach ( $default_types as $type  )
     860    foreach ( $default_types as $type )
    861861        $defaults_atts[$type] = '';
    862862
     
    964964    );
    965965
    966     foreach ( $default_types as $type  )
     966    foreach ( $default_types as $type )
    967967        $defaults_atts[$type] = '';
    968968
     
    14451445
    14461446    if ( ! isset( $args['mime_type'] ) ) {
    1447         $file_info  = wp_check_filetype( $args['path'] );
     1447        $file_info = wp_check_filetype( $args['path'] );
    14481448
    14491449        // If $file_info['type'] is false, then we let the editor attempt to
     
    14741474 * @access public
    14751475 *
    1476  * @param string|array $args Array of requirements.  Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1476 * @param string|array $args Array of requirements. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
    14771477 * @return boolean true if an eligible editor is found; false otherwise
    14781478 */
     
    18431843 *
    18441844 * @param string $type (Mime) type of media desired
    1845  * @param int $post_id  Post ID
     1845 * @param mixed $post Post ID or object
    18461846 * @return array Found attachments
    18471847 */
    1848 function get_attached_media( $type, $post_id = 0 ) {
    1849     if ( ! $post = get_post( $post_id ) )
    1850         return;
     1848function get_attached_media( $type, $post = 0 ) {
     1849    if ( ! $post = get_post( $post ) )
     1850        return array();
    18511851
    18521852    $args = array(
     
    18671867
    18681868/**
    1869  * Extract and parse {media type} shortcodes or srcs from the passed content
     1869 * Check the content blob for an <audio>, <video> <object>, <embed>, or <iframe>
    18701870 *
    18711871 * @since 3.6.0
     
    18731873 * @param string $type Type of media: audio or video
    18741874 * @param string $content A string which might contain media data.
    1875  * @param boolean $html Whether to return HTML or URLs
    1876  * @param int $limit Optional. The number of medias to return
    1877  * @return array A list of parsed shortcodes or extracted srcs
    1878  */
    1879 function get_content_media( $type, $content, $html = true, $limit = 0 ) {
    1880     $items = array();
    1881 
    1882     if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
    1883         foreach ( $matches as $shortcode ) {
    1884             if ( $type === $shortcode[2] ) {
    1885                 $count = 1;
    1886 
    1887                 $items[] = do_shortcode_tag( $shortcode );
    1888                 if ( $limit > 0 && count( $items ) >= $limit )
    1889                     break;
    1890             }
    1891         }
    1892     }
    1893 
    1894     if ( $html )
    1895         return $items;
    1896 
    1897     $data = array();
    1898 
    1899     foreach ( $items as $item ) {
    1900         preg_match_all( '#src=([\'"])(.+?)\1#is', $item, $src, PREG_SET_ORDER );
    1901         if ( ! empty( $src ) ) {
    1902             $srcs = array();
    1903             foreach ( $src as $s )
    1904                 $srcs[] = $s[2];
    1905 
    1906             $data[] = array_values( array_unique( $srcs ) );
    1907         }
    1908     }
    1909 
    1910     return $data;
    1911 }
    1912 
    1913 /**
    1914  * Check the content blob for an <{media type}>, <object>, <embed>, or <iframe>, in that order
    1915  * If no HTML tag is found, check the first line of the post for a URL
    1916  *
    1917  * @since 3.6.0
    1918  *
    1919  * @param string $type Type of media: audio or video
    1920  * @param string $content A string which might contain media data.
    1921  * @param int $limit Optional. The number of galleries to return
    1922  * @return array A list of found HTML media embeds and possibly a URL by itself
    1923  */
    1924 function get_embedded_media( $type, $content, $limit = 0 ) {
     1875 * @return array A list of found HTML media embeds
     1876 */
     1877function get_media_embedded_in_content( $content ) {
    19251878    $html = array();
    19261879
    1927     foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) {
     1880    foreach ( array( 'audio', 'video', 'object', 'embed', 'iframe' ) as $tag ) {
    19281881        if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
    19291882            $html[] = $matches[0];
    1930 
    1931             if ( $limit > 0 && count( $html ) >= $limit )
    1932                 break;
    1933         }
    1934     }
    1935 
    1936     if ( ! empty( $html ) && count( $html ) >= $limit )
    1937         return $html;
    1938 
    1939     $lines = explode( "\n", trim( $content ) );
    1940     $line = trim( array_shift( $lines  ) );
    1941     if ( 0 === stripos( $line, 'http' ) ) {
    1942         $html[] = $line;
    1943     }
     1883        }
     1884    }
     1885
    19441886    return $html;
    1945 }
    1946 
    1947 /**
    1948  * Extract the HTML or <source> srcs from the content's [audio]
    1949  *
    1950  * @since 3.6.0
    1951  *
    1952  * @param string $content A string which might contain audio data.
    1953  * @param boolean $html Whether to return HTML or URLs
    1954  * @return array A list of lists. Each item has a list of HTML or srcs corresponding
    1955  *      to an [audio]'s HTML or primary src and specified fallbacks
    1956  */
    1957 function get_content_audio( $content, $html = true ) {
    1958     return get_content_media( 'audio', $content, $html );
    1959 }
    1960 
    1961 /**
    1962  * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order
    1963  * If no HTML tag is found, check the first line of the post for a URL
    1964  *
    1965  * @since 3.6.0
    1966  *
    1967  * @param string $content A string which might contain audio data.
    1968  * @return array A list of found HTML audio embeds and possibly a URL by itself
    1969  */
    1970 function get_embedded_audio( $content ) {
    1971     return get_embedded_media( 'audio', $content );
    1972 }
    1973 
    1974 /**
    1975  * Extract the HTML or <source> srcs from the content's [video]
    1976  *
    1977  * @since 3.6.0
    1978  *
    1979  * @param string $content A string which might contain video data.
    1980  * @param boolean $html Whether to return HTML or URLs
    1981  * @return array A list of lists. Each item has a list of HTML or srcs corresponding
    1982  *      to a [video]'s HTML or primary src and specified fallbacks
    1983  */
    1984 function get_content_video( $content, $html = true ) {
    1985     return get_content_media( 'video', $content, $html );
    1986 }
    1987 
    1988 /**
    1989  * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order
    1990  * If no HTML tag is found, check the first line of the post for a URL
    1991  *
    1992  * @since 3.6.0
    1993  *
    1994  * @param string $content A string which might contain video data.
    1995  * @return array A list of found HTML video embeds and possibly a URL by itself
    1996  */
    1997 function get_embedded_video( $content ) {
    1998     return get_embedded_media( 'video', $content );
    1999 }
    2000 
    2001 /**
    2002  * Retrieve images attached to the passed post
    2003  *
    2004  * @since 3.6.0
    2005  *
    2006  * @param int $post_id Optional. Post ID.
    2007  * @return array Found image attachments
    2008  */
    2009 function get_attached_image_srcs( $post_id = 0 ) {
    2010     $children = get_attached_media( 'image', $post_id );
    2011     if ( empty( $children ) )
    2012         return array();
    2013 
    2014     $srcs = array();
    2015     foreach ( $children as $attachment )
    2016         $srcs[] = wp_get_attachment_url( $attachment->ID );
    2017 
    2018     return $srcs;
    20191887}
    20201888
     
    20261894 * @param string $content A string which might contain image data.
    20271895 * @param boolean $html Whether to return HTML or URLs in the array
    2028  * @param int $limit Optional. The number of image srcs to return
    20291896 * @return array The found images or srcs
    20301897 */
    2031 function get_content_images( $content, $html = true, $limit = 0 ) {
     1898function get_images_in_content( $content, $html = true ) {
    20321899    $tags = array();
    20331900    $captions = array();
    20341901
    2035     if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1902    if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) ) {
    20361903        foreach ( $matches as $shortcode ) {
    20371904            if ( 'caption' === $shortcode[2] ) {
     
    20401907                    $tags[] = do_shortcode_tag( $shortcode );
    20411908            }
    2042 
    2043             if ( $limit > 0 && count( $tags ) >= $limit )
    2044                 break;
    20451909        }
    20461910    }
    20471911
    20481912    foreach ( array( 'a', 'img' ) as $tag ) {
    2049         if ( preg_match_all( '#' . get_tag_regex( $tag ) .  '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1913        if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#i', $content, $matches, PREG_SET_ORDER ) ) {
    20501914            foreach ( $matches as $node ) {
    20511915                if ( ! strstr( $node[0], '<img ' ) )
     
    20631927                if ( ! $found )
    20641928                    $tags[] = $node[0];
    2065 
    2066                 if ( $limit > 0 && count( $tags ) >= $limit )
    2067                     break 2;
    20681929            }
    20691930        }
     
    20731934        return $tags;
    20741935
    2075     $srcs = array();
     1936    $image_srcs = array();
    20761937
    20771938    foreach ( $tags as $tag ) {
    20781939        preg_match( '#src=([\'"])(.+?)\1#is', $tag, $src );
    2079         if ( ! empty( $src[2] ) ) {
    2080             $srcs[] = $src[2];
    2081             if ( $limit > 0 && count( $srcs ) >= $limit )
    2082                 break;
    2083         }
    2084     }
    2085 
    2086     return apply_filters( 'content_images', array_values( array_unique( $srcs ) ), $content );
     1940        if ( ! empty( $src[2] ) )
     1941            $image_srcs[] = $src[2];
     1942    }
     1943
     1944    $image_srcs = array_values( array_unique( $image_srcs ) );
     1945    return apply_filters( 'get_images_in_content', $image_srcs, $content );
    20871946}
    20881947
     
    20961955 * @return string The found data
    20971956 */
    2098 function get_content_image( $content, $html = true ) {
    2099     $srcs = get_content_images( $content, $html, 1 );
    2100     if ( empty( $srcs ) )
    2101         return '';
    2102 
    2103     return apply_filters( 'content_image', reset( $srcs ), $content );
    2104 }
    2105 
    2106 /**
    2107  * Check the content blob for galleries and return their image srcs
     1957function get_image_in_content( $content, $html = true ) {
     1958    $srcs = get_images_from_content( $content, $html );
     1959    return apply_filters( 'get_image_in_content', reset( $srcs ), $content );
     1960}
     1961
     1962/**
     1963 * Retrieve galleries from the passed post's content
    21081964 *
    21091965 * @since 3.6.0
    21101966 *
    2111  * @param string $content A string which might contain image data.
     1967 * @param mixed $post Optional. Post ID or object.
    21121968 * @param boolean $html Whether to return HTML or data in the array
    2113  * @param int $limit Optional. The number of galleries to return
    2114  * @return array A list of galleries, which in turn are a list of their srcs in order
    2115  */
    2116 function get_content_galleries( $content, $html = true, $limit = 0 ) {
     1969 * @return array A list of arrays, each containing gallery data and srcs parsed
     1970 *      from the expanded shortcode
     1971 */
     1972function get_post_galleries( $post, $html = true ) {
     1973    if ( ! $post = get_post( $post ) )
     1974        return array();
     1975
     1976    if ( ! has_shortcode( $post->post_content, 'gallery' ) )
     1977        return array();
     1978
    21171979    $galleries = array();
    2118 
    2119     if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1980    if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
    21201981        foreach ( $matches as $shortcode ) {
    21211982            if ( 'gallery' === $shortcode[2] ) {
     
    21231984                $count = 1;
    21241985
    2125                 $data = shortcode_parse_atts( $shortcode[3] );
    21261986                $gallery = do_shortcode_tag( $shortcode );
    21271987                if ( $html ) {
     
    21341994                    }
    21351995
     1996                    $data = shortcode_parse_atts( $shortcode[3] );
    21361997                    $data['src'] = array_values( array_unique( $srcs ) );
    21371998                    $galleries[] = $data;
    21381999                }
    2139 
    2140                 if ( $limit > 0 && count( $galleries ) >= $limit )
    2141                     break;
    21422000            }
    21432001        }
    21442002    }
    21452003
    2146     return apply_filters( 'content_galleries', $galleries, $content );
    2147 }
    2148 
    2149 /**
    2150  * Retrieve galleries from the passed post's content
     2004    return apply_filters( 'get_post_galleries', $galleries, $post );
     2005}
     2006
     2007/**
     2008 * Check a specified post's content for gallery and, if present, return the first
    21512009 *
    21522010 * @since 3.6.0
    21532011 *
    2154  * @param int $post_id Optional. Post ID.
    2155  * @param boolean $html Whether to return HTML or data in the array
    2156  * @return array A list of arrays, each containing gallery data and srcs parsed
    2157  *      from the expanded shortcode
    2158  */
    2159 function get_post_galleries( $post_id = 0, $html = true ) {
    2160     if ( ! $post = get_post( $post_id ) )
    2161         return array();
    2162 
    2163     if ( ! has_shortcode( $post->post_content, 'gallery' )  )
    2164         return array();
    2165 
    2166     return get_content_galleries( $post->post_content, $html );
     2012 * @param mixed $post Optional. Post ID or object.
     2013 * @param boolean $html Whether to return HTML or data
     2014 * @return string|array Gallery data and srcs parsed from the expanded shortcode
     2015 */
     2016function get_post_gallery( $post = 0, $html = true ) {
     2017    $galleries = get_post_galleries( $post, $html );
     2018    $gallery = reset( $galleries );
     2019
     2020    return apply_filters( 'get_post_gallery', $gallery, $post, $galleries );
    21672021}
    21682022
     
    21722026 * @since 3.6.0
    21732027 *
    2174  * @param int $post_id Optional. Post ID.
     2028 * @param mixed $post Optional. Post ID or object.
    21752029 * @return array A list of lists, each containing image srcs parsed
    21762030 *      from an expanded shortcode
    21772031 */
    2178 function get_post_galleries_images( $post_id = 0 ) {
    2179     if ( ! $post = get_post( $post_id ) )
    2180         return array();
    2181 
    2182     if ( ! has_shortcode( $post->post_content, 'gallery' )  )
    2183         return array();
    2184 
    2185     $data = get_content_galleries( $post->post_content, false );
    2186     return wp_list_pluck( $data, 'src' );
    2187 }
    2188 
    2189 /**
    2190  * Check a specified post's content for gallery and, if present, return the first
     2032function get_post_galleries_images( $post = 0 ) {
     2033    $galleries = get_post_galleries( $post, false );
     2034    return wp_list_pluck( $galleries, 'src' );
     2035}
     2036
     2037/**
     2038 * Check a post's content for galleries and return the image srcs for the first found gallery
    21912039 *
    21922040 * @since 3.6.0
    21932041 *
    2194  * @param int $post_id Optional. Post ID.
    2195  * @param boolean $html Whether to return HTML or data
    2196  * @return string|array Gallery data and srcs parsed from the expanded shortcode
    2197  */
    2198 function get_post_gallery( $post_id = 0, $html = true ) {
    2199     if ( ! $post = get_post( $post_id ) )
    2200         return $html ? '' : array();
    2201 
    2202     if ( ! has_shortcode( $post->post_content, 'gallery' ) )
    2203         return $html ? '' : array();
    2204 
    2205     $data = get_content_galleries( $post->post_content, $html, false, 1 );
    2206     return reset( $data );
    2207 }
    2208 
    2209 /**
    2210  * Check a post's content for galleries and return the image srcs for the first found gallery
    2211  *
    2212  * @since 3.6.0
    2213  *
    2214  * @param int $post_id Optional. Post ID.
     2042 * @param mixed $post Optional. Post ID or object.
    22152043 * @return array A list of a gallery's image srcs in order
    22162044 */
    2217 function get_post_gallery_images( $post_id = 0 ) {
    2218     $gallery = get_post_gallery( $post_id, false );
    2219     if ( empty( $gallery['src'] ) )
    2220         return array();
    2221 
    2222     return $gallery['src'];
    2223 }
     2045function get_post_gallery_images( $post = 0 ) {
     2046    $galleries = get_post_gallery( $post, false );
     2047    return empty( $gallery['src'] ) ? array() : $gallery['src'];
     2048}
Note: See TracChangeset for help on using the changeset viewer.