mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-04 09:49:37 +01:00
030bc377dd
This adds the ability for themes to add support for videos in custom headers by passing `'video' => true` as an argument when adding theme support for custom headers. Custom video headers are managed through the “Header Visuals” (i.e. “Header Image”) panel in the Customizer where you can select a video from the media library or set a URL to an external video (YouTube for now) for use in custom headers. This introduces several new functions: `has_header_video()` – Check whether a header video is set or not. `get_header_video_url()` – Retrieve header video URL for custom header. `the_header_video_url()` – Display header video URL. `get_header_video_settings()` – Retrieve header video settings. `has_custom_header()` – Check whether a custom header is set or not. `get_custom_header_markup()` – Retrieve the markup for a custom header. `the_custom_header_markup()` – Print the markup for a custom header. And a new file, `wp-includes/js/wp-custom-header.js` that handles loading videos in custom headers. This also enables video headers in the Twenty Seventeen and Twenty Fourteen themes. Props davidakennedy, celloexpressions, bradyvercher, laurelfulford, joemcgill. Fixes #38172. Built from https://develop.svn.wordpress.org/trunk@38985 git-svn-id: http://core.svn.wordpress.org/trunk@38928 1a063a9b-81f0-0310-95a4-ce76da25c4cd
150 lines
3.7 KiB
JavaScript
150 lines
3.7 KiB
JavaScript
(function( window, $, settings ) {
|
|
|
|
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
|
|
}
|
|
};
|
|
|
|
function initialize() {
|
|
settings.container = document.getElementById( 'wp-custom-header' );
|
|
|
|
if ( supportsVideo() ) {
|
|
for ( var id in handlers ) {
|
|
var handler = handlers[ id ];
|
|
|
|
if ( handlers.hasOwnProperty( id ) && handler.test( settings ) ) {
|
|
handler.callback( settings );
|
|
break;
|
|
}
|
|
}
|
|
|
|
$( 'body' ).trigger( 'wp-custom-header-video-loaded' );
|
|
}
|
|
}
|
|
|
|
function supportsVideo() {
|
|
// Don't load video on small screens. @todo: consider bandwidth and other factors.
|
|
if ( window.innerWidth < settings.minWidth || window.innerHeight < settings.minHeight ) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return {
|
|
handlers: handlers,
|
|
initialize: initialize,
|
|
supportsVideo: supportsVideo
|
|
};
|
|
}
|
|
|
|
function nativeHandler( settings ) {
|
|
var video = document.createElement( 'video' );
|
|
|
|
video.id = 'wp-custom-header-video';
|
|
video.autoplay = 'autoplay';
|
|
video.loop = 'loop';
|
|
video.muted = 'muted';
|
|
video.width = settings.width;
|
|
video.height = settings.height;
|
|
|
|
video.addEventListener( 'click', function() {
|
|
if ( video.paused ) {
|
|
video.play();
|
|
} else {
|
|
video.pause();
|
|
}
|
|
});
|
|
|
|
settings.container.innerHTML = '';
|
|
settings.container.appendChild( video );
|
|
video.src = settings.videoUrl;
|
|
}
|
|
|
|
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];
|
|
|
|
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();
|
|
}
|
|
}
|
|
},
|
|
playerVars: {
|
|
autoplay: 1,
|
|
controls: 0,
|
|
disablekb: 1,
|
|
fs: 0,
|
|
iv_load_policy: 3,
|
|
loop: 1,
|
|
modestbranding: 1,
|
|
//origin: '',
|
|
playsinline: 1,
|
|
rel: 0,
|
|
showinfo: 0
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
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 );
|
|
}
|
|
}
|
|
|
|
window.wp = window.wp || {};
|
|
window.wp.customHeader = new wpCustomHeader();
|
|
document.addEventListener( 'DOMContentLoaded', window.wp.customHeader.initialize, false );
|
|
|
|
if ( 'customize' in window.wp ) {
|
|
wp.customize.selectiveRefresh.bind( 'render-partials-response', function( response ) {
|
|
if ( 'custom_header_settings' in response ) {
|
|
settings = response.custom_header_settings;
|
|
}
|
|
});
|
|
|
|
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
|
|
if ( 'custom_header' === placement.partial.id ) {
|
|
window.wp.customHeader.initialize();
|
|
}
|
|
});
|
|
}
|
|
|
|
})( window, jQuery, window._wpCustomHeaderSettings || {} );
|