Plugin Directory

Changeset 3066506


Ignore:
Timestamp:
04/08/2024 03:14:51 AM (23 months ago)
Author:
beleaf
Message:

Release 1.6.0

Location:
lazy-embed
Files:
8 added
2 edited

Legend:

Unmodified
Added
Removed
  • lazy-embed/trunk/lazy-embed.php

    r3053158 r3066506  
    77 * Plugin URI: https://bitbucket.org/beleaf-au/lazy-embed/
    88 * 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.1
     9 * Version: 1.6.0
    1010 * Requires PHP: 7.1
    1111 * Requires at least: 6.2.0
     
    3131        }
    3232
    33         public static function alterBlockIframeClass($html, $block): string
     33        public static function alterBlockIframeClass(string $html, array $block): string
    3434        {
    3535            if ($block['blockName'] === 'core/embed' || $block['blockName'] === 'core/video') {
     
    7373        private static function modifyVideos(string $html): string
    7474        {
    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): string
    82         {
    83             $html = $match[0];
    84 
    8575            $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        {
    8890            // Dont modify the iframe if it is ignored
    8991            if (self::ignoredVideo($processor)) {
    90                 return $html;
    91             }
     92                return $processor;
     93            }
     94
     95            // Extract the video src
     96            $src = $processor->get_attribute('src') ?? '';
    9297
    9398            // Extract the provider
    94             $provider = self::extractProvider($processor->get_attribute('src'));
     99            $provider = self::extractProvider($src);
    95100
    96101            // Extract an image
    97             $imageSrc = self::getImage($processor, $provider, $html);
     102            $imageSrc = self::getImage($processor, $provider);
    98103
    99104            // Dont modify the iframe if there is no image
    100105            if ($imageSrc === '') {
    101                 return $html;
     106                return $processor;
    102107            }
    103108
    104109            // 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);
    106111
    107112            // An empty string for iframeSrc is another nice way to bail out
    108113            if ($iframeSrc === '') {
    109                 return $html;
     114                return $processor;
     115            }
     116
     117            $srcdoc = self::srcdoc($iframeSrc, $imageSrc, $processor);
     118
     119            if ($srcdoc === '') {
     120                return $processor;
    110121            }
    111122
     
    113124            $processor->set_attribute('src', $iframeSrc);
    114125
    115             $srcdoc = self::srcdoc($iframeSrc, $imageSrc, $html);
    116 
    117             if ($srcdoc === '') {
    118                 return $html;
    119             }
    120 
    121126            // Add the srcdoc attribute which is the magic sauce
    122127            $processor->set_attribute('srcdoc', $srcdoc);
    123128
    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        {
    134134            // Dont modify the iframe if it is ignored
    135135            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') ?? '',
    159151                    $provider
    160152                );
     
    162154
    163155            // 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;
    167159        }
    168160
     
    179171        }
    180172
    181         private static function srcdoc(string $iframeSrc, string $imageSrc, string $html): string
     173        private static function srcdoc(string $iframeSrc, string $imageSrc, \WP_HTML_Tag_Processor $processor): string
    182174        {
    183175            // 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);
    186178
    187179            // Construct the srcdoc
     
    192184            $srcdoc .= '</a>';
    193185
    194             return apply_filters('lazy-embed/srcdoc', $srcdoc, $iframeSrc, $imageSrc);
     186            return apply_filters('lazy-embed/srcdoc', $srcdoc, $iframeSrc, $imageSrc, $processor);
    195187        }
    196188
     
    211203        }
    212204
    213         private static function modifyIframeSrc(string $src, string $provider, string $html): string
     205        private static function modifyIframeSrc(string $src, string $provider): string
    214206        {
    215207            if ($provider === 'vimeo') {
     
    233225
    234226            // 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);
    236228
    237229            return $src;
  • lazy-embed/trunk/readme.txt

    r3053158 r3066506  
    44Requires at least: 6.2.0
    55Tested up to: 6.4
    6 Stable tag: 1.5.1
     6Stable tag: 1.6.0
    77Requires PHP: 5.6
    88License: GPLv2 or later
     
    2626
    2727== Changelog ==
     28
     29= 1.6.0 =
     30Feat: Replace regular expressions with WP_HTML_Tag_Processor
     31Fix: A missing src tag on an iframe resulted in a fatal error. This nolonger happens. Thanks Dan
    2832
    2933= 1.5.1 - 18/03/2024 =
Note: See TracChangeset for help on using the changeset viewer.