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 *
1719
1820namespace EnhancedEmbedBlock ;
1921
20- use WP_HTML_TAG_Processor ;
21-
22- define ( 'EEB_VERSION ' , '1.1.0 ' );
22+ define ( 'EEB_VERSION ' , '1.2.0 ' );
2323
2424add_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 ' ;
0 commit comments