Ticket #23572: 23572-html.diff
File 23572-html.diff, 13.1 KB (added by , 11 years ago) |
---|
-
wp-includes/media.php
diff --git wp-includes/media.php wp-includes/media.php index 60677ba..8360502 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 $count = 1; 2036 2037 if ( has_post_format( $type ) ) { 2038 $meta = get_post_format_meta( $post->ID ); 2039 if ( ! empty( $meta['media'] ) ) { 2040 if ( is_numeric( $meta['media'] ) ) { 2041 $url = wp_get_attachment_url( $meta['media'] ); 2042 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2043 } elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['media'] ) ) { 2044 $shortcode = $meta['media']; 2045 } elseif ( preg_match( '#<[^>]+>#', $meta['media'] ) ) { 2046 return $meta['media']; 2047 } elseif ( 0 === strpos( $meta['media'], 'http' ) ) { 2048 $post->post_content = str_replace( $meta['media'], '', $post->post_content, $count ); 2049 setup_postdata( $post ); 2050 $shortcode = sprintf( '[%s src="%s"]', $type, $meta['media'] ); 2051 } 2052 2053 if ( ! empty( $shortcode ) ) 2054 return do_shortcode( $shortcode ); 2055 } 2056 } 2057 2058 $medias = call_user_func( 'get_attached_' . $type ); 2059 if ( ! empty( $medias ) ) { 2060 $media = reset( $medias ); 2061 $url = wp_get_attachment_url( $media->ID ); 2062 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2063 return do_shortcode( $shortcode ); 2064 } 2065 2066 // these functions expected a reference, not a value 2067 $content =& $post->post_content; 2068 2069 $htmls = get_content_media( $type, $content, true, true ); 2070 if ( ! empty( $htmls ) ) { 2071 $html = reset( $htmls ); 2072 $post->post_content = $content; 2073 setup_postdata( $post ); 2074 return $html; 2075 } 2076 2077 $embeds = get_embedded_media( $type, $content, true, 1 ); 2078 if ( ! empty( $embeds ) ) { 2079 $embed = reset( $embeds ); 2080 $post->post_content = $content; 2081 setup_postdata( $post ); 2082 if ( 0 === strpos( $embed, 'http' ) ) { 2083 $shortcode = sprintf( '[%s src="%s"]', $type, $embed ); 2084 return do_shortcode( $shortcode ); 2085 } else { 2086 return $embed; 2087 } 2088 } 2089 2090 return ''; 2091 } 2092 2093 /** 2094 * Output the first video in the current (@global) post's content 2095 * 2096 * @since 3.6.0 2097 * 2098 */ 2099 function the_video() { 2100 echo get_the_media( 'video' ); 2101 } 2102 /** 2103 * Output the first audio in the current (@global) post's content 2104 * 2105 * @since 3.6.0 2106 * 2107 */ 2108 function the_audio() { 2109 echo get_the_media( 'audio' ); 2110 } 2111 2112 /** 2004 2113 * Retrieve images attached to the passed post 2005 2114 * 2006 2115 * @since 3.6.0 … … function get_attached_image_srcs( $post_id = 0 ) { 2033 2142 } 2034 2143 2035 2144 /** 2036 * Check the content blob for image srcs2145 * Check the content blob for images or image srcs 2037 2146 * 2038 2147 * @since 3.6.0 2039 2148 * 2040 2149 * @param string $content A string which might contain image data. 2150 * @param boolean $html Whether to return HTML or URLs 2041 2151 * @param boolean $remove Whether to remove the found data from the passed content. 2042 2152 * @param int $limit Optional. The number of image srcs to return 2043 * @return array The found image srcs2153 * @return array The found images or srcs 2044 2154 */ 2045 function get_content_images( &$content, $remove = false, $limit = 0 ) { 2046 $src = ''; 2047 $srcs = array(); 2155 function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) { 2048 2156 $matches = array(); 2157 $tags = array(); 2158 $captions = array(); 2049 2159 2050 2160 if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2051 $captions = array();2052 2161 foreach ( $matches as $shortcode ) { 2053 2162 if ( 'caption' === $shortcode[2] ) 2054 2163 $captions[] = $shortcode[0]; … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2068 2177 $content = str_replace( $tag[0], '', $content, $count ); 2069 2178 } 2070 2179 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 } 2180 $tags[] = $tag[0]; 2181 2182 if ( $limit > 0 && count( $tags ) >= $limit ) 2183 break; 2184 } 2185 } 2186 2187 if ( $html ) 2188 return $tags; 2189 2190 $src = ''; 2191 $srcs = array(); 2192 2193 foreach ( $tags as $tag ) { 2194 preg_match( '#src=[\'"](.+?)[\'"]#is', $tag, $src ); 2195 if ( ! empty( $src[1] ) ) { 2196 $srcs[] = $src[1]; 2197 if ( $limit > 0 && count( $srcs ) >= $limit ) 2198 break; 2077 2199 } 2078 2200 } 2079 2201 … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2081 2203 } 2082 2204 2083 2205 /** 2084 * Check the content blob for image srcs and return the first2206 * Check the content blob for images or srcs and return the first 2085 2207 * 2086 2208 * @since 3.6.0 2087 2209 * 2088 2210 * @param string $content A string which might contain image data. 2211 * @param boolean $html Whether to return HTML or URLs 2089 2212 * @param boolean $remove Whether to remove the found data from the passed content. 2090 2213 * @return string The found data 2091 2214 */ 2092 function get_content_image( &$content, $ remove = false ) {2093 $srcs = get_content_images( $content, $ remove, 1 );2215 function get_content_image( &$content, $html = true, $remove = false ) { 2216 $srcs = get_content_images( $content, $html, $remove, 1 ); 2094 2217 if ( empty( $srcs ) ) 2095 2218 return ''; 2096 2219 … … function get_post_gallery_images( $post_id = 0 ) { 2205 2328 return array(); 2206 2329 2207 2330 return $gallery['src']; 2331 } 2332 2333 /** 2334 * Output the first image in the current (@global) post's content 2335 * 2336 * @since 3.6.0 2337 * 2338 * @param string $attached_size If an attached image is found, the size to display it. 2339 */ 2340 function the_image( $attached_size = 'full' ) { 2341 $post =& get_post(); 2342 if ( empty( $post ) ) 2343 return ''; 2344 2345 $medias = get_attached_images(); 2346 if ( ! empty( $medias ) ) { 2347 $media = reset( $medias ); 2348 $sizes = get_intermediate_image_sizes(); 2349 2350 $urls = array(); 2351 foreach ( $sizes as $size ) { 2352 $urls[] = reset( wp_get_attachment_image_src( $media->ID, $size ) ); 2353 $urls[] = get_attachment_link( $media->ID ); 2354 } 2355 2356 $count = 1; 2357 $matches = array(); 2358 $content =& $post->post_content; 2359 2360 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2361 foreach ( $matches as $shortcode ) { 2362 if ( 'caption' === $shortcode[2] ) { 2363 foreach ( $urls as $url ) { 2364 if ( strstr( $shortcode[0], $url ) ) 2365 $content = str_replace( $shortcode[0], '', $content, $count ); 2366 } 2367 } 2368 } 2369 } 2370 2371 foreach ( array( 'a', 'img' ) as $tag ) { 2372 if ( preg_match_all( '#' . get_tag_regex( $tag ) . '#', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2373 foreach ( $matches as $match ) { 2374 foreach ( $urls as $url ) { 2375 if ( strstr( $match[0], $url ) ) 2376 $content = str_replace( $match[0], '', $content, $count ); 2377 } 2378 } 2379 } 2380 } 2381 2382 $post->post_content = $content; 2383 setup_postdata( $post ); 2384 2385 echo wp_get_attachment_image( $media->ID, $attached_size ); 2386 return; 2387 } 2388 2389 $content =& $post->post_content; 2390 $htmls = get_content_images( $content, true, true, 1 ); 2391 if ( ! empty( $htmls ) ) { 2392 $html = reset( $htmls ); 2393 $post->post_content = $content; 2394 setup_postdata( $post ); 2395 echo $html; 2396 } 2208 2397 } 2398 No newline at end of file