-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcgit-wp-responsive-video.php
More file actions
169 lines (138 loc) · 4.12 KB
/
cgit-wp-responsive-video.php
File metadata and controls
169 lines (138 loc) · 4.12 KB
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
<?php
/*
Plugin Name: Castlegate IT WP Responsive Video
Plugin URI: http://github.com/castlegateit/cgit-wp-responsive-video
Description: Embeds videos responsively when embedding in post content.
Version: 1.6.0
Author: Castlegate IT
Author URI: http://www.castlegateit.co.uk/
License: MIT
*/
/**
* Filter post content and ACF fields
*/
add_filter('the_content', 'cgit_wp_responsive_video_sanitize_html', 20);
add_filter('acf/format_value/type=oembed', 'cgit_wp_responsive_video_sanitize_html', 20);
add_filter('acf/format_value/type=wysiwyg', 'cgit_wp_responsive_video_sanitize_html', 20);
/**
* Sanitize HTML
*
* Sanitize iframe elements identified by a regex pattern instead of DOMDocument
* to avoid modifying non-iframe elements.
*
* @param string $content HTML
* @return string
*/
function cgit_wp_responsive_video_sanitize_html(string $content): string
{
return preg_replace_callback('/<iframe.*?<\/iframe>/i', function ($matches) {
return cgit_wp_responsive_video_sanitize_embed($matches[0]);
}, $content);
}
/**
* Sanitize embed HTML
*
* Set the lazy loading attribute on all iframe elements. Set width, height, and
* aspect ratio styles on all video iframe elements.
*
* @param string $embed Embed HTML
* @return string
*/
function cgit_wp_responsive_video_sanitize_embed(string $embed): string
{
if ($embed === '') {
return '';
}
$document = new DOMDocument();
// Suppress LibXML HTML5 errors
libxml_use_internal_errors(true);
// Add UTF-8 encoding meta tag
$embed = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $embed;
$document->loadHTML($embed, LIBXML_HTML_NODEFDTD | LIBXML_NOERROR | LIBXML_NOWARNING);
// Clear LibXML HTML5 errors
libxml_clear_errors();
foreach ($document->getElementsByTagName('iframe') as $iframe) {
cgit_wp_responsive_video_sanitize_dom_element_iframe($iframe);
}
$body = $document->getElementsByTagName('body')->item(0);
if ($body) {
$embed = '';
foreach ($body->childNodes as $node) {
$embed .= $document->saveHTML($node);
}
}
return $embed;
}
/**
* Sanitize iframe DOMElement
*
* @param DOMElement $element
* @return void
*/
function cgit_wp_responsive_video_sanitize_dom_element_iframe(DOMElement $element): void
{
// Ignore non-iframe elements
if ($element->tagName !== 'iframe') {
return;
}
$style = $element->getAttribute('style');
// Ignore elements that already have a width or aspect ratio
if (str_contains($style, 'width:') || str_contains($style, 'aspect-ratio:')) {
return;
}
$element->setAttribute('loading', 'lazy');
cgit_wp_responsive_video_sanitize_dom_element_iframe_video($element);
}
/**
* Sanitize video iframe DOMElement
*
* If the aspect ratio can be determined, set appropriate width, height, and
* aspect ratio styles.
*
* @param DOMElement $element
* @return void
*/
function cgit_wp_responsive_video_sanitize_dom_element_iframe_video(DOMElement $element): void
{
if (!cgit_wp_responsive_video_is_dom_element_iframe_video($element)) {
return;
}
$width = (int) $element->getAttribute('width');
$height = (int) $element->getAttribute('height');
$style = 'height: auto; width: 100%;';
if ($width && $height) {
$style = "aspect-ratio: $width / $height; $style";
}
$element->setAttribute('style', $style);
}
/**
* Is DOMElement a video iframe?
*
* @param DOMElement $element
* @return bool
*/
function cgit_wp_responsive_video_is_dom_element_iframe_video(DOMElement $element): bool
{
if ($element->tagName !== 'iframe') {
return false;
}
$src = $element->getAttribute('src');
$domain = parse_url($src, PHP_URL_HOST);
if (!is_string($domain)) {
return false;
}
$domain = strtolower($domain);
$video_domains = [
'vimeo.com',
'youtube.com',
];
foreach ($video_domains as $video_domain) {
if (
$domain === $video_domain ||
str_ends_with($domain, '.' . $video_domain)
) {
return true;
}
}
return false;
}