| 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' ) ); |
| 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', '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 | $html .= sprintf( $source, $type['type'], $$fallback ); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | $html .= '</video>'; |
| 995 | |
| 996 | return apply_filters( 'wp_video_shortcode', $html, $src, $video, $post_id ); |
| 997 | } |
| 998 | add_shortcode( 'video', 'wp_video_shortcode' ); |
| 999 | |
| 1000 | /** |
| 1740 | |
| 1741 | /** |
| 1742 | * Retrieve audio attached to the passed post |
| 1743 | * |
| 1744 | * @since 3.6.0 |
| 1745 | * |
| 1746 | * @param int $post_id Post ID |
| 1747 | * @return array Found audio attachments |
| 1748 | */ |
| 1749 | function get_post_audio( $post_id = 0 ) { |
| 1750 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| 1751 | if ( empty( $post ) ) |
| 1752 | return; |
| 1753 | |
| 1754 | $children = get_children( array( |
| 1755 | 'post_parent' => $post->ID, |
| 1756 | 'post_type' => 'attachment', |
| 1757 | 'post_mime_type' => 'audio', |
| 1758 | 'posts_per_page' => -1 |
| 1759 | ) ); |
| 1760 | |
| 1761 | if ( ! empty( $children ) ) |
| 1762 | return $children; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * Check the content blob for an <audio>, <object>, <embed>, or <iframe>, in that order |
| 1767 | * If no HTML tag is found, check the first line of the post for a URL |
| 1768 | * |
| 1769 | * @param string $content A string which might contain audio data. |
| 1770 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| 1771 | * @return string The found data |
| 1772 | */ |
| 1773 | function get_content_audio( &$content, $remove = false ) { |
| 1774 | $html = $matches = ''; |
| 1775 | foreach ( array( 'audio', 'object', 'embed', 'iframe' ) as $tag ) { |
| 1776 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#i', $content, $matches ) ) { |
| 1777 | $html = $matches[1]; |
| 1778 | $count = 1; |
| 1779 | if ( $remove ) |
| 1780 | $content = str_replace( $matches[0], '', $content, $count ); |
| 1781 | |
| 1782 | return $html; |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | $lines = explode( "\n", trim( $content ) ); |
| 1787 | $line = trim( array_shift( $lines ) ); |
| 1788 | |
| 1789 | if ( 0 === stripos( $line, 'http' ) ) { |
| 1790 | if ( $remove ) |
| 1791 | $content = join( "\n", $lines ); |
| 1792 | |
| 1793 | return $line; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * Return the found audio data for the passed post |
| 1799 | * |
| 1800 | * @since 3.6.0 |
| 1801 | * |
| 1802 | * @param int $id Optional. Post ID |
| 1803 | */ |
| 1804 | function get_the_audio( $id = 0 ) { |
| 1805 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 1806 | if ( empty( $post ) ) |
| 1807 | return array(); |
| 1808 | |
| 1809 | $data = get_content_audio( $post->post_content ); |
| 1810 | if ( ! empty( $data ) ) |
| 1811 | return $data; |
| 1812 | |
| 1813 | $audios = get_post_audio( $post->ID ); |
| 1814 | if ( empty( $audios ) ) |
| 1815 | return array(); |
| 1816 | |
| 1817 | $audio = reset( $audios ); |
| 1818 | return wp_get_attachment_url( $audio->ID ); |
| 1819 | } |
| 1820 | |
| 1821 | /** |
| 1822 | * Output the found audio data for the current post |
| 1823 | * |
| 1824 | * @since 3.6.0 |
| 1825 | */ |
| 1826 | function the_audio() { |
| 1827 | echo apply_filters( 'the_audio', get_the_audio() ); |
| 1828 | } |
| 1829 | |
| 1830 | /** |
| 1831 | * Retrieve video attached to the passed post |
| 1832 | * |
| 1833 | * @since 3.6.0 |
| 1834 | * |
| 1835 | * @param int $post_id Post ID |
| 1836 | * @return array Found video attachments |
| 1837 | */ |
| 1838 | function get_post_video( $post_id = 0 ) { |
| 1839 | $post = empty( $post_id ) ? get_post() : get_post( $post_id ); |
| 1840 | if ( empty( $post ) ) |
| 1841 | return; |
| 1842 | |
| 1843 | $children = get_children( array( |
| 1844 | 'post_parent' => $post->ID, |
| 1845 | 'post_type' => 'attachment', |
| 1846 | 'post_mime_type' => 'video', |
| 1847 | 'posts_per_page' => -1 |
| 1848 | ) ); |
| 1849 | |
| 1850 | if ( ! empty( $children ) ) |
| 1851 | return $children; |
| 1852 | } |
| 1853 | |
| 1854 | /** |
| 1855 | * Check the content blob for a <video>, <object>, <embed>, or <iframe>, in that order |
| 1856 | * If no HTML tag is found, check the first line of the post for a URL |
| 1857 | * |
| 1858 | * @param string $content A string which might contain video data. |
| 1859 | * @param boolean $remove Whether to remove the found URL from the passed content. |
| 1860 | * @return string The found data |
| 1861 | */ |
| 1862 | function get_content_video( &$content, $remove = false ) { |
| 1863 | $html = $matches = ''; |
| 1864 | foreach ( array( 'video', 'object', 'embed', 'iframe' ) as $tag ) { |
| 1865 | if ( preg_match( '#' . get_tag_regex( $tag ) . '#i', $content, $matches ) ) { |
| 1866 | $html = $matches[1]; |
| 1867 | $count = 1; |
| 1868 | if ( $remove ) |
| 1869 | $content = str_replace( $matches[0], '', $content, $count ); |
| 1870 | |
| 1871 | return $html; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | $lines = explode( "\n", trim( $content ) ); |
| 1876 | $line = trim( array_shift( $lines ) ); |
| 1877 | |
| 1878 | if ( 0 === stripos( $line, 'http' ) ) { |
| 1879 | if ( $remove ) |
| 1880 | $content = join( "\n", $lines ); |
| 1881 | |
| 1882 | return $line; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * Return the found video data for the passed post |
| 1888 | * |
| 1889 | * @since 3.6.0 |
| 1890 | * |
| 1891 | * @param int $id Optional. Post ID |
| 1892 | */ |
| 1893 | function get_the_video( $id = 0 ) { |
| 1894 | $post = empty( $id ) ? get_post() : get_post( $id ); |
| 1895 | if ( empty( $post ) ) |
| 1896 | return array(); |
| 1897 | |
| 1898 | $data = get_content_video( $post->post_content ); |
| 1899 | if ( ! empty( $data ) ) |
| 1900 | return $data; |
| 1901 | |
| 1902 | $videos = get_post_video( $post->ID ); |
| 1903 | if ( empty( $videos ) ) |
| 1904 | return array(); |
| 1905 | |
| 1906 | $video = reset( $videos ); |
| 1907 | return wp_get_attachment_url( $video->ID ); |
| 1908 | } |
| 1909 | |
| 1910 | /** |
| 1911 | * Output the found video data for the current post |
| 1912 | * |
| 1913 | * @since 3.6.0 |
| 1914 | */ |
| 1915 | function the_video() { |
| 1916 | echo apply_filters( 'the_video', get_the_video() ); |
| 1917 | } |
| 1918 | |
| 1919 | /** |
| 1920 | * Audio embed handler callback. |
| 1921 | * |
| 1922 | * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. |
| 1923 | * @param array $attr Embed attributes. |
| 1924 | * @param string $url The original URL that was matched by the regex. |
| 1925 | * @param array $rawattr The original unmodified attributes. |
| 1926 | * @return string The embed HTML. |
| 1927 | */ |
| 1928 | function wp_audio_embed( $matches, $attr, $url, $rawattr ) { |
| 1929 | $audio = do_shortcode( '[audio src="' . $url . '" /]' ); |
| 1930 | return apply_filters( 'wp_audio_embed', $audio, $attr, $url, $rawattr ); |
| 1931 | } |
| 1932 | wp_embed_register_handler( 'wp_audio_embed', '#https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')#i', 'wp_audio_embed', 9999 ); |
| 1933 | |
| 1934 | /** |
| 1935 | * Video embed handler callback. |
| 1936 | * |
| 1937 | * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. |
| 1938 | * @param array $attr Embed attributes. |
| 1939 | * @param string $url The original URL that was matched by the regex. |
| 1940 | * @param array $rawattr The original unmodified attributes. |
| 1941 | * @return string The embed HTML. |
| 1942 | */ |
| 1943 | function wp_video_embed( $matches, $attr, $url, $rawattr ) { |
| 1944 | $dimensions = ''; |
| 1945 | if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) { |
| 1946 | $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] ); |
| 1947 | $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] ); |
| 1948 | } |
| 1949 | $video = do_shortcode( '[video ' . $dimensions . ' src="' . $url . '" /]' ); |
| 1950 | return apply_filters( 'wp_video_embed', $video, $attr, $url, $rawattr ); |
| 1951 | } |
| 1952 | wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')#i', 'wp_video_embed', 9999 ); |
| 1953 | No newline at end of file |