| 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 ( $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 | */ |
| 1973 | function 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 | */ |
| 1991 | function 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 | */ |
| 2032 | function 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 | */ |
| 2049 | function 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 | */ |
| 2066 | function 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 | */ |
| 2083 | function 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 | } |
| 2090 | No newline at end of file |