From c6ddc2f12fa98399a9d947782bb2ddd8f7e87d49 Mon Sep 17 00:00:00 2001
From: Fabien Quatravaux <fabien.quatravaux@1nterval.com>
Date: Tue, 24 Jun 2014 10:56:45 +0200
Subject: [PATCH] [dev #28619] Add filters to tune mediaelement library

---
 wp-includes/media.php | 92 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 82 insertions(+), 10 deletions(-)

diff --git a/wp-includes/media.php b/wp-includes/media.php
index 974aa79..7e61b30 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -1571,9 +1571,19 @@ function wp_audio_shortcode( $attr, $content = '' ) {
 		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
 	}
 
-	$html = '';
-	if ( 'mediaelement' === $library && 1 === $instances )
-		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
+	/**
+	 * Adds HTML before the audio shortcode output.
+	 *
+	 * Give the possibility to insert some HTML code before the <audio> tag 
+	 * generated by an audio shortcode.
+	 *
+	 * @since 4.0.0
+	 *
+	 * @param string $html      The HTML snippet to insert before the <audio> tag.
+	 * @param string $library   Media library used for the audio shortcode.
+	 * @param int    $instances Unique numeric ID of this audio shortcode instance.
+	 */
+	$html = apply_filters('wp_audio_shortcode_pre_html', '', $library, $instances);
 	$html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
 
 	$fileurl = '';
@@ -1588,8 +1598,20 @@ function wp_audio_shortcode( $attr, $content = '' ) {
 		}
 	}
 
-	if ( 'mediaelement' === $library )
-		$html .= wp_mediaelement_fallback( $fileurl );
+	/**
+	 * Adds HTML inside the audio shortcode output.
+	 *
+	 * Give the possibility to insert some HTML code inside the <audio> tag 
+	 * generated by an audio shortcode.
+	 *
+	 * @since 4.0.0
+	 *
+	 * @param string $html    The HTML snippet to append into the <audio> tag.
+	 * @param string $library Media library used for the audio shortcode.
+	 * @param string $fileurl The URL of the audio file.
+	 * @param int    $post_id Post ID.
+	 */
+	$html .= apply_filters('wp_audio_shortcode_inside_html', '', $library, $fileurl, $post_id);
 	$html .= '</audio>';
 
 	/**
@@ -1607,6 +1629,20 @@ function wp_audio_shortcode( $attr, $content = '' ) {
 }
 add_shortcode( 'audio', 'wp_audio_shortcode' );
 
+function wp_mediaelement_audio_shortcode_pre_html($html, $library, $instances) {
+    if ( 'mediaelement' === $library && 1 === $instances )
+		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
+    return $html;
+}
+add_filter('wp_audio_shortcode_pre_html', 'wp_mediaelement_audio_shortcode_pre_html', 10, 3);
+
+function wp_mediaelement_audio_shortcode_inside_html($html, $library, $fileurl, $post_id) {
+	if ( 'mediaelement' === $library )
+		$html .= wp_mediaelement_fallback( $fileurl );
+	return $html;
+}
+add_filter('wp_audio_shortcode_inside_html', 'wp_mediaelement_audio_shortcode_inside_html', 10, 4);
+
 /**
  * Return a filtered list of WP-supported video formats
  *
@@ -1788,9 +1824,19 @@ function wp_video_shortcode( $attr, $content = '' ) {
 		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
 	}
 
-	$html = '';
-	if ( 'mediaelement' === $library && 1 === $instances )
-		$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
+	/**
+	 * Adds HTML before the video shortcode output.
+	 *
+	 * Give the possibility to insert some HTML code before the <video> tag 
+	 * generated by a video shortcode.
+	 *
+	 * @since 4.0.0
+	 *
+	 * @param string $html      The HTML snippet to insert before the <video> tag.
+	 * @param string $library   Media library used for the video shortcode.
+	 * @param int    $instances Unique numeric ID of this video shortcode instance.
+	 */
+	$html = apply_filters('wp_video_shortcode_pre_html', '', $library, $instances);
 	$html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) );
 
 	$fileurl = '';
@@ -1817,8 +1863,20 @@ function wp_video_shortcode( $attr, $content = '' ) {
 		$html .= trim( $content );
 	}
 
-	if ( 'mediaelement' === $library )
-		$html .= wp_mediaelement_fallback( $fileurl );
+	/**
+	 * Adds HTML inside the video shortcode output.
+	 *
+	 * Give the possibility to insert some HTML code inside the <video> tag 
+	 * generated by a video shortcode.
+	 *
+	 * @since 4.0.0
+	 *
+	 * @param string $html    The HTML snippet to append into the <video> tag.
+	 * @param string $library Media library used for the video shortcode.
+	 * @param string $fileurl The URL of the video file.
+	 * @param int    $post_id Post ID.
+	 */
+	$html .= apply_filters('wp_video_shortcode_inside_html', '', $library, $fileurl, $post_id);
 	$html .= '</video>';
 
 	$html = sprintf( '<div style="width: %dpx; max-width: 100%%;" class="wp-video">%s</div>', $width, $html );
@@ -1838,6 +1896,20 @@ function wp_video_shortcode( $attr, $content = '' ) {
 }
 add_shortcode( 'video', 'wp_video_shortcode' );
 
+function wp_mediaelement_video_shortcode_pre_html($html, $library, $instances) {
+    if ( 'mediaelement' === $library && 1 === $instances )
+		$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
+    return $html;
+}
+add_filter('wp_video_shortcode_pre_html', 'wp_mediaelement_video_shortcode_pre_html', 10, 3);
+
+function wp_mediaelement_video_shortcode_inside_html($html, $library, $fileurl, $post_id) {
+	if ( 'mediaelement' === $library )
+		$html .= wp_mediaelement_fallback( $fileurl );
+	return $html;
+}
+add_filter('wp_video_shortcode_inside_html', 'wp_mediaelement_video_shortcode_inside_html', 10, 4);
+
 /**
  * Display previous image link that has the same post parent.
  *
-- 
1.9.1

