| 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 | */ |
| 1876 | function 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 | */ |
| 1904 | function 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 | */ |
| 1926 | function get_content_images( &$content, $remove = false, $limit = 0 ) { |
| 1927 | $src = ''; |
| 1928 | $srcs = array(); |
| 1929 | $matches = array(); |
| 1930 | |
| 1931 | if ( preg_match_all( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1932 | foreach ( $matches as $tag ) { |
| 1933 | $count = 1; |
| 1934 | if ( $remove ) |
| 1935 | $content = str_replace( $tag[0], '', $content, $count ); |
| 1936 | |
| 1937 | preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[1], $src ); |
| 1938 | if ( ! empty( $src[1] ) ) { |
| 1939 | $srcs[] = $src[1]; |
| 1940 | if ( $limit > 0 && count( $srcs ) >= $limit ) |
| 1941 | break; |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | return array_values( array_unique( $srcs ) ); |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * Check the content blob for image srcs and return the first |
| 1951 | * |
| 1952 | * @since 3.6.0 |
| 1953 | * |
| 1954 | * @param string $content A string which might contain image data. |
| 1955 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1956 | * @return string The found data |
| 1957 | */ |
| 1958 | function get_content_image( &$content, $remove = false ) { |
| 1959 | $srcs = get_content_images( $content, $remove, 1 ); |
| 1960 | if ( empty( $srcs ) ) |
| 1961 | return ''; |
| 1962 | |
| 1963 | return reset( $srcs ); |
| 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * Check the content blob for galleries and return their image srcs |
| 1968 | * |
| 1969 | * @since 3.6.0 |
| 1970 | * |
| 1971 | * @param string $content A string which might contain image data. |
| 1972 | * @param boolean $remove Optional. Whether to remove the found data from the passed content. |
| 1973 | * @param int $limit Optional. The number of galleries to return |
| 1974 | * @return array A list of galleries, which in turn are a list of their srcs in order |
| 1975 | */ |
| 1976 | function get_content_galleries( &$content, $remove = false, $limit = 0 ) { |
| 1977 | $src = ''; |
| 1978 | $galleries = array(); |
| 1979 | $matches = array(); |
| 1980 | |
| 1981 | if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1982 | foreach ( $matches as $shortcode ) { |
| 1983 | if ( 'gallery' === $shortcode[2] ) { |
| 1984 | $srcs = array(); |
| 1985 | $count = 1; |
| 1986 | if ( $remove ) |
| 1987 | $content = str_replace( $shortcode[0], '', $content, $count ); |
| 1988 | |
| 1989 | $data = shortcode_parse_atts( $shortcode[3] ); |
| 1990 | $gallery = do_shortcode_tag( $shortcode ); |
| 1991 | preg_match_all( '#src=[\'"](.+?)[\'"]#is', $gallery, $src, PREG_SET_ORDER ); |
| 1992 | if ( ! empty( $src ) ) { |
| 1993 | foreach ( $src as $s ) |
| 1994 | $srcs[] = $s[1]; |
| 1995 | } |
| 1996 | |
| 1997 | $data['src'] = array_values( array_unique( $srcs ) ); |
| 1998 | $galleries[] = $data; |
| 1999 | if ( $limit > 0 && count( $galleries ) >= $limit ) |
| 2000 | break; |
| 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | return $galleries; |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * Retrieve galleries from the passed post's content |
| 2010 | * |
| 2011 | * @since 3.6.0 |
| 2012 | * |
| 2013 | * @param int $post_id Optional. Post ID. |
| 2014 | * @return array A list of arrays, each containing gallery data and srcs parsed |
| 2015 | * from the expanded shortcode |
| 2016 | */ |
| 2017 | function get_post_galleries( $post_id = 0 ) { |
| 2018 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 2019 | if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' ) ) |
| 2020 | return array(); |
| 2021 | |
| 2022 | return get_content_galleries( $post->post_content ); |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * Retrieve the image srcs from galleries from a post's content, if present |
| 2027 | * |
| 2028 | * @since 3.6.0 |
| 2029 | * |
| 2030 | * @param int $post_id Optional. Post ID. |
| 2031 | * @return array A list of lists, each containing image srcs parsed |
| 2032 | * from an expanded shortcode |
| 2033 | */ |
| 2034 | function get_post_galleries_images( $post_id = 0 ) { |
| 2035 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 2036 | if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' ) ) |
| 2037 | return array(); |
| 2038 | |
| 2039 | $data = get_content_galleries( $post->post_content ); |
| 2040 | return wp_list_pluck( $data, 'src' ); |
| 2041 | } |
| 2042 | |
| 2043 | /** |
| 2044 | * Check a specified post's content for gallery and, if present, return the first |
| 2045 | * |
| 2046 | * @since 3.6.0 |
| 2047 | * |
| 2048 | * @param int $post_id Optional. Post ID. |
| 2049 | * @return array Gallery data and srcs parsed from the expanded shortcode |
| 2050 | */ |
| 2051 | function get_post_gallery( $post_id = 0 ) { |
| 2052 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 2053 | if ( empty( $post ) || ! has_shortcode( $post->post_content, 'gallery' ) ) |
| 2054 | return array(); |
| 2055 | |
| 2056 | $data = get_content_galleries( $post->post_content, false, 1 ); |
| 2057 | return reset( $data ); |
| 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * Check a post's content for galleries and return the image srcs for the first found gallery |
| 2062 | * |
| 2063 | * @since 3.6.0 |
| 2064 | * |
| 2065 | * @param int $post_id Optional. Post ID. |
| 2066 | * @return array A list of a gallery's image srcs in order |
| 2067 | */ |
| 2068 | function get_post_gallery_images( $post_id = 0 ) { |
| 2069 | $gallery = get_post_gallery( $post_id ); |
| 2070 | if ( empty( $gallery['src'] ) ) |
| 2071 | return array(); |
| 2072 | |
| 2073 | return $gallery['src']; |
| 2074 | } |
| 2075 | No newline at end of file |