<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39272] trunk/src/wp-includes: Themes: Improve a11y and extendability of custom video headers.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39272">39272</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39272","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joemcgill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-16 23:25:28 +0000 (Wed, 16 Nov 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Themes: Improve a11y and extendability of custom video headers.

This adds play/pause controls to video headers, along with voice
assistance, using `wp.a11y.speak`, to make custom video headers more
accessible. To make styling the play/pause button easier for themes,
CSS has been omitted from the default implementation.

This also includes a refactor of the `wp.customHeader` code to introduce
a `BaseHandler` class, which can be extended by plugins and themes to modify
or enhance the default video handlers.

Props davidakennedy, afercia, bradyvercher, joemcgill, adamsilverstein, rianrietveld.
Fixes <a href="https://core.trac.wordpress.org/ticket/38678">#38678</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesjswpcustomheaderjs">trunk/src/wp-includes/js/wp-custom-header.js</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php      2016-11-16 22:16:10 UTC (rev 39271)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2016-11-16 23:25:28 UTC (rev 39272)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3596,7 +3596,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->add_setting( 'external_header_video', array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'theme_supports'    => array( 'custom-header', 'video' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'transport'         => 'postMessage',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'sanitize_callback' => 'esc_url',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'sanitize_callback' => 'esc_url_raw',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'validate_callback' => array( $this, '_validate_external_header_video' ),
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesjswpcustomheaderjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/js/wp-custom-header.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/wp-custom-header.js      2016-11-16 22:16:10 UTC (rev 39271)
+++ trunk/src/wp-includes/js/wp-custom-header.js        2016-11-16 23:25:28 UTC (rev 39272)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,155 +1,446 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-(function( window, settings ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* global YT */
+( function( window, settings ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        var NativeHandler, YouTubeHandler;
+
+       window.wp = window.wp || {};
+
+       // Fail gracefully in unsupported browsers.
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! ( 'addEventListener' in window ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Fail gracefully in unsupported browsers.
</del><span class="cx" style="display: block; padding: 0 10px">                 return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function wpCustomHeader() {
-               var handlers = {
-                       nativeVideo: {
-                               test: function( settings ) {
-                                       var video = document.createElement( 'video' );
-                                       return video.canPlayType( settings.mimeType );
-                               },
-                               callback: nativeHandler
-                       },
-                       youtube: {
-                               test: function( settings ) {
-                                       return 'video/x-youtube' === settings.mimeType;
-                               },
-                               callback: youtubeHandler
-                       }
-               };
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Trigger an event.
+        *
+        * @param {Element} target HTML element to dispatch the event on.
+        * @param {string} name Event name.
+        */
+       function trigger( target, name ) {
+               var evt;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                function initialize() {
-                       settings.container = document.getElementById( 'wp-custom-header' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'function' === typeof window.Event ) {
+                       evt = new Event( name );
+               } else {
+                       evt = document.createEvent( 'Event' );
+                       evt.initEvent( name, true, true );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( supportsVideo() ) {
-                               for ( var id in handlers ) {
-                                       var handler = handlers[ id ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         target.dispatchEvent( evt );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        if ( handlers.hasOwnProperty( id ) && handler.test( settings ) ) {
-                                               handler.callback( settings );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Create a custom header instance.
+        *
+        * @class CustomHeader
+        */
+       function CustomHeader() {
+               this.handlers = {
+                       nativeVideo: new NativeHandler(),
+                       youtube: new YouTubeHandler()
+               };
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                // Set up and dispatch custom event when the video is loaded.
-                                               if ( 'dispatchEvent' in window ) {
-                                                       var videoLoaded = new Event( 'wp-custom-header-video-loaded' );
-                                                       document.dispatchEvent( videoLoaded );
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ CustomHeader.prototype = {
+               /**
+                * Initalize the custom header.
+                *
+                * If the environment supports video, loops through registered handlers
+                * until one is found that can handle the video.
+                */
+               initialize: function() {
+                       if ( this.supportsVideo() ) {
+                               for ( var id in this.handlers ) {
+                                       var handler = this.handlers[ id ];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        if ( 'test' in handler && handler.test( settings ) ) {
+                                               this.activeHandler = handler.initialize.call( handler, settings );
+
+                                               // Dispatch custom event when the video is loaded.
+                                               trigger( document, 'wp-custom-header-video-loaded' );
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 break;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         },
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                function supportsVideo() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Determines if the current environment supports video.
+                *
+                * Themes and plugins can override this method to change the criteria.
+                *
+                * @return {boolean}
+                */
+               supportsVideo: function() {
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Don't load video on small screens. @todo: consider bandwidth and other factors.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( window.innerWidth < settings.minWidth  || window.innerHeight < settings.minHeight ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( window.innerWidth < settings.minWidth || window.innerHeight < settings.minHeight ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        return true;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                },
+
+               /**
+                * Base handler for custom handlers to extend.
+                *
+                * @type {BaseHandler}
+                */
+               BaseVideoHandler: BaseHandler
+       };
+
+       /**
+        * Create a video handler instance.
+        *
+        * @class BaseHandler
+        */
+       function BaseHandler() {}
+
+       BaseHandler.prototype = {
+               /**
+                * Initialize the video handler.
+                *
+                * @param {object} settings Video settings.
+                */
+               initialize: function( settings ) {
+                       var handler = this,
+                               button = document.createElement( 'button' );
+
+                       this.settings = settings;
+                       this.container = document.getElementById( 'wp-custom-header' ),
+                       this.button = button;
+
+                       button.setAttribute( 'type', 'button' );
+                       button.setAttribute( 'id', 'wp-custom-header-video-button' );
+                       button.setAttribute( 'class', 'wp-custom-header-video-button wp-custom-header-video-play' );
+                       button.innerHTML = settings.l10n.play;
+
+                       // Toggle video playback when the button is clicked.
+                       button.addEventListener( 'click', function() {
+                               if ( handler.isPaused() ) {
+                                       handler.play();
+                               } else {
+                                       handler.pause();
+                               }
+                       });
+
+                       // Update the button class and text when the video state changes.
+                       this.container.addEventListener( 'play', function() {
+                               button.className = 'wp-custom-header-video-button wp-custom-header-video-play';
+                               button.innerHTML = settings.l10n.pause;
+                               if ( 'a11y' in window.wp ) {
+                                       window.wp.a11y.speak( settings.l10n.playSpeak);
+                               }
+                       });
+
+                       this.container.addEventListener( 'pause', function() {
+                               button.className = 'wp-custom-header-video-button wp-custom-header-video-pause';
+                               button.innerHTML = settings.l10n.play;
+                               if ( 'a11y' in window.wp ) {
+                                       window.wp.a11y.speak( settings.l10n.pauseSpeak);
+                               }
+                       });
+
+                       this.ready();
+               },
+
+               /**
+                * Ready method called after a handler is initialized.
+                *
+                * @abstract
+                */
+               ready: function() {},
+
+               /**
+                * Whether the video is paused.
+                *
+                * @abstract
+                * @return {boolean}
+                */
+               isPaused: function() {},
+
+               /**
+                * Pause the video.
+                *
+                * @abstract
+                */
+               pause: function() {},
+
+               /**
+                * Play the video.
+                *
+                * @abstract
+                */
+               play: function() {},
+
+               /**
+                * Append a video node to the header container.
+                *
+                * @param {Element} node HTML element.
+                */
+               setVideo: function( node ) {
+                       var editShortcutNode,
+                               editShortcut = this.container.getElementsByClassName( 'customize-partial-edit-shortcut' );
+
+                       if ( editShortcut.length ) {
+                               editShortcutNode = this.container.removeChild( editShortcut[0] );
+                       }
+
+                       this.container.innerHTML = '';
+                       this.container.appendChild( node );
+
+                       if ( editShortcutNode ) {
+                               this.container.appendChild( editShortcutNode );
+                       }
+               },
+
+               /**
+                * Show the video controls.
+                *
+                * Appends a play/pause button to header container.
+                */
+               showControls: function() {
+                       if ( ! this.container.contains( this.button ) ) {
+                               this.container.appendChild( this.button );
+                       }
+               },
+
+               /**
+                * Whether the handler can process a video.
+                *
+                * @abstract
+                * @param {object} settings Video settings.
+                * @return {boolean}
+                */
+               test: function() {
+                       return false;
+               },
+
+               /**
+                * Trigger an event on the header container.
+                *
+                * @param {string} name Event name.
+                */
+               trigger: function( name ) {
+                       trigger( this.container, name );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        };
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return {
-                       handlers: handlers,
-                       initialize: initialize,
-                       supportsVideo: supportsVideo
-               };
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Create a custom handler.
+        *
+        * @param  {object} protoProps Properties to apply to the prototype.
+        * @return CustomHandler The subclass.
+        */
+       BaseHandler.extend = function( protoProps ) {
+               var prop;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function nativeHandler( settings ) {
-               var video = document.createElement( 'video' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         function CustomHandler() {
+                       var result = BaseHandler.apply( this, arguments );
+                       return result;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                video.id = 'wp-custom-header-video';
-               video.autoplay = 'autoplay';
-               video.loop = 'loop';
-               video.muted = 'muted';
-               video.width = settings.width;
-               video.height = settings.height;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         CustomHandler.prototype = Object.create( BaseHandler.prototype );
+               CustomHandler.prototype.constructor = CustomHandler;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                video.addEventListener( 'click', function() {
-                       if ( video.paused ) {
-                               video.play();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         for ( prop in protoProps ) {
+                       CustomHandler.prototype[ prop ] = protoProps[ prop ];
+               }
+
+               return CustomHandler;
+       };
+
+       /**
+        * Native video handler.
+        *
+        * @class NativeHandler
+        */
+       NativeHandler = BaseHandler.extend({
+               /**
+                * Whether the native handler supports a video.
+                *
+                * @param {object} settings Video settings.
+                * @return {boolean}
+                */
+               test: function( settings ) {
+                       var video = document.createElement( 'video' );
+                       return video.canPlayType( settings.mimeType );
+               },
+
+               /**
+                * Set up a native video element.
+                */
+               ready: function() {
+                       var handler = this,
+                               video = document.createElement( 'video' );
+
+                       video.id = 'wp-custom-header-video';
+                       video.autoplay = 'autoplay';
+                       video.loop = 'loop';
+                       video.muted = 'muted';
+                       video.width = this.settings.width;
+                       video.height = this.settings.height;
+
+                       video.addEventListener( 'play', function() {
+                               handler.trigger( 'play' );
+                       });
+
+                       video.addEventListener( 'pause', function() {
+                               handler.trigger( 'pause' );
+                       });
+
+                       video.addEventListener( 'canplay', function() {
+                               handler.showControls();
+                       });
+
+                       this.video = video;
+                       handler.setVideo( video );
+                       video.src = this.settings.videoUrl;
+               },
+
+               /**
+                * Whether the video is paused.
+                *
+                * @return {boolean}
+                */
+               isPaused: function() {
+                       return this.video.paused;
+               },
+
+               /**
+                * Pause the video.
+                */
+               pause: function() {
+                       this.video.pause();
+               },
+
+               /**
+                * Play the video.
+                */
+               play: function() {
+                       this.video.play();
+               }
+       });
+
+       /**
+        * YouTube video handler.
+        *
+        * @class YouTubeHandler
+        */
+       YouTubeHandler = BaseHandler.extend({
+               /**
+                * Whether the handler supports a video.
+                *
+                * @param {object} settings Video settings.
+                * @return {boolean}
+                */
+               test: function( settings ) {
+                       return 'video/x-youtube' === settings.mimeType;
+               },
+
+               /**
+                * Set up a YouTube iframe.
+                *
+                * Loads the YouTube IFrame API if the 'YT' global doesn't exist.
+                */
+               ready: function() {
+                       var handler = this;
+
+                       if ( 'YT' in window ) {
+                               YT.ready( handler.loadVideo.bind( handler ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                video.pause();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         var tag = document.createElement( 'script' );
+                               tag.src = 'https://www.youtube.com/iframe_api';
+                               tag.onload = function () {
+                                       YT.ready( handler.loadVideo.bind( handler ) );
+                               };
+
+                               document.getElementsByTagName( 'head' )[0].appendChild( tag );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                });
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         },
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                settings.container.innerHTML = '';
-               settings.container.appendChild( video );
-               video.src = settings.videoUrl;
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Load a YouTube video.
+                */
+               loadVideo: function() {
+                       var handler = this,
+                               video = document.createElement( 'div' ),
+                               // @link http://stackoverflow.com/a/27728417
+                               VIDEO_ID_REGEX = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function youtubeHandler( settings ) {
-               // @link http://stackoverflow.com/a/27728417
-               var VIDEO_ID_REGEX = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/,
-                       videoId = settings.videoUrl.match( VIDEO_ID_REGEX )[1];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 video.id = 'wp-custom-header-video';
+                       handler.setVideo( video );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                function loadVideo() {
-                       var YT = window.YT || {};
-
-                       YT.ready(function() {
-                               var video = document.createElement( 'div' );
-                               video.id = 'wp-custom-header-video';
-                               settings.container.innerHTML = '';
-                               settings.container.appendChild( video );
-
-                               new YT.Player( video, {
-                                       height: settings.height,
-                                       width: settings.width,
-                                       videoId: videoId,
-                                       events: {
-                                               onReady: function( e ) {
-                                                       e.target.mute();
-                                               },
-                                               onStateChange: function( e ) {
-                                                       if ( YT.PlayerState.ENDED === e.data ) {
-                                                               e.target.playVideo();
-                                                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 handler.player = new YT.Player( video, {
+                               height: this.settings.height,
+                               width: this.settings.width,
+                               videoId: this.settings.videoUrl.match( VIDEO_ID_REGEX )[1],
+                               events: {
+                                       onReady: function( e ) {
+                                               e.target.mute();
+                                               handler.showControls();
+                                       },
+                                       onStateChange: function( e ) {
+                                               if ( YT.PlayerState.PLAYING === e.data ) {
+                                                       handler.trigger( 'play' );
+                                               } else if ( YT.PlayerState.PAUSED === e.data ) {
+                                                       handler.trigger( 'pause' );
+                                               } else if ( YT.PlayerState.ENDED === e.data ) {
+                                                       e.target.playVideo();
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        },
-                                       playerVars: {
-                                               autoplay: 1,
-                                               controls: 0,
-                                               disablekb: 1,
-                                               fs: 0,
-                                               iv_load_policy: 3,
-                                               loop: 1,
-                                               modestbranding: 1,
-                                               //origin: '',
-                                               playsinline: 1,
-                                               rel: 0,
-                                               showinfo: 0
</del><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                });
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         },
+                               playerVars: {
+                                       autoplay: 1,
+                                       controls: 0,
+                                       disablekb: 1,
+                                       fs: 0,
+                                       iv_load_policy: 3,
+                                       loop: 1,
+                                       modestbranding: 1,
+                                       playsinline: 1,
+                                       rel: 0,
+                                       showinfo: 0
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         },
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 'YT' in window ) {
-                       loadVideo();
-               } else {
-                       var tag = document.createElement( 'script' );
-                       tag.src = 'https://www.youtube.com/player_api';
-                       tag.onload = function () { loadVideo(); };
-                       document.getElementsByTagName( 'head' )[0].appendChild( tag );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Whether the video is paused.
+                *
+                * @return {boolean}
+                */
+               isPaused: function() {
+                       return YT.PlayerState.PAUSED === this.player.getPlayerState();
+               },
+
+               /**
+                * Pause the video.
+                */
+               pause: function() {
+                       this.player.pauseVideo();
+               },
+
+               /**
+                * Play the video.
+                */
+               play: function() {
+                       this.player.playVideo();
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ });
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        window.wp = window.wp || {};
-       window.wp.customHeader = new wpCustomHeader();
-       document.addEventListener( 'DOMContentLoaded', window.wp.customHeader.initialize, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Initialize the custom header when the DOM is ready.
+       window.wp.customHeader = new CustomHeader();
+       document.addEventListener( 'DOMContentLoaded', window.wp.customHeader.initialize.bind( window.wp.customHeader ), false );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Selective refresh support in the Customizer.
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( 'customize' in window.wp ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp.customize.selectiveRefresh.bind( 'render-partials-response', function( response ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         window.wp.customize.selectiveRefresh.bind( 'render-partials-response', function( response ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( 'custom_header_settings' in response ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                settings = response.custom_header_settings;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         window.wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( 'custom_header' === placement.partial.id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                window.wp.customHeader.initialize();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php   2016-11-16 22:16:10 UTC (rev 39271)
+++ trunk/src/wp-includes/script-loader.php     2016-11-16 23:25:28 UTC (rev 39272)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,7 +481,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $scripts->add( 'customize-nav-menus', "/wp-admin/js/customize-nav-menus$suffix.js", array( 'jquery', 'wp-backbone', 'customize-controls', 'accordion', 'nav-menu' ), false, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">        $scripts->add( 'customize-preview-nav-menus', "/wp-includes/js/customize-preview-nav-menus$suffix.js", array( 'jquery', 'wp-util', 'customize-preview', 'customize-selective-refresh' ), false, 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $scripts->add( 'wp-custom-header', "/wp-includes/js/wp-custom-header$suffix.js", array(), false, 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $scripts->add( 'wp-custom-header', "/wp-includes/js/wp-custom-header$suffix.js", array( 'wp-a11y' ), false, 1 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $scripts->add( 'accordion', "/wp-admin/js/accordion$suffix.js", array( 'jquery' ), false, 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2016-11-16 22:16:10 UTC (rev 39271)
+++ trunk/src/wp-includes/theme.php     2016-11-16 23:25:28 UTC (rev 39272)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1381,6 +1381,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'height'    => absint( $header->height ),
</span><span class="cx" style="display: block; padding: 0 10px">                'minWidth'  => 900,
</span><span class="cx" style="display: block; padding: 0 10px">                'minHeight' => 500,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'l10n'      => array(
+                       'pause'      => __( 'Pause' ),
+                       'play'       => __( 'Play' ),
+                       'pauseSpeak' => __( 'Video is paused.'),
+                       'playSpeak'  => __( 'Video is playing.'),
+               ),
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( preg_match( '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#', $video_url ) ) {
</span></span></pre>
</div>
</div>

</body>
</html>