Make WordPress Core

Changeset 23774


Ignore:
Timestamp:
03/22/2013 05:55:08 AM (11 years ago)
Author:
markjaquith
Message:

Add functions to parse audio or video data out of arbitrary content or a post

props wonderboymusic. see #23572

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r23772 r23774  
    18231823
    18241824/**
     1825 * Extract the srcs from the post's [{media type}] <source>s
     1826 *
     1827 * @since 3.6.0
     1828 *
     1829 * @param string $content A string which might contain media data.
     1830 * @param boolean $remove Whether to remove the found URL from the passed content.
     1831 * @return array A list of lists. Each item has a list of sources corresponding
     1832 *      to a [{media type}]'s primary src and specified fallbacks
     1833 */
     1834function get_content_media( $type, &$content, $remove = false ) {
     1835    $src = '';
     1836    $items = array();
     1837    $matches = array();
     1838
     1839    if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) && ! empty( $matches ) ) {
     1840        foreach ( $matches as $shortcode ) {
     1841            if ( $type === $shortcode[2] ) {
     1842                $srcs = array();
     1843                $count = 1;
     1844                if ( $remove )
     1845                    $content = str_replace( $shortcode[0], '', $content, $count );
     1846
     1847                $item = do_shortcode_tag( $shortcode );
     1848                preg_match_all( '#src=[\'"](.+?)[\'"]#is', $item, $src, PREG_SET_ORDER );
     1849                if ( ! empty( $src ) ) {
     1850                    foreach ( $src as $s )
     1851                        $srcs[] = $s[1];
     1852
     1853                    $items[] = array_values( array_unique( $srcs ) );
     1854                }
     1855            }
     1856        }
     1857    }
     1858    return $items;
     1859}
     1860
     1861/**
     1862 * Check the content blob for an <{media type}>, <object>, <embed>, or <iframe>, in that order
     1863 * If no HTML tag is found, check the first line of the post for a URL
     1864 *
     1865 * @since 3.6.0
     1866 *
     1867 * @param string $content A string which might contain media data.
     1868 * @param boolean $remove Whether to remove the found URL from the passed content.
     1869 * @return array A list of found HTML media embeds and possibly a URL by itself
     1870 */
     1871function get_embedded_media( $type, &$content, $remove = false ) {
     1872    $html = array();
     1873    $matches = '';
     1874
     1875    foreach ( array( $type, 'object', 'embed', 'iframe' ) as $tag ) {
     1876        if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
     1877            $html[] = $matches[1];
     1878            if ( $remove )
     1879                $content = str_replace( $matches[0], '', $content );
     1880
     1881            return $html;
     1882        }
     1883    }
     1884
     1885    $lines = explode( "\n", trim( $content ) );
     1886    $line = trim( array_shift( $lines  ) );
     1887
     1888    if ( 0 === stripos( $line, 'http' ) ) {
     1889        if ( $remove )
     1890            $content = join( "\n", $lines );
     1891
     1892        $html[] = $line;
     1893    }
     1894    return $html;
     1895}
     1896
     1897/**
     1898 * Extract the srcs from the post's [audio] <source>s
     1899 *
     1900 * @since 3.6.0
     1901 *
     1902 * @param string $content A string which might contain audio data.
     1903 * @param boolean $remove Whether to remove the found URL from the passed content.
     1904 * @return array A list of lists. Each item has a list of sources corresponding
     1905 *      to a [audio]'s primary src and specified fallbacks
     1906 */
     1907function get_content_audio( &$content, $remove = false ) {
     1908    return get_content_media( 'audio', $content, $remove );
     1909}
     1910
     1911/**
     1912 * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order
     1913 * If no HTML tag is found, check the first line of the post for a URL
     1914 *
     1915 * @since 3.6.0
     1916 *
     1917 * @param string $content A string which might contain audio data.
     1918 * @param boolean $remove Whether to remove the found URL from the passed content.
     1919 * @return array A list of found HTML audio embeds and possibly a URL by itself
     1920 */
     1921function get_embedded_audio( &$content, $remove = false ) {
     1922    return get_embedded_media( 'audio', $content, $remove );
     1923}
     1924
     1925/**
     1926 * Extract the srcs from the post's [video] <source>s
     1927 *
     1928 * @since 3.6.0
     1929 *
     1930 * @param string $content A string which might contain video data.
     1931 * @param boolean $remove Whether to remove the found URL from the passed content.
     1932 * @return array A list of lists. Each item has a list of sources corresponding
     1933 *      to a [video]'s primary src and specified fallbacks
     1934 */
     1935function get_content_video( &$content, $remove = false ) {
     1936    return get_content_media( 'video', $content, $remove );
     1937}
     1938
     1939/**
     1940 * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order
     1941 * If no HTML tag is found, check the first line of the post for a URL
     1942 *
     1943 * @since 3.6.0
     1944 *
     1945 * @param string $content A string which might contain video data.
     1946 * @param boolean $remove Whether to remove the found URL from the passed content.
     1947 * @return array A list of found HTML video embeds and possibly a URL by itself
     1948 */
     1949function get_embedded_video( &$content, $remove = false ) {
     1950    return get_embedded_media( 'video', $content, $remove );
     1951}
     1952
     1953/**
    18251954 * Audio embed handler callback.
    18261955 *
Note: See TracChangeset for help on using the changeset viewer.