From 0082a438c583d049a29946268a392666659b4f09 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 20 Sep 2015 03:16:23 +0000 Subject: [PATCH] MediaElement, update `wp-mediaelement.js`: * create a public `initialize` method on the `wp.mediaelement` namespace * make it idempotent to prevent nasty side-effects caused by initializing media elements more than once Props bradyvercher. Fixes #32423. git-svn-id: https://develop.svn.wordpress.org/trunk@34346 602fd350-edb4-49c9-b593-d223f7449a82 --- .../js/mediaelement/wp-mediaelement.js | 71 +++++++++++++------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/js/mediaelement/wp-mediaelement.js b/src/wp-includes/js/mediaelement/wp-mediaelement.js index 8c4364aa32..fe47d2f0c1 100644 --- a/src/wp-includes/js/mediaelement/wp-mediaelement.js +++ b/src/wp-includes/js/mediaelement/wp-mediaelement.js @@ -1,34 +1,61 @@ /* global mejs, _wpmejsSettings */ -(function ($) { +(function( window, $ ) { + + window.wp = window.wp || {}; + // add mime-type aliases to MediaElement plugin support mejs.plugins.silverlight[0].types.push('video/x-ms-wmv'); mejs.plugins.silverlight[0].types.push('audio/x-ms-wma'); - $(function () { + function wpMediaElement() { var settings = {}; - if ( typeof _wpmejsSettings !== 'undefined' ) { - settings = _wpmejsSettings; + /** + * Initialize media elements. + * + * Ensures media elements that have already been initialized won't be + * processed again. + * + * @since 4.4.0 + */ + function initialize() { + if ( typeof _wpmejsSettings !== 'undefined' ) { + settings = _wpmejsSettings; + } + + settings.success = settings.success || function (mejs) { + var autoplay, loop; + + if ( 'flash' === mejs.pluginType ) { + autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay; + loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop; + + autoplay && mejs.addEventListener( 'canplay', function () { + mejs.play(); + }, false ); + + loop && mejs.addEventListener( 'ended', function () { + mejs.play(); + }, false ); + } + }; + + // Only initialize new media elements. + $( '.wp-audio-shortcode, .wp-video-shortcode' ) + .not( '.mejs-container' ) + .filter(function () { + return ! $( this ).parent().hasClass( '.mejs-mediaelement' ); + }) + .mediaelementplayer( settings ); } - settings.success = settings.success || function (mejs) { - var autoplay, loop; - - if ( 'flash' === mejs.pluginType ) { - autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay; - loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop; - - autoplay && mejs.addEventListener( 'canplay', function () { - mejs.play(); - }, false ); - - loop && mejs.addEventListener( 'ended', function () { - mejs.play(); - }, false ); - } + return { + initialize: initialize }; + } - $('.wp-audio-shortcode, .wp-video-shortcode').mediaelementplayer( settings ); - }); + window.wp.mediaelement = new wpMediaElement(); -}(jQuery)); + $( document ).on( 'ready', window.wp.mediaelement.initialize ); + +})( window, jQuery );