Changeset 2874026
- Timestamp:
- 03/03/2023 02:11:41 AM (3 years ago)
- Location:
- lazy-embed
- Files:
-
- 8 added
- 2 edited
-
tags/1.3.0 (added)
-
tags/1.3.0/index.php (added)
-
tags/1.3.0/lazy-embed.php (added)
-
tags/1.3.0/licence.txt (added)
-
tags/1.3.0/partials (added)
-
tags/1.3.0/partials/embed-styles.css (added)
-
tags/1.3.0/partials/play.svg (added)
-
tags/1.3.0/readme.txt (added)
-
trunk/lazy-embed.php (modified) (5 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lazy-embed/trunk/lazy-embed.php
r2866070 r2874026 7 7 * Plugin URI: https://bitbucket.org/beleaf-au/lazy-embed/ 8 8 * Description: Improves the performance and reduces the emissions of your website by only loading embeds (youtube, vimeo, etc) when they are clicked. 9 * Version: 1. 2.19 * Version: 1.3.0 10 10 * Requires PHP: 7.1 11 11 * Requires at least: 4.0 … … 46 46 } 47 47 48 return self::modifyIframes($content); 49 } 50 51 private static function modifyIframes(string $html): string 52 { 53 return preg_replace_callback('/<iframe[^>]*>/i', [__CLASS__, 'replaceCallback'], $html); 54 55 $dom = new \DOMDocument(); 56 57 // If errors occur its most likely here... 58 $dom->loadHTML($html, LIBXML_NOERROR); 59 60 // Extract the iframes from the html 61 $iframes = $dom->getElementsByTagName('iframe'); 62 63 // The iframes extracted above are passed by reference so we never 64 // have to assign the changed output to anything. Its just done. 65 foreach ($iframes as $iframe) { 66 $provider = self::extractProvider($iframe->getAttribute('src')); 67 68 // Dont modify the iframe if the provider is not supported 69 if (empty($provider)) { 70 continue; 71 } 72 73 // Dont modify the iframe if it is ignored 74 if (self::ignoredIframe($iframe)) { 75 continue; 76 } 77 78 // This will normally add things like autoplay, mute and cleanup the embed 79 $iframeSrc = self::modifyIframeSrc($iframe->getAttribute('src'), $provider); 80 81 // Allow for modification of the iframesrc 82 $iframeSrc = apply_filters('beleaf/lazy-embed/iframesrc', $iframeSrc, $provider); 83 $iframeSrc = apply_filters('beleaf/lazy-embed/iframesrc/' . $provider, $iframeSrc, $provider); 84 85 // Try to pull the image 86 $imageSrc = self::imageSrcFromSrc( 87 $iframeSrc, 88 $provider 89 ); 90 91 // Allow for modification of the image src 92 $imageSrc = apply_filters('beleaf/lazy-embed/imagesrc', $imageSrc, $provider); 93 $imageSrc = apply_filters('beleaf/lazy-embed/imagesrc/' . $provider, $imageSrc, $provider); 94 95 // If the image is empty fallback to the default render 96 if (empty($imageSrc)) { 97 continue; 98 } 99 100 // Update the iframe src with the modified src 101 $iframe->setAttribute('src', $iframeSrc); 102 103 // Add the srcdoc attribute which is the magic source (excuse the pun) 104 $iframe->setAttribute('srcdoc', self::srcdoc([ 105 'iframesrc' => $iframeSrc, 106 'imagesrc' => $imageSrc 107 ])); 108 } 109 110 // Render it and return it 111 return $dom->saveHTML($dom->documentElement); 112 } 113 114 private static function replaceCallback($match):string 48 return self::modifyVideos($content); 49 } 50 51 private static function modifyVideos(string $html): string 52 { 53 $html = preg_replace_callback('/<iframe[^>]*>/i', [__CLASS__, 'iframeReplaceCallback'], $html); 54 $html = preg_replace_callback('/<video[^>]*>/i', [__CLASS__, 'videoReplaceCallback'], $html); 55 56 return $html; 57 } 58 59 private static function iframeReplaceCallback($match): string 115 60 { 116 61 $html = $match[0]; … … 132 77 133 78 // Dont modify the iframe if it is ignored 134 if (self::ignored Iframe($iframe)) {79 if (self::ignoredVideo($iframe)) { 135 80 return $html; 136 81 } … … 170 115 } 171 116 172 private static function ignoredIframe(\DOMElement $iframe): bool 117 private static function videoReplaceCallback($match): string 118 { 119 $html = $match[0]; 120 121 $dom = new \DOMDocument(); 122 // If errors occur its most likely here... 123 $dom->loadHTML($html, LIBXML_NOERROR); 124 125 $videos = $dom->getElementsByTagName('video'); 126 $video = $videos[0]; 127 128 // Dont modify the iframe if it is ignored 129 if (self::ignoredVideo($video)) { 130 return $html; 131 } 132 133 if (str_contains($html, 'preload="')) { 134 if (str_contains($html, 'preload="auto"')) { 135 $html = str_replace('preload="auto"', 'preload="none"', $html); 136 } elseif (str_contains($html, 'preload="metadata"')) { 137 $html = str_replace('preload="metadata"', 'preload="none"', $html); 138 } 139 } else { 140 $html = str_replace('<video', '<video preload="none"', $html); 141 } 142 143 return $html; 144 } 145 146 private static function ignoredVideo(\DOMElement $element): bool 173 147 { 174 148 $needle = 'lazy-embed-ignore'; 175 149 176 150 // Check if the element itself has the ignore class 177 if (self::strContains($needle, $ iframe->getAttribute('class'))) {151 if (self::strContains($needle, $element->getAttribute('class'))) { 178 152 return true; 179 153 } … … 181 155 // Check if the gutenberg figure has the ignore class. This is two 182 156 // layers up from the iframe itself. 183 if (self::strContains($needle, $ iframe->parentNode->parentNode->getAttribute('class'))) {157 if (self::strContains($needle, $element->parentNode->parentNode->getAttribute('class'))) { 184 158 return true; 185 159 } -
lazy-embed/trunk/readme.txt
r2866070 r2874026 4 4 Requires at least: 4.0 5 5 Tested up to: 6.1 6 Stable tag: 1. 2.16 Stable tag: 1.3.0 7 7 Requires PHP: 5.6 8 8 License: GPLv2 or later … … 26 26 27 27 == Changelog == 28 29 = 1.3.0 - 03/03/2023 = 30 Feat: Add support for native video tags 28 31 29 32 = 1.2.1 - 16/02/2023 =
Note: See TracChangeset
for help on using the changeset viewer.