| | 806 | * Return a filtered list of WP-supported audio formats |
| | 807 | * |
| | 808 | * @since 3.6.0 |
| | 809 | * @return array |
| | 810 | */ |
| | 811 | function wp_get_audio_extensions() { |
| | 812 | return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ) ); |
| | 813 | } |
| | 814 | |
| | 815 | /** |
| | 816 | * The Audio shortcode. |
| | 817 | * |
| | 818 | * This implements the functionality of the Audio Shortcode for displaying |
| | 819 | * WordPress mp3s in a post. |
| | 820 | * |
| | 821 | * @since 3.6.0 |
| | 822 | * |
| | 823 | * @param array $attr Attributes of the shortcode. |
| | 824 | * @return string HTML content to display audio. |
| | 825 | */ |
| | 826 | function wp_audio_shortcode( $attr ) { |
| | 827 | $post_id = get_post() ? get_the_ID() : 0; |
| | 828 | |
| | 829 | static $instances = 0; |
| | 830 | $instances++; |
| | 831 | |
| | 832 | $audio = null; |
| | 833 | |
| | 834 | $default_types = wp_get_audio_extensions(); |
| | 835 | $defaults_atts = array( 'src' => '' ); |
| | 836 | foreach ( $default_types as $type ) |
| | 837 | $defaults_atts[$type] = ''; |
| | 838 | |
| | 839 | extract( shortcode_atts( $defaults_atts, $attr ) ); |
| | 840 | |
| | 841 | $primary = false; |
| | 842 | if ( ! empty( $src ) ) { |
| | 843 | $type = wp_check_filetype( $src ); |
| | 844 | if ( ! in_array( $type['ext'], $default_types ) ) { |
| | 845 | printf( '<a class="wp-post-format-link-audio" href="%1$s">%1$s</a>', $src ); |
| | 846 | return; |
| | 847 | } |
| | 848 | $primary = true; |
| | 849 | array_unshift( $default_types, 'src' ); |
| | 850 | } else { |
| | 851 | foreach ( $default_types as $ext ) { |
| | 852 | if ( ! empty( $$ext ) ) { |
| | 853 | $type = wp_check_filetype( $$ext ); |
| | 854 | if ( $type['ext'] === $ext ) |
| | 855 | $primary = true; |
| | 856 | } |
| | 857 | } |
| | 858 | } |
| | 859 | |
| | 860 | if ( ! $primary ) { |
| | 861 | $audios = get_post_audio( $post_id ); |
| | 862 | if ( empty( $audios ) ) |
| | 863 | return; |
| | 864 | |
| | 865 | $audio = reset( $audios ); |
| | 866 | $src = wp_get_attachment_url( $audio->ID ); |
| | 867 | if ( empty( $src ) ) |
| | 868 | return; |
| | 869 | |
| | 870 | array_unshift( $default_types, 'src' ); |
| | 871 | } |
| | 872 | |
| | 873 | wp_enqueue_style( 'wp-mediaelement' ); |
| | 874 | wp_enqueue_script( 'wp-mediaelement' ); |
| | 875 | |
| | 876 | $atts = array( |
| | 877 | sprintf( 'class="%s"', apply_filters( 'wp_audio_shortcode_class', 'wp-audio-shortcode' ) ), |
| | 878 | sprintf( 'id="audio-%d-%d"', $post_id, $instances ), |
| | 879 | ); |
| | 880 | |
| | 881 | $html = sprintf( '<audio %s controls="controls" preload="none">', join( ' ', $atts ) ); |
| | 882 | |
| | 883 | $source = '<source type="%s" src="%s" />'; |
| | 884 | foreach ( $default_types as $fallback ) { |
| | 885 | if ( ! empty( $$fallback ) ) { |
| | 886 | $type = wp_check_filetype( $$fallback ); |
| | 887 | $html .= sprintf( $source, $type['type'], $$fallback ); |
| | 888 | } |
| | 889 | } |
| | 890 | |
| | 891 | $html .= '</audio>'; |
| | 892 | |
| | 893 | return apply_filters( 'wp_audio_shortcode', $html, $src, $audio, $post_id ); |
| | 894 | } |
| | 895 | add_shortcode( 'audio', 'wp_audio_shortcode' ); |
| | 896 | |
| | 897 | /** |
| | 898 | * Return a filtered list of WP-supported video formats |
| | 899 | * |
| | 900 | * @since 3.6.0 |
| | 901 | * @return array |
| | 902 | */ |
| | 903 | function wp_get_video_extensions() { |
| | 904 | return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv' ) ); |
| | 905 | } |
| | 906 | |
| | 907 | /** |
| | 908 | * The Video shortcode. |
| | 909 | * |
| | 910 | * This implements the functionality of the Video Shortcode for displaying |
| | 911 | * WordPress mp4s in a post. |
| | 912 | * |
| | 913 | * @since 3.6.0 |
| | 914 | * |
| | 915 | * @param array $attr Attributes of the shortcode. |
| | 916 | * @return string HTML content to display video. |
| | 917 | */ |
| | 918 | function wp_video_shortcode( $attr ) { |
| | 919 | global $content_width; |
| | 920 | $post_id = get_post() ? get_the_ID() : 0; |
| | 921 | |
| | 922 | static $instances = 0; |
| | 923 | $instances++; |
| | 924 | |
| | 925 | $video = null; |
| | 926 | |
| | 927 | $default_types = wp_get_video_extensions(); |
| | 928 | $defaults_atts = array( |
| | 929 | 'src' => '', |
| | 930 | 'poster' => '', |
| | 931 | 'height' => 360, |
| | 932 | 'width' => empty( $content_width ) ? 640 : $content_width, |
| | 933 | ); |
| | 934 | foreach ( $default_types as $type ) |
| | 935 | $defaults_atts[$type] = ''; |
| | 936 | |
| | 937 | extract( shortcode_atts( $defaults_atts, $attr ) ); |
| | 938 | |
| | 939 | $primary = false; |
| | 940 | if ( ! empty( $src ) ) { |
| | 941 | $type = wp_check_filetype( $src ); |
| | 942 | if ( ! in_array( $type['ext'], $default_types ) ) { |
| | 943 | printf( '<a class="wp-post-format-link-video" href="%1$s">%1$s</a>', $src ); |
| | 944 | return; |
| | 945 | } |
| | 946 | $primary = true; |
| | 947 | array_unshift( $default_types, 'src' ); |
| | 948 | } else { |
| | 949 | foreach ( $default_types as $ext ) { |
| | 950 | if ( ! empty( $$ext ) ) { |
| | 951 | $type = wp_check_filetype( $$ext ); |
| | 952 | if ( $type['ext'] === $ext ) |
| | 953 | $primary = true; |
| | 954 | } |
| | 955 | } |
| | 956 | } |
| | 957 | |
| | 958 | if ( ! $primary ) { |
| | 959 | $videos = get_post_video( $post_id ); |
| | 960 | if ( empty( $videos ) ) |
| | 961 | return; |
| | 962 | |
| | 963 | $video = reset( $videos ); |
| | 964 | $src = wp_get_attachment_url( $video->ID ); |
| | 965 | if ( empty( $src ) ) |
| | 966 | return; |
| | 967 | |
| | 968 | array_unshift( $default_types, 'src' ); |
| | 969 | } |
| | 970 | |
| | 971 | wp_enqueue_style( 'wp-mediaelement' ); |
| | 972 | wp_enqueue_script( 'wp-mediaelement' ); |
| | 973 | |
| | 974 | $atts = array( |
| | 975 | sprintf( 'class="%s"', apply_filters( 'video_shortcode_class', 'wp-video-shortcode' ) ), |
| | 976 | sprintf( 'id="video-%d-%d"', $post_id, $instances ), |
| | 977 | sprintf( 'width="%d"', $width ), |
| | 978 | sprintf( 'height="%d"', $height ), |
| | 979 | ); |
| | 980 | |
| | 981 | if ( ! empty( $poster ) ) |
| | 982 | $atts[] = sprintf( 'poster="%s"', esc_url( $poster ) ); |
| | 983 | |
| | 984 | $html = sprintf( '<video %s controls="controls" preload="none">', join( ' ', $atts ) ); |
| | 985 | |
| | 986 | $source = '<source type="%s" src="%s" />'; |
| | 987 | foreach ( $default_types as $fallback ) { |
| | 988 | if ( ! empty( $$fallback ) ) { |
| | 989 | $type = wp_check_filetype( $$fallback ); |
| | 990 | // m4v sometimes shows up as video/mpeg which collides with mp4 |
| | 991 | if ( 'm4v' === $type['ext'] ) |
| | 992 | $type['type'] = 'video/m4v'; |
| | 993 | $html .= sprintf( $source, $type['type'], $$fallback ); |
| | 994 | } |
| | 995 | } |
| | 996 | |
| | 997 | $html .= '</video>'; |
| | 998 | |
| | 999 | return apply_filters( 'wp_video_shortcode', $html, $src, $video, $post_id ); |
| | 1000 | } |
| | 1001 | add_shortcode( 'video', 'wp_video_shortcode' ); |
| | 1002 | |
| | 1003 | /** |
| | 1743 | |
| | 1744 | /** |
| | 1745 | * Retrieve audio attached to the passed post |
| | 1746 | * |
| | 1747 | * @since 3.6.0 |
| | 1748 | * |
| | 1749 | * @param int $post_id Post ID |
| | 1750 | * @return array Found audio attachments |
| | 1751 | */ |
| | 1752 | function get_post_audio( $post_id = 0 ) { |
| | 1753 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| | 1754 | if ( empty( $post ) ) |
| | 1755 | return; |
| | 1756 | |
| | 1757 | $children = get_children( array( |
| | 1758 | 'post_parent' => $post->ID, |
| | 1759 | 'post_type' => 'attachment', |
| | 1760 | 'post_mime_type' => 'audio', |
| | 1761 | 'posts_per_page' => -1 |
| | 1762 | ) ); |
| | 1763 | |
| | 1764 | if ( ! empty( $children ) ) |
| | 1765 | return $children; |
| | 1766 | } |
| | 1767 | |
| | 1768 | /** |
| | 1769 | * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order |
| | 1770 | * If no HTML tag is found, check the first line of the post for a URL |
| | 1771 | * |
| | 1772 | * @param string $content A string which might contain audio data. |
| | 1773 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| | 1774 | * @return string The found data |
| | 1775 | */ |
| | 1776 | function get_content_audio( &$content, $remove = false ) { |
| | 1777 | $html = $matches = ''; |
| | 1778 | foreach ( array( 'audio', 'object', 'embed', 'iframe' ) as $tag ) { |
| | 1779 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#i', $content, $matches ) ) { |
| | 1780 | $html = $matches[1]; |
| | 1781 | $count = 1; |
| | 1782 | if ( $remove ) |
| | 1783 | $content = str_replace( $matches[0], '', $content, $count ); |
| | 1784 | |
| | 1785 | return $html; |
| | 1786 | } |
| | 1787 | } |
| | 1788 | |
| | 1789 | $lines = explode( "\n", trim( $content ) ); |
| | 1790 | $line = trim( array_shift( $lines ) ); |
| | 1791 | |
| | 1792 | if ( 0 === stripos( $line, 'http' ) ) { |
| | 1793 | if ( $remove ) |
| | 1794 | $content = join( "\n", $lines ); |
| | 1795 | |
| | 1796 | return $line; |
| | 1797 | } |
| | 1798 | } |
| | 1799 | |
| | 1800 | /** |
| | 1801 | * Return the found audio data for the passed post |
| | 1802 | * |
| | 1803 | * @since 3.6.0 |
| | 1804 | * |
| | 1805 | * @param int $id Optional. Post ID |
| | 1806 | */ |
| | 1807 | function get_the_audio( $id = 0 ) { |
| | 1808 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 1809 | if ( empty( $post ) ) |
| | 1810 | return array(); |
| | 1811 | |
| | 1812 | $data = get_content_audio( $post->post_content ); |
| | 1813 | if ( ! empty( $data ) ) |
| | 1814 | return $data; |
| | 1815 | |
| | 1816 | $audios = get_post_audio( $post->ID ); |
| | 1817 | if ( empty( $audios ) ) |
| | 1818 | return array(); |
| | 1819 | |
| | 1820 | $audio = reset( $audios ); |
| | 1821 | return wp_get_attachment_url( $audio->ID ); |
| | 1822 | } |
| | 1823 | |
| | 1824 | /** |
| | 1825 | * Output the found audio data for the current post |
| | 1826 | * |
| | 1827 | * @since 3.6.0 |
| | 1828 | */ |
| | 1829 | function the_audio() { |
| | 1830 | echo apply_filters( 'the_audio', get_the_audio() ); |
| | 1831 | } |
| | 1832 | |
| | 1833 | /** |
| | 1834 | * Retrieve video attached to the passed post |
| | 1835 | * |
| | 1836 | * @since 3.6.0 |
| | 1837 | * |
| | 1838 | * @param int $post_id Post ID |
| | 1839 | * @return array Found video attachments |
| | 1840 | */ |
| | 1841 | function get_post_video( $post_id = 0 ) { |
| | 1842 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| | 1843 | if ( empty( $post ) ) |
| | 1844 | return; |
| | 1845 | |
| | 1846 | $children = get_children( array( |
| | 1847 | 'post_parent' => $post->ID, |
| | 1848 | 'post_type' => 'attachment', |
| | 1849 | 'post_mime_type' => 'video', |
| | 1850 | 'posts_per_page' => -1 |
| | 1851 | ) ); |
| | 1852 | |
| | 1853 | if ( ! empty( $children ) ) |
| | 1854 | return $children; |
| | 1855 | } |
| | 1856 | |
| | 1857 | /** |
| | 1858 | * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order |
| | 1859 | * If no HTML tag is found, check the first line of the post for a URL |
| | 1860 | * |
| | 1861 | * @param string $content A string which might contain video data. |
| | 1862 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| | 1863 | * @return string The found data |
| | 1864 | */ |
| | 1865 | function get_content_video( &$content, $remove = false ) { |
| | 1866 | $html = $matches = ''; |
| | 1867 | foreach ( array( 'video', 'object', 'embed', 'iframe' ) as $tag ) { |
| | 1868 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#i', $content, $matches ) ) { |
| | 1869 | $html = $matches[1]; |
| | 1870 | $count = 1; |
| | 1871 | if ( $remove ) |
| | 1872 | $content = str_replace( $matches[0], '', $content, $count ); |
| | 1873 | |
| | 1874 | return $html; |
| | 1875 | } |
| | 1876 | } |
| | 1877 | |
| | 1878 | $lines = explode( "\n", trim( $content ) ); |
| | 1879 | $line = trim( array_shift( $lines ) ); |
| | 1880 | |
| | 1881 | if ( 0 === stripos( $line, 'http' ) ) { |
| | 1882 | if ( $remove ) |
| | 1883 | $content = join( "\n", $lines ); |
| | 1884 | |
| | 1885 | return $line; |
| | 1886 | } |
| | 1887 | } |
| | 1888 | |
| | 1889 | /** |
| | 1890 | * Return the found video data for the passed post |
| | 1891 | * |
| | 1892 | * @since 3.6.0 |
| | 1893 | * |
| | 1894 | * @param int $id Optional. Post ID |
| | 1895 | * @return string A video URL or embed code, if present. |
| | 1896 | */ |
| | 1897 | function get_the_video( $id = 0 ) { |
| | 1898 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| | 1899 | if ( empty( $post ) ) |
| | 1900 | return ''; |
| | 1901 | |
| | 1902 | $data = get_content_video( $post->post_content ); |
| | 1903 | if ( ! empty( $data ) ) |
| | 1904 | return $data; |
| | 1905 | |
| | 1906 | $videos = get_post_video( $post->ID ); |
| | 1907 | if ( empty( $videos ) ) |
| | 1908 | return ''; |
| | 1909 | |
| | 1910 | $video = reset( $videos ); |
| | 1911 | return wp_get_attachment_url( $video->ID ); |
| | 1912 | } |
| | 1913 | |
| | 1914 | /** |
| | 1915 | * Output the found video data for the current post |
| | 1916 | * |
| | 1917 | * @since 3.6.0 |
| | 1918 | */ |
| | 1919 | function the_video() { |
| | 1920 | echo apply_filters( 'the_video', get_the_video() ); |
| | 1921 | } |
| | 1922 | |
| | 1923 | /** |
| | 1924 | * Audio embed handler callback. |
| | 1925 | * |
| | 1926 | * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. |
| | 1927 | * @param array $attr Embed attributes. |
| | 1928 | * @param string $url The original URL that was matched by the regex. |
| | 1929 | * @param array $rawattr The original unmodified attributes. |
| | 1930 | * @return string The embed HTML. |
| | 1931 | */ |
| | 1932 | function wp_audio_embed( $matches, $attr, $url, $rawattr ) { |
| | 1933 | $audio = do_shortcode( '[audio src="' . $url . '" /]' ); |
| | 1934 | return apply_filters( 'wp_audio_embed', $audio, $attr, $url, $rawattr ); |
| | 1935 | } |
| | 1936 | wp_embed_register_handler( 'wp_audio_embed', '#https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')#i', 'wp_audio_embed', 9999 ); |
| | 1937 | |
| | 1938 | /** |
| | 1939 | * Video embed handler callback. |
| | 1940 | * |
| | 1941 | * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. |
| | 1942 | * @param array $attr Embed attributes. |
| | 1943 | * @param string $url The original URL that was matched by the regex. |
| | 1944 | * @param array $rawattr The original unmodified attributes. |
| | 1945 | * @return string The embed HTML. |
| | 1946 | */ |
| | 1947 | function wp_video_embed( $matches, $attr, $url, $rawattr ) { |
| | 1948 | $dimensions = ''; |
| | 1949 | if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) { |
| | 1950 | $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] ); |
| | 1951 | $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] ); |
| | 1952 | } |
| | 1953 | $video = do_shortcode( '[video ' . $dimensions . 'src="' . $url . '" /]' ); |
| | 1954 | return apply_filters( 'wp_video_embed', $video, $attr, $url, $rawattr ); |
| | 1955 | } |
| | 1956 | wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')#i', 'wp_video_embed', 9999 ); |
| | 1957 | No newline at end of file |