JAVASCRIPT   72
wp media element
Guest on 8th March 2023 12:31:06 AM


  1. /* global _wpmejsSettings, mejsL10n */
  2. (function( window, $ ) {
  3.  
  4.         window.wp = window.wp || {};
  5.  
  6.         function wpMediaElement() {
  7.                 var settings = {};
  8.  
  9.                 /**
  10.                  * Initialize media elements.
  11.                  *
  12.                  * Ensures media elements that have already been initialized won't be
  13.                  * processed again.
  14.                  *
  15.                  * @memberOf wp.mediaelement
  16.                  *
  17.                  * @since 4.4.0
  18.                  *
  19.                  * @return {void}
  20.                  */
  21.                 function initialize() {
  22.                         if ( typeof _wpmejsSettings !== 'undefined' ) {
  23.                                 settings = $.extend( true, {}, _wpmejsSettings );
  24.                         }
  25.                         settings.classPrefix = 'mejs-';
  26.                         settings.success = settings.success || function ( mejs ) {
  27.                                 var autoplay, loop;
  28.  
  29.                                 if ( mejs.rendererName && -1 !== mejs.rendererName.indexOf( 'flash' ) ) {
  30.                                         autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
  31.                                         loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
  32.  
  33.                                         if ( autoplay ) {
  34.                                                 mejs.addEventListener( 'canplay', function() {
  35.                                                         mejs.play();
  36.                                                 }, false );
  37.                                         }
  38.  
  39.                                         if ( loop ) {
  40.                                                 mejs.addEventListener( 'ended', function() {
  41.                                                         mejs.play();
  42.                                                 }, false );
  43.                                         }
  44.                                 }
  45.                         };
  46.  
  47.                         /**
  48.                          * Custom error handler.
  49.                          *
  50.                          * Sets up a custom error handler in case a video render fails, and provides a download
  51.                          * link as the fallback.
  52.                          *
  53.                          * @since 4.9.3
  54.                          *
  55.                          * @param {object} media The wrapper that mimics all the native events/properties/methods for all renderers.
  56.                          * @param {object} node  The original HTML video, audio, or iframe tag where the media was loaded.
  57.                          * @return {string}
  58.                          */
  59.                         settings.customError = function ( media, node ) {
  60.                                 // Make sure we only fall back to a download link for flash files.
  61.                                 if ( -1 !== media.rendererName.indexOf( 'flash' ) || -1 !== media.rendererName.indexOf( 'flv' ) ) {
  62.                                         return '<a href="' + node.src + '">' + mejsL10n.strings['mejs.download-file'] + '</a>';
  63.                                 }
  64.                         };
  65.  
  66.                         // Only initialize new media elements.
  67.                         $( '.wp-audio-shortcode, .wp-video-shortcode' )
  68.                                 .not( '.mejs-container' )
  69.                                 .filter(function () {
  70.                                         return ! $( this ).parent().hasClass( 'mejs-mediaelement' );
  71.                                 })
  72.                                 .mediaelementplayer( settings );
  73.                 }
  74.  
  75.                 return {
  76.                         initialize: initialize
  77.                 };
  78.         }
  79.  
  80.         /**
  81.          * @namespace wp.mediaelement
  82.          * @memberOf wp
  83.          */
  84.         window.wp.mediaelement = new wpMediaElement();
  85.  
  86.         $( window.wp.mediaelement.initialize );
  87.  
  88. })( window, jQuery );

Raw Paste

Login or Register to edit or fork this paste. It's free.