Ticket #23572: 23572-html.2.diff
| File 23572-html.2.diff, 11.6 KB (added by , 14 years ago) |
|---|
-
wp-includes/media.php
diff --git wp-includes/media.php wp-includes/media.php index 60677ba..60068bd 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 );1850 $content =& str_replace( $shortcode[0], '', $content, $count ); 1852 1851 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]; 1858 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 ( has_post_format( $type ) ) { 2036 $meta = get_post_format_meta( $post->ID ); 2037 if ( ! empty( $meta['media'] ) ) { 2038 if ( is_numeric( $meta['media'] ) ) { 2039 $url = wp_get_attachment_url( $meta['media'] ); 2040 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2041 } elseif ( preg_match( '/' . get_shortcode_regex() . '/s', $meta['media'] ) ) { 2042 $shortcode = $meta['media']; 2043 } elseif ( preg_match( '#<[^>]+>#', $meta['media'] ) ) { 2044 return $meta['media']; 2045 } elseif ( 0 === strpos( $meta['media'], 'http' ) ) { 2046 $shortcode = sprintf( '[%s src="%s"]', $type, $meta['media'] ); 2047 } 2048 2049 if ( ! empty( $shortcode ) ) 2050 return do_shortcode( $shortcode ); 2051 } 2052 } 2053 2054 $medias = call_user_func( 'get_attached_' . $type ); 2055 if ( ! empty( $medias ) ) { 2056 $media = reset( $medias ); 2057 $url = wp_get_attachment_url( $media->ID ); 2058 $shortcode = sprintf( '[%s src="%s"]', $type, $url ); 2059 return do_shortcode( $shortcode ); 2060 } 2061 2062 // these functions expected a reference, not a value 2063 $content =& $post->post_content; 2064 2065 $htmls = get_content_media( $type, $content ); 2066 if ( ! empty( $htmls ) ) { 2067 $html = reset( $htmls ); 2068 return $html; 2069 } 2070 2071 $embeds = get_embedded_media( $type, $content ); 2072 if ( ! empty( $embeds ) ) { 2073 $embed = reset( $embeds ); 2074 if ( 0 === strpos( $embed, 'http' ) ) { 2075 $shortcode = sprintf( '[%s src="%s"]', $type, $embed ); 2076 return do_shortcode( $shortcode ); 2077 } else { 2078 return $embed; 2079 } 2080 } 2081 2082 return ''; 2083 } 2084 2085 /** 2086 * Output the first video in the current (@global) post's content 2087 * 2088 * @since 3.6.0 2089 * 2090 */ 2091 function the_video() { 2092 echo get_the_media( 'video' ); 2093 } 2094 /** 2095 * Output the first audio in the current (@global) post's content 2096 * 2097 * @since 3.6.0 2098 * 2099 */ 2100 function the_audio() { 2101 echo get_the_media( 'audio' ); 2102 } 2103 2104 /** 2004 2105 * Retrieve images attached to the passed post 2005 2106 * 2006 2107 * @since 3.6.0 … … function get_attached_image_srcs( $post_id = 0 ) { 2033 2134 } 2034 2135 2035 2136 /** 2036 * Check the content blob for image srcs2137 * Check the content blob for images or image srcs 2037 2138 * 2038 2139 * @since 3.6.0 2039 2140 * 2040 2141 * @param string $content A string which might contain image data. 2142 * @param boolean $html Whether to return HTML or URLs 2041 2143 * @param boolean $remove Whether to remove the found data from the passed content. 2042 2144 * @param int $limit Optional. The number of image srcs to return 2043 * @return array The found image srcs2145 * @return array The found images or srcs 2044 2146 */ 2045 function get_content_images( &$content, $remove = false, $limit = 0 ) { 2046 $src = ''; 2047 $srcs = array(); 2147 function get_content_images( &$content, $html = true, $remove = false, $limit = 0 ) { 2048 2148 $matches = array(); 2149 $tags = array(); 2150 $captions = array(); 2049 2151 2050 2152 if ( $remove && preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) { 2051 $captions = array();2052 2153 foreach ( $matches as $shortcode ) { 2053 2154 if ( 'caption' === $shortcode[2] ) 2054 2155 $captions[] = $shortcode[0]; … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2068 2169 $content = str_replace( $tag[0], '', $content, $count ); 2069 2170 } 2070 2171 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 } 2172 $tags[] = $tag[0]; 2173 2174 if ( $limit > 0 && count( $tags ) >= $limit ) 2175 break; 2176 } 2177 } 2178 2179 if ( $html ) 2180 return $tags; 2181 2182 $src = ''; 2183 $srcs = array(); 2184 2185 foreach ( $tags as $tag ) { 2186 preg_match( '#src=[\'"](.+?)[\'"]#is', $tag, $src ); 2187 if ( ! empty( $src[1] ) ) { 2188 $srcs[] = $src[1]; 2189 if ( $limit > 0 && count( $srcs ) >= $limit ) 2190 break; 2077 2191 } 2078 2192 } 2079 2193 … … function get_content_images( &$content, $remove = false, $limit = 0 ) { 2081 2195 } 2082 2196 2083 2197 /** 2084 * Check the content blob for image srcs and return the first2198 * Check the content blob for images or srcs and return the first 2085 2199 * 2086 2200 * @since 3.6.0 2087 2201 * 2088 2202 * @param string $content A string which might contain image data. 2203 * @param boolean $html Whether to return HTML or URLs 2089 2204 * @param boolean $remove Whether to remove the found data from the passed content. 2090 2205 * @return string The found data 2091 2206 */ 2092 function get_content_image( &$content, $ remove = false ) {2093 $srcs = get_content_images( $content, $ remove, 1 );2207 function get_content_image( &$content, $html = true, $remove = false ) { 2208 $srcs = get_content_images( $content, $html, $remove, 1 ); 2094 2209 if ( empty( $srcs ) ) 2095 2210 return ''; 2096 2211 … … function get_post_gallery_images( $post_id = 0 ) { 2205 2320 return array(); 2206 2321 2207 2322 return $gallery['src']; 2323 } 2324 2325 /** 2326 * Output the first image in the current (@global) post's content 2327 * 2328 * @since 3.6.0 2329 * 2330 * @param string $attached_size If an attached image is found, the size to display it. 2331 */ 2332 function the_image( $attached_size = 'full' ) { 2333 $post = get_post(); 2334 if ( empty( $post ) ) 2335 return ''; 2336 2337 $medias = get_attached_images(); 2338 if ( ! empty( $medias ) ) { 2339 $media = reset( $medias ); 2340 echo wp_get_attachment_image( $media->ID, $attached_size ); 2341 return; 2342 } 2343 2344 $content =& $post->post_content; 2345 $htmls = get_content_images( $content ); 2346 if ( ! empty( $htmls ) ) { 2347 $html = reset( $htmls ); 2348 echo $html; 2349 } 2208 2350 } 2351 No newline at end of file