Changeset 24682 for trunk/wp-includes/media.php
- Timestamp:
- 07/12/2013 07:34:59 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r24585 r24682 858 858 $default_types = wp_get_audio_extensions(); 859 859 $defaults_atts = array( 'src' => '' ); 860 foreach ( $default_types as $type 860 foreach ( $default_types as $type ) 861 861 $defaults_atts[$type] = ''; 862 862 … … 964 964 ); 965 965 966 foreach ( $default_types as $type 966 foreach ( $default_types as $type ) 967 967 $defaults_atts[$type] = ''; 968 968 … … 1445 1445 1446 1446 if ( ! isset( $args['mime_type'] ) ) { 1447 $file_info 1447 $file_info = wp_check_filetype( $args['path'] ); 1448 1448 1449 1449 // If $file_info['type'] is false, then we let the editor attempt to … … 1474 1474 * @access public 1475 1475 * 1476 * @param string|array $args Array of requirements. 1476 * @param string|array $args Array of requirements. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} } 1477 1477 * @return boolean true if an eligible editor is found; false otherwise 1478 1478 */ … … 1843 1843 * 1844 1844 * @param string $type (Mime) type of media desired 1845 * @param int $post_id Post ID1845 * @param mixed $post Post ID or object 1846 1846 * @return array Found attachments 1847 1847 */ 1848 function get_attached_media( $type, $post _id= 0 ) {1849 if ( ! $post = get_post( $post _id) )1850 return ;1848 function get_attached_media( $type, $post = 0 ) { 1849 if ( ! $post = get_post( $post ) ) 1850 return array(); 1851 1851 1852 1852 $args = array( … … 1867 1867 1868 1868 /** 1869 * Extract and parse {media type} shortcodes or srcs from the passed content1869 * Check the content blob for an <audio>, <video> <object>, <embed>, or <iframe> 1870 1870 * 1871 1871 * @since 3.6.0 … … 1873 1873 * @param string $type Type of media: audio or video 1874 1874 * @param string $content A string which might contain media data. 1875 * @param boolean $html Whether to return HTML or URLs 1876 * @param int $limit Optional. The number of medias to return 1877 * @return array A list of parsed shortcodes or extracted srcs 1878 */ 1879 function get_content_media( $type, $content, $html = true, $limit = 0 ) { 1880 $items = array(); 1881 1882 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 1883 foreach ( $matches as $shortcode ) { 1884 if ( $type === $shortcode[2] ) { 1885 $count = 1; 1886 1887 $items[] = do_shortcode_tag( $shortcode ); 1888 if ( $limit > 0 && count( $items ) >= $limit ) 1889 break; 1890 } 1891 } 1892 } 1893 1894 if ( $html ) 1895 return $items; 1896 1897 $data = array(); 1898 1899 foreach ( $items as $item ) { 1900 preg_match_all( '#src=([\'"])(.+?)\1#is', $item, $src, PREG_SET_ORDER ); 1901 if ( ! empty( $src ) ) { 1902 $srcs = array(); 1903 foreach ( $src as $s ) 1904 $srcs[] = $s[2]; 1905 1906 $data[] = array_values( array_unique( $srcs ) ); 1907 } 1908 } 1909 1910 return $data; 1911 } 1912 1913 /** 1914 * Check the content blob for an <{media type}>, <object>, <embed>, or <iframe>, in that order 1915 * If no HTML tag is found, check the first line of the post for a URL 1916 * 1917 * @since 3.6.0 1918 * 1919 * @param string $type Type of media: audio or video 1920 * @param string $content A string which might contain media data. 1921 * @param int $limit Optional. The number of galleries to return 1922 * @return array A list of found HTML media embeds and possibly a URL by itself 1923 */ 1924 function get_embedded_media( $type, $content, $limit = 0 ) { 1875 * @return array A list of found HTML media embeds 1876 */ 1877 function get_media_embedded_in_content( $content ) { 1925 1878 $html = array(); 1926 1879 1927 foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) {1880 foreach ( array( 'audio', 'video', 'object', 'embed', 'iframe' ) as $tag ) { 1928 1881 if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { 1929 1882 $html[] = $matches[0]; 1930 1931 if ( $limit > 0 && count( $html ) >= $limit ) 1932 break; 1933 } 1934 } 1935 1936 if ( ! empty( $html ) && count( $html ) >= $limit ) 1937 return $html; 1938 1939 $lines = explode( "\n", trim( $content ) ); 1940 $line = trim( array_shift( $lines ) ); 1941 if ( 0 === stripos( $line, 'http' ) ) { 1942 $html[] = $line; 1943 } 1883 } 1884 } 1885 1944 1886 return $html; 1945 }1946 1947 /**1948 * Extract the HTML or <source> srcs from the content's [audio]1949 *1950 * @since 3.6.01951 *1952 * @param string $content A string which might contain audio data.1953 * @param boolean $html Whether to return HTML or URLs1954 * @return array A list of lists. Each item has a list of HTML or srcs corresponding1955 * to an [audio]'s HTML or primary src and specified fallbacks1956 */1957 function get_content_audio( $content, $html = true ) {1958 return get_content_media( 'audio', $content, $html );1959 }1960 1961 /**1962 * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order1963 * If no HTML tag is found, check the first line of the post for a URL1964 *1965 * @since 3.6.01966 *1967 * @param string $content A string which might contain audio data.1968 * @return array A list of found HTML audio embeds and possibly a URL by itself1969 */1970 function get_embedded_audio( $content ) {1971 return get_embedded_media( 'audio', $content );1972 }1973 1974 /**1975 * Extract the HTML or <source> srcs from the content's [video]1976 *1977 * @since 3.6.01978 *1979 * @param string $content A string which might contain video data.1980 * @param boolean $html Whether to return HTML or URLs1981 * @return array A list of lists. Each item has a list of HTML or srcs corresponding1982 * to a [video]'s HTML or primary src and specified fallbacks1983 */1984 function get_content_video( $content, $html = true ) {1985 return get_content_media( 'video', $content, $html );1986 }1987 1988 /**1989 * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order1990 * If no HTML tag is found, check the first line of the post for a URL1991 *1992 * @since 3.6.01993 *1994 * @param string $content A string which might contain video data.1995 * @return array A list of found HTML video embeds and possibly a URL by itself1996 */1997 function get_embedded_video( $content ) {1998 return get_embedded_media( 'video', $content );1999 }2000 2001 /**2002 * Retrieve images attached to the passed post2003 *2004 * @since 3.6.02005 *2006 * @param int $post_id Optional. Post ID.2007 * @return array Found image attachments2008 */2009 function get_attached_image_srcs( $post_id = 0 ) {2010 $children = get_attached_media( 'image', $post_id );2011 if ( empty( $children ) )2012 return array();2013 2014 $srcs = array();2015 foreach ( $children as $attachment )2016 $srcs[] = wp_get_attachment_url( $attachment->ID );2017 2018 return $srcs;2019 1887 } 2020 1888 … … 2026 1894 * @param string $content A string which might contain image data. 2027 1895 * @param boolean $html Whether to return HTML or URLs in the array 2028 * @param int $limit Optional. The number of image srcs to return2029 1896 * @return array The found images or srcs 2030 1897 */ 2031 function get_ content_images( $content, $html = true, $limit = 0) {1898 function get_images_in_content( $content, $html = true ) { 2032 1899 $tags = array(); 2033 1900 $captions = array(); 2034 1901 2035 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches )) {1902 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) ) { 2036 1903 foreach ( $matches as $shortcode ) { 2037 1904 if ( 'caption' === $shortcode[2] ) { … … 2040 1907 $tags[] = do_shortcode_tag( $shortcode ); 2041 1908 } 2042 2043 if ( $limit > 0 && count( $tags ) >= $limit )2044 break;2045 1909 } 2046 1910 } 2047 1911 2048 1912 foreach ( array( 'a', 'img' ) as $tag ) { 2049 if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#i', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches) ) {1913 if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#i', $content, $matches, PREG_SET_ORDER ) ) { 2050 1914 foreach ( $matches as $node ) { 2051 1915 if ( ! strstr( $node[0], '<img ' ) ) … … 2063 1927 if ( ! $found ) 2064 1928 $tags[] = $node[0]; 2065 2066 if ( $limit > 0 && count( $tags ) >= $limit )2067 break 2;2068 1929 } 2069 1930 } … … 2073 1934 return $tags; 2074 1935 2075 $ srcs = array();1936 $image_srcs = array(); 2076 1937 2077 1938 foreach ( $tags as $tag ) { 2078 1939 preg_match( '#src=([\'"])(.+?)\1#is', $tag, $src ); 2079 if ( ! empty( $src[2] ) ) { 2080 $srcs[] = $src[2]; 2081 if ( $limit > 0 && count( $srcs ) >= $limit ) 2082 break; 2083 } 2084 } 2085 2086 return apply_filters( 'content_images', array_values( array_unique( $srcs ) ), $content ); 1940 if ( ! empty( $src[2] ) ) 1941 $image_srcs[] = $src[2]; 1942 } 1943 1944 $image_srcs = array_values( array_unique( $image_srcs ) ); 1945 return apply_filters( 'get_images_in_content', $image_srcs, $content ); 2087 1946 } 2088 1947 … … 2096 1955 * @return string The found data 2097 1956 */ 2098 function get_content_image( $content, $html = true ) { 2099 $srcs = get_content_images( $content, $html, 1 ); 2100 if ( empty( $srcs ) ) 2101 return ''; 2102 2103 return apply_filters( 'content_image', reset( $srcs ), $content ); 2104 } 2105 2106 /** 2107 * Check the content blob for galleries and return their image srcs 1957 function get_image_in_content( $content, $html = true ) { 1958 $srcs = get_images_from_content( $content, $html ); 1959 return apply_filters( 'get_image_in_content', reset( $srcs ), $content ); 1960 } 1961 1962 /** 1963 * Retrieve galleries from the passed post's content 2108 1964 * 2109 1965 * @since 3.6.0 2110 1966 * 2111 * @param string $content A string which might contain image data.1967 * @param mixed $post Optional. Post ID or object. 2112 1968 * @param boolean $html Whether to return HTML or data in the array 2113 * @param int $limit Optional. The number of galleries to return 2114 * @return array A list of galleries, which in turn are a list of their srcs in order 2115 */ 2116 function get_content_galleries( $content, $html = true, $limit = 0 ) { 1969 * @return array A list of arrays, each containing gallery data and srcs parsed 1970 * from the expanded shortcode 1971 */ 1972 function get_post_galleries( $post, $html = true ) { 1973 if ( ! $post = get_post( $post ) ) 1974 return array(); 1975 1976 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 1977 return array(); 1978 2117 1979 $galleries = array(); 2118 2119 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 1980 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { 2120 1981 foreach ( $matches as $shortcode ) { 2121 1982 if ( 'gallery' === $shortcode[2] ) { … … 2123 1984 $count = 1; 2124 1985 2125 $data = shortcode_parse_atts( $shortcode[3] );2126 1986 $gallery = do_shortcode_tag( $shortcode ); 2127 1987 if ( $html ) { … … 2134 1994 } 2135 1995 1996 $data = shortcode_parse_atts( $shortcode[3] ); 2136 1997 $data['src'] = array_values( array_unique( $srcs ) ); 2137 1998 $galleries[] = $data; 2138 1999 } 2139 2140 if ( $limit > 0 && count( $galleries ) >= $limit )2141 break;2142 2000 } 2143 2001 } 2144 2002 } 2145 2003 2146 return apply_filters( ' content_galleries', $galleries, $content );2147 } 2148 2149 /** 2150 * Retrieve galleries from the passed post's content2004 return apply_filters( 'get_post_galleries', $galleries, $post ); 2005 } 2006 2007 /** 2008 * Check a specified post's content for gallery and, if present, return the first 2151 2009 * 2152 2010 * @since 3.6.0 2153 2011 * 2154 * @param int $post_id Optional. Post ID. 2155 * @param boolean $html Whether to return HTML or data in the array 2156 * @return array A list of arrays, each containing gallery data and srcs parsed 2157 * from the expanded shortcode 2158 */ 2159 function get_post_galleries( $post_id = 0, $html = true ) { 2160 if ( ! $post = get_post( $post_id ) ) 2161 return array(); 2162 2163 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2164 return array(); 2165 2166 return get_content_galleries( $post->post_content, $html ); 2012 * @param mixed $post Optional. Post ID or object. 2013 * @param boolean $html Whether to return HTML or data 2014 * @return string|array Gallery data and srcs parsed from the expanded shortcode 2015 */ 2016 function get_post_gallery( $post = 0, $html = true ) { 2017 $galleries = get_post_galleries( $post, $html ); 2018 $gallery = reset( $galleries ); 2019 2020 return apply_filters( 'get_post_gallery', $gallery, $post, $galleries ); 2167 2021 } 2168 2022 … … 2172 2026 * @since 3.6.0 2173 2027 * 2174 * @param int $post_id Optional. Post ID.2028 * @param mixed $post Optional. Post ID or object. 2175 2029 * @return array A list of lists, each containing image srcs parsed 2176 2030 * from an expanded shortcode 2177 2031 */ 2178 function get_post_galleries_images( $post_id = 0 ) { 2179 if ( ! $post = get_post( $post_id ) ) 2180 return array(); 2181 2182 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2183 return array(); 2184 2185 $data = get_content_galleries( $post->post_content, false ); 2186 return wp_list_pluck( $data, 'src' ); 2187 } 2188 2189 /** 2190 * Check a specified post's content for gallery and, if present, return the first 2032 function get_post_galleries_images( $post = 0 ) { 2033 $galleries = get_post_galleries( $post, false ); 2034 return wp_list_pluck( $galleries, 'src' ); 2035 } 2036 2037 /** 2038 * Check a post's content for galleries and return the image srcs for the first found gallery 2191 2039 * 2192 2040 * @since 3.6.0 2193 2041 * 2194 * @param int $post_id Optional. Post ID. 2195 * @param boolean $html Whether to return HTML or data 2196 * @return string|array Gallery data and srcs parsed from the expanded shortcode 2197 */ 2198 function get_post_gallery( $post_id = 0, $html = true ) { 2199 if ( ! $post = get_post( $post_id ) ) 2200 return $html ? '' : array(); 2201 2202 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2203 return $html ? '' : array(); 2204 2205 $data = get_content_galleries( $post->post_content, $html, false, 1 ); 2206 return reset( $data ); 2207 } 2208 2209 /** 2210 * Check a post's content for galleries and return the image srcs for the first found gallery 2211 * 2212 * @since 3.6.0 2213 * 2214 * @param int $post_id Optional. Post ID. 2042 * @param mixed $post Optional. Post ID or object. 2215 2043 * @return array A list of a gallery's image srcs in order 2216 2044 */ 2217 function get_post_gallery_images( $post_id = 0 ) { 2218 $gallery = get_post_gallery( $post_id, false ); 2219 if ( empty( $gallery['src'] ) ) 2220 return array(); 2221 2222 return $gallery['src']; 2223 } 2045 function get_post_gallery_images( $post = 0 ) { 2046 $galleries = get_post_gallery( $post, false ); 2047 return empty( $gallery['src'] ) ? array() : $gallery['src']; 2048 }
Note: See TracChangeset
for help on using the changeset viewer.