Ticket #24202: 24202.5.diff
File 24202.5.diff, 13.5 KB (added by , 11 years ago) |
---|
-
wp-content/themes/twentyeleven/functions.php
644 644 $images = array(); 645 645 646 646 if ( function_exists( 'get_post_galleries' ) ) { 647 $galleries = get_post_galleries( get_the_ID(), false ); 647 $galleries = get_post_galleries( array( 648 'post_id' => get_the_ID(), 649 'html' => false 650 ) ); 651 648 652 if ( isset( $galleries[0]['ids'] ) ) 649 653 $images = explode( ',', $galleries[0]['ids'] ); 650 654 } else { -
wp-content/themes/twentyten/functions.php
529 529 $images = array(); 530 530 531 531 if ( function_exists( 'get_post_galleries' ) ) { 532 $galleries = get_post_galleries( get_the_ID(), false ); 532 $galleries = get_post_galleries( array( 533 'post_id' => get_the_ID(), 534 'html' => false 535 ) ); 536 533 537 if ( isset( $galleries[0]['ids'] ) ) 534 538 $images = explode( ',', $galleries[0]['ids'] ); 535 539 } else { -
wp-includes/media.php
1868 1868 /** 1869 1869 * Extract and parse {media type} shortcodes or srcs from the passed content 1870 1870 * 1871 * $args contents: 1872 * - type - Type of media: audio or video. 1873 * - return - Whether to return HTML or URLs. 1874 * - limit - The number of medias to return. 1875 * 1871 1876 * @since 3.6.0 1872 1877 * 1873 * @param string $type Type of media: audio or video1874 1878 * @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 1879 * @param array $args An array of arguments. 1877 1880 * @return array A list of parsed shortcodes or extracted srcs 1878 1881 */ 1879 function get_content_media( $type, $content, $html = true, $limit = 0 ) { 1882 function get_content_media( $content, $args ) { 1883 $defaults = array( 1884 'type' => null, 1885 'html' => true, 1886 'limit' => 0 1887 ); 1888 1889 $args = wp_parse_args( $args, $defaults ); 1890 1891 if ( empty( $args['type'] ) ) 1892 return; 1893 1880 1894 $items = array(); 1881 1895 1882 1896 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 1883 1897 foreach ( $matches as $shortcode ) { 1884 if ( $ type=== $shortcode[2] ) {1898 if ( $args['type'] === $shortcode[2] ) { 1885 1899 $count = 1; 1886 1900 1887 1901 $items[] = do_shortcode_tag( $shortcode ); 1888 if ( $ limit > 0 && count( $items ) >= $limit)1902 if ( $args['limit'] > 0 && count( $items ) >= $args['limit'] ) 1889 1903 break; 1890 1904 } 1891 1905 } 1892 1906 } 1893 1907 1894 if ( $ html)1908 if ( $args['html'] ) 1895 1909 return $items; 1896 1910 1897 1911 $data = array(); … … 1914 1928 * Check the content blob for an <{media type}>, <object>, <embed>, or <iframe>, in that order 1915 1929 * If no HTML tag is found, check the first line of the post for a URL 1916 1930 * 1931 * $args contents: 1932 * - type - Type of media: audio or video. 1933 * - limit - The number of medias to return. 1934 * 1917 1935 * @since 3.6.0 1918 1936 * 1919 * @param string $type Type of media: audio or video1920 1937 * @param string $content A string which might contain media data. 1921 * @param int $limit Optional. The number of galleries to return1938 * @param array $args An array of arguments. 1922 1939 * @return array A list of found HTML media embeds and possibly a URL by itself 1923 1940 */ 1924 function get_embedded_media( $type, $content, $limit = 0 ) { 1941 function get_embedded_media( $content, $args ) { 1942 $defaults = array( 1943 'type' => null, 1944 'limit' => 0 1945 ); 1946 1947 $args = wp_parse_args( $args, $defaults ); 1948 1949 if ( empty( $args['type'] ) ) 1950 return; 1951 1925 1952 $html = array(); 1926 1953 1927 foreach ( array( $ type, 'object', 'embed', 'iframe' ) as $tag ) {1954 foreach ( array( $args['type'], 'object', 'embed', 'iframe' ) as $tag ) { 1928 1955 if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { 1929 1956 $html[] = $matches[0]; 1930 1957 1931 if ( $ limit > 0 && count( $html ) >= $limit)1958 if ( $args['limit'] > 0 && count( $html ) >= $args['limit'] ) 1932 1959 break; 1933 1960 } 1934 1961 } 1935 1962 1936 if ( ! empty( $html ) && count( $html ) >= $ limit)1963 if ( ! empty( $html ) && count( $html ) >= $args['limit'] ) 1937 1964 return $html; 1938 1965 1939 1966 $lines = explode( "\n", trim( $content ) ); … … 1949 1976 * 1950 1977 * @since 3.6.0 1951 1978 * 1979 * @uses get_content_media() 1980 * 1952 1981 * @param string $content A string which might contain audio data. 1953 * @param boolean $html Whether to return HTML or URLs1982 * @param array $args (optional) An array of arguments. 1954 1983 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 1955 1984 * to an [audio]'s HTML or primary src and specified fallbacks 1956 1985 */ 1957 function get_content_audio( $content, $ html = true) {1958 return get_content_media( 'audio', $content, $html);1986 function get_content_audio( $content, $args = array() ) { 1987 return get_content_media( $content, array_merge( $args, array( 'type' => 'audio' ) ) ); 1959 1988 } 1960 1989 1961 1990 /** … … 1964 1993 * 1965 1994 * @since 3.6.0 1966 1995 * 1996 * @uses get_embedded_media() 1997 * 1967 1998 * @param string $content A string which might contain audio data. 1999 * @param array $args (optional) An array of arguments. 1968 2000 * @return array A list of found HTML audio embeds and possibly a URL by itself 1969 2001 */ 1970 function get_embedded_audio( $content ) {1971 return get_embedded_media( 'audio', $content);2002 function get_embedded_audio( $content, $args = array() ) { 2003 return get_embedded_media( $content, array_merge( $args, array( 'type' => 'audio' ) ) ); 1972 2004 } 1973 2005 1974 2006 /** … … 1976 2008 * 1977 2009 * @since 3.6.0 1978 2010 * 2011 * @uses get_content_media() 2012 * 1979 2013 * @param string $content A string which might contain video data. 1980 * @param boolean $html Whether to return HTML or URLs2014 * @param array $args (optional) An array of arguments. 1981 2015 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 1982 2016 * to a [video]'s HTML or primary src and specified fallbacks 1983 2017 */ 1984 function get_content_video( $content, $ html = true) {1985 return get_content_media( 'video', $content, $html);2018 function get_content_video( $content, $args = array() ) { 2019 return get_content_media( $content, array_merge( $args, array( 'type' => 'video' ) ) ); 1986 2020 } 1987 2021 1988 2022 /** … … 1991 2025 * 1992 2026 * @since 3.6.0 1993 2027 * 2028 * @uses get_embedded_media() 2029 * 1994 2030 * @param string $content A string which might contain video data. 2031 * @param array $args (optional) An array of arguments. 1995 2032 * @return array A list of found HTML video embeds and possibly a URL by itself 1996 2033 */ 1997 function get_embedded_video( $content ) {1998 return get_embedded_media( 'video', $content);2034 function get_embedded_video( $content, $args = array() ) { 2035 return get_embedded_media( $content, array_merge( $args, array( 'type' => 'video' ) ) ); 1999 2036 } 2000 2037 2001 2038 /** … … 2021 2058 /** 2022 2059 * Check the content blob for images or image srcs 2023 2060 * 2061 * $args contents: 2062 * - html - Whether to return HTML or URLs. 2063 * - limit - The number of images to return. 2064 * 2024 2065 * @since 3.6.0 2025 2066 * 2026 2067 * @param string $content A string which might contain image data. 2027 * @param boolean $html Whether to return HTML or URLs in the array 2028 * @param int $limit Optional. The number of image srcs to return 2068 * @param array $args An array of arguments. 2029 2069 * @return array The found images or srcs 2030 2070 */ 2031 function get_content_images( $content, $html = true, $limit = 0 ) { 2071 function get_content_images( $content, $args ) { 2072 $defaults = array( 2073 'html' => true, 2074 'limit' => 0 2075 ); 2076 2077 $args = wp_parse_args( $args, $defaults ); 2078 2032 2079 $tags = array(); 2033 2080 $captions = array(); 2034 2081 … … 2036 2083 foreach ( $matches as $shortcode ) { 2037 2084 if ( 'caption' === $shortcode[2] ) { 2038 2085 $captions[] = $shortcode[0]; 2039 if ( $ html)2086 if ( $args['html'] ) 2040 2087 $tags[] = do_shortcode_tag( $shortcode ); 2041 2088 } 2042 2089 2043 if ( $ limit > 0 && count( $tags ) >= $limit)2090 if ( $args['limit'] > 0 && count( $tags ) >= $args['limit'] ) 2044 2091 break; 2045 2092 } 2046 2093 } … … 2063 2110 if ( ! $found ) 2064 2111 $tags[] = $node[0]; 2065 2112 2066 if ( $ limit > 0 && count( $tags ) >= $limit)2113 if ( $args['limit'] > 0 && count( $tags ) >= $args['limit'] ) 2067 2114 break 2; 2068 2115 } 2069 2116 } 2070 2117 } 2071 2118 2072 if ( $ html)2119 if ( $args['html'] ) 2073 2120 return $tags; 2074 2121 2075 2122 $srcs = array(); … … 2078 2125 preg_match( '#src=([\'"])(.+?)\1#is', $tag, $src ); 2079 2126 if ( ! empty( $src[2] ) ) { 2080 2127 $srcs[] = $src[2]; 2081 if ( $ limit > 0 && count( $srcs ) >= $limit)2128 if ( $args['limit'] > 0 && count( $srcs ) >= $args['limit'] ) 2082 2129 break; 2083 2130 } 2084 2131 } … … 2092 2139 * @since 3.6.0 2093 2140 * 2094 2141 * @param string $content A string which might contain image data. 2095 * @param boolean $html Whether to return HTML or URLs2142 * @param array $args (optional) An array of arguments. 2096 2143 * @return string The found data 2097 2144 */ 2098 function get_content_image( $content, $ html = true) {2099 $srcs = get_content_images( $content, $html, 1);2145 function get_content_image( $content, $args = array() ) { 2146 $srcs = get_content_images( $content, array_merge( $args, array( 'limit' => 1 ) ) ); 2100 2147 if ( empty( $srcs ) ) 2101 2148 return ''; 2102 2149 … … 2106 2153 /** 2107 2154 * Check the content blob for galleries and return their image srcs 2108 2155 * 2156 * $args contents: 2157 * - html - Whether to return HTML or URLs. 2158 * - limit - The number of galleries to return. 2159 * 2109 2160 * @since 3.6.0 2110 2161 * 2111 2162 * @param string $content A string which might contain image data. 2112 * @param boolean $html Whether to return HTML or data in the array 2113 * @param int $limit Optional. The number of galleries to return 2163 * @param array $args (optional) An array of arguments. 2114 2164 * @return array A list of galleries, which in turn are a list of their srcs in order 2115 2165 */ 2116 function get_content_galleries( $content, $html = true, $limit = 0 ) { 2166 function get_content_galleries( $content, $args = array() ) { 2167 $defaults = array( 2168 'html' => true, 2169 'limit' => 0 2170 ); 2171 2172 $args = wp_parse_args( $args, $defaults ); 2173 2117 2174 $galleries = array(); 2118 2175 2119 2176 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { … … 2124 2181 2125 2182 $data = shortcode_parse_atts( $shortcode[3] ); 2126 2183 $gallery = do_shortcode_tag( $shortcode ); 2127 if ( $ html) {2184 if ( $args['html'] ) { 2128 2185 $galleries[] = $gallery; 2129 2186 } else { 2130 2187 preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); … … 2137 2194 $galleries[] = $data; 2138 2195 } 2139 2196 2140 if ( $ limit > 0 && count( $galleries ) >= $limit)2197 if ( $args['limit'] > 0 && count( $galleries ) >= $args['limit'] ) 2141 2198 break; 2142 2199 } 2143 2200 } … … 2149 2206 /** 2150 2207 * Retrieve galleries from the passed post's content 2151 2208 * 2209 * $args contents: 2210 * - post_id - The post id. 2211 * - html - Whether to return HTML or data. 2212 * 2152 2213 * @since 3.6.0 2153 2214 * 2154 * @param int $post_id Optional. Post ID. 2155 * @param boolean $html Whether to return HTML or data in the array 2215 * @uses get_content_galleries() 2216 * 2217 * @param array $args (optional) An array of arguments. 2156 2218 * @return array A list of arrays, each containing gallery data and srcs parsed 2157 2219 * from the expanded shortcode 2158 2220 */ 2159 function get_post_galleries( $post_id = 0, $html = true ) { 2160 if ( ! $post = get_post( $post_id ) ) 2221 function get_post_galleries( $args = array() ) { 2222 $defaults = array( 2223 'post_id' => 0, 2224 'html' => true 2225 ); 2226 2227 $args = wp_parse_args( $args, $defaults ); 2228 2229 if ( ! $post = get_post( $args['post_id'] ) ) 2161 2230 return array(); 2162 2231 2163 2232 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2164 2233 return array(); 2165 2234 2166 return get_content_galleries( $post->post_content, $html);2235 return get_content_galleries( $post->post_content, array( 'html' => $args['html'] ) ); 2167 2236 } 2168 2237 2169 2238 /** … … 2171 2240 * 2172 2241 * @since 3.6.0 2173 2242 * 2243 * @uses get_content_galleries() 2244 * 2174 2245 * @param int $post_id Optional. Post ID. 2175 2246 * @return array A list of lists, each containing image srcs parsed 2176 2247 * from an expanded shortcode … … 2182 2253 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2183 2254 return array(); 2184 2255 2185 $data = get_content_galleries( $post->post_content, false);2256 $data = get_content_galleries( $post->post_content, array( 'html' => false ) ); 2186 2257 return wp_list_pluck( $data, 'src' ); 2187 2258 } 2188 2259 2189 2260 /** 2190 2261 * Check a specified post's content for gallery and, if present, return the first 2191 2262 * 2263 * $args contents: 2264 * - post_id - The post id. 2265 * - html - Whether to return HTML or URLs. 2266 * 2192 2267 * @since 3.6.0 2193 2268 * 2194 * @param int $post_id Optional. Post ID. 2195 * @param boolean $html Whether to return HTML or data 2269 * @uses get_content_galleries() 2270 * 2271 * @param array $args (optional) An array of arguments. 2196 2272 * @return string|array Gallery data and srcs parsed from the expanded shortcode 2197 2273 */ 2198 function get_post_gallery( $post_id = 0, $html = true ) { 2199 if ( ! $post = get_post( $post_id ) ) 2274 function get_post_gallery( $args = array() ) { 2275 $defaults = array( 2276 'post_id' => 0, 2277 'html' => true 2278 ); 2279 2280 $args = wp_parse_args( $args, $defaults ); 2281 2282 if ( ! $post = get_post( $args['post_id'] ) ) 2200 2283 return $html ? '' : array(); 2201 2284 2202 2285 if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 2203 2286 return $html ? '' : array(); 2204 2287 2205 $data = get_content_galleries( $post->post_content, $html, false, 1 ); 2288 $data = get_content_galleries( $post->post_content, array( 2289 'html' => $args['html'], 2290 'limit' => 1 2291 ) ); 2292 2206 2293 return reset( $data ); 2207 2294 } 2208 2295 … … 2215 2302 * @return array A list of a gallery's image srcs in order 2216 2303 */ 2217 2304 function get_post_gallery_images( $post_id = 0 ) { 2218 $gallery = get_post_gallery( $post_id, false ); 2305 $gallery = get_post_gallery( array( 2306 'post_id' => $post_id, 2307 'html' => false 2308 ) ); 2309 2219 2310 if ( empty( $gallery['src'] ) ) 2220 2311 return array(); 2221 2312