Ticket #23572: 23572-html.3.diff
File 23572-html.3.diff, 13.5 KB (added by , 11 years ago) |
---|
-
wp-includes/media.php
diff --git wp-includes/media.php wp-includes/media.php index 60677ba..d5f755a 100644
function wp_audio_shortcode( $attr ) { 887 887 } 888 888 889 889 $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); 890 if ( 'mediaelement' === $library ) {890 if ( 'mediaelement' === $library && did_action( 'init' ) ) { 891 891 wp_enqueue_style( 'wp-mediaelement' ); 892 892 wp_enqueue_script( 'wp-mediaelement' ); 893 893 } … … function wp_video_shortcode( $attr ) { 994 994 } 995 995 996 996 $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); 997 if ( 'mediaelement' === $library ) {997 if ( 'mediaelement' === $library && did_action( 'init' ) ) { 998 998 wp_enqueue_style( 'wp-mediaelement' ); 999 999 wp_enqueue_script( 'wp-mediaelement' ); 1000 1000 } … … function get_attached_video( $post_id = 0 ) { 1828 1828 } 1829 1829 1830 1830 /** 1831 * Extract the srcs from the post's [{media type}] <source>s1831 * Extract and parse {media type} shortcodes or srcs from the passed content 1832 1832 * 1833 1833 * @since 3.6.0 1834 1834 * 1835 * @param string $type Type of media: audio or video 1835 1836 * @param string $content A string which might contain media data. 1837 * @param boolean $html Whether to return HTML or URLs 1836 1838 * @param boolean $remove Whether to remove the found URL from the passed content. 1837 * @return array A list of lists. Each item has a list of sources corresponding 1838 * to a [{media type}]'s primary src and specified fallbacks 1839 * @return array A list of parsed shortcodes or extracted srcs 1839 1840 */ 1840 function get_content_media( $type, &$content, $remove = false ) { 1841 $src = ''; 1841 function get_content_media( $type, &$content, $html = true, $remove = false ) { 1842 1842 $items = array(); 1843 1843 $matches = array(); 1844 1844 1845 1845 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 1846 1846 foreach ( $matches as $shortcode ) { 1847 1847 if ( $type === $shortcode[2] ) { 1848 $srcs = array();1849 1848 $count = 1; 1850 1849 if ( $remove ) 1851 $content = str_replace( $shortcode[0], '', $content, $count ); 1852 1853 $item = do_shortcode_tag( $shortcode ); 1854 preg_match_all( '#src=[\'"](.+?)[\'"]#is', $item, $src, PREG_SET_ORDER ); 1855 if ( ! empty( $src ) ) { 1856 foreach ( $src as $s ) 1857 $srcs[] = $s[1]; 1850 $content =& str_replace( $shortcode[0], '', $content, $count ); 1858 1851 1859 $items[] = array_values( array_unique( $srcs ) ); 1860 } 1852 $items[] = do_shortcode_tag( $shortcode ); 1861 1853 } 1862 1854 } 1863 1855 } 1864 return $items; 1856 1857 if ( $html ) 1858 return $items; 1859 1860 $src = ''; 1861 $data = array(); 1862 1863 foreach ( $items as $item ) { 1864 preg_match_all( '#src=[\'"](.+?)[\'"]#is', $item, $src, PREG_SET_ORDER ); 1865 if ( ! empty( $src ) ) { 1866 $srcs = array(); 1867 foreach ( $src as $s ) 1868 $srcs[] = $s[1]; 1869 1870 $data[] = array_values( array_unique( $srcs ) ); 1871 } 1872 } 1873 1874 return $data; 1865 1875 } 1866 1876 1867 1877 /** … … function get_content_media( $type, &$content, $remove = false ) { 1870 1880 * 1871 1881 * @since 3.6.0 1872 1882 * 1883 * @param string $type Type of media: audio or video 1873 1884 * @param string $content A string which might contain media data. 1874 1885 * @param boolean $remove Whether to remove the found URL from the passed content. 1886 * @param int $limit Optional. The number of galleries to return 1875 1887 * @return array A list of found HTML media embeds and possibly a URL by itself 1876 1888 */ 1877 function get_embedded_media( $type, &$content, $remove = false ) {1889 function get_embedded_media( $type, &$content, $remove = false, $limit = 0 ) { 1878 1890 $html = array(); 1879 1891 $matches = ''; 1880 1892 … … function get_embedded_media( $type, &$content, $remove = false ) { 1884 1896 if ( $remove ) 1885 1897 $content = str_replace( $matches[0], '', $content ); 1886 1898 1887 return $html; 1899 if ( $limit > 0 && count( $html ) >= $limit ) 1900 break; 1888 1901 } 1889 1902 } 1890 1903 1904 if ( ! empty( $html ) && count( $html ) >= $limit ) 1905 return $html; 1906 1891 1907 $lines = explode( "\n", trim( $content ) ); 1892 1908 $line = trim( array_shift( $lines ) ); 1893 1909 … … function get_embedded_media( $type, &$content, $remove = false ) { 1901 1917 } 1902 1918 1903 1919 /** 1904 * Extract the srcs from the post's [audio] <source>s1920 * Extract the HTML or <source> srcs from the content's [audio] 1905 1921 * 1906 1922 * @since 3.6.0 1907 1923 * 1908 1924 * @param string $content A string which might contain audio data. 1925 * @param boolean $html Whether to return HTML or URLs 1909 1926 * @param boolean $remove Whether to remove the found URL from the passed content. 1910 * @return array A list of lists. Each item has a list of sources corresponding1911 * to a [audio]'sprimary src and specified fallbacks1927 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 1928 * to an [audio]'s HTML or primary src and specified fallbacks 1912 1929 */ 1913 function get_content_audio( &$content, $ remove = false ) {1914 return get_content_media( 'audio', $content, $ remove );1930 function get_content_audio( &$content, $html = true, $remove = false ) { 1931 return get_content_media( 'audio', $content, $html, $remove ); 1915 1932 } 1916 1933 1917 1934 /** … … function get_embedded_audio( &$content, $remove = false ) { 1929 1946 } 1930 1947 1931 1948 /** 1932 * Extract the srcs from the post's [video] <source>s1949 * Extract the HTML or <source> srcs from the content's [video] 1933 1950 * 1934 1951 * @since 3.6.0 1935 1952 * 1936 1953 * @param string $content A string which might contain video data. 1954 * @param boolean $html Whether to return HTML or URLs 1937 1955 * @param boolean $remove Whether to remove the found URL from the passed content. 1938 * @return array A list of lists. Each item has a list of sources corresponding1939 * to a [video]'s primary src and specified fallbacks1956 * @return array A list of lists. Each item has a list of HTML or srcs corresponding 1957 * to a [video]'s HTML or primary src and specified fallbacks 1940 1958 */ 1941 function get_content_video( &$content, $ remove = false ) {1942 return get_content_media( 'video', $content, $ remove );1959 function get_content_video( &$content, $html = true, $remove = false ) { 1960 return get_content_media( 'video', $content, $html, $remove ); 1943 1961 } 1944 1962 1945 1963 /** … … function wp_video_embed( $matches, $attr, $url, $rawattr ) { 2001 2019 wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')#i', apply_filters( 'wp_video_embed_handler', 'wp_video_embed' ), 9999 ); 2002 2020 2003 2021 /** 2022 * Return suitable HTML code for output based on the content related to the global $post 2023 * If found, remove the content from the @global $post's post_content field 2024 * 2025 * @since 3.6.0 2026 * 2027 * @param string $type Required. 'audio' or 'video' 2028 * @return string 2029 */ 2030 function get_the_media( $type ) { 2031 $post = get_post(); 2032 if ( empty( $post ) ) 2033 return ''; 2034 2035 if ( ! empty( $post->format_content ) ) 2036 return $post->format_content; 2037 2038 $count = 1; 2039 2040 if ( has_post_format( $type ) ) { 2041 $meta = get_post_format_meta( $post->ID ); 2042 if ( ! empty( $meta['media'] ) ) { 2043 if ( is_numeric( $meta['media'] ) ) { 2044 $url = wp_get_attachment_url( $meta['media'] ); 2045 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2046 } elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['media'] ) ) { 2047 $shortcode = $meta['media']; 2048 } elseif ( preg_match( '#<[^>]+>#', $meta['media'] ) ) { 2049 $post->format_content = $meta['media']; 2050 return $post->format_content; 2051 } elseif ( 0 === strpos( $meta['media'], 'http' ) ) { 2052 $post->split_content = str_replace( $meta['media'], '', $post->post_content, $count ); 2053 $shortcode = sprintf( '[%s src="%s"]', $type, $meta['media'] ); 2054 } 2055 2056 if ( ! empty( $shortcode ) ) { 2057 $post->format_content = do_shortcode( $shortcode ); 2058 return $post->format_content; 2059 } 2060 } 2061 } 2062 2063 $medias = call_user_func( 'get_attached_' . $type ); 2064 if ( ! empty( $medias ) ) { 2065 $media = reset( $medias ); 2066 $url = wp_get_attachment_url( $media->ID ); 2067 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2068 $post->format_content = do_shortcode( $shortcode ); 2069 return $post->format_content; 2070 } 2071 2072 // these functions expected a reference, not a value 2073 $content =& $post->post_content; 2074 2075 $htmls = get_content_media( $type, $content, true, true ); 2076 if ( ! empty( $htmls ) ) { 2077 $html = reset( $htmls ); 2078 $post->split_content = $content; 2079 $post->format_content = $html; 2080 return $post->format_content; 2081 } 2082 2083 $embeds = get_embedded_media( $type, $content, true, 1 ); 2084 if ( ! empty( $embeds ) ) { 2085 $embed = reset( $embeds ); 2086 $post->split_content = $content; 2087 if ( 0 === strpos( $embed, 'http' ) ) { 2088 $shortcode = sprintf( '[%s src="%s"]', $type, $embed ); 2089 $post->format_content = do_shortcode( $shortcode ); 2090 return $post->format_content; 2091 } else { 2092 $post->format_content = $embed; 2093 return $post->format_content; 2094 } 2095 } 2096 2097 return ''; 2098 } 2099 2100 /** 2101 * Output the first video in the current (@global) post's content 2102 * 2103 * @since 3.6.0 2104 * 2105 */ 2106 function the_video() { 2107 echo get_the_media( 'video' ); 2108 } 2109 /** 2110 * Output the first audio in the current (@global) post's content 2111 * 2112 * @since 3.6.0 2113 * 2114 */ 2115 function the_audio() { 2116 echo get_the_media( 'audio' ); 2117 } 2118 2119 /** 2004 2120 * Retrieve images attached to the passed post 2005 2121 * 2006 2122 * @since 3.6.0 … … function get_attached_image_srcs( $post_id = 0 ) { 2033 2149 } 2034 2150 2035 2151 /** 2036 * Check the content blob for image srcs2152 * Check the content blob for images or image srcs 2037 2153 * 2038 2154 * @since 3.6.0 2039 2155 * 2040 2156 * @param string $content A string which might contain image data. 2157 * @param boolean $html Whether to return HTML or URLs 2041 2158 * @param boolean $remove Whether to remove the found data from the passed content. 2042 2159 * @param int $limit Optional. The number of image srcs to return 2043 * @return array The found image srcs2160 * @return array The found images or srcs 2044 2161 */ 2045 function get_content_images( &$content, $remove = false, $limit = 0 ) { 2046 $src = ''; 2047 $srcs = array(); 2162 function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) { 2048 2163 $matches = array(); 2164 $tags = array(); 2165 $captions = array(); 2049 2166 2050 2167 if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2051 $captions = array();2052 2168 foreach ( $matches as $shortcode ) { 2053 2169 if ( 'caption' === $shortcode[2] ) 2054 2170 $captions[] = $shortcode[0]; … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2068 2184 $content = str_replace( $tag[0], '', $content, $count ); 2069 2185 } 2070 2186 2071 preg_match( '#src=[\'"](.+?)[\'"]#is', $tag[0], $src ); 2072 if ( ! empty( $src[1] ) ) { 2073 $srcs[] = $src[1]; 2074 if ( $limit > 0 && count( $srcs ) >= $limit ) 2075 break; 2076 } 2187 $tags[] = $tag[0]; 2188 2189 if ( $limit > 0 && count( $tags ) >= $limit ) 2190 break; 2191 } 2192 } 2193 2194 if ( $html ) 2195 return $tags; 2196 2197 $src = ''; 2198 $srcs = array(); 2199 2200 foreach ( $tags as $tag ) { 2201 preg_match( '#src=[\'"](.+?)[\'"]#is', $tag, $src ); 2202 if ( ! empty( $src[1] ) ) { 2203 $srcs[] = $src[1]; 2204 if ( $limit > 0 && count( $srcs ) >= $limit ) 2205 break; 2077 2206 } 2078 2207 } 2079 2208 … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2081 2210 } 2082 2211 2083 2212 /** 2084 * Check the content blob for image srcs and return the first2213 * Check the content blob for images or srcs and return the first 2085 2214 * 2086 2215 * @since 3.6.0 2087 2216 * 2088 2217 * @param string $content A string which might contain image data. 2218 * @param boolean $html Whether to return HTML or URLs 2089 2219 * @param boolean $remove Whether to remove the found data from the passed content. 2090 2220 * @return string The found data 2091 2221 */ 2092 function get_content_image( &$content, $ remove = false ) {2093 $srcs = get_content_images( $content, $ remove, 1 );2222 function get_content_image( &$content, $html = true, $remove = false ) { 2223 $srcs = get_content_images( $content, $html, $remove, 1 ); 2094 2224 if ( empty( $srcs ) ) 2095 2225 return ''; 2096 2226 … … function get_post_gallery_images( $post_id = 0 ) { 2205 2335 return array(); 2206 2336 2207 2337 return $gallery['src']; 2338 } 2339 2340 /** 2341 * Output the first image in the current (@global) post's content 2342 * 2343 * @since 3.6.0 2344 * 2345 * @param string $attached_size If an attached image is found, the size to display it. 2346 */ 2347 function the_image( $attached_size = 'full' ) { 2348 $post = get_post(); 2349 if ( empty( $post ) ) 2350 return ''; 2351 2352 if ( ! empty( $post->format_content ) ) { 2353 echo $post->format_content; 2354 return; 2355 } 2356 2357 $medias = get_attached_images(); 2358 if ( ! empty( $medias ) ) { 2359 $media = reset( $medias ); 2360 $sizes = get_intermediate_image_sizes(); 2361 2362 $urls = array(); 2363 foreach ( $sizes as $size ) { 2364 $urls[] = reset( wp_get_attachment_image_src( $media->ID, $size ) ); 2365 $urls[] = get_attachment_link( $media->ID ); 2366 } 2367 2368 $count = 1; 2369 $matches = array(); 2370 $content =& $post->post_content; 2371 2372 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2373 foreach ( $matches as $shortcode ) { 2374 if ( 'caption' === $shortcode[2] ) { 2375 foreach ( $urls as $url ) { 2376 if ( strstr( $shortcode[0], $url ) ) 2377 $content = str_replace( $shortcode[0], '', $content, $count ); 2378 } 2379 } 2380 } 2381 } 2382 2383 foreach ( array( 'a', 'img' ) as $tag ) { 2384 if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2385 foreach ( $matches as $match ) { 2386 foreach ( $urls as $url ) { 2387 if ( strstr( $match[0], $url ) ) 2388 $content = str_replace( $match[0], '', $content, $count ); 2389 } 2390 } 2391 } 2392 } 2393 2394 $post->split_content = $content; 2395 $post->format_content = wp_get_attachment_image( $media->ID, $attached_size ); 2396 echo $post->format_content; 2397 return; 2398 } 2399 2400 $content =& $post->post_content; 2401 $htmls = get_content_images( $content, true, true, 1 ); 2402 if ( ! empty( $htmls ) ) { 2403 $html = reset( $htmls ); 2404 $post->split_content = $content; 2405 $post->format_content = $html; 2406 echo $post->format_content; 2407 } 2208 2408 } 2409 No newline at end of file