WordPress.org

Make WordPress Core

Changeset 23772


Ignore:
Timestamp:
03/22/2013 05:20:38 AM (7 years ago)
Author:
markjaquith
Message:

Add functions to extract images from posts in various forms

props wonderboymusic. fixes #22960

File:
1 edited

Legend:

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

    r23729 r23772  
    875875
    876876    if ( ! $primary ) {
    877         $audios = get_post_audio( $post_id );
     877        $audios = get_attached_audio( $post_id );
    878878        if ( empty( $audios ) )
    879879            return;
     
    982982
    983983    if ( ! $primary ) {
    984         $videos = get_post_video( $post_id );
     984        $videos = get_attached_video( $post_id );
    985985        if ( empty( $videos ) )
    986986            return;
     
    17821782 * @return array Found audio attachments
    17831783 */
    1784 function get_post_audio( $post_id = 0 ) {
     1784function get_attached_audio( $post_id = 0 ) {
    17851785    $post = empty( $post_id ) ? get_post() : get_post( $post_id );
    17861786    if ( empty( $post ) )
     
    18061806 * @return array Found video attachments
    18071807 */
    1808 function get_post_video( $post_id = 0 ) {
     1808function get_attached_video( $post_id = 0 ) {
    18091809    $post = empty( $post_id ) ? get_post() : get_post( $post_id );
    18101810    if ( empty( $post ) )
     
    18251825 * Audio embed handler callback.
    18261826 *
     1827 * @since 3.6.0
     1828 *
    18271829 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
    18281830 * @param array $attr Embed attributes.
     
    18411843/**
    18421844 * Video embed handler callback.
     1845 *
     1846 * @since 3.6.0
    18431847 *
    18441848 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
     
    18611865}
    18621866wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')#i', apply_filters( 'wp_video_embed_handler', 'wp_video_embed' ), 9999 );
     1867
     1868/**
     1869 * Retrieve images attached to the passed post
     1870 *
     1871 * @since 3.6.0
     1872 *
     1873 * @param int $post_id Optional. Post ID.
     1874 * @return array Found image attachments
     1875 */
     1876function get_attached_images( $post_id = 0 ) {
     1877    $post = empty( $post_id ) ? get_post() : get_post( $post_id );
     1878    if ( empty( $post ) )
     1879        return array();
     1880
     1881    $children = get_children( array(
     1882        'post_parent' => $post->ID,
     1883        'post_type' => 'attachment',
     1884        'post_mime_type' => 'image',
     1885        'posts_per_page' => -1,
     1886        'orderby' => 'menu_order',
     1887        'order' => 'ASC'
     1888    ) );
     1889
     1890    if ( ! empty( $children ) )
     1891        return $children;
     1892
     1893    return array();
     1894}
     1895
     1896/**
     1897 * Retrieve images attached to the passed post
     1898 *
     1899 * @since 3.6.0
     1900 *
     1901 * @param int $post_id Optional. Post ID.
     1902 * @return array Found image attachments
     1903 */
     1904function get_attached_image_srcs( $post_id = 0 ) {
     1905    $children = get_attached_images( $post_id );
     1906    if ( empty( $children ) )
     1907        return array();
     1908
     1909    $srcs = array();
     1910    foreach ( $children as $attachment )
     1911        $srcs[] = wp_get_attachment_url( $attachment->ID );
     1912
     1913    return $srcs;
     1914}
     1915
     1916/**
     1917 * Check the content blob for image srcs
     1918 *
     1919 * @since 3.6.0
     1920 *
     1921 * @param string $content A string which might contain image data.
     1922 * @param boolean $remove Whether to remove the found data from the passed content.
     1923 * @param int $limit Optional. The number of image srcs to return
     1924 * @return array The found image srcs
     1925 */
     1926function get_content_images( &$content, $remove = false, $limit = 0 ) {
     1927    $src = '';
     1928    $srcs = array();
     1929    $matches = array();
     1930
     1931    if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1932        $captions = array();
     1933        foreach ( $matches as $shortcode ) {
     1934            if ( 'caption' === $shortcode[2] )
     1935                $captions[] = $shortcode[0];
     1936        }
     1937    }
     1938
     1939    if ( preg_match_all( '#<img[^>]+/?>#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1940        foreach ( $matches as $tag ) {
     1941            $count = 1;
     1942            if ( $remove ) {
     1943                foreach ( $captions as $caption ) {
     1944                    if ( strstr( $caption, $tag[0] ) ) {
     1945                        $content = str_replace( $caption, '', $content, $count );
     1946                    }
     1947                }
     1948
     1949                $content = str_replace( $tag[0], '', $content, $count );
     1950            }
     1951
     1952            preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[0], $src );
     1953            if ( ! empty( $src[1] ) ) {
     1954                $srcs[] = $src[1];
     1955                if ( $limit > 0 && count( $srcs ) >= $limit )
     1956                    break;
     1957            }
     1958        }
     1959    }
     1960
     1961    return array_values( array_unique( $srcs ) );
     1962}
     1963
     1964/**
     1965 * Check the content blob for image srcs and return the first
     1966 *
     1967 * @since 3.6.0
     1968 *
     1969 * @param string $content A string which might contain image data.
     1970 * @param boolean $remove Whether to remove the found data from the passed content.
     1971 * @return string The found data
     1972 */
     1973function get_content_image( &$content, $remove = false ) {
     1974    $srcs = get_content_images( $content, $remove, 1 );
     1975    if ( empty( $srcs ) )
     1976        return '';
     1977
     1978    return reset( $srcs );
     1979}
     1980
     1981/**
     1982 * Check the content blob for galleries and return their image srcs
     1983 *
     1984 * @since 3.6.0
     1985 *
     1986 * @param string $content A string which might contain image data.
     1987 * @param boolean $remove Optional. Whether to remove the found data from the passed content.
     1988 * @param int $limit Optional. The number of galleries to return
     1989 * @return array A list of galleries, which in turn are a list of their srcs in order
     1990 */
     1991function get_content_galleries( &$content, $remove = false, $limit = 0 ) {
     1992    $src = '';
     1993    $galleries = array();
     1994    $matches = array();
     1995
     1996    if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1997        foreach ( $matches as $shortcode ) {
     1998            if ( 'gallery' === $shortcode[2] ) {
     1999                $srcs = array();
     2000                $count = 1;
     2001                if ( $remove )
     2002                    $content = str_replace( $shortcode[0], '', $content, $count );
     2003
     2004                $data = shortcode_parse_atts( $shortcode[3] );
     2005                $gallery = do_shortcode_tag( $shortcode );
     2006                preg_match_all( '#src=[\'"](.+?)[\'"]#is', $gallery, $src, PREG_SET_ORDER );
     2007                if ( ! empty( $src ) ) {
     2008                    foreach ( $src as $s )
     2009                        $srcs[] = $s[1];
     2010                }
     2011
     2012                $data['src'] = array_values( array_unique( $srcs ) );
     2013                $galleries[] = $data;
     2014                if ( $limit > 0 && count( $galleries ) >= $limit )
     2015                    break;
     2016            }
     2017        }
     2018    }
     2019
     2020    return $galleries;
     2021}
     2022
     2023/**
     2024 * Retrieve galleries from the passed post's content
     2025 *
     2026 * @since 3.6.0
     2027 *
     2028 * @param int $post_id Optional. Post ID.
     2029 * @return array A list of arrays, each containing gallery data and srcs parsed
     2030 *      from the expanded shortcode
     2031 */
     2032function get_post_galleries( $post_id = 0 ) {
     2033    $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
     2034    if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' )  )
     2035        return array();
     2036
     2037    return get_content_galleries( $post->post_content );
     2038}
     2039
     2040/**
     2041 * Retrieve the image srcs from galleries from a post's content, if present
     2042 *
     2043 * @since 3.6.0
     2044 *
     2045 * @param int $post_id Optional. Post ID.
     2046 * @return array A list of lists, each containing image srcs parsed
     2047 *      from an expanded shortcode
     2048 */
     2049function get_post_galleries_images( $post_id = 0 ) {
     2050    $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
     2051    if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' )  )
     2052        return array();
     2053
     2054    $data = get_content_galleries( $post->post_content );
     2055    return wp_list_pluck( $data, 'src' );
     2056}
     2057
     2058/**
     2059 * Check a specified post's content for gallery and, if present, return the first
     2060 *
     2061 * @since 3.6.0
     2062 *
     2063 * @param int $post_id Optional. Post ID.
     2064 * @return array Gallery data and srcs parsed from the expanded shortcode
     2065 */
     2066function get_post_gallery( $post_id = 0 ) {
     2067    $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
     2068    if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' ) )
     2069        return array();
     2070
     2071    $data = get_content_galleries( $post->post_content, false, 1 );
     2072    return reset( $data );
     2073}
     2074
     2075/**
     2076 * Check a post's content for galleries and return the image srcs for the first found gallery
     2077 *
     2078 * @since 3.6.0
     2079 *
     2080 * @param int $post_id Optional. Post ID.
     2081 * @return array A list of a gallery's image srcs in order
     2082 */
     2083function get_post_gallery_images( $post_id = 0 ) {
     2084    $gallery = get_post_gallery( $post_id );
     2085    if ( empty( $gallery['src'] ) )
     2086        return array();
     2087
     2088    return $gallery['src'];
     2089}
Note: See TracChangeset for help on using the changeset viewer.