Plugin Directory

Changeset 2874026


Ignore:
Timestamp:
03/03/2023 02:11:41 AM (3 years ago)
Author:
beleaf
Message:

Release 1.3.0

Location:
lazy-embed
Files:
8 added
2 edited

Legend:

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

    r2866070 r2874026  
    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.2.1
     9 * Version: 1.3.0
    1010 * Requires PHP: 7.1
    1111 * Requires at least: 4.0
     
    4646            }
    4747
    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
    11560        {
    11661            $html = $match[0];
     
    13277
    13378            // Dont modify the iframe if it is ignored
    134             if (self::ignoredIframe($iframe)) {
     79            if (self::ignoredVideo($iframe)) {
    13580                return $html;
    13681            }
     
    170115        }
    171116
    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
    173147        {
    174148            $needle = 'lazy-embed-ignore';
    175149
    176150            // 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'))) {
    178152                return true;
    179153            }
     
    181155            // Check if the gutenberg figure has the ignore class. This is two
    182156            // 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'))) {
    184158                return true;
    185159            }
  • lazy-embed/trunk/readme.txt

    r2866070 r2874026  
    44Requires at least: 4.0
    55Tested up to: 6.1
    6 Stable tag: 1.2.1
     6Stable tag: 1.3.0
    77Requires PHP: 5.6
    88License: GPLv2 or later
     
    2626
    2727== Changelog ==
     28
     29= 1.3.0 - 03/03/2023 =
     30Feat: Add support for native video tags
    2831
    2932= 1.2.1 - 16/02/2023 =
Note: See TracChangeset for help on using the changeset viewer.