Skip to content

Commit b362862

Browse files
authored
Merge pull request #18 from mrwweb/v1.2.0
V1.2.0 - Add Vimeo Support
2 parents d32719f + 9b1114f commit b362862

File tree

15 files changed

+1162
-426
lines changed

15 files changed

+1162
-426
lines changed

.wordpress-org/blueprints/blueprint.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
"wp": "latest"
77
},
88
"siteOptions": {
9-
"blogname": "Testing \"Enhanced Embed Block for YouTube\" plugin"
9+
"blogname": "Testing \"Enhanced Embed Block for YouTube & Vimeo\" plugin"
1010
},
11-
"phpExtensionBundles": ["kitchen-sink"],
1211
"login": true,
1312
"steps": [
1413
{
1514
"step": "installPlugin",
16-
"pluginZipFile": {
15+
"pluginData": {
1716
"resource": "wordpress.org/plugins",
1817
"slug": "enhanced-embed-block"
1918
},

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Enhanced Embed Block for YouTube
22

3-
A WordPress plugin for better user experience and faster page loads on any page with a YouTube video.
3+
A WordPress plugin for better user experience and faster page loads on any page with a YouTube or Vimeo video.
44

55
Mark Root-Wiley, [MRW Web Design](https://MRWweb.com)
66

77
## What this plugin does
88

9-
This plugin makes the default WordPress YouTube Embed block infinitely better by replacing each video with the [`lite-youtube` custom element](https://github.com/justinribeiro/lite-youtube).
9+
This plugin makes the default WordPress YouTube and Vimeo Embed blocks infinitely better by replacing each video with the [`lite-youtube` custom element](https://github.com/justinribeiro/lite-youtube) or [`lite-vimeo` custom element](https://github.com/cshawaus/lite-vimeo), respectively.
1010

1111
The plugin further improves things by providing a no-JS fallback and ensuring that the video placeholder doesn't cause a layout shift from when the page loads to when the custom element is initialized.
1212

13+
## Open Source Credits
14+
15+
- [`lite-youtube` custom-element](https://github.com/justinribeiro/lite-youtube) by @justinribeiro (MIT License)
16+
- [`lite-vimeo` custom-element](https://github.com/cshawaus/lite-vimeo) by @cshawaus (MIT License)
17+
1318
## See also
1419

1520
`readme.txt`` for the WordPress plugin repository readme
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
/*
2-
The "old-school" method of using padding-bottom does not work
3-
in combination with an element that relies on max-width.
4-
5-
This more modern approach works in either context.
6-
*/
7-
lite-youtube {
8-
display: block !important; /* required for layout if JS fails to load */
9-
padding-bottom: 0 !important; /* remove old method of aspect ratio */
10-
aspect-ratio: 16/9 !important;
11-
}
12-
13-
.lite-youtube-fallback {
1+
.lite-embed-fallback {
142
--eeb-fallback-background: #000;
3+
--eeb-focus-outline-color: red;
154

165
display: flex !important;
176
flex-direction: column !important;
@@ -23,42 +12,51 @@ lite-youtube {
2312
padding: 5% !important;
2413
background-color: var(--eeb-fallback-background) !important;
2514
text-decoration: none !important;
26-
transition: opacity 0.15s ease-in-out !important;
2715
}
2816

29-
.lite-youtube-fallback:hover {
17+
.lite-embed-fallback:hover {
3018
text-decoration: underline !important;
3119
}
32-
.lite-youtube-fallback:hover::before {
20+
.lite-embed-fallback:hover::before {
3321
transform: scale(1.1) !important;
3422
}
3523
@media (prefers-reduced-motion: reduce) {
36-
.lite-youtube-fallback:hover::before {
24+
.lite-embed-fallback:hover::before {
3725
transform: none !important;
3826
}
3927
}
4028

41-
.lite-youtube-fallback:focus-visible {
42-
--eeb-focus-outline-color: red;
29+
.lite-embed-fallback:focus-visible {
4330
outline: 2px solid var(--eeb-focus-outline-color) !important;
44-
outline-offset: 3px !important;
31+
outline-offset: -4px !important;
4532
}
4633

47-
.lite-youtube-fallback,
48-
.lite-youtube-fallback:link,
49-
.lite-youtube-fallback:hover,
50-
.lite-youtube-fallback:focus,
51-
.lite-youtube-fallback:active {
34+
.lite-embed-fallback,
35+
.lite-embed-fallback:link,
36+
.lite-embed-fallback:hover,
37+
.lite-embed-fallback:focus,
38+
.lite-embed-fallback:active {
5239
color: #fff !important;
5340
}
5441

55-
.lite-youtube-fallback::before {
42+
.lite-embed-fallback::before {
5643
display: block !important;
5744
content: "" !important;
5845
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 68 48"><path d="M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55c-2.93.78-4.63 3.26-5.42 6.19C.06 13.05 0 24 0 24s.06 10.95 1.48 16.26c.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26z" fill="red"/><path d="M45 24 27 14v20" fill="white"/></svg>')
5946
center / cover no-repeat !important;
6047
width: 68px !important;
6148
height: 48px !important;
6249
aspect-ratio: 94/67 !important;
50+
transform: scale(1) translateZ(0) !important;
6351
transition: 0.15s transform ease-in-out !important;
6452
}
53+
54+
.is-provider-vimeo {
55+
--ebb-focus-outline-color: #17D5FF;
56+
}
57+
58+
.is-provider-vimeo .lite-embed-fallback::before {
59+
background: #17D5ff url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 68 48"><path d="M45 24 27 14v20" fill="white"/></svg>')
60+
center / cover no-repeat !important;
61+
border-radius: 10%;
62+
}

enhanced-embed-block.php

Lines changed: 47 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22
/**
3-
* Plugin Name: Enhanced Embed Block for YouTube
3+
* Plugin Name: Enhanced Embed Block for YouTube & Vimeo
44
* Plugin URI: https://mrwweb.com/wordpress-plugins/enhanced-embed-block/
5-
* Description: Enhance the default YouTube Embed block to load faster.
5+
* Description: Make the default YouTube and Vimeo Embed blocks load faster.
66
* Author: Mark Root-Wiley, MRW Web Design
77
* Author URI: https://MRWweb.com
88
* Text Domain: enhanced-embed-block
9-
* Version: 1.1.0
9+
* Version: 1.2.0
1010
* Requires at least: 6.5
1111
* Requires PHP: 7.4
12+
* GitHub Plugin URI: mrwweb/enhanced-embed-block
13+
* Primary Branch: main
1214
* License: GPLv3 or later
1315
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
1416
*
@@ -17,9 +19,7 @@
1719

1820
namespace EnhancedEmbedBlock;
1921

20-
use WP_HTML_TAG_Processor;
21-
22-
define( 'EEB_VERSION', '1.1.0' );
22+
define( 'EEB_VERSION', '1.2.0' );
2323

2424
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_lite_youtube_component' );
2525
/**
@@ -32,149 +32,68 @@ function enqueue_lite_youtube_component() {
3232
'lite-youtube',
3333
plugins_url( 'vendor/lite-youtube/lite-youtube.js', __FILE__ ),
3434
array(),
35-
'1.5.0',
36-
array( 'in_footer' => true )
35+
'1.8.1',
36+
array( 'async' => true )
3737
);
38-
}
3938

40-
add_action( 'after_setup_theme', __NAMESPACE__ . '\enqueue_embed_block_style' );
41-
function enqueue_embed_block_style() {
42-
wp_enqueue_block_style(
43-
'core/embed',
44-
array(
45-
'handle' => 'lite-youtube-custom',
46-
'src' => plugins_url( 'css/lite-youtube-custom.css', __FILE__ ),
47-
'path' => plugin_dir_path( __FILE__ ) . '/css/lite-youtube-custom.css',
48-
'ver' => EEB_VERSION,
49-
)
39+
wp_register_script_module(
40+
'lite-vimeo',
41+
plugins_url( 'vendor/lite-vimeo/lite-vimeo.js', __FILE__ ),
42+
array(),
43+
'1.0.2',
44+
array( 'async' => true )
45+
);
46+
47+
wp_register_style(
48+
'lite-embed-fallback',
49+
plugins_url( 'css/lite-embed-fallback.css', __FILE__ ),
50+
array(),
51+
EEB_VERSION,
5052
);
5153
}
5254

5355

56+
5457
/* Pre-2020 Blocks */
55-
add_filter( 'render_block_core-embed/youtube', __NAMESPACE__ . '\replace_youtube_embed_with_web_component', 10, 2 );
58+
add_filter( 'render_block_core-embed/youtube', __NAMESPACE__ . '\replace_embeds_with_web_components', 10, 2 );
5659
/* 2020-onward Block */
57-
add_filter( 'render_block_core/embed', __NAMESPACE__ . '\replace_youtube_embed_with_web_component', 10, 2 );
60+
add_filter( 'render_block_core/embed', __NAMESPACE__ . '\replace_embeds_with_web_components', 10, 2 );
5861
/**
5962
* Filter the Embed output and replace it with the web component
6063
*
6164
* @param string $content Block html content.
6265
* @param array $block The block attributes.
6366
* @return string HTML for embed block
6467
*/
65-
function replace_youtube_embed_with_web_component( $content, $block ) {
66-
$isValidYouTube = 'youtube' === $block['attrs']['providerNameSlug'] && isset( $block['attrs']['url'] );
67-
if( ! $isValidYouTube || is_feed() ) {
68-
return $content;
69-
}
70-
71-
$video_id = extract_youtube_id_from_uri( $block['attrs']['url'] );
72-
if ( ! $video_id ) {
68+
function replace_embeds_with_web_components( $content, $block ) {
69+
70+
if (
71+
! isset( $block['attrs']['url'] ) ||
72+
is_feed() ||
73+
! in_array(
74+
$block['attrs']['providerNameSlug'],
75+
array( 'youtube', 'vimeo' ),
76+
true
77+
)
78+
) {
7379
return $content;
7480
}
7581

76-
wp_enqueue_script_module( 'lite-youtube' );
77-
78-
$video_title = extract_title_from_embed_code( $content );
79-
$start_time = extract_start_time_from_uri( $block['attrs']['url'] );
80-
$embed_caption = extract_figcaption_from_embed_code( $content );
81-
82-
/* translators: %s: title from YouTube video */
83-
$play_button = sprintf( __( 'Play: %s', 'enhanced-embed-block' ), $video_title );
84-
85-
/**
86-
* Filter the poster quality for the YouTube preview thumbnail
87-
*
88-
* @since 1.1.0
89-
* @param string $quality One of mqdefault, hqdefault, sddefault, or maxresdefault (default)
90-
*/
91-
$poster_quality = apply_filters( 'eeb_posterquality', 'maxresdefault' );
82+
wp_enqueue_style( 'lite-embed-fallback' );
9283

93-
/**
94-
* Filter to determine whether to load embed from nocookie YouTube domain
95-
*
96-
* @since 1.1.0
97-
* @param bool $use_nocookie Whether to use the nocookie domain (default: true)
98-
*/
99-
$nocookie = apply_filters( 'eeb_nocookie', true );
100-
101-
/* Craft the new output: the web component with HTML fallback link */
102-
$content = sprintf(
103-
'<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube">
104-
<div class="wp-block-embed__wrapper">
105-
<lite-youtube videoid="%1$s" videoplay="%2$s" videoStartAt="%3$d" posterquality="%4$s" posterloading="lazy"%5$s>
106-
<a href="%6$s" class="lite-youtube-fallback" target="_blank" rel="noreferrer noopenner">Watch "%7$s" on YouTube</a>
107-
</lite-youtube>
108-
</div>
109-
%8$s
110-
</figure>',
111-
esc_attr( $video_id ),
112-
esc_attr( $play_button ),
113-
$start_time ? intval( $start_time ) : 0,
114-
in_array( $poster_quality, array( 'mqdefault', 'hqdefault', 'sddefault', 'maxresdefault' ), true ) ? $poster_quality : 'maxresdefault',
115-
$nocookie ? ' nocookie' : '',
116-
esc_url( $block['attrs']['url'] ),
117-
esc_html( $video_title ),
118-
$embed_caption
119-
);
120-
121-
return $content;
122-
}
84+
switch ( $block['attrs']['providerNameSlug'] ) {
85+
case 'youtube':
86+
$content = render_youtube_embed( $content, $block );
87+
break;
12388

124-
/**
125-
* Extract the value of the title attribute from HTML that contains an iframe of an existing YouTube embed code
126-
*
127-
* @param string $html A block of HTML containing a YouTube iframe.
128-
* @return string the title attribute valube
129-
*/
130-
function extract_title_from_embed_code( $html ) {
131-
$processor = new WP_HTML_TAG_Processor( $html );
132-
$processor->next_tag( 'iframe' );
133-
$title = $processor->get_attribute( 'title' );
134-
135-
return $title;
136-
}
137-
138-
/**
139-
* Extract the figcaption from the embed code
140-
*
141-
* @param string $html A block of HTML containing a YouTube iframe.
142-
* @return string The figcaption OR an empty string
143-
*
144-
* @todo Replace this with HTML Tag Processor, if possible
145-
*/
146-
function extract_figcaption_from_embed_code( $html ) {
147-
preg_match( '/<figcaption(.*?)<\/figcaption>/s', $html, $match );
148-
return isset( $match[0] ) ? $match[0] : false;
149-
}
150-
151-
/**
152-
* Get the YouTube video ID from a YouTube video URL, either the default youtube.com one or the shortened youtu.be one
153-
*
154-
* @param string $uri A YouTube video URL.
155-
* @return string video ID for a YouTube video
156-
*/
157-
function extract_youtube_id_from_uri( $uri ) {
158-
$host = wp_parse_url( $uri, PHP_URL_HOST );
159-
160-
/* Handle Shortlinks */
161-
if ( 'youtu.be' === $host ) {
162-
return ltrim( wp_parse_url( $uri, PHP_URL_PATH ), '/' );
89+
case 'vimeo':
90+
$content = render_vimeo_embed( $content, $block );
91+
break;
16392
}
16493

165-
$params = wp_parse_url( $uri, PHP_URL_QUERY );
166-
parse_str( $params, $query );
167-
return $query['v'] ?? false;
94+
return $content;
16895
}
16996

170-
/**
171-
* Extract the start time parameter "t" from YouTube video URL
172-
*
173-
* @param string $uri URL of YouTube video that may or may not contain a start time parameter.
174-
* @return string|bool The value of the t parameter or false if it isn't present
175-
*/
176-
function extract_start_time_from_uri( $uri ) {
177-
$params = wp_parse_url( $uri, PHP_URL_QUERY );
178-
parse_str( $params, $query );
179-
return $query['t'] ?? false;
180-
}
97+
require_once plugin_dir_path( __FILE__ ) . 'inc/generic.php';
98+
require_once plugin_dir_path( __FILE__ ) . 'inc/vimeo.php';
99+
require_once plugin_dir_path( __FILE__ ) . 'inc/youtube.php';

inc/generic.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Generic helper functions
4+
*
5+
* @package Enhanced_Embed_Block
6+
*/
7+
8+
namespace EnhancedEmbedBlock;
9+
10+
use WP_HTML_TAG_Processor;
11+
12+
/**
13+
* Extract the value of the title attribute from HTML that contains an iframe of an existing YouTube embed code
14+
*
15+
* @param string $html A block of HTML containing a YouTube iframe.
16+
* @return string the title attribute valube
17+
*/
18+
function extract_title_from_embed_code( $html ) {
19+
$processor = new WP_HTML_TAG_Processor( $html );
20+
$processor->next_tag( 'iframe' );
21+
$title = $processor->get_attribute( 'title' );
22+
23+
return $title;
24+
}
25+
26+
/**
27+
* Extract the figcaption from the embed code
28+
*
29+
* @param string $html A block of HTML containing a YouTube iframe.
30+
* @return string The figcaption OR an empty string
31+
*
32+
* @todo Replace this with HTML Tag Processor, if possible
33+
*/
34+
function extract_figcaption_from_embed_code( $html ) {
35+
preg_match( '/<figcaption(.*?)<\/figcaption>/s', $html, $match );
36+
return isset( $match[0] ) ? $match[0] : false;
37+
}

0 commit comments

Comments
 (0)