Changeset 23772
- Timestamp:
- 03/22/2013 05:20:38 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r23729 r23772 875 875 876 876 if ( ! $primary ) { 877 $audios = get_ post_audio( $post_id );877 $audios = get_attached_audio( $post_id ); 878 878 if ( empty( $audios ) ) 879 879 return; … … 982 982 983 983 if ( ! $primary ) { 984 $videos = get_ post_video( $post_id );984 $videos = get_attached_video( $post_id ); 985 985 if ( empty( $videos ) ) 986 986 return; … … 1782 1782 * @return array Found audio attachments 1783 1783 */ 1784 function get_ post_audio( $post_id = 0 ) {1784 function get_attached_audio( $post_id = 0 ) { 1785 1785 $post = empty( $post_id ) ? get_post() : get_post( $post_id ); 1786 1786 if ( empty( $post ) ) … … 1806 1806 * @return array Found video attachments 1807 1807 */ 1808 function get_ post_video( $post_id = 0 ) {1808 function get_attached_video( $post_id = 0 ) { 1809 1809 $post = empty( $post_id ) ? get_post() : get_post( $post_id ); 1810 1810 if ( empty( $post ) ) … … 1825 1825 * Audio embed handler callback. 1826 1826 * 1827 * @since 3.6.0 1828 * 1827 1829 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. 1828 1830 * @param array $attr Embed attributes. … … 1841 1843 /** 1842 1844 * Video embed handler callback. 1845 * 1846 * @since 3.6.0 1843 1847 * 1844 1848 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. … … 1861 1865 } 1862 1866 wp_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 */ 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 }
Note: See TracChangeset
for help on using the changeset viewer.