-
Notifications
You must be signed in to change notification settings - Fork 5
/
better-core-video-embeds.php
693 lines (533 loc) · 18.5 KB
/
better-core-video-embeds.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<?php
/*
Plugin Name: Better Core Video Embeds
Description: A plugin which enhances the core video embeds for Youtube and Vimeo videos by not loading unnecessary scripts until they are needed.
Requires at least: 6.0
Requires PHP: 7.0
Version: 1.3.7
Author: Highrise Digital
Author URI: https://highrise.digital/
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: better-core-video-embeds
*/
// define variable for path to this plugin file.
define( 'HD_BCVE_LOCATION', dirname( __FILE__ ) );
define( 'HD_BCVE_LOCATION_URL', plugins_url( '', __FILE__ ) );
define( 'HD_BCVE_VERSION', '1.3.7' );
/**
* Function to run on plugins load.
*/
function hd_bcve_plugins_loaded() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'better-core-video-embeds' );
load_textdomain( 'better-core-video-embeds', WP_LANG_DIR . '/better-core-video-embeds/better-core-video-embeds-' . $locale . '.mo' );
load_plugin_textdomain( 'better-core-video-embeds', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'hd_bcve_plugins_loaded' );
/**
* Enqueues the frontend JS for the plugin.
*/
function hd_bcve_register_enqueue_scripts() {
// register the script for the front end.
wp_register_script(
'better-core-video-embeds-js',
HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.min.js',
false,
HD_BCVE_VERSION,
true
);
}
add_action( 'init', 'hd_bcve_register_enqueue_scripts' );
/**
* Add the front end JS to the core/embed block metadata.
*
* @param array $metadata The block metadata.
* @return array $metadata The block metadata.
*/
function hd_bcve_filter_embed_metadata( $metadata ) {
// if this is the core/embed block.
if ( 'core/embed' === $metadata['name'] ) {
$metadata['viewScript'] = array_merge(
(array) ( $metadata['viewScript'] ?? array() ),
['better-core-video-embeds-js']
);
}
// return the metadata.
return $metadata;
}
add_filter( 'block_type_metadata', 'hd_bcve_filter_embed_metadata' );
/**
* Enqueues the block editor JS for the plugin.
*/
function hd_bcve_enqueue_block_editor_assets() {
// get the assets file.
$asset_file = include( plugin_dir_path( __FILE__ ) . '/build/index.asset.php' );
// enqueue the block editor script.
wp_enqueue_script(
'hd-bcve-block-editor-js',
HD_BCVE_LOCATION_URL . '/build/index.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
}
add_action( 'enqueue_block_editor_assets', 'hd_bcve_enqueue_block_editor_assets' );
/**
* Register a stylesheet for this block.
*/
function hd_bcve_register_block_style() {
// register the block styles.
wp_register_style(
'better-core-video-embeds-styles',
HD_BCVE_LOCATION_URL . '/assets/css/better-core-video-embeds.min.css',
[],
HD_BCVE_VERSION
);
// enqueue the registered block style.
wp_enqueue_block_style(
'core/embed',
[
'handle' => 'better-core-video-embeds-styles',
]
);
}
add_action( 'init', 'hd_bcve_register_block_style' );
/**
* Filters the code embed block output for improved performance on Youtube videos.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*
* @return string $block_content The block content.
*/
function hd_bcve_render_core_embed_block( $block_content, $block, $instance ) {
// if the provider slug name is empty.
if ( empty( $block['attrs']['providerNameSlug'] ) ) {
return $block_content;
}
// if for some reason there is no embed URL.
if ( empty( $block['attrs']['url'] ) ) {
return $block_content;
}
// create a default video id, url and thumbnail url.
$video_id = '';
$thumbnail_url = '';
// if we have a thumbnail ID attribute.
if ( ! empty( $block['attrs']['thumbnailId'] ) ) {
// get the thumbnail URL from the thumbnail ID.
$thumbnail_url = wp_get_attachment_image_url( $block['attrs']['thumbnailId'], 'full' );
}
// grab the video id.
$video_url = $block['attrs']['url'];
$parsed_video_url = parse_url( $video_url );
// switch based on the host.
switch( $parsed_video_url['host'] ) {
// for standard youtube URLs
case 'www.youtube.com':
case 'youtube.com':
if ( empty( $parsed_video_url['query'] ) ) {
// if we have a path.
if ( empty( $parsed_video_url['path'] ) ) {
return $block_content;
}
// remove live/embed path and trailing slash.
$video_id = str_replace( array( 'live/', 'embed/', '/' ), '', $parsed_video_url['path'] );
} else {
// parse the query part of the URL into its arguments.
parse_str( $parsed_video_url['query'], $video_url_query_args );
// if we cannot find a youtube video id.
if ( empty( $video_url_query_args['v'] ) ) {
return $block_content;
}
// set the video id to the v query arg.
$video_id = $video_url_query_args['v'];
}
// if we don't have a custom thumbnail.
if ( empty( $thumbnail_url ) ) {
// get the youtube thumbnail url.
$thumbnail_url = hd_bcve_get_youtube_video_thumbnail_url( $video_id );
}
// break out the switch.
break;
// for youtube short urls.
case 'youtu.be':
// if we have a path.
if ( empty( $parsed_video_url['path'] ) ) {
return $block_content;
}
// remove the preceeding slash.
$video_id = str_replace( '/', '', $parsed_video_url['path'] );
// if we don't have a custom thumbnail.
if ( empty( $thumbnail_url ) ) {
// get the youtube thumbnail url.
$thumbnail_url = hd_bcve_get_youtube_video_thumbnail_url( $video_id );
}
// break out the switch.
break;
// for vimeo urls.
case 'vimeo.com':
case 'www.vimeo.com':
// if we have a path.
if ( empty( $parsed_video_url['path'] ) ) {
return $block_content;
}
// remove the preceeding slash.
$video_id = str_replace( '/', '', $parsed_video_url['path'] );
// if we don't have a custom thumbnail.
if ( empty( $thumbnail_url ) ) {
// get the vimeo thumbnail url for this video.
$thumbnail_url = hd_bcve_get_vimeo_video_thumbnail_url( $video_id );
}
// break out the switch.
break;
// for vimeo urls.
case 'www.dailymotion.com':
case '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'] );
// if we don't have a custom thumbnail.
if ( empty( $thumbnail_url ) ) {
// get the vimeo thumbnail url for this video.
$thumbnail_url = hd_bcve_get_dailymotion_video_thumbnail_url( $video_id );
}
// break out the switch.
break;
}
// if we don't have a video id.
if ( '' === $video_id ) {
return $block_content;
}
// if we don't have a video thumbnail url.
if ( '' === $thumbnail_url ) {
return $block_content;
}
// create an array of classes to add to the placeholder image wrapper.
$wrapper_classes = [
'wp-block-image',
'hd-bcve-wrapper',
'hd-bcve-wrapper-js',
'is--' . $block['attrs']['providerNameSlug'],
];
// if we have classNames on the embed block.
if ( ! empty( $block['attrs']['className'] ) ) {
// explode the className string into array.
$class_names = explode( ' ', $block['attrs']['className'] );
// merge the class names into the figures classes array.
$wrapper_classes = array_merge( $wrapper_classes, $class_names );
}
// if the embed block has an alignment.
if ( ! empty( $block['attrs']['align'] ) ) {
// add the alignment class to the figure classes.
$wrapper_classes[] = 'align' . $block['attrs']['align'];
}
// allow the classes to be filtered.
$wrapper_classes = apply_filters( 'hd_bcve_wrapper_classes', $wrapper_classes, $block, $video_id, $thumbnail_url );
/**
* Lets grab the video caption.
*/
// set a default video caption.
$video_caption = '';
/**
* Perform a regular expression match on the block content to find the caption.
* Should match any figcaption tag with any attributes and any content.
*/
preg_match( '/<figcaption(?:\s+.*?)?>(.*?)<\/figcaption>/s', $block_content, $match );
// if we have a match.
if ( ! empty( $match[1] ) ) {
// set the caption to the text content of the matched element - the video caption.
$video_caption = $match[1];
}
// buffer the output as we need to return not echo.
ob_start();
// output the registered "block" styles for the thubmnail.
wp_print_styles( 'better-core-video-embeds-styles' );
/**
* Fires and action to which the new block markup is added too.
*
* @hooked hd_bvce_open_markup_figure_element - 10
* @hooked hd_bcve_add_video_play_button - 20
* @hooked hd_bcve_add_video_thumbnail_markup - 30
* @hooked hd_bcve_add_video_caption_markup - 35
* @hooked hd_bvce_close_markup_figure_element - 40
* @hooked hd_bcve_add_original_embed_template - 50
*/
do_action( 'hd_bcve_video_thumbnail_markup', $block, $video_id, $thumbnail_url, $wrapper_classes, $video_caption );
// return the new block markup.
return ob_get_clean();
}
add_filter( 'render_block_core/embed', 'hd_bcve_render_core_embed_block', 10, 3 );
/**
* Return the youtube 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_youtube_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 ) {
// set the normal image url.
$image_url = 'https://img.youtube.com/vi/' . esc_attr( $video_id ) . '/mqdefault.jpg';
// check if there is a max res image available.
$max_res_img = wp_remote_get(
'https://img.youtube.com/vi/' . esc_attr( $video_id ) . '/maxresdefault.jpg'
);
// if the request to the hi res image doesn't errors and returns a http 200 response code.
if ( ( ! is_wp_error( $max_res_img ) ) && ( 200 === wp_remote_retrieve_response_code( $max_res_img ) ) ) {
// set the name as max res.
$image_url = 'https://img.youtube.com/vi/' . esc_attr( $video_id ) . '/maxresdefault.jpg';
}
// set the transient, storing the image url.
set_transient( 'hd_bcve_' . $video_id, $image_url, DAY_IN_SECONDS );
}
// return the thumbnail url.
return apply_filters( 'hd_bcve_youtube_video_thumbnail_url', $image_url, $video_id );
}
/**
* Return the vimeo 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_vimeo_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://vimeo.com/api/v2/video/' . esc_attr( $video_id ) . '.json'
);
// 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[0]->thumbnail_large;
// 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 );
}
/**
* 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_dailymotion_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.
*
* @param array An array of HTML elements allowed.
*/
function hd_bcve_allowed_innerblock_html() {
/**
* Return the allowed html
* These are the elements in the rendered embed block for supported videos.
* This also includes everything you can add to an embed caption.
* Therefore we need to allow these to keep the same structure.
*/
return [
'iframe' => [
'src' => true,
'height' => true,
'width' => true,
'frameborder' => true,
'allowfullscreen' => true,
],
'figure' => [
'class' => true,
],
'figcaption' => [
'class' => true,
],
'div' => [
'class' => true,
],
'a' => [
'class' => true,
'href' => true,
'data-type' => true,
],
'strong' => [],
'em' => [],
'sub' => [],
'sup' => [],
's' => [],
'kbd' => [],
'img' => [
'class' => true,
'style' => true,
'src' => true,
'alt' => true,
],
'code' => [],
'mark' => [
'style' => true,
'class' => true,
],
];
}
/**
* Adds the opening figure element to the thumbnail markup.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
*/
function hd_bvce_open_markup_figure_element( $block, $video_id, $thumbnail_url, $wrapper_classes ) {
?>
<figure class="<?php echo esc_attr( implode( ' ', $wrapper_classes ) ); ?>" data-id="<?php echo esc_attr( $video_id ); ?>">
<?php
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bvce_open_markup_figure_element', 10, 4 );
/**
* Adds the play button div to the markup.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
*/
function hd_bcve_add_video_play_button( $block, $video_id, $thumbnail_url, $wrapper_classes ) {
?>
<button class="play-button" aria-label="<?php echo esc_attr_x( 'Play video', 'Video play button', 'better-core-video-embeds' ); ?>"></button>
<?php
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bcve_add_video_play_button', 20, 4 );
/**
* Adds the video thumbnail markup output.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
*/
function hd_bcve_add_video_thumbnail_markup( $block, $video_id, $thumbnail_url, $wrapper_classes ) {
// get the image ID from the URL.
$imageid = attachment_url_to_postid( $thumbnail_url );
// if we have an image ID, therefore the image is a local image.
if ( ! empty( $imageid ) ) {
// output the image.
echo wp_get_attachment_image(
$imageid,
'full',
false,
[
'class' => 'hd-bcve-thumbnail',
'loading' => 'lazy',
]
);
} else {
?>
<img loading="lazy" class="hd-bcve-thumbnail" alt="" src="<?php echo esc_url( $thumbnail_url ); ?>" />
<?php
}
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bcve_add_video_thumbnail_markup', 30, 4 );
/**
* Adds the video thumbnail markup output.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
* @param string $video_caption The caption of the video or an empty string.
*/
function hd_bcve_add_video_caption_markup( $block, $video_id, $thumbnail_url, $wrapper_classes, $video_caption ) {
// if we have no caption available.
if ( '' === $video_caption ) {
return;
}
?>
<figcaption class="wp-element-caption">
<?php echo wp_kses( $video_caption, hd_bcve_allowed_innerblock_html() ); ?>
</figcaption>
<?php
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bcve_add_video_caption_markup', 35, 5 );
/**
* Adds the closing figure element to the thumbnail markup.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
*/
function hd_bcve_close_markup_figure_element( $block, $video_id, $thumbnail_url, $wrapper_classes ) {
?>
</figure>
<?php
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bcve_close_markup_figure_element', 40, 4 );
/**
* Adds the original block markup to the template element.
* This is used when the item is cloned when the thumbnail is clicked.
*
* @param array $block The block array.
* @param string $video_id The ID of the embedded video.
* @param string $thumbnail_url The URL of the video thumbnail.
* @param array $wrapper_classes An array of CSS classes to add to the wrapper.
*/
function hd_bcve_add_original_embed_template( $block, $video_id, $thumbnail_url, $wrapper_classes ) {
?>
<template id="hd-bcve-embed-html-<?php echo esc_attr( $video_id ); ?>">
<?php echo wp_kses( $block['innerHTML'], hd_bcve_allowed_innerblock_html() ); ?>
</template>
<?php
}
add_action( 'hd_bcve_video_thumbnail_markup', 'hd_bcve_add_original_embed_template', 50, 4 );