Changeset 3066506
- Timestamp:
- 04/08/2024 03:14:51 AM (23 months ago)
- Location:
- lazy-embed
- Files:
-
- 8 added
- 2 edited
-
tags/1.5.1 (added)
-
tags/1.5.1/index.php (added)
-
tags/1.5.1/lazy-embed.php (added)
-
tags/1.5.1/licence.txt (added)
-
tags/1.5.1/partials (added)
-
tags/1.5.1/partials/embed-styles.css (added)
-
tags/1.5.1/partials/play.svg (added)
-
tags/1.5.1/readme.txt (added)
-
trunk/lazy-embed.php (modified) (9 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lazy-embed/trunk/lazy-embed.php
r3053158 r3066506 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. 5.19 * Version: 1.6.0 10 10 * Requires PHP: 7.1 11 11 * Requires at least: 6.2.0 … … 31 31 } 32 32 33 public static function alterBlockIframeClass( $html,$block): string33 public static function alterBlockIframeClass(string $html, array $block): string 34 34 { 35 35 if ($block['blockName'] === 'core/embed' || $block['blockName'] === 'core/video') { … … 73 73 private static function modifyVideos(string $html): string 74 74 { 75 $html = preg_replace_callback('/<iframe[^>]*>/i', [__CLASS__, 'iframeReplaceCallback'], $html);76 $html = preg_replace_callback('/<video[^>]*>/i', [__CLASS__, 'videoReplaceCallback'], $html);77 78 return $html;79 }80 81 private static function iframeReplaceCallback(array $match): string82 {83 $html = $match[0];84 85 75 $processor = new \WP_HTML_Tag_Processor($html); 86 $processor->next_tag('iframe'); 87 76 77 while ($processor->next_tag()) { 78 if ($processor->get_tag() === 'IFRAME') { 79 $processor = self::iframeReplaceCallback($processor); 80 } elseif ($processor->get_tag() === 'VIDEO') { 81 $processor = self::videoReplaceCallback($processor); 82 } 83 } 84 85 return $processor->get_updated_html(); 86 } 87 88 private static function iframeReplaceCallback(\WP_HTML_Tag_Processor $processor): \WP_HTML_Tag_Processor 89 { 88 90 // Dont modify the iframe if it is ignored 89 91 if (self::ignoredVideo($processor)) { 90 return $html; 91 } 92 return $processor; 93 } 94 95 // Extract the video src 96 $src = $processor->get_attribute('src') ?? ''; 92 97 93 98 // Extract the provider 94 $provider = self::extractProvider($ processor->get_attribute('src'));99 $provider = self::extractProvider($src); 95 100 96 101 // Extract an image 97 $imageSrc = self::getImage($processor, $provider , $html);102 $imageSrc = self::getImage($processor, $provider); 98 103 99 104 // Dont modify the iframe if there is no image 100 105 if ($imageSrc === '') { 101 return $ html;106 return $processor; 102 107 } 103 108 104 109 // This will normally add things like autoplay, mute, and cleanup the embed 105 $iframeSrc = self::modifyIframeSrc($ processor->get_attribute('src'), $provider, $html);110 $iframeSrc = self::modifyIframeSrc($src, $provider); 106 111 107 112 // An empty string for iframeSrc is another nice way to bail out 108 113 if ($iframeSrc === '') { 109 return $html; 114 return $processor; 115 } 116 117 $srcdoc = self::srcdoc($iframeSrc, $imageSrc, $processor); 118 119 if ($srcdoc === '') { 120 return $processor; 110 121 } 111 122 … … 113 124 $processor->set_attribute('src', $iframeSrc); 114 125 115 $srcdoc = self::srcdoc($iframeSrc, $imageSrc, $html);116 117 if ($srcdoc === '') {118 return $html;119 }120 121 126 // Add the srcdoc attribute which is the magic sauce 122 127 $processor->set_attribute('srcdoc', $srcdoc); 123 128 124 return $processor->get_updated_html(); 125 } 126 127 private static function videoReplaceCallback($match): string 128 { 129 $html = $match[0]; 130 131 $processor = new \WP_HTML_Tag_Processor($html); 132 $processor->next_tag('video'); 133 129 return $processor; 130 } 131 132 private static function videoReplaceCallback(\WP_HTML_Tag_Processor $processor): \WP_HTML_Tag_Processor 133 { 134 134 // Dont modify the iframe if it is ignored 135 135 if (self::ignoredVideo($processor)) { 136 return $html; 137 } 138 139 if (str_contains($html, 'preload="')) { 140 if (str_contains($html, 'preload="auto"')) { 141 $html = str_replace('preload="auto"', 'preload="none"', $html); 142 } elseif (str_contains($html, 'preload="metadata"')) { 143 $html = str_replace('preload="metadata"', 'preload="none"', $html); 144 } 145 } else { 146 $html = str_replace('<video', '<video preload="none"', $html); 147 } 148 149 return $html; 150 } 151 152 private static function getImage(\WP_HTML_Tag_Processor $processor, string $provider, string $html): string 153 { 154 $src = $processor->get_attribute('data-image') ?? ''; 155 156 if ($src === '') { 157 $src = self::imageSrcFromSrc( 158 $processor->get_attribute('src'), 136 return $processor; 137 } 138 139 $processor->set_attribute('preload', 'none'); 140 141 return $processor; 142 } 143 144 private static function getImage(\WP_HTML_Tag_Processor $processor, string $provider): string 145 { 146 $imageURL = $processor->get_attribute('data-image') ?? ''; 147 148 if ($imageURL === '') { 149 $imageURL = self::imageSrcFromSrc( 150 $processor->get_attribute('src') ?? '', 159 151 $provider 160 152 ); … … 162 154 163 155 // Allow for modification of the image src 164 $ src = apply_filters('lazy-embed/imagesrc', $src, $html, $provider);165 166 return $ src;156 $imageURL = apply_filters('lazy-embed/imagesrc', $imageURL, $provider); 157 158 return $imageURL; 167 159 } 168 160 … … 179 171 } 180 172 181 private static function srcdoc(string $iframeSrc, string $imageSrc, string $html): string173 private static function srcdoc(string $iframeSrc, string $imageSrc, \WP_HTML_Tag_Processor $processor): string 182 174 { 183 175 // Pull content for the srcdoc 184 $play = apply_filters('lazy-embed/partial/play', self::partialContent('play.svg'), $ html);185 $css = apply_filters('lazy-embed/partial/css', self::partialContent('embed-styles.css'), $ html);176 $play = apply_filters('lazy-embed/partial/play', self::partialContent('play.svg'), $processor); 177 $css = apply_filters('lazy-embed/partial/css', self::partialContent('embed-styles.css'), $processor); 186 178 187 179 // Construct the srcdoc … … 192 184 $srcdoc .= '</a>'; 193 185 194 return apply_filters('lazy-embed/srcdoc', $srcdoc, $iframeSrc, $imageSrc );186 return apply_filters('lazy-embed/srcdoc', $srcdoc, $iframeSrc, $imageSrc, $processor); 195 187 } 196 188 … … 211 203 } 212 204 213 private static function modifyIframeSrc(string $src, string $provider , string $html): string205 private static function modifyIframeSrc(string $src, string $provider): string 214 206 { 215 207 if ($provider === 'vimeo') { … … 233 225 234 226 // Allow for modification of the iframesrc 235 $src = apply_filters('lazy-embed/iframesrc', $src, $ html, $provider);227 $src = apply_filters('lazy-embed/iframesrc', $src, $provider); 236 228 237 229 return $src; -
lazy-embed/trunk/readme.txt
r3053158 r3066506 4 4 Requires at least: 6.2.0 5 5 Tested up to: 6.4 6 Stable tag: 1. 5.16 Stable tag: 1.6.0 7 7 Requires PHP: 5.6 8 8 License: GPLv2 or later … … 26 26 27 27 == Changelog == 28 29 = 1.6.0 = 30 Feat: Replace regular expressions with WP_HTML_Tag_Processor 31 Fix: A missing src tag on an iframe resulted in a fatal error. This nolonger happens. Thanks Dan 28 32 29 33 = 1.5.1 - 18/03/2024 =
Note: See TracChangeset
for help on using the changeset viewer.