From afad1eb85a926434343bd0efd1e050ecc0272224 Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Sat, 26 Nov 2022 13:18:04 +0000 Subject: [PATCH 01/11] Add support for Daily Motion videos --- better-core-video-embeds.php | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index 84cb683..142defd 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -151,6 +151,23 @@ function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) { // break out the switch. break; + + // for vimeo urls. + case 'www.dailymotion.com'; + + // if we have a path. + if ( empty( $parsed_video_url['path'] ) ) { + return $block_content; + } + + // remove the preceeding slash. + $video_id = str_replace( '/video/', '', $parsed_video_url['path'] ); + + // get the vimeo thumbnail url for this video. + $thumbnail_url = hd_bcve_get_dailymotion_video_thumbnail_url( $video_id ); + + // break out the switch. + break; } @@ -309,6 +326,55 @@ function hd_bcve_get_vimeo_video_thumbnail_url( $video_id = '' ) { } +/** + * Return the dailymotion video thumbnail url. + * + * @param string $video_id The ID of the video. + * @return string $url The URL of the thumbnail or an empty string if no URL found. + */ +function hd_bcve_get_dailymotion_video_thumbnail_url( $video_id = '' ) { + + // if we have no video id. + if ( '' === $video_id ) { + return ''; + } + + // get the URL from the transient. + $image_url = get_transient( 'hd_bcve_' . $video_id ); + + // if we don't have a transient. + if ( false === $image_url ) { + + // get the video details from the api. + $video_details = wp_remote_get( + 'https://api.dailymotion.com/video/' . $video_id . '?fields=thumbnail_url' + ); + + // if the request to the hi res image errors or returns anything other than a http 200 response code. + if ( ( is_wp_error( $video_details )) && ( 200 !== wp_remote_retrieve_response_code( $video_details ) ) ) { + return ''; + } + + // grab the body of the response. + $video_details = json_decode( + wp_remote_retrieve_body( + $video_details + ) + ); + + // get the image url from the json. + $image_url = $video_details->thumbnail_url; + + // set the transient, storing the image url. + set_transient( 'hd_bcve_' . $video_id, $image_url, DAY_IN_SECONDS ); + + } + + // return the url. + return apply_filters( 'hd_bcve_vimeo_video_thumbnail_url', $image_url, $video_id ); + +} + /** * Creates a escaping function to allowed certain HTML for embed content. * Needed for when echoing the innerblock HTML. From f039d5fc219d058354bf01764370adc8e0554081 Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Mon, 28 Nov 2022 18:44:10 +0000 Subject: [PATCH 02/11] Register styles on wp rather than wp_head hook --- better-core-video-embeds.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index 84cb683..c7dfcce 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -64,7 +64,7 @@ function hd_bcve_register_block_style() { } -add_action( 'wp_head', 'hd_bcve_register_block_style' ); +add_action( 'wp', 'hd_bcve_register_block_style' ); /** * Filters the code embed block output for improved performance on Youtube videos. From 4ff4cd6405125d483a7e7ff4293623ccb85b06f6 Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Mon, 28 Nov 2022 18:59:21 +0000 Subject: [PATCH 03/11] Update CSS to give image max width --- assets/css/better-core-video-embeds.css | 1 + assets/css/better-core-video-embeds.min.css | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/assets/css/better-core-video-embeds.css b/assets/css/better-core-video-embeds.css index 42b68c2..ada4d9c 100644 --- a/assets/css/better-core-video-embeds.css +++ b/assets/css/better-core-video-embeds.css @@ -5,6 +5,7 @@ .hd-bcve-thumbnail { height: auto; max-width: 100%; + width: 100%; } .hd-bcve-wrapper .play-button { --hd-play-button-size: 80px; diff --git a/assets/css/better-core-video-embeds.min.css b/assets/css/better-core-video-embeds.min.css index ff29fff..e5e1b2c 100644 --- a/assets/css/better-core-video-embeds.min.css +++ b/assets/css/better-core-video-embeds.min.css @@ -1 +1 @@ -.hd-bcve-wrapper{position:relative;cursor:pointer}.hd-bcve-thumbnail{height:auto;max-width:100%}.hd-bcve-wrapper .play-button{--hd-play-button-size:80px;--hd-play-button-background-color:#FFFFFF;display:flex;align-items:center;justify-content:center;position:absolute;top:calc(50% - 0.5 * var(--hd-play-button-size));left:calc(50% - 0.5 * var(--hd-play-button-size));z-index:2;width:var(--hd-play-button-size);height:var(--hd-play-button-size)}.hd-bcve-wrapper .play-button:after{--hd-play-button-opacity:0.8;content:'';display:block;width:0;height:0;border-style:solid;border-width:37.5px 0 37.5px 65.0px;border-color:transparent transparent transparent var(--hd-play-button-background-color);opacity:var(--hd-play-button-opacity);transition:all 0.2s linear}.hd-bcve-wrapper:hover .play-button:after{--hd-play-button-opacity:1} \ No newline at end of file +.hd-bcve-wrapper{position:relative;cursor:pointer}.hd-bcve-thumbnail{height:auto;max-width:100%;width:100%}.hd-bcve-wrapper .play-button{--hd-play-button-size:80px;--hd-play-button-background-color:#FFFFFF;display:flex;align-items:center;justify-content:center;position:absolute;top:calc(50% - 0.5 * var(--hd-play-button-size));left:calc(50% - 0.5 * var(--hd-play-button-size));z-index:2;width:var(--hd-play-button-size);height:var(--hd-play-button-size)}.hd-bcve-wrapper .play-button:after{--hd-play-button-opacity:0.8;content:'';display:block;width:0;height:0;border-style:solid;border-width:37.5px 0 37.5px 65.0px;border-color:transparent transparent transparent var(--hd-play-button-background-color);opacity:var(--hd-play-button-opacity);transition:all 0.2s linear}.hd-bcve-wrapper:hover .play-button:after{--hd-play-button-opacity:1} \ No newline at end of file From 56ebc97bd329facc5000b98339c72e337a8c940d Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Mon, 28 Nov 2022 21:22:10 +0000 Subject: [PATCH 04/11] Add caption support --- assets/js/better-core-video-embeds.js | 36 +++++++++++++++++++-------- better-core-video-embeds.php | 4 +-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/assets/js/better-core-video-embeds.js b/assets/js/better-core-video-embeds.js index eedd0fd..eeb36b6 100644 --- a/assets/js/better-core-video-embeds.js +++ b/assets/js/better-core-video-embeds.js @@ -2,31 +2,47 @@ // these are the elements that should contain the thumbnail. document.querySelectorAll( '.hd-bcve-wrapper' ).forEach(( el, i ) => { - // when the wrapper is clicked. - el.addEventListener( 'click', () => { + // get the youtube video id for the data attribute on the wrapper. + var videoId = el.getAttribute( 'data-id' ); + + // get the associated template element which holds the embed code. + var template = document.querySelector( '#hd-bcve-embed-html-' + videoId ); + var caption = template.content.textContent; + caption = caption.trim(); + + // if we have a caption. + if ( '' !== caption ) { - // get the youtube video id for the data attribute on the wrapper. - var videoId = el.getAttribute( 'data-id' ); + // create an element for the embed caption. + var captionEl = document.createElement('figcaption'); + captionEl.classList.add( 'wp-element-caption' ); + captionEl.textContent = caption; + + // add the caption, after the thumbnail. + el.append( captionEl ); - // get the associated template element which holds the embed code. - var template = document.querySelector( '#hd-bcve-embed-html-' + videoId ); + } + + // when the wrapper is clicked. + el.addEventListener( 'click', () => { // clone the template element. var contentOuter = template.content.cloneNode( true ); // grab just the first child of the template - this is the figure block element which wraps the iframe. - var content =contentOuter.children[0] + var content = contentOuter.children[0]; // find the iframe in the embed content. var iframe = content.querySelector( 'iframe' ); // add auto play true to the iframe src. // ensures the video plays when the thumbnail is clicked. - var iframeSrc = iframe.getAttribute( 'src' ) + '&autoplay=true&rel=0&showinfo=0'; - var iframeSrcNew = iframeSrc.replace( 'www.youtube.com', 'www.youtube-nocookie.com' ); + var iframeSrc = iframe.getAttribute( 'src' ) + '&rel=0&showinfo=0&autoplay=1'; + iframeSrc = iframeSrc.replace( 'www.youtube.com', 'www.youtube-nocookie.com' ); + console.log( iframeSrc ); // set the new iframe src including autoplay true. - iframe.setAttribute( 'src', iframeSrcNew ); + iframe.setAttribute( 'src', iframeSrc ); // add the iframe embed content before the embed wrapper. diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index c7dfcce..213913e 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -40,7 +40,7 @@ function hd_bcve_enqueue_scripts() { // enqueue the front end script to invoking the video embed on image click. wp_enqueue_script( 'better-core-video-embeds-js', - HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.min.js', + HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.js', false, false, true @@ -192,8 +192,6 @@ function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) { // buffer the output as we need to return not echo. ob_start(); - //wp_var_dump( hd_job_allowed_innerblock_html() ); - ?>
From dbbf98110e386d99141f5ac048518a8c12c62632 Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Tue, 29 Nov 2022 09:03:31 +0000 Subject: [PATCH 05/11] JS improvements --- assets/js/better-core-video-embeds.js | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/assets/js/better-core-video-embeds.js b/assets/js/better-core-video-embeds.js index eeb36b6..bb07897 100644 --- a/assets/js/better-core-video-embeds.js +++ b/assets/js/better-core-video-embeds.js @@ -7,8 +7,22 @@ document.querySelectorAll( '.hd-bcve-wrapper' ).forEach(( el, i ) => { // get the associated template element which holds the embed code. var template = document.querySelector( '#hd-bcve-embed-html-' + videoId ); - var caption = template.content.textContent; - caption = caption.trim(); + + /** + * Add autoplay on the iframe as well as loading from youtube no cookie. + * Grabs the iframe from the embed template. + * Adds the autoplay and other attrs to the iframe src URL + * Replaces the standard youtube domain with the no cookie version. + */ + var iframe = template.content.children[0].querySelector( 'iframe' ); + var iframeSrc = iframe.getAttribute( 'src' ) + '&rel=0&showinfo=0&autoplay=1'; + iframeSrc = iframeSrc.replace( 'www.youtube.com', 'www.youtube-nocookie.com' ); + + // set the new iframe src including autoplay true. + iframe.setAttribute( 'src', iframeSrc ); + + // get the text content of the embed element - this is the caption. + var caption = template.content.textContent.trim(); // if we have a caption. if ( '' !== caption ) { @@ -28,23 +42,10 @@ document.querySelectorAll( '.hd-bcve-wrapper' ).forEach(( el, i ) => { // clone the template element. var contentOuter = template.content.cloneNode( true ); - + // grab just the first child of the template - this is the figure block element which wraps the iframe. var content = contentOuter.children[0]; - // find the iframe in the embed content. - var iframe = content.querySelector( 'iframe' ); - - // add auto play true to the iframe src. - // ensures the video plays when the thumbnail is clicked. - var iframeSrc = iframe.getAttribute( 'src' ) + '&rel=0&showinfo=0&autoplay=1'; - iframeSrc = iframeSrc.replace( 'www.youtube.com', 'www.youtube-nocookie.com' ); - console.log( iframeSrc ); - - // set the new iframe src including autoplay true. - iframe.setAttribute( 'src', iframeSrc ); - - // add the iframe embed content before the embed wrapper. el.before( content ); From a64da2c241a6d6929d4718e90ccd39f969756bc9 Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Tue, 29 Nov 2022 09:07:46 +0000 Subject: [PATCH 06/11] Minify JS and load minified js file --- assets/js/better-core-video-embeds.min.js | 2 +- better-core-video-embeds.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/better-core-video-embeds.min.js b/assets/js/better-core-video-embeds.min.js index 5006b35..23c4501 100644 --- a/assets/js/better-core-video-embeds.min.js +++ b/assets/js/better-core-video-embeds.min.js @@ -1 +1 @@ -document.querySelectorAll('.hd-bcve-wrapper').forEach((el,i)=>{el.addEventListener('click',()=>{var videoId=el.getAttribute('data-id');var template=document.querySelector('#hd-bcve-embed-html-'+videoId);var contentOuter=template.content.cloneNode(true);var content=contentOuter.children[0];var iframe=content.querySelector('iframe');var iframeSrc=iframe.getAttribute('src')+'&autoplay=true&rel=0&showinfo=0';var iframeSrcNew=iframeSrc.replace('www.youtube.com','www.youtube-nocookie.com');iframe.setAttribute('src',iframeSrcNew);el.before(content);el.remove();template.remove()})}); \ No newline at end of file +document.querySelectorAll('.hd-bcve-wrapper').forEach((el,i)=>{var videoId=el.getAttribute('data-id');var template=document.querySelector('#hd-bcve-embed-html-'+videoId);var iframe=template.content.children[0].querySelector('iframe');var iframeSrc=iframe.getAttribute('src')+'&rel=0&showinfo=0&autoplay=1';iframeSrc=iframeSrc.replace('www.youtube.com','www.youtube-nocookie.com');iframe.setAttribute('src',iframeSrc);var caption=template.content.textContent.trim();if(''!==caption){var captionEl=document.createElement('figcaption');captionEl.classList.add('wp-element-caption');captionEl.textContent=caption;el.append(captionEl)}el.addEventListener('click',()=>{var contentOuter=template.content.cloneNode(true);var content=contentOuter.children[0];el.before(content);el.remove();template.remove()})}); \ No newline at end of file diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index 213913e..5c6613c 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -40,7 +40,7 @@ function hd_bcve_enqueue_scripts() { // enqueue the front end script to invoking the video embed on image click. wp_enqueue_script( 'better-core-video-embeds-js', - HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.js', + HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.min.js', false, false, true From faf4470a6ea476893dee9783fc9689e59dcc65ab Mon Sep 17 00:00:00 2001 From: Mark Wilkinson Date: Tue, 29 Nov 2022 09:38:09 +0000 Subject: [PATCH 07/11] Add provider slug class --- better-core-video-embeds.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index 5c6613c..38396d3 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -77,7 +77,7 @@ function hd_bcve_register_block_style() { */ function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) { - // if the provider slug name is empty or not youtube. + // if the provider slug name is empty. if ( empty( $block['attrs']['providerNameSlug'] ) ) { return $block_content; } @@ -168,6 +168,7 @@ function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) { $figure_classes = [ 'wp-block-image', 'hd-bcve-wrapper', + 'is--' . $block['attrs']['providerNameSlug'], ]; // if we have classNames on the embed block. @@ -198,6 +199,7 @@ function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) {
+