| 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 | |
| 819 | static $instances = 0; |
| 820 | $instances++; |
| 821 | |
| 822 | $audio = null; |
| 823 | |
| 824 | extract( shortcode_atts( array( 'src' => '' ), $attr ) ); |
| 825 | |
| 826 | if ( empty( $src ) ) { |
| 827 | $children = get_children( array( |
| 828 | 'post_parent' => $post->ID, |
| 829 | 'post_type' => 'attachment', |
| 830 | 'post_mime_type' => 'audio', |
| 831 | 'posts_per_page' => 1 |
| 832 | ) ); |
| 833 | |
| 834 | if ( empty( $children ) ) |
| 835 | return; |
| 836 | |
| 837 | $audio = reset( $children ); |
| 838 | $src = wp_get_attachment_url( $audio->ID ); |
| 839 | if ( empty( $src ) ) |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | wp_enqueue_style( 'wp-mediaelement' ); |
| 844 | wp_enqueue_script( 'wp-mediaelement' ); |
| 845 | |
| 846 | $html = sprintf( '<audio class="%s" id="%s" src="%s" type="audio/mp3" controls="controls"></audio>', |
| 847 | apply_filters( 'audio_shortcode_class', 'wp-audio-shortcode' ), |
| 848 | sprintf( 'audio-%d-%d', $post->ID, $instances ), |
| 849 | esc_url( $src ) |
| 850 | ); |
| 851 | |
| 852 | return apply_filters( 'audio_shortcode', $html, $src, $audio, $post ); |
| 853 | } |
| 854 | add_shortcode( 'audio', 'wp_audio_shortcode' ); |
| 855 | |
| 856 | /** |
| 857 | * The Video shortcode. |
| 858 | * |
| 859 | * This implements the functionality of the Video Shortcode for displaying |
| 860 | * WordPress mp4s in a post. |
| 861 | * |
| 862 | * @since 3.6.0 |
| 863 | * |
| 864 | * @param array $attr Attributes of the shortcode. |
| 865 | * @return string HTML content to display video. |
| 866 | */ |
| 867 | function wp_video_shortcode( $attr ) { |
| 868 | global $content_width; |
| 869 | $post = get_post(); |
| 870 | |
| 871 | static $instances = 0; |
| 872 | $instances++; |
| 873 | |
| 874 | $video = null; |
| 875 | |
| 876 | extract( shortcode_atts( array( |
| 877 | 'src' => '', |
| 878 | 'width' => empty( $content_width ) ? 640 : $content_width, |
| 879 | 'height' => 360, |
| 880 | 'poster' => '', |
| 881 | 'webm' => '', |
| 882 | 'ogv' => '', |
| 883 | 'flash' => true |
| 884 | ), $attr ) ); |
| 885 | |
| 886 | if ( empty( $src ) ) { |
| 887 | $children = get_children( array( |
| 888 | 'post_parent' => $post->ID, |
| 889 | 'post_type' => 'attachment', |
| 890 | 'post_mime_type' => 'video', |
| 891 | 'posts_per_page' => 1 |
| 892 | ) ); |
| 893 | |
| 894 | if ( empty( $children ) ) |
| 895 | return; |
| 896 | |
| 897 | $video = reset( $children ); |
| 898 | $src = wp_get_attachment_url( $video->ID ); |
| 899 | if ( empty( $src ) ) |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | wp_enqueue_style( 'wp-mediaelement' ); |
| 904 | wp_enqueue_script( 'wp-mediaelement' ); |
| 905 | |
| 906 | $atts = array( |
| 907 | sprintf( 'class="%s"', apply_filters( 'video_shortcode_class', 'wp-video-shortcode' ) ), |
| 908 | sprintf( 'id="video-%d-%d"', $post->ID, $instances ), |
| 909 | sprintf( 'width="%d"', $width ), |
| 910 | sprintf( 'height="%d"', $height ), |
| 911 | ); |
| 912 | |
| 913 | if ( ! empty( $poster ) ) |
| 914 | $atts[] = sprintf( 'poster="%s"', esc_url( $poster ) ); |
| 915 | |
| 916 | if ( empty( $webm ) && empty( $ogv ) ) { |
| 917 | $atts[] = sprintf( 'src="%s"', esc_url( $src ) ); |
| 918 | $atts[] = 'type="video/mp4"'; |
| 919 | |
| 920 | $html = sprintf( '<video %s controls="controls" preload="none"></video>', |
| 921 | join( ' ', $atts ) |
| 922 | ); |
| 923 | } else { |
| 924 | $source = '<source type="%s" src="%s" />'; |
| 925 | |
| 926 | $html = sprintf( '<video %s controls="controls" preload="none">', join( ' ', $atts ) ); |
| 927 | $html .= sprintf( $source, 'video/mp4', $src ); |
| 928 | if ( ! empty( $webm ) ) |
| 929 | $html .= sprintf( $source, 'video/webm', $webm ); |
| 930 | if ( ! empty( $ogv ) ) |
| 931 | $html .= sprintf( $source, 'video/ogg', $ogv ); |
| 932 | |
| 933 | if ( $flash && 'false' !== $flash ) { |
| 934 | $flashsrc = '/wp-includes/js/mediaelement/flashmediaelement.swf'; |
| 935 | |
| 936 | $html .= sprintf( '<object width="%d" height="%d" type="application/x-shockwave-flash" data="%s">', |
| 937 | $width, |
| 938 | $height, |
| 939 | $flashsrc |
| 940 | ); |
| 941 | |
| 942 | $html .= sprintf( '<param name="movie" value="%s"/>', esc_url( $flashsrc ) ); |
| 943 | $html .= sprintf( '<param name="flashvars" value="controls=true&file=%s"/>', esc_url( $src ) ); |
| 944 | if ( ! empty( $poster ) ) |
| 945 | $html .= sprintf( '<img src="%s" width="%d" height="%d" />', |
| 946 | esc_url( $poster ), |
| 947 | $width, |
| 948 | $height |
| 949 | ); |
| 950 | $html .= '</object>'; |
| 951 | } |
| 952 | $html .= '</video>'; |
| 953 | } |
| 954 | |
| 955 | return apply_filters( 'video_shortcode', $html, $src, $video, $post ); |
| 956 | } |
| 957 | add_shortcode( 'video', 'wp_video_shortcode' ); |
| 958 | |
| 959 | /** |