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