| 1863 | |
| 1864 | /** |
| 1865 | * Retrieve images attached to the passed post |
| 1866 | * |
| 1867 | * @since 3.6.0 |
| 1868 | * |
| 1869 | * @param int $post_id Post ID |
| 1870 | * @return array Found image attachments |
| 1871 | */ |
| 1872 | function get_attached_images( $post_id = 0 ) { |
| 1873 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| 1874 | if ( empty( $post ) ) |
| 1875 | return array(); |
| 1876 | |
| 1877 | $children = get_children( array( |
| 1878 | 'post_parent' => $post->ID, |
| 1879 | 'post_type' => 'attachment', |
| 1880 | 'post_mime_type' => 'image', |
| 1881 | 'posts_per_page' => -1, |
| 1882 | 'orderby' => 'menu_order', |
| 1883 | 'order' => 'ASC' |
| 1884 | ) ); |
| 1885 | |
| 1886 | if ( ! empty( $children ) ) |
| 1887 | return $children; |
| 1888 | |
| 1889 | return array(); |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * Retrieve images attached to the passed post |
| 1894 | * |
| 1895 | * @since 3.6.0 |
| 1896 | * |
| 1897 | * @param int $post_id Post ID |
| 1898 | * @return array Found image attachments |
| 1899 | */ |
| 1900 | function get_attached_image_srcs( $post_id = 0 ) { |
| 1901 | $children = get_attached_images( $post_id ); |
| 1902 | if ( empty( $children ) ) |
| 1903 | return array(); |
| 1904 | |
| 1905 | $srcs = array(); |
| 1906 | foreach ( $children as $attachment ) |
| 1907 | $srcs[] = wp_get_attachment_url( $attachment->ID ); |
| 1908 | |
| 1909 | return $srcs; |
| 1910 | } |
| 1911 | |
| 1912 | /** |
| 1913 | * Check the content blob for images |
| 1914 | * |
| 1915 | * @since 3.6.0 |
| 1916 | * |
| 1917 | * @param string $content A string which might contain image data. |
| 1918 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1919 | * @param int $limit Optional. The number of galleries to return |
| 1920 | * @return string The found data |
| 1921 | */ |
| 1922 | function get_content_images( &$content, $remove = false, $limit = 0 ) { |
| 1923 | $src = ''; |
| 1924 | $srcs = array(); |
| 1925 | $matches = array(); |
| 1926 | |
| 1927 | if ( preg_match_all( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1928 | foreach ( $matches as $tag ) { |
| 1929 | $count = 1; |
| 1930 | if ( $remove ) |
| 1931 | $content = str_replace( $tag[0], '', $content, $count ); |
| 1932 | |
| 1933 | preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[1], $src ); |
| 1934 | if ( ! empty( $src[1] ) ) { |
| 1935 | $srcs[] = $src[1]; |
| 1936 | if ( $limit > 0 && count( $srcs ) >= $limit ) |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | return array_values( array_unique( $srcs ) ); |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Check the content blob for images and return the first |
| 1947 | * |
| 1948 | * @since 3.6.0 |
| 1949 | * |
| 1950 | * @param string $content A string which might contain image data. |
| 1951 | * @param boolean $remove Whether to remove the found data from the passed content. |
| 1952 | * @return string The found data |
| 1953 | */ |
| 1954 | function get_content_image( &$content, $remove = false ) { |
| 1955 | $srcs = get_content_images( $content, $remove, 1 ); |
| 1956 | return reset( $srcs ); |
| 1957 | } |
| 1958 | |
| 1959 | /** |
| 1960 | * Check the content blob for galleries and return their image srcs |
| 1961 | * |
| 1962 | * @since 3.6.0 |
| 1963 | * |
| 1964 | * @param string $content A string which might contain image data. |
| 1965 | * @param boolean $remove Optional. Whether to remove the found data from the passed content. |
| 1966 | * @param int $limit Optional. The number of galleries to return |
| 1967 | * @return array A list of galleries, which in turn are a list of their srcs in order |
| 1968 | */ |
| 1969 | function get_content_galleries( &$content, $remove = false, $limit = 0 ) { |
| 1970 | $src = ''; |
| 1971 | $galleries = array(); |
| 1972 | $matches = array(); |
| 1973 | |
| 1974 | if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { |
| 1975 | foreach ( $matches as $shortcode ) { |
| 1976 | if ( 'gallery' === $shortcode[2] ) { |
| 1977 | $srcs = array(); |
| 1978 | $data = array(); |
| 1979 | $count = 1; |
| 1980 | if ( $remove ) |
| 1981 | $content = str_replace( $shortcode[0], '', $content, $count ); |
| 1982 | |
| 1983 | $gallery = do_shortcode_tag( $shortcode ); |
| 1984 | preg_match_all( '#src=[\'"](.+?)[\'"]#is', $gallery, $src, PREG_SET_ORDER ); |
| 1985 | if ( ! empty( $src ) ) { |
| 1986 | foreach ( $src as $s ) |
| 1987 | $srcs[] = $s[1]; |
| 1988 | } |
| 1989 | // the function will eventually return more data about the gallery |
| 1990 | $data['src'] = array_values( array_unique( $srcs ) ); |
| 1991 | $galleries[] = $data; |
| 1992 | if ( $limit > 0 && count( $galleries ) >= $limit ) |
| 1993 | break; |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | return $galleries; |
| 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * Retrieve galleries from the passed post's content |
| 2003 | * |
| 2004 | * @since 3.6.0 |
| 2005 | * |
| 2006 | * @param int $post_id A string which might contain image data. |
| 2007 | * @return array A list of galleries, which in turn are a list of their srcs in order |
| 2008 | */ |
| 2009 | function get_post_galleries_images( $post_id = 0 ) { |
| 2010 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 2011 | if ( empty( $post ) ) |
| 2012 | return array(); |
| 2013 | |
| 2014 | $data = get_content_galleries( $post->post_content ); |
| 2015 | return wp_list_pluck( $data, 'src' ); |
| 2016 | } |
| 2017 | |
| 2018 | /** |
| 2019 | * Check the content blob for galleries and return the image srcs for the first found gallery |
| 2020 | * |
| 2021 | * @since 3.6.0 |
| 2022 | * |
| 2023 | * @param int $post_id A string which might contain image data. |
| 2024 | * @return array A list of a gallery's image srcs in order |
| 2025 | */ |
| 2026 | function get_post_gallery_images( $post_id = 0 ) { |
| 2027 | $post = empty( $post_id ) ? clone get_post() : get_post( $post_id ); |
| 2028 | if ( empty( $post ) ) |
| 2029 | return array(); |
| 2030 | |
| 2031 | $data = get_content_galleries( $post->post_content, false, 1 ); |
| 2032 | return reset( wp_list_pluck( $data, 'src' ) ); |
| 2033 | } |
| 2034 | No newline at end of file |