From d9ed7df4851a4056636aafac655b6e343e7560bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Mon, 4 Nov 2019 18:55:35 -0500 Subject: [PATCH 01/17] Add filter for enabling native lazyload (#106) --- composer.lock | 193 +++++++++++---------- phpcs.xml | 8 +- rocket-lazy-load.php | 2 +- src/Dependencies/RocketLazyload/Assets.php | 16 +- src/Dependencies/RocketLazyload/Iframe.php | 36 +++- src/Dependencies/RocketLazyload/Image.php | 13 +- src/Subscriber/LazyloadSubscriber.php | 27 ++- 7 files changed, 174 insertions(+), 121 deletions(-) diff --git a/composer.lock b/composer.lock index 0bb699a..7240cb6 100644 --- a/composer.lock +++ b/composer.lock @@ -1,7 +1,7 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], "content-hash": "ad8bf18ef0f69ab2d4fbfa356dcb019f", @@ -275,16 +275,16 @@ }, { "name": "wp-media/rocket-lazyload-common", - "version": "v2.5.2", + "version": "v2.5.4", "source": { "type": "git", "url": "https://github.com/wp-media/rocket-lazyload-common.git", - "reference": "37ca967029f6f86a207a9ae128c9e96bc95219d7" + "reference": "e5ad49418784776e54d181791522c0fa6bf172a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/37ca967029f6f86a207a9ae128c9e96bc95219d7", - "reference": "37ca967029f6f86a207a9ae128c9e96bc95219d7", + "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/e5ad49418784776e54d181791522c0fa6bf172a4", + "reference": "e5ad49418784776e54d181791522c0fa6bf172a4", "shasum": "" }, "require": { @@ -319,27 +319,30 @@ } ], "description": "Common Code between WP Rocket and Lazyload by WP Rocket", - "time": "2019-09-07T12:37:44+00:00" + "time": "2019-10-28T15:10:39+00:00" } ], "packages-dev": [ { "name": "antecedent/patchwork", - "version": "2.1.9", + "version": "2.1.11", "source": { "type": "git", "url": "https://github.com/antecedent/patchwork.git", - "reference": "1229bb22283177e196b572120de0772d9ee9baac" + "reference": "ff7aae02f1c5492716fe13d59de4cfc389b8c4b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/antecedent/patchwork/zipball/1229bb22283177e196b572120de0772d9ee9baac", - "reference": "1229bb22283177e196b572120de0772d9ee9baac", + "url": "https://api.github.com/repos/antecedent/patchwork/zipball/ff7aae02f1c5492716fe13d59de4cfc389b8c4b0", + "reference": "ff7aae02f1c5492716fe13d59de4cfc389b8c4b0", "shasum": "" }, "require": { "php": ">=5.4.0" }, + "require-dev": { + "phpunit/phpunit": ">=4" + }, "type": "library", "notification-url": "https://packagist.org/downloads/", "license": [ @@ -362,7 +365,7 @@ "runkit", "testing" ], - "time": "2019-08-18T15:18:18+00:00" + "time": "2019-10-26T07:10:56+00:00" }, { "name": "brain/monkey", @@ -646,26 +649,26 @@ }, { "name": "league/flysystem", - "version": "1.0.46", + "version": "1.0.57", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "f3e0d925c18b92cf3ce84ea5cc58d62a1762a2b2" + "reference": "0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/f3e0d925c18b92cf3ce84ea5cc58d62a1762a2b2", - "reference": "f3e0d925c18b92cf3ce84ea5cc58d62a1762a2b2", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a", + "reference": "0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a", "shasum": "" }, "require": { + "ext-fileinfo": "*", "php": ">=5.5.9" }, "conflict": { "league/flysystem-sftp": "<1.0.6" }, "require-dev": { - "ext-fileinfo": "*", "phpspec/phpspec": "^3.4", "phpunit/phpunit": "^5.7.10" }, @@ -726,20 +729,20 @@ "sftp", "storage" ], - "time": "2018-08-22T07:45:22+00:00" + "time": "2019-10-16T21:01:05+00:00" }, { "name": "mockery/mockery", - "version": "1.2.3", + "version": "1.2.4", "source": { "type": "git", "url": "https://github.com/mockery/mockery.git", - "reference": "4eff936d83eb809bde2c57a3cea0ee9643769031" + "reference": "b3453f75fd23d9fd41685f2148f4abeacabc6405" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mockery/mockery/zipball/4eff936d83eb809bde2c57a3cea0ee9643769031", - "reference": "4eff936d83eb809bde2c57a3cea0ee9643769031", + "url": "https://api.github.com/repos/mockery/mockery/zipball/b3453f75fd23d9fd41685f2148f4abeacabc6405", + "reference": "b3453f75fd23d9fd41685f2148f4abeacabc6405", "shasum": "" }, "require": { @@ -753,7 +756,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.2.x-dev" } }, "autoload": { @@ -791,7 +794,7 @@ "test double", "testing" ], - "time": "2019-08-07T15:01:07+00:00" + "time": "2019-09-30T08:30:27+00:00" }, { "name": "myclabs/deep-copy", @@ -843,16 +846,16 @@ }, { "name": "phpcompatibility/php-compatibility", - "version": "9.3.1", + "version": "9.3.2", "source": { "type": "git", "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", - "reference": "9999344e47e7af6b00e1a898eacc4e4368fb7196" + "reference": "bfca2be3992f40e92206e5a7ebe5eaee37280b58" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9999344e47e7af6b00e1a898eacc4e4368fb7196", - "reference": "9999344e47e7af6b00e1a898eacc4e4368fb7196", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/bfca2be3992f40e92206e5a7ebe5eaee37280b58", + "reference": "bfca2be3992f40e92206e5a7ebe5eaee37280b58", "shasum": "" }, "require": { @@ -897,20 +900,20 @@ "phpcs", "standards" ], - "time": "2019-09-05T18:36:49+00:00" + "time": "2019-10-16T21:24:24+00:00" }, { "name": "phpcompatibility/phpcompatibility-paragonie", - "version": "1.1.0", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", - "reference": "b1bb79a7cab1fb856b56f1b5cf110b6e52d8e936" + "reference": "94b2388c4fe99e9e2ef0772e280fa0eafa1d0603" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/b1bb79a7cab1fb856b56f1b5cf110b6e52d8e936", - "reference": "b1bb79a7cab1fb856b56f1b5cf110b6e52d8e936", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/94b2388c4fe99e9e2ef0772e280fa0eafa1d0603", + "reference": "94b2388c4fe99e9e2ef0772e280fa0eafa1d0603", "shasum": "" }, "require": { @@ -949,7 +952,7 @@ "polyfill", "standards" ], - "time": "2019-08-28T15:58:19+00:00" + "time": "2019-10-16T21:41:26+00:00" }, { "name": "phpcompatibility/phpcompatibility-wp", @@ -1003,35 +1006,33 @@ }, { "name": "phpdocumentor/reflection-common", - "version": "1.0.1", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", - "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/63a995caa1ca9e5590304cd845c15ad6d482a62a", + "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a", "shasum": "" }, "require": { - "php": ">=5.5" + "php": ">=7.1" }, "require-dev": { - "phpunit/phpunit": "^4.6" + "phpunit/phpunit": "~6" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.x-dev" } }, "autoload": { "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src" - ] + "phpDocumentor\\Reflection\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -1053,30 +1054,30 @@ "reflection", "static analysis" ], - "time": "2017-09-11T18:02:19+00:00" + "time": "2018-08-07T13:53:10+00:00" }, { "name": "phpdocumentor/reflection-docblock", - "version": "4.3.1", + "version": "4.3.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c" + "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c", - "reference": "bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/b83ff7cfcfee7827e1e78b637a5904fe6a96698e", + "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e", "shasum": "" }, "require": { "php": "^7.0", - "phpdocumentor/reflection-common": "^1.0.0", - "phpdocumentor/type-resolver": "^0.4.0", + "phpdocumentor/reflection-common": "^1.0.0 || ^2.0.0", + "phpdocumentor/type-resolver": "~0.4 || ^1.0.0", "webmozart/assert": "^1.0" }, "require-dev": { - "doctrine/instantiator": "~1.0.5", + "doctrine/instantiator": "^1.0.5", "mockery/mockery": "^1.0", "phpunit/phpunit": "^6.4" }, @@ -1104,41 +1105,40 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2019-04-30T17:48:53+00:00" + "time": "2019-09-12T14:27:41+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "0.4.0", + "version": "1.0.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7" + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9c977708995954784726e25d0cd1dddf4e65b0f7", - "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", "shasum": "" }, "require": { - "php": "^5.5 || ^7.0", - "phpdocumentor/reflection-common": "^1.0" + "php": "^7.1", + "phpdocumentor/reflection-common": "^2.0" }, "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^5.2||^4.8.24" + "ext-tokenizer": "^7.1", + "mockery/mockery": "~1", + "phpunit/phpunit": "^7.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.x-dev" } }, "autoload": { "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] + "phpDocumentor\\Reflection\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -1151,26 +1151,27 @@ "email": "me@mikevanriel.com" } ], - "time": "2017-07-14T14:27:02+00:00" + "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "time": "2019-08-22T18:11:29+00:00" }, { "name": "phpspec/prophecy", - "version": "1.8.1", + "version": "1.9.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "1927e75f4ed19131ec9bcc3b002e07fb1173ee76" + "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/1927e75f4ed19131ec9bcc3b002e07fb1173ee76", - "reference": "1927e75f4ed19131ec9bcc3b002e07fb1173ee76", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/f6811d96d97bdf400077a0cc100ae56aa32b9203", + "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", "php": "^5.3|^7.0", - "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0|^5.0", "sebastian/comparator": "^1.1|^2.0|^3.0", "sebastian/recursion-context": "^1.0|^2.0|^3.0" }, @@ -1214,7 +1215,7 @@ "spy", "stub" ], - "time": "2019-06-13T12:50:23+00:00" + "time": "2019-10-03T11:07:50+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2122,16 +2123,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.4.2", + "version": "3.5.2", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" + "reference": "65b12cdeaaa6cd276d4c3033a95b9b88b12701e7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", - "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/65b12cdeaaa6cd276d4c3033a95b9b88b12701e7", + "reference": "65b12cdeaaa6cd276d4c3033a95b9b88b12701e7", "shasum": "" }, "require": { @@ -2169,20 +2170,20 @@ "phpcs", "standards" ], - "time": "2019-04-10T23:49:02+00:00" + "time": "2019-10-28T04:36:32+00:00" }, { "name": "symfony/console", - "version": "v4.3.4", + "version": "v4.3.5", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "de63799239b3881b8a08f8481b22348f77ed7b36" + "reference": "929ddf360d401b958f611d44e726094ab46a7369" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/de63799239b3881b8a08f8481b22348f77ed7b36", - "reference": "de63799239b3881b8a08f8481b22348f77ed7b36", + "url": "https://api.github.com/repos/symfony/console/zipball/929ddf360d401b958f611d44e726094ab46a7369", + "reference": "929ddf360d401b958f611d44e726094ab46a7369", "shasum": "" }, "require": { @@ -2244,20 +2245,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2019-08-26T08:26:39+00:00" + "time": "2019-10-07T12:36:49+00:00" }, { "name": "symfony/finder", - "version": "v4.3.4", + "version": "v4.3.5", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "86c1c929f0a4b24812e1eb109262fc3372c8e9f2" + "reference": "5e575faa95548d0586f6bedaeabec259714e44d1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/86c1c929f0a4b24812e1eb109262fc3372c8e9f2", - "reference": "86c1c929f0a4b24812e1eb109262fc3372c8e9f2", + "url": "https://api.github.com/repos/symfony/finder/zipball/5e575faa95548d0586f6bedaeabec259714e44d1", + "reference": "5e575faa95548d0586f6bedaeabec259714e44d1", "shasum": "" }, "require": { @@ -2293,7 +2294,7 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2019-08-14T12:26:46+00:00" + "time": "2019-09-16T11:29:48+00:00" }, { "name": "symfony/polyfill-ctype", @@ -2472,16 +2473,16 @@ }, { "name": "symfony/service-contracts", - "version": "v1.1.6", + "version": "v1.1.7", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3" + "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ea7263d6b6d5f798b56a45a5b8d686725f2719a3", - "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ffcde9615dc5bb4825b9f6aed07716f1f57faae0", + "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0", "shasum": "" }, "require": { @@ -2526,20 +2527,20 @@ "interoperability", "standards" ], - "time": "2019-08-20T14:44:19+00:00" + "time": "2019-09-17T11:12:18+00:00" }, { "name": "symfony/yaml", - "version": "v4.3.4", + "version": "v4.3.5", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686" + "reference": "41e16350a2a1c7383c4735aa2f9fce74cf3d1178" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", - "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", + "url": "https://api.github.com/repos/symfony/yaml/zipball/41e16350a2a1c7383c4735aa2f9fce74cf3d1178", + "reference": "41e16350a2a1c7383c4735aa2f9fce74cf3d1178", "shasum": "" }, "require": { @@ -2585,7 +2586,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2019-08-20T14:27:59+00:00" + "time": "2019-09-11T15:41:19+00:00" }, { "name": "webmozart/assert", diff --git a/phpcs.xml b/phpcs.xml index 82d2042..336246e 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -3,9 +3,9 @@ Rocket LazyLoad . - - - + + + *.js /vendor/* @@ -15,7 +15,7 @@ *\.php$ - + diff --git a/rocket-lazy-load.php b/rocket-lazy-load.php index f2c7dd9..a785174 100644 --- a/rocket-lazy-load.php +++ b/rocket-lazy-load.php @@ -30,7 +30,7 @@ defined('ABSPATH') || die('Cheatin\' uh?'); define('ROCKET_LL_VERSION', '2.3.2'); -define('ROCKET_LL_WP_VERSION', '4.7'); +define('ROCKET_LL_WP_VERSION', '4.9'); define('ROCKET_LL_PHP_VERSION', '5.6'); define('ROCKET_LL_BASENAME', plugin_basename(__FILE__)); define('ROCKET_LL_PATH', realpath(plugin_dir_path(__FILE__)) . '/'); diff --git a/src/Dependencies/RocketLazyload/Assets.php b/src/Dependencies/RocketLazyload/Assets.php index ca600a9..6f93765 100644 --- a/src/Dependencies/RocketLazyload/Assets.php +++ b/src/Dependencies/RocketLazyload/Assets.php @@ -91,11 +91,11 @@ class_loaded: "lazyloaded", } $script .= '};'; - + $script .= ' window.addEventListener(\'LazyLoad::Initialized\', function (e) { var lazyLoadInstance = e.detail.instance; - + if (window.MutationObserver) { var observer = new MutationObserver(function(mutations) { var image_count = 0; @@ -121,7 +121,7 @@ class_loaded: "lazyloaded", image_count += images.length; iframe_count += iframes.length; rocketlazy_count += rocket_lazy.length; - + if(is_image){ image_count += 1; } @@ -136,10 +136,10 @@ class_loaded: "lazyloaded", lazyLoadInstance.update(); } } ); - + var b = document.getElementsByTagName("body")[0]; var config = { childList: true, subtree: true }; - + observer.observe(b, config); } }, false);'; @@ -222,7 +222,7 @@ public function getYoutubeThumbnailScript($args = []) 'width' => 640, 'height' => 480, ], - + 'maxresdefault' => [ 'width' => 1280, 'height' => 720, @@ -239,7 +239,7 @@ public function getYoutubeThumbnailScript($args = []) $image = ''; } - return ""; + return ""; } /** @@ -271,7 +271,7 @@ public function getYoutubeThumbnailCSS($args = []) $args = wp_parse_args($args, $defaults); $css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . $args['base_url'] . 'img/youtube.png) no-repeat;cursor:pointer}'; - + if ($args['responsive_embeds']) { $css .= '.wp-has-aspect-ratio .rll-youtube-player{position:absolute;padding-bottom:0;width:100%;height:100%;top:0;bottom:0;left:0;right:0}'; } diff --git a/src/Dependencies/RocketLazyload/Iframe.php b/src/Dependencies/RocketLazyload/Iframe.php index 9180860..400433b 100644 --- a/src/Dependencies/RocketLazyload/Iframe.php +++ b/src/Dependencies/RocketLazyload/Iframe.php @@ -164,6 +164,8 @@ private function replaceYoutubeThumbnail($iframe) $query = wp_parse_url(htmlspecialchars_decode($iframe['src']), PHP_URL_QUERY); + $youtube_url = $this->changeYoutubeUrlForYoutuDotBe( $iframe['src'] ); + $youtube_url = $this->cleanYoutubeUrl( $iframe['src'] ); /** * Filter the LazyLoad HTML output on Youtube iframes * @@ -171,7 +173,7 @@ private function replaceYoutubeThumbnail($iframe) * * @param array $html Output that will be printed. */ - $youtube_lazyload = apply_filters('rocket_lazyload_youtube_html', '
'); + $youtube_lazyload = apply_filters('rocket_lazyload_youtube_html', '
'); $youtube_lazyload .= ''; return $youtube_lazyload; @@ -199,4 +201,36 @@ public function getYoutubeIDFromURL($url) return $matches[1]; } + + /** + * Changes URL youtu.be/ID to youtube.com/embed/ID + * + * @param string $url URL to replace. + * @return string Unchanged URL or modified URL. + */ + public function changeYoutubeUrlForYoutuDotBe( $url ) { + $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; + $result = preg_match( $pattern, $url, $matches ); + + if ( ! $result ) { + return $url; + } + + return 'https://www.youtube.com/embed/' . $matches[1]; + } + + /** + * Cleans Youtube URL. Keeps only scheme, host and path. + * + * @param string $url URL to be cleaned. + * @return string Cleaned URL + */ + public function cleanYoutubeUrl ( $url ) { + $parsed_url = wp_parse_url( $url, -1 ); + $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; + $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; + $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; + + return $scheme . $host . $path; + } } diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index bda24b4..068c0f2 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -29,7 +29,7 @@ public function lazyloadImages($html, $buffer) foreach ($images as $image) { $image = $this->canLazyload($image); - + if (! $image) { continue; } @@ -65,7 +65,7 @@ public function lazyloadBackgroundImages($html, $buffer) if (! preg_match('#background-image\s*:\s*(?\s*url\s*\((?[^)]+)\))\s*;?#is', $element['styles'], $url)) { continue; } - + $url['url'] = trim($url['url'], '\'" '); if ($this->isExcluded($url['url'], $this->getExcludedSrc())) { @@ -218,7 +218,7 @@ public function isExcluded($string, $excluded_values) return true; } } - + return false; } @@ -311,7 +311,8 @@ private function replaceImage($image) $image_lazyload = str_replace($image['atts'], $placeholder_atts . ' data-lazy-src="' . $image['src'] . '"', $image[0]); - if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload)) { + // this filter is documented in src/Subscriber/LazyloadSubscriber.php. + if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload) && apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals $image_lazyload = str_replace(' $threshold, - 'options' => [ - 'use_native' => 'true', - ], ]; + /** + * Filters the use of native lazyload + * + * @since 2.3.3 + * @param bool $use_native True to enable native lazyload usage. + */ + if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $inline_args['options'] = [ + 'use_native' => 'true', + ]; + } + if ($this->option_array->get('images') || $this->option_array->get('iframes')) { - $inline_args['elements']['loading'] = '[loading=lazy]'; + // this filter is documented in src/Subscriber/LazyloadSubscriber.php. + if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $inline_args['elements']['loading'] = '[loading=lazy]'; + } } if ($this->option_array->get('images')) { + $inline_args['elements']['image'] = 'img[data-lazy-src]'; $inline_args['elements']['background_image'] = '.rocket-lazyload'; } + if ($this->option_array->get('iframes')) { + $inline_args['elements']['iframe'] = 'iframe[data-lazy-src]'; + } + /** * Filters the arguments array for the lazyload script options * @@ -276,7 +293,7 @@ private function shouldLazyload() * * @param bool $do_rocket_lazyload True to apply lazyload, false otherwise. */ - if (! apply_filters('do_rocket_lazyload', true)) { // WPCS: prefix ok. + if (! apply_filters('do_rocket_lazyload', true)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals return false; } From 495a112efcb2793cf1dfa35d1a9555d50bbb6ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 21 Feb 2020 15:25:57 -0500 Subject: [PATCH 02/17] update dependency packages --- composer.lock | 316 +++++++++++---------- src/Dependencies/RocketLazyload/Iframe.php | 4 +- src/Dependencies/RocketLazyload/Image.php | 14 +- 3 files changed, 173 insertions(+), 161 deletions(-) diff --git a/composer.lock b/composer.lock index 7240cb6..0477deb 100644 --- a/composer.lock +++ b/composer.lock @@ -1,23 +1,23 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], "content-hash": "ad8bf18ef0f69ab2d4fbfa356dcb019f", "packages": [ { "name": "composer/installers", - "version": "v1.7.0", + "version": "v1.8.0", "source": { "type": "git", "url": "https://github.com/composer/installers.git", - "reference": "141b272484481432cda342727a427dc1e206bfa0" + "reference": "7d610d50aae61ae7ed6675e58efeabdf279bb5e3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/installers/zipball/141b272484481432cda342727a427dc1e206bfa0", - "reference": "141b272484481432cda342727a427dc1e206bfa0", + "url": "https://api.github.com/repos/composer/installers/zipball/7d610d50aae61ae7ed6675e58efeabdf279bb5e3", + "reference": "7d610d50aae61ae7ed6675e58efeabdf279bb5e3", "shasum": "" }, "require": { @@ -65,6 +65,7 @@ "Kanboard", "Lan Management System", "MODX Evo", + "MantisBT", "Mautic", "Maya", "OXID", @@ -119,6 +120,7 @@ "shopware", "silverstripe", "sydes", + "sylius", "symfony", "typo3", "wordpress", @@ -126,7 +128,7 @@ "zend", "zikula" ], - "time": "2019-08-12T15:00:31+00:00" + "time": "2020-02-07T10:39:20+00:00" }, { "name": "container-interop/container-interop", @@ -157,6 +159,7 @@ ], "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", "homepage": "https://github.com/container-interop/container-interop", + "abandoned": "psr/container", "time": "2017-02-14T19:40:03+00:00" }, { @@ -275,16 +278,16 @@ }, { "name": "wp-media/rocket-lazyload-common", - "version": "v2.5.4", + "version": "v2.5.6", "source": { "type": "git", "url": "https://github.com/wp-media/rocket-lazyload-common.git", - "reference": "e5ad49418784776e54d181791522c0fa6bf172a4" + "reference": "1ea0cf2ae5d302a0214e05176c1dda321b1d6690" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/e5ad49418784776e54d181791522c0fa6bf172a4", - "reference": "e5ad49418784776e54d181791522c0fa6bf172a4", + "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/1ea0cf2ae5d302a0214e05176c1dda321b1d6690", + "reference": "1ea0cf2ae5d302a0214e05176c1dda321b1d6690", "shasum": "" }, "require": { @@ -319,22 +322,22 @@ } ], "description": "Common Code between WP Rocket and Lazyload by WP Rocket", - "time": "2019-10-28T15:10:39+00:00" + "time": "2020-01-16T13:46:49+00:00" } ], "packages-dev": [ { "name": "antecedent/patchwork", - "version": "2.1.11", + "version": "2.1.12", "source": { "type": "git", "url": "https://github.com/antecedent/patchwork.git", - "reference": "ff7aae02f1c5492716fe13d59de4cfc389b8c4b0" + "reference": "b98e046dd4c0acc34a0846604f06f6111654d9ea" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/antecedent/patchwork/zipball/ff7aae02f1c5492716fe13d59de4cfc389b8c4b0", - "reference": "ff7aae02f1c5492716fe13d59de4cfc389b8c4b0", + "url": "https://api.github.com/repos/antecedent/patchwork/zipball/b98e046dd4c0acc34a0846604f06f6111654d9ea", + "reference": "b98e046dd4c0acc34a0846604f06f6111654d9ea", "shasum": "" }, "require": { @@ -365,20 +368,20 @@ "runkit", "testing" ], - "time": "2019-10-26T07:10:56+00:00" + "time": "2019-12-22T17:52:09+00:00" }, { "name": "brain/monkey", - "version": "2.3.1", + "version": "2.4.0", "source": { "type": "git", "url": "https://github.com/Brain-WP/BrainMonkey.git", - "reference": "74cdccad5dbb7edc59f59e1ad6ef392f83a34776" + "reference": "b3ce8b619c26db6abd01b9dcfd6a2c0254060956" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Brain-WP/BrainMonkey/zipball/74cdccad5dbb7edc59f59e1ad6ef392f83a34776", - "reference": "74cdccad5dbb7edc59f59e1ad6ef392f83a34776", + "url": "https://api.github.com/repos/Brain-WP/BrainMonkey/zipball/b3ce8b619c26db6abd01b9dcfd6a2c0254060956", + "reference": "b3ce8b619c26db6abd01b9dcfd6a2c0254060956", "shasum": "" }, "require": { @@ -387,7 +390,9 @@ "php": ">=5.6.0" }, "require-dev": { - "phpunit/phpunit": "~5.7.9" + "dealerdirect/phpcodesniffer-composer-installer": "^0.4", + "phpcompatibility/php-compatibility": "^9.3.0", + "phpunit/phpunit": "^5.7.9 || ^6.0 || ^7.0" }, "type": "library", "extra": { @@ -411,9 +416,9 @@ "authors": [ { "name": "Giuseppe Mazzapica", - "role": "Developer", "email": "giuseppe.mazzapica@gmail.com", - "homepage": "https://gmazzap.me" + "homepage": "https://gmazzap.me", + "role": "Developer" } ], "description": "Mocking utility for PHP functions and WordPress plugin API", @@ -429,7 +434,7 @@ "test", "testing" ], - "time": "2019-07-26T19:00:28+00:00" + "time": "2019-11-24T16:03:21+00:00" }, { "name": "coenjacobs/mozart", @@ -545,16 +550,16 @@ }, { "name": "doctrine/instantiator", - "version": "1.2.0", + "version": "1.3.0", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "a2c590166b2133a4633738648b6b064edae0814a" + "reference": "ae466f726242e637cebdd526a7d991b9433bacf1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/a2c590166b2133a4633738648b6b064edae0814a", - "reference": "a2c590166b2133a4633738648b6b064edae0814a", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/ae466f726242e637cebdd526a7d991b9433bacf1", + "reference": "ae466f726242e637cebdd526a7d991b9433bacf1", "shasum": "" }, "require": { @@ -597,7 +602,7 @@ "constructor", "instantiate" ], - "time": "2019-03-17T17:37:11+00:00" + "time": "2019-10-21T16:45:58+00:00" }, { "name": "hamcrest/hamcrest-php", @@ -649,16 +654,16 @@ }, { "name": "league/flysystem", - "version": "1.0.57", + "version": "1.0.64", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a" + "reference": "d13c43dbd4b791f815215959105a008515d1a2e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a", - "reference": "0e9db7f0b96b9f12dcf6f65bc34b72b1a30ea55a", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/d13c43dbd4b791f815215959105a008515d1a2e0", + "reference": "d13c43dbd4b791f815215959105a008515d1a2e0", "shasum": "" }, "require": { @@ -670,7 +675,7 @@ }, "require-dev": { "phpspec/phpspec": "^3.4", - "phpunit/phpunit": "^5.7.10" + "phpunit/phpunit": "^5.7.26" }, "suggest": { "ext-fileinfo": "Required for MimeType", @@ -729,20 +734,20 @@ "sftp", "storage" ], - "time": "2019-10-16T21:01:05+00:00" + "time": "2020-02-05T18:14:17+00:00" }, { "name": "mockery/mockery", - "version": "1.2.4", + "version": "1.3.1", "source": { "type": "git", "url": "https://github.com/mockery/mockery.git", - "reference": "b3453f75fd23d9fd41685f2148f4abeacabc6405" + "reference": "f69bbde7d7a75d6b2862d9ca8fab1cd28014b4be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mockery/mockery/zipball/b3453f75fd23d9fd41685f2148f4abeacabc6405", - "reference": "b3453f75fd23d9fd41685f2148f4abeacabc6405", + "url": "https://api.github.com/repos/mockery/mockery/zipball/f69bbde7d7a75d6b2862d9ca8fab1cd28014b4be", + "reference": "f69bbde7d7a75d6b2862d9ca8fab1cd28014b4be", "shasum": "" }, "require": { @@ -756,7 +761,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2.x-dev" + "dev-master": "1.3.x-dev" } }, "autoload": { @@ -794,20 +799,20 @@ "test double", "testing" ], - "time": "2019-09-30T08:30:27+00:00" + "time": "2019-12-26T09:49:15+00:00" }, { "name": "myclabs/deep-copy", - "version": "1.9.3", + "version": "1.9.5", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "007c053ae6f31bba39dfa19a7726f56e9763bbea" + "reference": "b2c28789e80a97badd14145fda39b545d83ca3ef" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/007c053ae6f31bba39dfa19a7726f56e9763bbea", - "reference": "007c053ae6f31bba39dfa19a7726f56e9763bbea", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/b2c28789e80a97badd14145fda39b545d83ca3ef", + "reference": "b2c28789e80a97badd14145fda39b545d83ca3ef", "shasum": "" }, "require": { @@ -842,20 +847,20 @@ "object", "object graph" ], - "time": "2019-08-09T12:45:53+00:00" + "time": "2020-01-17T21:11:47+00:00" }, { "name": "phpcompatibility/php-compatibility", - "version": "9.3.2", + "version": "9.3.5", "source": { "type": "git", "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", - "reference": "bfca2be3992f40e92206e5a7ebe5eaee37280b58" + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/bfca2be3992f40e92206e5a7ebe5eaee37280b58", - "reference": "bfca2be3992f40e92206e5a7ebe5eaee37280b58", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", "shasum": "" }, "require": { @@ -900,20 +905,20 @@ "phpcs", "standards" ], - "time": "2019-10-16T21:24:24+00:00" + "time": "2019-12-27T09:44:58+00:00" }, { "name": "phpcompatibility/phpcompatibility-paragonie", - "version": "1.2.0", + "version": "1.3.0", "source": { "type": "git", "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", - "reference": "94b2388c4fe99e9e2ef0772e280fa0eafa1d0603" + "reference": "b862bc32f7e860d0b164b199bd995e690b4b191c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/94b2388c4fe99e9e2ef0772e280fa0eafa1d0603", - "reference": "94b2388c4fe99e9e2ef0772e280fa0eafa1d0603", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/b862bc32f7e860d0b164b199bd995e690b4b191c", + "reference": "b862bc32f7e860d0b164b199bd995e690b4b191c", "shasum": "" }, "require": { @@ -952,7 +957,7 @@ "polyfill", "standards" ], - "time": "2019-10-16T21:41:26+00:00" + "time": "2019-11-04T15:17:54+00:00" }, { "name": "phpcompatibility/phpcompatibility-wp", @@ -1058,40 +1063,38 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "4.3.2", + "version": "5.0.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e" + "reference": "a48807183a4b819072f26e347bbd0b5199a9d15f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/b83ff7cfcfee7827e1e78b637a5904fe6a96698e", - "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/a48807183a4b819072f26e347bbd0b5199a9d15f", + "reference": "a48807183a4b819072f26e347bbd0b5199a9d15f", "shasum": "" }, "require": { - "php": "^7.0", - "phpdocumentor/reflection-common": "^1.0.0 || ^2.0.0", - "phpdocumentor/type-resolver": "~0.4 || ^1.0.0", - "webmozart/assert": "^1.0" + "ext-filter": "^7.1", + "php": "^7.2", + "phpdocumentor/reflection-common": "^2.0", + "phpdocumentor/type-resolver": "^1.0", + "webmozart/assert": "^1" }, "require-dev": { - "doctrine/instantiator": "^1.0.5", - "mockery/mockery": "^1.0", - "phpunit/phpunit": "^6.4" + "doctrine/instantiator": "^1", + "mockery/mockery": "^1" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "4.x-dev" + "dev-master": "5.x-dev" } }, "autoload": { "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] + "phpDocumentor\\Reflection\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -1102,10 +1105,14 @@ { "name": "Mike van Riel", "email": "me@mikevanriel.com" + }, + { + "name": "Jaap van Otterdijk", + "email": "account@ijaap.nl" } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2019-09-12T14:27:41+00:00" + "time": "2020-02-09T09:16:15+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -1156,33 +1163,33 @@ }, { "name": "phpspec/prophecy", - "version": "1.9.0", + "version": "v1.10.2", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203" + "reference": "b4400efc9d206e83138e2bb97ed7f5b14b831cd9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/f6811d96d97bdf400077a0cc100ae56aa32b9203", - "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/b4400efc9d206e83138e2bb97ed7f5b14b831cd9", + "reference": "b4400efc9d206e83138e2bb97ed7f5b14b831cd9", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", "php": "^5.3|^7.0", "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0|^5.0", - "sebastian/comparator": "^1.1|^2.0|^3.0", - "sebastian/recursion-context": "^1.0|^2.0|^3.0" + "sebastian/comparator": "^1.2.3|^2.0|^3.0|^4.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0|^4.0" }, "require-dev": { - "phpspec/phpspec": "^2.5|^3.2", + "phpspec/phpspec": "^2.5 || ^3.2", "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.8.x-dev" + "dev-master": "1.10.x-dev" } }, "autoload": { @@ -1215,7 +1222,7 @@ "spy", "stub" ], - "time": "2019-10-03T11:07:50+00:00" + "time": "2020-01-20T15:57:02+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2123,16 +2130,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.5.2", + "version": "3.5.4", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "65b12cdeaaa6cd276d4c3033a95b9b88b12701e7" + "reference": "dceec07328401de6211037abbb18bda423677e26" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/65b12cdeaaa6cd276d4c3033a95b9b88b12701e7", - "reference": "65b12cdeaaa6cd276d4c3033a95b9b88b12701e7", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/dceec07328401de6211037abbb18bda423677e26", + "reference": "dceec07328401de6211037abbb18bda423677e26", "shasum": "" }, "require": { @@ -2170,31 +2177,32 @@ "phpcs", "standards" ], - "time": "2019-10-28T04:36:32+00:00" + "time": "2020-01-30T22:20:29+00:00" }, { "name": "symfony/console", - "version": "v4.3.5", + "version": "v4.4.4", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "929ddf360d401b958f611d44e726094ab46a7369" + "reference": "f512001679f37e6a042b51897ed24a2f05eba656" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/929ddf360d401b958f611d44e726094ab46a7369", - "reference": "929ddf360d401b958f611d44e726094ab46a7369", + "url": "https://api.github.com/repos/symfony/console/zipball/f512001679f37e6a042b51897ed24a2f05eba656", + "reference": "f512001679f37e6a042b51897ed24a2f05eba656", "shasum": "" }, "require": { "php": "^7.1.3", "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php73": "^1.8", - "symfony/service-contracts": "^1.1" + "symfony/service-contracts": "^1.1|^2" }, "conflict": { "symfony/dependency-injection": "<3.4", - "symfony/event-dispatcher": "<4.3", + "symfony/event-dispatcher": "<4.3|>=5", + "symfony/lock": "<4.4", "symfony/process": "<3.3" }, "provide": { @@ -2202,12 +2210,12 @@ }, "require-dev": { "psr/log": "~1.0", - "symfony/config": "~3.4|~4.0", - "symfony/dependency-injection": "~3.4|~4.0", + "symfony/config": "^3.4|^4.0|^5.0", + "symfony/dependency-injection": "^3.4|^4.0|^5.0", "symfony/event-dispatcher": "^4.3", - "symfony/lock": "~3.4|~4.0", - "symfony/process": "~3.4|~4.0", - "symfony/var-dumper": "^4.3" + "symfony/lock": "^4.4|^5.0", + "symfony/process": "^3.4|^4.0|^5.0", + "symfony/var-dumper": "^4.3|^5.0" }, "suggest": { "psr/log": "For using the console logger", @@ -2218,7 +2226,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.3-dev" + "dev-master": "4.4-dev" } }, "autoload": { @@ -2245,20 +2253,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2019-10-07T12:36:49+00:00" + "time": "2020-01-25T12:44:29+00:00" }, { "name": "symfony/finder", - "version": "v4.3.5", + "version": "v4.4.4", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "5e575faa95548d0586f6bedaeabec259714e44d1" + "reference": "3a50be43515590faf812fbd7708200aabc327ec3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/5e575faa95548d0586f6bedaeabec259714e44d1", - "reference": "5e575faa95548d0586f6bedaeabec259714e44d1", + "url": "https://api.github.com/repos/symfony/finder/zipball/3a50be43515590faf812fbd7708200aabc327ec3", + "reference": "3a50be43515590faf812fbd7708200aabc327ec3", "shasum": "" }, "require": { @@ -2267,7 +2275,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.3-dev" + "dev-master": "4.4-dev" } }, "autoload": { @@ -2294,20 +2302,20 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2019-09-16T11:29:48+00:00" + "time": "2020-01-04T13:00:46+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.12.0", + "version": "v1.14.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4" + "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/550ebaac289296ce228a706d0867afc34687e3f4", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", + "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", "shasum": "" }, "require": { @@ -2319,7 +2327,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.12-dev" + "dev-master": "1.14-dev" } }, "autoload": { @@ -2352,20 +2360,20 @@ "polyfill", "portable" ], - "time": "2019-08-06T08:03:45+00:00" + "time": "2020-01-13T11:15:53+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.12.0", + "version": "v1.14.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "b42a2f66e8f1b15ccf25652c3424265923eb4f17" + "reference": "34094cfa9abe1f0f14f48f490772db7a775559f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/b42a2f66e8f1b15ccf25652c3424265923eb4f17", - "reference": "b42a2f66e8f1b15ccf25652c3424265923eb4f17", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/34094cfa9abe1f0f14f48f490772db7a775559f2", + "reference": "34094cfa9abe1f0f14f48f490772db7a775559f2", "shasum": "" }, "require": { @@ -2377,7 +2385,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.12-dev" + "dev-master": "1.14-dev" } }, "autoload": { @@ -2411,20 +2419,20 @@ "portable", "shim" ], - "time": "2019-08-06T08:03:45+00:00" + "time": "2020-01-13T11:15:53+00:00" }, { "name": "symfony/polyfill-php73", - "version": "v1.12.0", + "version": "v1.14.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "2ceb49eaccb9352bff54d22570276bb75ba4a188" + "reference": "5e66a0fa1070bf46bec4bea7962d285108edd675" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/2ceb49eaccb9352bff54d22570276bb75ba4a188", - "reference": "2ceb49eaccb9352bff54d22570276bb75ba4a188", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/5e66a0fa1070bf46bec4bea7962d285108edd675", + "reference": "5e66a0fa1070bf46bec4bea7962d285108edd675", "shasum": "" }, "require": { @@ -2433,7 +2441,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.12-dev" + "dev-master": "1.14-dev" } }, "autoload": { @@ -2469,24 +2477,24 @@ "portable", "shim" ], - "time": "2019-08-06T08:03:45+00:00" + "time": "2020-01-13T11:15:53+00:00" }, { "name": "symfony/service-contracts", - "version": "v1.1.7", + "version": "v2.0.1", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0" + "reference": "144c5e51266b281231e947b51223ba14acf1a749" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ffcde9615dc5bb4825b9f6aed07716f1f57faae0", - "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/144c5e51266b281231e947b51223ba14acf1a749", + "reference": "144c5e51266b281231e947b51223ba14acf1a749", "shasum": "" }, "require": { - "php": "^7.1.3", + "php": "^7.2.5", "psr/container": "^1.0" }, "suggest": { @@ -2495,7 +2503,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-master": "2.0-dev" } }, "autoload": { @@ -2527,20 +2535,20 @@ "interoperability", "standards" ], - "time": "2019-09-17T11:12:18+00:00" + "time": "2019-11-18T17:27:11+00:00" }, { "name": "symfony/yaml", - "version": "v4.3.5", + "version": "v4.4.4", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "41e16350a2a1c7383c4735aa2f9fce74cf3d1178" + "reference": "cd014e425b3668220adb865f53bff64b3ad21767" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/41e16350a2a1c7383c4735aa2f9fce74cf3d1178", - "reference": "41e16350a2a1c7383c4735aa2f9fce74cf3d1178", + "url": "https://api.github.com/repos/symfony/yaml/zipball/cd014e425b3668220adb865f53bff64b3ad21767", + "reference": "cd014e425b3668220adb865f53bff64b3ad21767", "shasum": "" }, "require": { @@ -2551,7 +2559,7 @@ "symfony/console": "<3.4" }, "require-dev": { - "symfony/console": "~3.4|~4.0" + "symfony/console": "^3.4|^4.0|^5.0" }, "suggest": { "symfony/console": "For validating YAML files using the lint command" @@ -2559,7 +2567,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.3-dev" + "dev-master": "4.4-dev" } }, "autoload": { @@ -2586,35 +2594,33 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2019-09-11T15:41:19+00:00" + "time": "2020-01-21T11:12:16+00:00" }, { "name": "webmozart/assert", - "version": "1.5.0", + "version": "1.7.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4" + "reference": "aed98a490f9a8f78468232db345ab9cf606cf598" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/88e6d84706d09a236046d686bbea96f07b3a34f4", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4", + "url": "https://api.github.com/repos/webmozart/assert/zipball/aed98a490f9a8f78468232db345ab9cf606cf598", + "reference": "aed98a490f9a8f78468232db345ab9cf606cf598", "shasum": "" }, "require": { "php": "^5.3.3 || ^7.0", "symfony/polyfill-ctype": "^1.8" }, + "conflict": { + "vimeo/psalm": "<3.6.0" + }, "require-dev": { "phpunit/phpunit": "^4.8.36 || ^7.5.13" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3-dev" - } - }, "autoload": { "psr-4": { "Webmozart\\Assert\\": "src/" @@ -2636,20 +2642,20 @@ "check", "validate" ], - "time": "2019-08-24T08:43:50+00:00" + "time": "2020-02-14T12:15:55+00:00" }, { "name": "wp-coding-standards/wpcs", - "version": "2.1.1", + "version": "2.2.1", "source": { "type": "git", "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", - "reference": "bd9c33152115e6741e3510ff7189605b35167908" + "reference": "b5a453203114cc2284b1a614c4953456fbe4f546" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/bd9c33152115e6741e3510ff7189605b35167908", - "reference": "bd9c33152115e6741e3510ff7189605b35167908", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/b5a453203114cc2284b1a614c4953456fbe4f546", + "reference": "b5a453203114cc2284b1a614c4953456fbe4f546", "shasum": "" }, "require": { @@ -2657,12 +2663,12 @@ "squizlabs/php_codesniffer": "^3.3.1" }, "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", "phpcompatibility/php-compatibility": "^9.0", "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" }, "suggest": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." + "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." }, "type": "phpcodesniffer-standard", "notification-url": "https://packagist.org/downloads/", @@ -2672,7 +2678,7 @@ "authors": [ { "name": "Contributors", - "homepage": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/graphs/contributors" + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" } ], "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", @@ -2681,7 +2687,7 @@ "standards", "wordpress" ], - "time": "2019-05-21T02:50:00+00:00" + "time": "2020-02-04T02:52:06+00:00" } ], "aliases": [], diff --git a/src/Dependencies/RocketLazyload/Iframe.php b/src/Dependencies/RocketLazyload/Iframe.php index 400433b..264df1e 100644 --- a/src/Dependencies/RocketLazyload/Iframe.php +++ b/src/Dependencies/RocketLazyload/Iframe.php @@ -107,6 +107,8 @@ private function getExcludedPatterns() 'data-no-lazy=', 'recaptcha/api/fallback', 'loading="eager"', + 'data-skip-lazy', + 'skip-lazy', ] ); } @@ -227,7 +229,7 @@ public function changeYoutubeUrlForYoutuDotBe( $url ) { */ public function cleanYoutubeUrl ( $url ) { $parsed_url = wp_parse_url( $url, -1 ); - $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; + $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '//'; $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index 068c0f2..506ac7b 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -92,10 +92,13 @@ public function lazyloadBackgroundImages($html, $buffer) private function addLazyClass($element) { if (preg_match('#class=["\']?(?[^"\'>]*)["\']?#is', $element, $class)) { + if (empty($class['classes'])) { + return str_replace($class[0], 'class="rocket-lazyload"', $element); + } + $classes = str_replace($class['classes'], $class['classes'] . ' rocket-lazyload', $class[0]); - $element = str_replace($class[0], $classes, $element); - return $element; + return str_replace($class[0], $classes, $element); } return preg_replace('#<(img|div|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element); @@ -259,6 +262,8 @@ public function getExcludedAttributes() 'data-height-percentage', 'data-large_image', 'avia-bg-style-fixed', + 'data-skip-lazy', + 'skip-lazy', ] ); } @@ -311,8 +316,7 @@ private function replaceImage($image) $image_lazyload = str_replace($image['atts'], $placeholder_atts . ' data-lazy-src="' . $image['src'] . '"', $image[0]); - // this filter is documented in src/Subscriber/LazyloadSubscriber.php. - if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload) && apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload) && apply_filters('rocket_use_native_lazyload', false)) { $image_lazyload = str_replace(' Date: Fri, 21 Feb 2020 15:31:44 -0500 Subject: [PATCH 03/17] update stable tag --- rocket-lazy-load.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocket-lazy-load.php b/rocket-lazy-load.php index d12b04c..1e8ec5e 100644 --- a/rocket-lazy-load.php +++ b/rocket-lazy-load.php @@ -3,7 +3,7 @@ * Plugin Name: Lazy Load - Optimize Images * Plugin URI: http://wordpress.org/plugins/rocket-lazy-load/ * Description: The tiny Lazy Load script for WordPress without jQuery or others libraries. - * Version: 2.3.2 + * Version: 2.3.3 * Author: WP Rocket * Author URI: https://wp-rocket.me * Text Domain: rocket-lazy-load @@ -29,7 +29,7 @@ defined('ABSPATH') || die('Cheatin\' uh?'); -define('ROCKET_LL_VERSION', '2.3.2'); +define('ROCKET_LL_VERSION', '2.3.3'); define('ROCKET_LL_WP_VERSION', '4.9'); define('ROCKET_LL_PHP_VERSION', '5.6'); define('ROCKET_LL_BASENAME', plugin_basename(__FILE__)); From ee7b05382f29e9cacd652061fbf5cbe341d5de18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 21 Feb 2020 15:31:57 -0500 Subject: [PATCH 04/17] update stable tag, FAQ and changelog --- readme.txt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 3c1e2a2..5c3998e 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: lazyload, lazy load, images, iframes, thumbnail, thumbnails, smiley, smili Requires at least: 4.7 Tested up to: 5.3 Requires PHP: 5.6 -Stable tag: 2.3.2 +Stable tag: 2.3.3 Lazy Load your images and iframes, replace Youtube videos by a preview thumbnail. @@ -32,6 +32,13 @@ Lazyload script: [https://github.com/verlok/lazyload](https://github.com/verlok/ == Frequently Asked Questions == += How can I use native lazyload? = +To use native lazyload on browsers supporting this feature, You need to use the following line: + +`add_filter( 'rocket_use_native_lazyload', '__return_true' );` + +Browsers not support native lazyload will use the JS-based solution as before. + = How can I deactivate Lazy Load on some pages? = You can use the `do_rocket_lazyload` filter. @@ -85,6 +92,13 @@ You can also apply it manually. The element you want to apply lazyload on must h The element must have the class `rocket-lazyload`, and a `data-bg` attribute, which value is the CSS url for the image. == Changelog == += 2.3.3 = +Enhancement: Add data-skip-lazy and skip-lazy class to exclusions list as part of the interoperability initiative between lazyload plugins +Enhancement: Add loading attribute only if filter `rocket_use_native_lazyload` is true +Bugfix: Correctly add the rocket-lazyload class when class attribute is empty on an element with a background image +Bugfix: Correctly replace YouTube iframe with preview image when using relative protocol +Bugfix: Preserve youtube-nocookie.com during LazyLoad + = 2.3.2 = Bugfix: Incorrect characters used in Youtube thumbnail HTML code From 0e75f32244688bbdf0cc1dd3dc59eb587d3c24b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 25 Feb 2020 10:10:33 -0500 Subject: [PATCH 05/17] Apply Greg' suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grégory Viguier --- readme.txt | 2 +- rocket-lazy-load.php | 1 + src/Subscriber/LazyloadSubscriber.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/readme.txt b/readme.txt index 5c3998e..36de88d 100644 --- a/readme.txt +++ b/readme.txt @@ -94,7 +94,7 @@ The element must have the class `rocket-lazyload`, and a `data-bg` attribute, wh == Changelog == = 2.3.3 = Enhancement: Add data-skip-lazy and skip-lazy class to exclusions list as part of the interoperability initiative between lazyload plugins -Enhancement: Add loading attribute only if filter `rocket_use_native_lazyload` is true +Enhancement: Use native lazyload only if filter `rocket_use_native_lazyload` is true Bugfix: Correctly add the rocket-lazyload class when class attribute is empty on an element with a background image Bugfix: Correctly replace YouTube iframe with preview image when using relative protocol Bugfix: Preserve youtube-nocookie.com during LazyLoad diff --git a/rocket-lazy-load.php b/rocket-lazy-load.php index 1e8ec5e..947f006 100644 --- a/rocket-lazy-load.php +++ b/rocket-lazy-load.php @@ -4,6 +4,7 @@ * Plugin URI: http://wordpress.org/plugins/rocket-lazy-load/ * Description: The tiny Lazy Load script for WordPress without jQuery or others libraries. * Version: 2.3.3 + * Requires PHP: 5.6 * Author: WP Rocket * Author URI: https://wp-rocket.me * Text Domain: rocket-lazy-load diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index 282a911..596e132 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -159,7 +159,7 @@ public function insertLazyloadScript() } if ($this->option_array->get('images') || $this->option_array->get('iframes')) { - // this filter is documented in src/Subscriber/LazyloadSubscriber.php. + // This filter is documented in src/Subscriber/LazyloadSubscriber.php. if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals $inline_args['elements']['loading'] = '[loading=lazy]'; } From e61488fd05379a03c564fc43485d561d57032e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 25 Feb 2020 10:15:50 -0500 Subject: [PATCH 06/17] update wording in FAQ --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 36de88d..3f6aaf5 100644 --- a/readme.txt +++ b/readme.txt @@ -37,7 +37,7 @@ To use native lazyload on browsers supporting this feature, You need to use the `add_filter( 'rocket_use_native_lazyload', '__return_true' );` -Browsers not support native lazyload will use the JS-based solution as before. +Browsers that do not support native lazyload will use the JS-based solution as before. = How can I deactivate Lazy Load on some pages? = From 0ed75444949c54f103ac15717f3c96cd2e71ea2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 27 Feb 2020 10:20:04 -0500 Subject: [PATCH 07/17] Apply suggestions from Greg code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grégory Viguier --- readme.txt | 2 +- src/Subscriber/LazyloadSubscriber.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 3f6aaf5..8ad8ffd 100644 --- a/readme.txt +++ b/readme.txt @@ -33,7 +33,7 @@ Lazyload script: [https://github.com/verlok/lazyload](https://github.com/verlok/ == Frequently Asked Questions == = How can I use native lazyload? = -To use native lazyload on browsers supporting this feature, You need to use the following line: +To use native lazyload on browsers supporting this feature, you need to use the following line: `add_filter( 'rocket_use_native_lazyload', '__return_true' );` diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index 596e132..6c6509d 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -161,6 +161,7 @@ public function insertLazyloadScript() if ($this->option_array->get('images') || $this->option_array->get('iframes')) { // This filter is documented in src/Subscriber/LazyloadSubscriber.php. if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; $inline_args['elements']['loading'] = '[loading=lazy]'; } } From 457b6de6b6ca44a970797a56979da3ba264b7f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 27 Feb 2020 11:09:19 -0500 Subject: [PATCH 08/17] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grégory Viguier --- src/Subscriber/LazyloadSubscriber.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index 6c6509d..c6d7a9c 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -167,11 +167,13 @@ public function insertLazyloadScript() } if ($this->option_array->get('images')) { + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; $inline_args['elements']['image'] = 'img[data-lazy-src]'; $inline_args['elements']['background_image'] = '.rocket-lazyload'; } if ($this->option_array->get('iframes')) { + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; $inline_args['elements']['iframe'] = 'iframe[data-lazy-src]'; } From 3339f6b03761f257270d55f225ecea85f89ba352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Thu, 30 Apr 2020 22:26:19 +0200 Subject: [PATCH 09/17] Remove unnecessary variable in main.php (PR #109) --- main.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.php b/main.php index 6dab495..e288f19 100644 --- a/main.php +++ b/main.php @@ -6,6 +6,4 @@ require ROCKET_LL_PATH . 'vendor/autoload.php'; } -$rocket_lazyload = new RocketLazyLoadPlugin\Plugin(); - -add_action('plugins_loaded', [ $rocket_lazyload, 'load' ]); +add_action('plugins_loaded', [ new RocketLazyLoadPlugin\Plugin(), 'load' ]); From 3c3dbdc5bce6e68b48888854914cdd23ac7b27f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Fri, 1 May 2020 17:46:19 +0200 Subject: [PATCH 10/17] Constants and other fixes from PHPStan (PR #110) --- src/Dependencies/RocketLazyload/Image.php | 4 ++-- src/Plugin.php | 4 ++-- src/Subscriber/AdminPageSubscriber.php | 2 +- src/Subscriber/LazyloadSubscriber.php | 8 ++++---- src/Subscriber/ThirdParty/AMPSubscriber.php | 5 +++++ 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index 506ac7b..64c5ef7 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -125,11 +125,11 @@ public function lazyloadPictures($html, $buffer) continue; } + $lazy_sources = 0; + if (preg_match_all('#\s.+)>#iUs', $picture['sources'], $sources, PREG_SET_ORDER)) { $sources = array_unique($sources, SORT_REGULAR); - $lazy_sources = 0; - foreach ($sources as $source) { $lazyload_srcset = preg_replace('/([\s"\'])srcset/i', '\1data-lazy-srcset', $source[0]); $html = str_replace($source[0], $lazyload_srcset, $html); diff --git a/src/Plugin.php b/src/Plugin.php index 331a760..c9538cf 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -58,8 +58,8 @@ public function load() $container = new Container(); - $container->add('template_path', ROCKET_LL_PATH . 'views/'); - $container->add('plugin_basename', ROCKET_LL_BASENAME); + $container->add('template_path', \ROCKET_LL_PATH . 'views/'); + $container->add('plugin_basename', \ROCKET_LL_BASENAME); $container->add('options', function () { return new Options('rocket_lazyload'); diff --git a/src/Subscriber/AdminPageSubscriber.php b/src/Subscriber/AdminPageSubscriber.php index 5f6fa6c..c4f08c6 100644 --- a/src/Subscriber/AdminPageSubscriber.php +++ b/src/Subscriber/AdminPageSubscriber.php @@ -137,6 +137,6 @@ public function enqueueAdminStyle($hook_suffix) return; } - wp_enqueue_style('rocket-lazyload', ROCKET_LL_ASSETS_URL . 'css/admin.css', null, ROCKET_LL_VERSION); + wp_enqueue_style('rocket-lazyload', \ROCKET_LL_ASSETS_URL . 'css/admin.css', null, \ROCKET_LL_VERSION); } } diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index c6d7a9c..93f16fc 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -87,11 +87,11 @@ public function getSubscribedEvents() { return [ 'wp_footer' => [ - [ 'insertLazyloadScript', ROCKET_LL_INT_MAX ], - ['insertYoutubeThumbnailScript', ROCKET_LL_INT_MAX ], + [ 'insertLazyloadScript', \ROCKET_LL_INT_MAX ], + ['insertYoutubeThumbnailScript', \ROCKET_LL_INT_MAX ], ], - 'wp_head' => ['insertNoJSStyle', ROCKET_LL_INT_MAX ], - 'wp_enqueue_scripts' => ['insertYoutubeThumbnailStyle', ROCKET_LL_INT_MAX], + 'wp_head' => ['insertNoJSStyle', \ROCKET_LL_INT_MAX ], + 'wp_enqueue_scripts' => ['insertYoutubeThumbnailStyle', \ROCKET_LL_INT_MAX], 'template_redirect' => ['lazyload', 2], 'rocket_lazyload_html' => 'lazyloadResponsive', 'init' => 'lazyloadSmilies', diff --git a/src/Subscriber/ThirdParty/AMPSubscriber.php b/src/Subscriber/ThirdParty/AMPSubscriber.php index 938b7cf..bb27a0e 100644 --- a/src/Subscriber/ThirdParty/AMPSubscriber.php +++ b/src/Subscriber/ThirdParty/AMPSubscriber.php @@ -20,6 +20,11 @@ */ class AMPSubscriber implements EventManagerAwareSubscriberInterface { + /** + * @var EventManager + */ + protected $event_manager; + /** * @inheritDoc */ From b942a8502f3f896b8c3bc472b84d1d7bbd8c80aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Mon, 11 May 2020 14:04:09 -0400 Subject: [PATCH 11/17] update dependencies --- composer.lock | 177 +++++++++++----------- src/Dependencies/RocketLazyload/Image.php | 4 +- 2 files changed, 90 insertions(+), 91 deletions(-) diff --git a/composer.lock b/composer.lock index 0477deb..c256e3d 100644 --- a/composer.lock +++ b/composer.lock @@ -8,28 +8,31 @@ "packages": [ { "name": "composer/installers", - "version": "v1.8.0", + "version": "v1.9.0", "source": { "type": "git", "url": "https://github.com/composer/installers.git", - "reference": "7d610d50aae61ae7ed6675e58efeabdf279bb5e3" + "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/installers/zipball/7d610d50aae61ae7ed6675e58efeabdf279bb5e3", - "reference": "7d610d50aae61ae7ed6675e58efeabdf279bb5e3", + "url": "https://api.github.com/repos/composer/installers/zipball/b93bcf0fa1fccb0b7d176b0967d969691cd74cca", + "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca", "shasum": "" }, "require": { - "composer-plugin-api": "^1.0" + "composer-plugin-api": "^1.0 || ^2.0" }, "replace": { "roundcube/plugin-installer": "*", "shama/baton": "*" }, "require-dev": { - "composer/composer": "1.0.*@dev", - "phpunit/phpunit": "^4.8.36" + "composer/composer": "1.6.* || 2.0.*@dev", + "composer/semver": "1.0.* || 2.0.*@dev", + "phpunit/phpunit": "^4.8.36", + "sebastian/comparator": "^1.2.4", + "symfony/process": "^2.3" }, "type": "composer-plugin", "extra": { @@ -128,7 +131,7 @@ "zend", "zikula" ], - "time": "2020-02-07T10:39:20+00:00" + "time": "2020-04-07T06:57:05+00:00" }, { "name": "container-interop/container-interop", @@ -278,16 +281,16 @@ }, { "name": "wp-media/rocket-lazyload-common", - "version": "v2.5.6", + "version": "v2.5.7", "source": { "type": "git", "url": "https://github.com/wp-media/rocket-lazyload-common.git", - "reference": "1ea0cf2ae5d302a0214e05176c1dda321b1d6690" + "reference": "408590c87a3ad07a07ea263073a46146fc42ae7a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/1ea0cf2ae5d302a0214e05176c1dda321b1d6690", - "reference": "1ea0cf2ae5d302a0214e05176c1dda321b1d6690", + "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/408590c87a3ad07a07ea263073a46146fc42ae7a", + "reference": "408590c87a3ad07a07ea263073a46146fc42ae7a", "shasum": "" }, "require": { @@ -322,7 +325,7 @@ } ], "description": "Common Code between WP Rocket and Lazyload by WP Rocket", - "time": "2020-01-16T13:46:49+00:00" + "time": "2020-04-29T15:33:21+00:00" } ], "packages-dev": [ @@ -654,16 +657,16 @@ }, { "name": "league/flysystem", - "version": "1.0.64", + "version": "1.0.67", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "d13c43dbd4b791f815215959105a008515d1a2e0" + "reference": "5b1f36c75c4bdde981294c2a0ebdb437ee6f275e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/d13c43dbd4b791f815215959105a008515d1a2e0", - "reference": "d13c43dbd4b791f815215959105a008515d1a2e0", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/5b1f36c75c4bdde981294c2a0ebdb437ee6f275e", + "reference": "5b1f36c75c4bdde981294c2a0ebdb437ee6f275e", "shasum": "" }, "require": { @@ -734,7 +737,7 @@ "sftp", "storage" ], - "time": "2020-02-05T18:14:17+00:00" + "time": "2020-04-16T13:21:26+00:00" }, { "name": "mockery/mockery", @@ -1011,24 +1014,21 @@ }, { "name": "phpdocumentor/reflection-common", - "version": "2.0.0", + "version": "2.1.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a" + "reference": "6568f4687e5b41b054365f9ae03fcb1ed5f2069b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/63a995caa1ca9e5590304cd845c15ad6d482a62a", - "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/6568f4687e5b41b054365f9ae03fcb1ed5f2069b", + "reference": "6568f4687e5b41b054365f9ae03fcb1ed5f2069b", "shasum": "" }, "require": { "php": ">=7.1" }, - "require-dev": { - "phpunit/phpunit": "~6" - }, "type": "library", "extra": { "branch-alias": { @@ -1059,20 +1059,20 @@ "reflection", "static analysis" ], - "time": "2018-08-07T13:53:10+00:00" + "time": "2020-04-27T09:25:28+00:00" }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.0.0", + "version": "5.1.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "a48807183a4b819072f26e347bbd0b5199a9d15f" + "reference": "cd72d394ca794d3466a3b2fc09d5a6c1dc86b47e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/a48807183a4b819072f26e347bbd0b5199a9d15f", - "reference": "a48807183a4b819072f26e347bbd0b5199a9d15f", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/cd72d394ca794d3466a3b2fc09d5a6c1dc86b47e", + "reference": "cd72d394ca794d3466a3b2fc09d5a6c1dc86b47e", "shasum": "" }, "require": { @@ -1112,30 +1112,29 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2020-02-09T09:16:15+00:00" + "time": "2020-02-22T12:28:44+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "1.0.1", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9" + "reference": "7462d5f123dfc080dfdf26897032a6513644fc95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", - "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/7462d5f123dfc080dfdf26897032a6513644fc95", + "reference": "7462d5f123dfc080dfdf26897032a6513644fc95", "shasum": "" }, "require": { - "php": "^7.1", + "php": "^7.2", "phpdocumentor/reflection-common": "^2.0" }, "require-dev": { - "ext-tokenizer": "^7.1", - "mockery/mockery": "~1", - "phpunit/phpunit": "^7.0" + "ext-tokenizer": "^7.2", + "mockery/mockery": "~1" }, "type": "library", "extra": { @@ -1159,20 +1158,20 @@ } ], "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", - "time": "2019-08-22T18:11:29+00:00" + "time": "2020-02-18T18:59:58+00:00" }, { "name": "phpspec/prophecy", - "version": "v1.10.2", + "version": "v1.10.3", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "b4400efc9d206e83138e2bb97ed7f5b14b831cd9" + "reference": "451c3cd1418cf640de218914901e51b064abb093" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/b4400efc9d206e83138e2bb97ed7f5b14b831cd9", - "reference": "b4400efc9d206e83138e2bb97ed7f5b14b831cd9", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/451c3cd1418cf640de218914901e51b064abb093", + "reference": "451c3cd1418cf640de218914901e51b064abb093", "shasum": "" }, "require": { @@ -1222,7 +1221,7 @@ "spy", "stub" ], - "time": "2020-01-20T15:57:02+00:00" + "time": "2020-03-05T15:02:03+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2130,16 +2129,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.5.4", + "version": "3.5.5", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "dceec07328401de6211037abbb18bda423677e26" + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/dceec07328401de6211037abbb18bda423677e26", - "reference": "dceec07328401de6211037abbb18bda423677e26", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", "shasum": "" }, "require": { @@ -2177,20 +2176,20 @@ "phpcs", "standards" ], - "time": "2020-01-30T22:20:29+00:00" + "time": "2020-04-17T01:09:41+00:00" }, { "name": "symfony/console", - "version": "v4.4.4", + "version": "v4.4.8", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "f512001679f37e6a042b51897ed24a2f05eba656" + "reference": "10bb3ee3c97308869d53b3e3d03f6ac23ff985f7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/f512001679f37e6a042b51897ed24a2f05eba656", - "reference": "f512001679f37e6a042b51897ed24a2f05eba656", + "url": "https://api.github.com/repos/symfony/console/zipball/10bb3ee3c97308869d53b3e3d03f6ac23ff985f7", + "reference": "10bb3ee3c97308869d53b3e3d03f6ac23ff985f7", "shasum": "" }, "require": { @@ -2253,20 +2252,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2020-01-25T12:44:29+00:00" + "time": "2020-03-30T11:41:10+00:00" }, { "name": "symfony/finder", - "version": "v4.4.4", + "version": "v4.4.8", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "3a50be43515590faf812fbd7708200aabc327ec3" + "reference": "5729f943f9854c5781984ed4907bbb817735776b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/3a50be43515590faf812fbd7708200aabc327ec3", - "reference": "3a50be43515590faf812fbd7708200aabc327ec3", + "url": "https://api.github.com/repos/symfony/finder/zipball/5729f943f9854c5781984ed4907bbb817735776b", + "reference": "5729f943f9854c5781984ed4907bbb817735776b", "shasum": "" }, "require": { @@ -2302,20 +2301,20 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2020-01-04T13:00:46+00:00" + "time": "2020-03-27T16:54:36+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.14.0", + "version": "v1.16.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38" + "reference": "1aab00e39cebaef4d8652497f46c15c1b7e45294" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", - "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/1aab00e39cebaef4d8652497f46c15c1b7e45294", + "reference": "1aab00e39cebaef4d8652497f46c15c1b7e45294", "shasum": "" }, "require": { @@ -2327,7 +2326,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.14-dev" + "dev-master": "1.16-dev" } }, "autoload": { @@ -2360,20 +2359,20 @@ "polyfill", "portable" ], - "time": "2020-01-13T11:15:53+00:00" + "time": "2020-05-08T16:50:20+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.14.0", + "version": "v1.16.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "34094cfa9abe1f0f14f48f490772db7a775559f2" + "reference": "a54881ec0ab3b2005c406aed0023c062879031e7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/34094cfa9abe1f0f14f48f490772db7a775559f2", - "reference": "34094cfa9abe1f0f14f48f490772db7a775559f2", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/a54881ec0ab3b2005c406aed0023c062879031e7", + "reference": "a54881ec0ab3b2005c406aed0023c062879031e7", "shasum": "" }, "require": { @@ -2385,7 +2384,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.14-dev" + "dev-master": "1.16-dev" } }, "autoload": { @@ -2419,20 +2418,20 @@ "portable", "shim" ], - "time": "2020-01-13T11:15:53+00:00" + "time": "2020-05-08T16:50:20+00:00" }, { "name": "symfony/polyfill-php73", - "version": "v1.14.0", + "version": "v1.16.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "5e66a0fa1070bf46bec4bea7962d285108edd675" + "reference": "7e95fe59d12169fcf4041487e4bf34fca37ee0ed" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/5e66a0fa1070bf46bec4bea7962d285108edd675", - "reference": "5e66a0fa1070bf46bec4bea7962d285108edd675", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/7e95fe59d12169fcf4041487e4bf34fca37ee0ed", + "reference": "7e95fe59d12169fcf4041487e4bf34fca37ee0ed", "shasum": "" }, "require": { @@ -2441,7 +2440,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.14-dev" + "dev-master": "1.16-dev" } }, "autoload": { @@ -2477,7 +2476,7 @@ "portable", "shim" ], - "time": "2020-01-13T11:15:53+00:00" + "time": "2020-05-02T14:56:09+00:00" }, { "name": "symfony/service-contracts", @@ -2539,16 +2538,16 @@ }, { "name": "symfony/yaml", - "version": "v4.4.4", + "version": "v4.4.8", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "cd014e425b3668220adb865f53bff64b3ad21767" + "reference": "b385dce1c0e9f839b384af90188638819433e252" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/cd014e425b3668220adb865f53bff64b3ad21767", - "reference": "cd014e425b3668220adb865f53bff64b3ad21767", + "url": "https://api.github.com/repos/symfony/yaml/zipball/b385dce1c0e9f839b384af90188638819433e252", + "reference": "b385dce1c0e9f839b384af90188638819433e252", "shasum": "" }, "require": { @@ -2594,20 +2593,20 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2020-01-21T11:12:16+00:00" + "time": "2020-04-28T17:55:16+00:00" }, { "name": "webmozart/assert", - "version": "1.7.0", + "version": "1.8.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "aed98a490f9a8f78468232db345ab9cf606cf598" + "reference": "ab2cb0b3b559010b75981b1bdce728da3ee90ad6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/aed98a490f9a8f78468232db345ab9cf606cf598", - "reference": "aed98a490f9a8f78468232db345ab9cf606cf598", + "url": "https://api.github.com/repos/webmozart/assert/zipball/ab2cb0b3b559010b75981b1bdce728da3ee90ad6", + "reference": "ab2cb0b3b559010b75981b1bdce728da3ee90ad6", "shasum": "" }, "require": { @@ -2615,7 +2614,7 @@ "symfony/polyfill-ctype": "^1.8" }, "conflict": { - "vimeo/psalm": "<3.6.0" + "vimeo/psalm": "<3.9.1" }, "require-dev": { "phpunit/phpunit": "^4.8.36 || ^7.5.13" @@ -2642,7 +2641,7 @@ "check", "validate" ], - "time": "2020-02-14T12:15:55+00:00" + "time": "2020-04-18T12:12:48+00:00" }, { "name": "wp-coding-standards/wpcs", diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index 506ac7b..2d425dd 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -53,7 +53,7 @@ public function lazyloadImages($html, $buffer) */ public function lazyloadBackgroundImages($html, $buffer) { - if (! preg_match_all('#<(?div|section|span|li)\s+(?[^>]+[\'"\s])?style\s*=\s*([\'"])(?.*?)\3(?[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER)) { + if (! preg_match_all('#<(?div|figure|section|span|li)\s+(?[^>]+[\'"\s])?style\s*=\s*([\'"])(?.*?)\3(?[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER)) { return $html; } @@ -101,7 +101,7 @@ private function addLazyClass($element) return str_replace($class[0], $classes, $element); } - return preg_replace('#<(img|div|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element); + return preg_replace('#<(img|div|figure|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element); } /** From 4645582507cb241d093864c209433059b60a9015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Tue, 12 May 2020 15:08:25 +0200 Subject: [PATCH 12/17] Fix CI config (PR #112) don't cache `vendor` --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5f4fa4c..7beffff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ sudo: false language: php +services: +- mysql notifications: slack: secure: AhtX1RztruH3HYMnXGH1IcGXWRNTdnjJIRVMHI4zeKQfFaomyJstLK6LD43vKxEaefMrHALhcB7csEvcQYBmlgvNgxOzcjHuGE5BVPW2OM6OUt/lv8r6979o0iIKC1dS7Ngc6S41UqWUy7CzmSxNSiapxT+gg0P7T2mHcYCrIrNb14gLLW8oKlem9JxG2Os1U47PGe9n5vNymCdLERprnzuDYkyYkzwLjNFwsl8rkFTU1mSScI6K+O/sbKNe1UEBXzJYAcB0Ta0j7ORPwJua0j+qAcqm9d9QUpzsrQyXev69yGCa0eSDDSiRWUIidpn3wq1uEHYjkFPF1XaXeUJYU9tvan1H7wjynCFmzJAQnxnEGOR8LwE+r6aYgWQfbwSIrxw+Hnc6CK90tA79mgsZoRxeHnLZo+hukeaXIUWn3kNCiRR/vWtwd67bzG3LOq4evDs34a1CzfdJWF17nAoilDI7kKUyXo/I4lbwPB1aveoCracTDKuCSl+IjQkSTH2a2afYk6pibFImAN73G50IFqk5DVgVwGMlCI6WvXs8FN1Mi9vR49F+Sxe5gCPx/Vdx9sGIfQEGC628sO92TKASEdcAjMtxaCKMZUnlBG9M0IyIb7RP2Uh3twTOaWaowZ81Zcs8hY2pFPQTLUpBS+XOqP7WmbPsDD+V+c0r613qSeE= @@ -13,7 +15,6 @@ branches: - master cache: directories: - - vendor - "$HOME/.composer/cache" matrix: include: @@ -29,7 +30,7 @@ matrix: env: WP_VERSION=latest - php: 5.6 env: WP_VERSION=latest -before_script: +install: - export PATH="$HOME/.composer/vendor/bin:$PATH" - composer install --no-progress - | From a23948b29dd9e4afa895df44a0e86ddaab942e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 12 May 2020 09:12:41 -0400 Subject: [PATCH 13/17] add missing docblock --- src/Dependencies/RocketLazyload/Image.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index 810f479..b3daab2 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -316,6 +316,12 @@ private function replaceImage($image) $image_lazyload = str_replace($image['atts'], $placeholder_atts . ' data-lazy-src="' . $image['src'] . '"', $image[0]); + /** + * Filters the use of native lazyload + * + * @since 2.5 + * @param bool $use_native True to enable native lazyload usage. + */ if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload) && apply_filters('rocket_use_native_lazyload', false)) { $image_lazyload = str_replace(' Date: Wed, 13 May 2020 11:48:16 -0400 Subject: [PATCH 14/17] PHPCS formatting update --- composer.lock | 56 +- main.php | 8 +- phpcs.xml | 92 +- rocket-lazy-load.php | 53 +- src/Dependencies/RocketLazyload/Assets.php | 419 +++++---- src/Dependencies/RocketLazyload/Iframe.php | 444 +++++----- src/Dependencies/RocketLazyload/Image.php | 924 ++++++++++---------- src/Plugin.php | 136 +-- src/Subscriber/AdminPageSubscriber.php | 224 +++-- src/Subscriber/LazyloadSubscriber.php | 842 +++++++++--------- src/Subscriber/ThirdParty/AMPSubscriber.php | 109 +-- 11 files changed, 1649 insertions(+), 1658 deletions(-) diff --git a/composer.lock b/composer.lock index c256e3d..77d69e5 100644 --- a/composer.lock +++ b/composer.lock @@ -281,16 +281,16 @@ }, { "name": "wp-media/rocket-lazyload-common", - "version": "v2.5.7", + "version": "v2.5.8", "source": { "type": "git", "url": "https://github.com/wp-media/rocket-lazyload-common.git", - "reference": "408590c87a3ad07a07ea263073a46146fc42ae7a" + "reference": "7c8d2c718b3ac177c171164133502fbf0928f220" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/408590c87a3ad07a07ea263073a46146fc42ae7a", - "reference": "408590c87a3ad07a07ea263073a46146fc42ae7a", + "url": "https://api.github.com/repos/wp-media/rocket-lazyload-common/zipball/7c8d2c718b3ac177c171164133502fbf0928f220", + "reference": "7c8d2c718b3ac177c171164133502fbf0928f220", "shasum": "" }, "require": { @@ -325,7 +325,7 @@ } ], "description": "Common Code between WP Rocket and Lazyload by WP Rocket", - "time": "2020-04-29T15:33:21+00:00" + "time": "2020-05-13T15:14:47+00:00" } ], "packages-dev": [ @@ -657,16 +657,16 @@ }, { "name": "league/flysystem", - "version": "1.0.67", + "version": "1.0.68", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "5b1f36c75c4bdde981294c2a0ebdb437ee6f275e" + "reference": "3e4198372276ec99ac3409a21d7c9d1ced9026e4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/5b1f36c75c4bdde981294c2a0ebdb437ee6f275e", - "reference": "5b1f36c75c4bdde981294c2a0ebdb437ee6f275e", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/3e4198372276ec99ac3409a21d7c9d1ced9026e4", + "reference": "3e4198372276ec99ac3409a21d7c9d1ced9026e4", "shasum": "" }, "require": { @@ -737,7 +737,7 @@ "sftp", "storage" ], - "time": "2020-04-16T13:21:26+00:00" + "time": "2020-05-12T20:33:44+00:00" }, { "name": "mockery/mockery", @@ -2305,16 +2305,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.16.0", + "version": "v1.17.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "1aab00e39cebaef4d8652497f46c15c1b7e45294" + "reference": "e94c8b1bbe2bc77507a1056cdb06451c75b427f9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/1aab00e39cebaef4d8652497f46c15c1b7e45294", - "reference": "1aab00e39cebaef4d8652497f46c15c1b7e45294", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e94c8b1bbe2bc77507a1056cdb06451c75b427f9", + "reference": "e94c8b1bbe2bc77507a1056cdb06451c75b427f9", "shasum": "" }, "require": { @@ -2326,7 +2326,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.16-dev" + "dev-master": "1.17-dev" } }, "autoload": { @@ -2359,20 +2359,20 @@ "polyfill", "portable" ], - "time": "2020-05-08T16:50:20+00:00" + "time": "2020-05-12T16:14:59+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.16.0", + "version": "v1.17.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "a54881ec0ab3b2005c406aed0023c062879031e7" + "reference": "fa79b11539418b02fc5e1897267673ba2c19419c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/a54881ec0ab3b2005c406aed0023c062879031e7", - "reference": "a54881ec0ab3b2005c406aed0023c062879031e7", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fa79b11539418b02fc5e1897267673ba2c19419c", + "reference": "fa79b11539418b02fc5e1897267673ba2c19419c", "shasum": "" }, "require": { @@ -2384,7 +2384,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.16-dev" + "dev-master": "1.17-dev" } }, "autoload": { @@ -2418,20 +2418,20 @@ "portable", "shim" ], - "time": "2020-05-08T16:50:20+00:00" + "time": "2020-05-12T16:47:27+00:00" }, { "name": "symfony/polyfill-php73", - "version": "v1.16.0", + "version": "v1.17.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "7e95fe59d12169fcf4041487e4bf34fca37ee0ed" + "reference": "a760d8964ff79ab9bf057613a5808284ec852ccc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/7e95fe59d12169fcf4041487e4bf34fca37ee0ed", - "reference": "7e95fe59d12169fcf4041487e4bf34fca37ee0ed", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/a760d8964ff79ab9bf057613a5808284ec852ccc", + "reference": "a760d8964ff79ab9bf057613a5808284ec852ccc", "shasum": "" }, "require": { @@ -2440,7 +2440,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.16-dev" + "dev-master": "1.17-dev" } }, "autoload": { @@ -2476,7 +2476,7 @@ "portable", "shim" ], - "time": "2020-05-02T14:56:09+00:00" + "time": "2020-05-12T16:47:27+00:00" }, { "name": "symfony/service-contracts", diff --git a/main.php b/main.php index e288f19..90135b2 100644 --- a/main.php +++ b/main.php @@ -1,9 +1,9 @@ - - Rocket LazyLoad - - . - - - - - *.js - /vendor/* - /assets/* - - - - *\.php$ - - - - - - - - - - - - - + + The custom ruleset for WP Media. + + + + + + + . + + /tests/* + /vendor/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -38,4 +53,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/rocket-lazy-load.php b/rocket-lazy-load.php index 947f006..c9d46fe 100644 --- a/rocket-lazy-load.php +++ b/rocket-lazy-load.php @@ -28,16 +28,16 @@ * along with this program. If not, see . */ -defined('ABSPATH') || die('Cheatin\' uh?'); +defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); -define('ROCKET_LL_VERSION', '2.3.3'); -define('ROCKET_LL_WP_VERSION', '4.9'); -define('ROCKET_LL_PHP_VERSION', '5.6'); -define('ROCKET_LL_BASENAME', plugin_basename(__FILE__)); -define('ROCKET_LL_PATH', realpath(plugin_dir_path(__FILE__)) . '/'); -define('ROCKET_LL_ASSETS_URL', plugin_dir_url(__FILE__) . 'assets/'); -define('ROCKET_LL_FRONT_JS_URL', ROCKET_LL_ASSETS_URL . 'js/'); -define('ROCKET_LL_INT_MAX', PHP_INT_MAX - 15); +define( 'ROCKET_LL_VERSION', '2.3.3' ); +define( 'ROCKET_LL_WP_VERSION', '4.9' ); +define( 'ROCKET_LL_PHP_VERSION', '5.6' ); +define( 'ROCKET_LL_BASENAME', plugin_basename( __FILE__ ) ); +define( 'ROCKET_LL_PATH', realpath( plugin_dir_path( __FILE__ ) ) . '/' ); +define( 'ROCKET_LL_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets/' ); +define( 'ROCKET_LL_FRONT_JS_URL', ROCKET_LL_ASSETS_URL . 'js/' ); +define( 'ROCKET_LL_INT_MAX', PHP_INT_MAX - 15 ); require ROCKET_LL_PATH . 'src/rocket-lazyload-requirements-check.php'; @@ -49,31 +49,30 @@ * * @return void */ -function rocket_lazyload_textdomain() -{ - // Load translations from the languages directory. - $locale = get_locale(); +function rocket_lazyload_textdomain() { + // Load translations from the languages directory. + $locale = get_locale(); - // This filter is documented in /wp-includes/l10n.php. - $locale = apply_filters('plugin_locale', $locale, 'rocket-lazy-load'); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals - load_textdomain('rocket-lazy-load', WP_LANG_DIR . '/plugins/rocket-lazy-load-' . $locale . '.mo'); + // This filter is documented in /wp-includes/l10n.php. + $locale = apply_filters( 'plugin_locale', $locale, 'rocket-lazy-load' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + load_textdomain( 'rocket-lazy-load', WP_LANG_DIR . '/plugins/rocket-lazy-load-' . $locale . '.mo' ); - load_plugin_textdomain('rocket-lazy-load', false, dirname(plugin_basename(__FILE__)) . '/languages/'); + load_plugin_textdomain( 'rocket-lazy-load', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } -add_action('plugins_loaded', 'rocket_lazyload_textdomain'); +add_action( 'plugins_loaded', 'rocket_lazyload_textdomain' ); $rocket_lazyload_requirement_checks = new Rocket_Lazyload_Requirements_Check( - array( - 'plugin_name' => 'Lazy Load by WP Rocket', - 'plugin_version' => ROCKET_LL_VERSION, - 'wp_version' => ROCKET_LL_WP_VERSION, - 'php_version' => ROCKET_LL_PHP_VERSION, - ) + [ + 'plugin_name' => 'Lazy Load by WP Rocket', + 'plugin_version' => ROCKET_LL_VERSION, + 'wp_version' => ROCKET_LL_WP_VERSION, + 'php_version' => ROCKET_LL_PHP_VERSION, + ] ); -if ($rocket_lazyload_requirement_checks->check()) { - require ROCKET_LL_PATH . 'main.php'; +if ( $rocket_lazyload_requirement_checks->check() ) { + require ROCKET_LL_PATH . 'main.php'; } -unset($rocket_lazyload_requirement_checks); +unset( $rocket_lazyload_requirement_checks ); diff --git a/src/Dependencies/RocketLazyload/Assets.php b/src/Dependencies/RocketLazyload/Assets.php index 6f93765..4c6bc9e 100644 --- a/src/Dependencies/RocketLazyload/Assets.php +++ b/src/Dependencies/RocketLazyload/Assets.php @@ -10,64 +10,62 @@ /** * Class containing the methods to return or print the assets needed for lazyloading */ -class Assets -{ - /** - * Inserts the lazyload script in the HTML - * - * @param array $args Array of arguments to populate the lazyload script tag. - * @return void - */ - public function insertLazyloadScript($args = []) - { - echo $this->getLazyloadScript($args); - } - - /** - * Gets the inline lazyload script configuration - * - * @param array $args Array of arguments to populate the lazyload script options. - * @return string - */ - public function getInlineLazyloadScript($args = []) - { - $defaults = [ - 'elements' => [ - 'img', - 'iframe', - ], - 'threshold' => 300, - 'options' => [], - ]; - - $allowed_options = [ - 'container' => 1, - 'thresholds' => 1, - 'data_bg' => 1, - 'class_error' => 1, - 'load_delay' => 1, - 'auto_unobserve' => 1, - 'callback_enter' => 1, - 'callback_exit' => 1, - 'callback_reveal' => 1, - 'callback_error' => 1, - 'callback_finish' => 1, - 'use_native' => 1, - ]; - - $args = wp_parse_args($args, $defaults); - $script = ''; - - $args['options'] = array_intersect_key($args['options'], $allowed_options); - - $script .= 'window.lazyLoadOptions = { - elements_selector: "' . esc_attr(implode(',', $args['elements'])) . '", +class Assets { + + /** + * Inserts the lazyload script in the HTML + * + * @param array $args Array of arguments to populate the lazyload script tag. + * @return void + */ + public function insertLazyloadScript( $args = [] ) { + echo $this->getLazyloadScript( $args ); + } + + /** + * Gets the inline lazyload script configuration + * + * @param array $args Array of arguments to populate the lazyload script options. + * @return string + */ + public function getInlineLazyloadScript( $args = [] ) { + $defaults = [ + 'elements' => [ + 'img', + 'iframe', + ], + 'threshold' => 300, + 'options' => [], + ]; + + $allowed_options = [ + 'container' => 1, + 'thresholds' => 1, + 'data_bg' => 1, + 'class_error' => 1, + 'load_delay' => 1, + 'auto_unobserve' => 1, + 'callback_enter' => 1, + 'callback_exit' => 1, + 'callback_reveal' => 1, + 'callback_error' => 1, + 'callback_finish' => 1, + 'use_native' => 1, + ]; + + $args = wp_parse_args( $args, $defaults ); + $script = ''; + + $args['options'] = array_intersect_key( $args['options'], $allowed_options ); + + $script .= 'window.lazyLoadOptions = { + elements_selector: "' . esc_attr( implode( ',', $args['elements'] ) ) . '", data_src: "lazy-src", data_srcset: "lazy-srcset", data_sizes: "lazy-sizes", class_loading: "lazyloading", class_loaded: "lazyloaded", - threshold: ' . esc_attr($args['threshold']) . ', + threshold: ' . esc_attr( $args['threshold'] ) . ', callback_loaded: function(element) { if ( element.tagName === "IFRAME" && element.dataset.rocketLazyload == "fitvidscompatible" ) { if (element.classList.contains("lazyloaded") ) { @@ -80,19 +78,19 @@ class_loaded: "lazyloaded", } }'; - if (! empty($args['options'])) { - $script .= ',' . PHP_EOL; + if ( ! empty( $args['options'] ) ) { + $script .= ',' . PHP_EOL; - foreach ($args['options'] as $option => $value) { - $script .= $option . ': ' . $value . ','; - } + foreach ( $args['options'] as $option => $value ) { + $script .= $option . ': ' . $value . ','; + } - $script = rtrim($script, ','); - } + $script = rtrim( $script, ',' ); + } - $script .= '};'; + $script .= '};'; - $script .= ' + $script .= ' window.addEventListener(\'LazyLoad::Initialized\', function (e) { var lazyLoadInstance = e.detail.instance; @@ -144,156 +142,149 @@ class_loaded: "lazyloaded", } }, false);'; - return $script; - } - - /** - * Returns the lazyload inline script - * - * @param array $args Array of arguments to populate the lazyload script options. - * @return string - */ - public function getLazyloadScript($args = []) - { - $defaults = [ - 'base_url' => '', - 'version' => '', - 'polyfill' => false, - ]; - - $args = wp_parse_args($args, $defaults); - $min = ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) ? '' : '.min'; - $script = ''; - - if (isset($args['polyfill']) && $args['polyfill']) { - $script .= ''; - } - - /** - * Filters the script tag for the lazyload script - * - * @since 2.2.6 - * - * @param $script_tag HTML tag for the lazyload script. - */ - $script .= apply_filters('rocket_lazyload_script_tag', ''); - - return $script; - } - - /** - * Inserts in the HTML the script to replace the Youtube thumbnail by the iframe. - * - * @param array $args Array of arguments to populate the script options. - * @return void - */ - public function insertYoutubeThumbnailScript($args = []) - { - echo $this->getYoutubeThumbnailScript($args); - } - - /** - * Returns the Youtube Thumbnail inline script - * - * @param array $args Array of arguments to populate the script options. - * @return string - */ - public function getYoutubeThumbnailScript($args = []) - { - $defaults = [ - 'resolution' => 'hqdefault', - 'lazy_image' => false, - ]; - - $allowed_resolutions = [ - 'default' => [ - 'width' => 120, - 'height' => 90, - ], - 'mqdefault' => [ - 'width' => 320, - 'height' => 180, - ], - 'hqdefault' => [ - 'width' => 480, - 'height' => 360, - ], - 'sddefault' => [ - 'width' => 640, - 'height' => 480, - ], - - 'maxresdefault' => [ - 'width' => 1280, - 'height' => 720, - ], - ]; - - $args['resolution'] = ( isset($args['resolution']) && isset($allowed_resolutions[ $args['resolution'] ]) ) ? $args['resolution'] : 'hqdefault'; - - $args = wp_parse_args($args, $defaults); - - $image = ''; - - if (isset($args['lazy_image']) && $args['lazy_image']) { - $image = ''; - } - - return ""; - } - - /** - * Inserts the CSS to style the Youtube thumbnail container - * - * @param array $args Array of arguments to populate the CSS. - * @return void - */ - public function insertYoutubeThumbnailCSS($args = []) - { - wp_register_style('rocket-lazyload', false); - wp_enqueue_style('rocket-lazyload'); - wp_add_inline_style('rocket-lazyload', $this->getYoutubeThumbnailCSS($args)); - } - - /** - * Returns the CSS for the Youtube Thumbnail - * - * @param array $args Array of arguments to populate the CSS. - * @return string - */ - public function getYoutubeThumbnailCSS($args = []) - { - $defaults = [ - 'base_url' => '', - 'responsive_embeds' => true, - ]; - - $args = wp_parse_args($args, $defaults); - - $css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . $args['base_url'] . 'img/youtube.png) no-repeat;cursor:pointer}'; - - if ($args['responsive_embeds']) { - $css .= '.wp-has-aspect-ratio .rll-youtube-player{position:absolute;padding-bottom:0;width:100%;height:100%;top:0;bottom:0;left:0;right:0}'; - } - - return $css; - } - - /** - * Inserts the CSS needed when Javascript is not enabled to keep the display correct - */ - public function insertNoJSCSS() - { - echo $this->getNoJSCSS(); - } - - /** - * Returns the CSS to correctly display images when JavaScript is disabled - * - * @return string - */ - public function getNoJSCSS() - { - return ''; - } + return $script; + } + + /** + * Returns the lazyload inline script + * + * @param array $args Array of arguments to populate the lazyload script options. + * @return string + */ + public function getLazyloadScript( $args = [] ) { + $defaults = [ + 'base_url' => '', + 'version' => '', + 'polyfill' => false, + ]; + + $args = wp_parse_args( $args, $defaults ); + $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; + $script = ''; + + if ( isset( $args['polyfill'] ) && $args['polyfill'] ) { + $script .= ''; + } + + /** + * Filters the script tag for the lazyload script + * + * @since 2.2.6 + * + * @param $script_tag HTML tag for the lazyload script. + */ + $script .= apply_filters( 'rocket_lazyload_script_tag', '' ); + + return $script; + } + + /** + * Inserts in the HTML the script to replace the Youtube thumbnail by the iframe. + * + * @param array $args Array of arguments to populate the script options. + * @return void + */ + public function insertYoutubeThumbnailScript( $args = [] ) { + echo $this->getYoutubeThumbnailScript( $args ); + } + + /** + * Returns the Youtube Thumbnail inline script + * + * @param array $args Array of arguments to populate the script options. + * @return string + */ + public function getYoutubeThumbnailScript( $args = [] ) { + $defaults = [ + 'resolution' => 'hqdefault', + 'lazy_image' => false, + ]; + + $allowed_resolutions = [ + 'default' => [ + 'width' => 120, + 'height' => 90, + ], + 'mqdefault' => [ + 'width' => 320, + 'height' => 180, + ], + 'hqdefault' => [ + 'width' => 480, + 'height' => 360, + ], + 'sddefault' => [ + 'width' => 640, + 'height' => 480, + ], + + 'maxresdefault' => [ + 'width' => 1280, + 'height' => 720, + ], + ]; + + $args['resolution'] = ( isset( $args['resolution'] ) && isset( $allowed_resolutions[ $args['resolution'] ] ) ) ? $args['resolution'] : 'hqdefault'; + + $args = wp_parse_args( $args, $defaults ); + + $image = ''; + + if ( isset( $args['lazy_image'] ) && $args['lazy_image'] ) { + $image = ''; + } + + return ""; + } + + /** + * Inserts the CSS to style the Youtube thumbnail container + * + * @param array $args Array of arguments to populate the CSS. + * @return void + */ + public function insertYoutubeThumbnailCSS( $args = [] ) { + wp_register_style( 'rocket-lazyload', false ); + wp_enqueue_style( 'rocket-lazyload' ); + wp_add_inline_style( 'rocket-lazyload', $this->getYoutubeThumbnailCSS( $args ) ); + } + + /** + * Returns the CSS for the Youtube Thumbnail + * + * @param array $args Array of arguments to populate the CSS. + * @return string + */ + public function getYoutubeThumbnailCSS( $args = [] ) { + $defaults = [ + 'base_url' => '', + 'responsive_embeds' => true, + ]; + + $args = wp_parse_args( $args, $defaults ); + + $css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . $args['base_url'] . 'img/youtube.png) no-repeat;cursor:pointer}'; + + if ( $args['responsive_embeds'] ) { + $css .= '.wp-has-aspect-ratio .rll-youtube-player{position:absolute;padding-bottom:0;width:100%;height:100%;top:0;bottom:0;left:0;right:0}'; + } + + return $css; + } + + /** + * Inserts the CSS needed when Javascript is not enabled to keep the display correct + */ + public function insertNoJSCSS() { + echo $this->getNoJSCSS(); + } + + /** + * Returns the CSS to correctly display images when JavaScript is disabled + * + * @return string + */ + public function getNoJSCSS() { + return ''; + } } diff --git a/src/Dependencies/RocketLazyload/Iframe.php b/src/Dependencies/RocketLazyload/Iframe.php index 264df1e..a4fc295 100644 --- a/src/Dependencies/RocketLazyload/Iframe.php +++ b/src/Dependencies/RocketLazyload/Iframe.php @@ -10,229 +10,223 @@ /** * A class to provide the methods needed to lazyload iframes in WP Rocket and Lazyload by WP Rocket */ -class Iframe -{ - /** - * Finds iframes in the HTML provided and call the methods to lazyload them - * - * @param string $html Original HTML. - * @param string $buffer Content to parse. - * @param array $args Array of arguments to use. - * @return string - */ - public function lazyloadIframes($html, $buffer, $args = []) - { - $defaults = [ - 'youtube' => false, - ]; - - $args = wp_parse_args($args, $defaults); - - if (! preg_match_all('@\s.+)>.*@iUs', $buffer, $iframes, PREG_SET_ORDER) ) { - return $html; - } - - $iframes = array_unique($iframes, SORT_REGULAR); - - foreach ($iframes as $iframe) { - if ($this->isIframeExcluded($iframe)) { - continue; - } - - // Given the previous regex pattern, $iframe['atts'] starts with a whitespace character. - if (! preg_match('@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', $iframe['atts'], $atts)) { - continue; - } - - $iframe['src'] = trim($atts['src']); - - if ('' === $iframe['src']) { - continue; - } - - if ($args['youtube']) { - $iframe_lazyload = $this->replaceYoutubeThumbnail($iframe); - } - - if (empty($iframe_lazyload)) { - $iframe_lazyload = $this->replaceIframe($iframe); - } - - $html = str_replace($iframe[0], $iframe_lazyload, $html); - - unset($iframe_lazyload); - } - - return $html; - } - - /** - * Checks if the provided iframe is excluded from lazyload - * - * @param array $iframe Array of matched patterns. - * @return boolean - */ - public function isIframeExcluded($iframe) - { - - foreach ($this->getExcludedPatterns() as $excluded_pattern) { - if (strpos($iframe[0], $excluded_pattern) !== false) { - return true; - } - } - - return false; - } - - /** - * Gets patterns excluded from lazyload for iframes - * - * @since 2.1.1 - * - * @return array - */ - private function getExcludedPatterns() - { - /** - * Filters the patterns excluded from lazyload for iframes - * - * @since 2.1.1 - * - * @param array $excluded_patterns Array of excluded patterns. - */ - return apply_filters( - 'rocket_lazyload_iframe_excluded_patterns', - [ - 'gform_ajax_frame', - 'data-no-lazy=', - 'recaptcha/api/fallback', - 'loading="eager"', - 'data-skip-lazy', - 'skip-lazy', - ] - ); - } - - /** - * Applies lazyload on the iframe provided - * - * @param array $iframe Array of matched elements. - * @return string - */ - private function replaceIframe($iframe) - { - /** - * Filter the LazyLoad placeholder on src attribute - * - * @since 1.0 - * - * @param string $placeholder placeholder that will be printed. - */ - $placeholder = apply_filters('rocket_lazyload_placeholder', 'about:blank'); - - $placeholder_atts = str_replace($iframe['src'], $placeholder, $iframe['atts']); - $iframe_lazyload = str_replace($iframe['atts'], $placeholder_atts . ' data-rocket-lazyload="fitvidscompatible" data-lazy-src="' . esc_url($iframe['src']) . '"', $iframe[0]); - - if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $iframe_lazyload)) { - $iframe_lazyload = str_replace(''; - - return $iframe_lazyload; - } - - /** - * Replaces the iframe provided by the Youtube thumbnail - * - * @param array $iframe Array of matched elements. - * @return bool|string - */ - private function replaceYoutubeThumbnail($iframe) - { - $youtube_id = $this->getYoutubeIDFromURL($iframe['src']); - - if (! $youtube_id) { - return false; - } - - $query = wp_parse_url(htmlspecialchars_decode($iframe['src']), PHP_URL_QUERY); - - $youtube_url = $this->changeYoutubeUrlForYoutuDotBe( $iframe['src'] ); - $youtube_url = $this->cleanYoutubeUrl( $iframe['src'] ); - /** - * Filter the LazyLoad HTML output on Youtube iframes - * - * @since 2.11 - * - * @param array $html Output that will be printed. - */ - $youtube_lazyload = apply_filters('rocket_lazyload_youtube_html', '
'); - $youtube_lazyload .= ''; - - return $youtube_lazyload; - } - - /** - * Gets the Youtube ID from the URL provided - * - * @param string $url URL to search. - * @return bool|string - */ - public function getYoutubeIDFromURL($url) - { - $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; - $result = preg_match($pattern, $url, $matches); - - if (! $result) { - return false; - } - - // exclude playlist. - if ('videoseries' === $matches[1]) { - return false; - } - - return $matches[1]; - } - - /** - * Changes URL youtu.be/ID to youtube.com/embed/ID - * - * @param string $url URL to replace. - * @return string Unchanged URL or modified URL. - */ - public function changeYoutubeUrlForYoutuDotBe( $url ) { - $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; - $result = preg_match( $pattern, $url, $matches ); - - if ( ! $result ) { - return $url; - } - - return 'https://www.youtube.com/embed/' . $matches[1]; - } - - /** - * Cleans Youtube URL. Keeps only scheme, host and path. - * - * @param string $url URL to be cleaned. - * @return string Cleaned URL - */ - public function cleanYoutubeUrl ( $url ) { - $parsed_url = wp_parse_url( $url, -1 ); - $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '//'; - $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; - $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; - - return $scheme . $host . $path; - } +class Iframe { + + /** + * Finds iframes in the HTML provided and call the methods to lazyload them + * + * @param string $html Original HTML. + * @param string $buffer Content to parse. + * @param array $args Array of arguments to use. + * @return string + */ + public function lazyloadIframes( $html, $buffer, $args = [] ) { + $defaults = [ + 'youtube' => false, + ]; + + $args = wp_parse_args( $args, $defaults ); + + if ( ! preg_match_all( '@\s.+)>.*@iUs', $buffer, $iframes, PREG_SET_ORDER ) ) { + return $html; + } + + $iframes = array_unique( $iframes, SORT_REGULAR ); + + foreach ( $iframes as $iframe ) { + if ( $this->isIframeExcluded( $iframe ) ) { + continue; + } + + // Given the previous regex pattern, $iframe['atts'] starts with a whitespace character. + if ( ! preg_match( '@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', $iframe['atts'], $atts ) ) { + continue; + } + + $iframe['src'] = trim( $atts['src'] ); + + if ( '' === $iframe['src'] ) { + continue; + } + + if ( $args['youtube'] ) { + $iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe ); + } + + if ( empty( $iframe_lazyload ) ) { + $iframe_lazyload = $this->replaceIframe( $iframe ); + } + + $html = str_replace( $iframe[0], $iframe_lazyload, $html ); + + unset( $iframe_lazyload ); + } + + return $html; + } + + /** + * Checks if the provided iframe is excluded from lazyload + * + * @param array $iframe Array of matched patterns. + * @return boolean + */ + public function isIframeExcluded( $iframe ) { + + foreach ( $this->getExcludedPatterns() as $excluded_pattern ) { + if ( strpos( $iframe[0], $excluded_pattern ) !== false ) { + return true; + } + } + + return false; + } + + /** + * Gets patterns excluded from lazyload for iframes + * + * @since 2.1.1 + * + * @return array + */ + private function getExcludedPatterns() { + /** + * Filters the patterns excluded from lazyload for iframes + * + * @since 2.1.1 + * + * @param array $excluded_patterns Array of excluded patterns. + */ + return apply_filters( + 'rocket_lazyload_iframe_excluded_patterns', + [ + 'gform_ajax_frame', + 'data-no-lazy=', + 'recaptcha/api/fallback', + 'loading="eager"', + 'data-skip-lazy', + 'skip-lazy', + ] + ); + } + + /** + * Applies lazyload on the iframe provided + * + * @param array $iframe Array of matched elements. + * @return string + */ + private function replaceIframe( $iframe ) { + /** + * Filter the LazyLoad placeholder on src attribute + * + * @since 1.0 + * + * @param string $placeholder placeholder that will be printed. + */ + $placeholder = apply_filters( 'rocket_lazyload_placeholder', 'about:blank' ); + + $placeholder_atts = str_replace( $iframe['src'], $placeholder, $iframe['atts'] ); + $iframe_lazyload = str_replace( $iframe['atts'], $placeholder_atts . ' data-rocket-lazyload="fitvidscompatible" data-lazy-src="' . esc_url( $iframe['src'] ) . '"', $iframe[0] ); + + if ( ! preg_match( '@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $iframe_lazyload ) ) { + $iframe_lazyload = str_replace( ''; + + return $iframe_lazyload; + } + + /** + * Replaces the iframe provided by the Youtube thumbnail + * + * @param array $iframe Array of matched elements. + * @return bool|string + */ + private function replaceYoutubeThumbnail( $iframe ) { + $youtube_id = $this->getYoutubeIDFromURL( $iframe['src'] ); + + if ( ! $youtube_id ) { + return false; + } + + $query = wp_parse_url( htmlspecialchars_decode( $iframe['src'] ), PHP_URL_QUERY ); + + $youtube_url = $this->changeYoutubeUrlForYoutuDotBe( $iframe['src'] ); + $youtube_url = $this->cleanYoutubeUrl( $iframe['src'] ); + /** + * Filter the LazyLoad HTML output on Youtube iframes + * + * @since 2.11 + * + * @param array $html Output that will be printed. + */ + $youtube_lazyload = apply_filters( 'rocket_lazyload_youtube_html', '
' ); + $youtube_lazyload .= ''; + + return $youtube_lazyload; + } + + /** + * Gets the Youtube ID from the URL provided + * + * @param string $url URL to search. + * @return bool|string + */ + public function getYoutubeIDFromURL( $url ) { + $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; + $result = preg_match( $pattern, $url, $matches ); + + if ( ! $result ) { + return false; + } + + // exclude playlist. + if ( 'videoseries' === $matches[1] ) { + return false; + } + + return $matches[1]; + } + + /** + * Changes URL youtu.be/ID to youtube.com/embed/ID + * + * @param string $url URL to replace. + * @return string Unchanged URL or modified URL. + */ + public function changeYoutubeUrlForYoutuDotBe( $url ) { + $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; + $result = preg_match( $pattern, $url, $matches ); + + if ( ! $result ) { + return $url; + } + + return 'https://www.youtube.com/embed/' . $matches[1]; + } + + /** + * Cleans Youtube URL. Keeps only scheme, host and path. + * + * @param string $url URL to be cleaned. + * @return string Cleaned URL + */ + public function cleanYoutubeUrl( $url ) { + $parsed_url = wp_parse_url( $url, -1 ); + $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '//'; + $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; + $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; + + return $scheme . $host . $path; + } } diff --git a/src/Dependencies/RocketLazyload/Image.php b/src/Dependencies/RocketLazyload/Image.php index b3daab2..7b14ad0 100644 --- a/src/Dependencies/RocketLazyload/Image.php +++ b/src/Dependencies/RocketLazyload/Image.php @@ -10,476 +10,456 @@ /** * A class to provide the methods needed to lazyload images in WP Rocket and Lazyload by WP Rocket */ -class Image -{ - /** - * Finds the images to be lazyloaded and call the callback method to replace them. - * - * @param string $html Original HTML. - * @param string $buffer Content to parse. - * @return string - */ - public function lazyloadImages($html, $buffer) - { - if (! preg_match_all('#\s.+)\s?/?>#iUs', $buffer, $images, PREG_SET_ORDER)) { - return $html; - } - - $images = array_unique($images, SORT_REGULAR); - - foreach ($images as $image) { - $image = $this->canLazyload($image); - - if (! $image) { - continue; - } - - $image_lazyload = $this->replaceImage($image); - $image_lazyload .= $this->noscript($image[0]); - $html = str_replace($image[0], $image_lazyload, $html); - - unset($image_lazyload); - } - - return $html; - } - - /** - * Applies lazyload on background images defined in style attributes - * - * @param string $html Original HTML. - * @param string $buffer Content to parse. - * @return string - */ - public function lazyloadBackgroundImages($html, $buffer) - { - if (! preg_match_all('#<(?div|figure|section|span|li)\s+(?[^>]+[\'"\s])?style\s*=\s*([\'"])(?.*?)\3(?[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER)) { - return $html; - } - - foreach ($elements as $element) { - if ($this->isExcluded($element['before'] . $element['after'], $this->getExcludedAttributes())) { - continue; - } - - if (! preg_match('#background-image\s*:\s*(?\s*url\s*\((?[^)]+)\))\s*;?#is', $element['styles'], $url)) { - continue; - } - - $url['url'] = trim($url['url'], '\'" '); - - if ($this->isExcluded($url['url'], $this->getExcludedSrc())) { - continue; - } - - $lazy_bg = $this->addLazyCLass($element[0]); - $lazy_bg = str_replace($url[0], '', $lazy_bg); - $lazy_bg = str_replace('<' . $element['tag'], '<' . $element['tag'] . ' data-bg="url(' . esc_attr($url['url']) . ')"', $lazy_bg); - - $html = str_replace($element[0], $lazy_bg, $html); - unset($lazy_bg); - } - - return $html; - } - - /** - * Add the identifier class to the element - * - * @param string $element Element to add the class to. - * @return string - */ - private function addLazyClass($element) - { - if (preg_match('#class=["\']?(?[^"\'>]*)["\']?#is', $element, $class)) { - if (empty($class['classes'])) { - return str_replace($class[0], 'class="rocket-lazyload"', $element); - } - - $classes = str_replace($class['classes'], $class['classes'] . ' rocket-lazyload', $class[0]); - - return str_replace($class[0], $classes, $element); - } - - return preg_replace('#<(img|div|figure|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element); - } - - /** - * Applies lazyload on picture elements found in the HTML. - * - * @param string $html Original HTML. - * @param string $buffer Content to parse. - * @return string - */ - public function lazyloadPictures($html, $buffer) - { - if (! preg_match_all('#(?.*)#iUs', $buffer, $pictures, PREG_SET_ORDER)) { - return $html; - } - - $pictures = array_unique($pictures, SORT_REGULAR); - $excluded = array_merge($this->getExcludedAttributes(), $this->getExcludedSrc()); - - foreach ($pictures as $picture) { - if ($this->isExcluded($picture[0], $excluded)) { - continue; - } - - $lazy_sources = 0; - - if (preg_match_all('#\s.+)>#iUs', $picture['sources'], $sources, PREG_SET_ORDER)) { - $sources = array_unique($sources, SORT_REGULAR); - - foreach ($sources as $source) { - $lazyload_srcset = preg_replace('/([\s"\'])srcset/i', '\1data-lazy-srcset', $source[0]); - $html = str_replace($source[0], $lazyload_srcset, $html); - - unset($lazyload_srcset); - $lazy_sources++; - } - } - - if (0 === $lazy_sources) { - continue; - } - - if (! preg_match('#\s.+)\s?/?>#iUs', $picture[0], $img)) { - continue; - } - - $img = $this->canLazyload($img); - - if (! $img) { - continue; - } - - $img_lazy = $this->replaceImage($img); - $html = str_replace($img[0], $img_lazy, $html); - - unset($img_lazy); - } - - return $html; - } - - /** - * Checks if the image can be lazyloaded - * - * @param Array $image Array of image data coming from Regex. - * @return bool|Array - */ - private function canLazyload($image) - { - if ($this->isExcluded($image['atts'], $this->getExcludedAttributes())) { - return false; - } - - // Given the previous regex pattern, $image['atts'] starts with a whitespace character. - if (! preg_match('@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts)) { - return false; - } - - $image['src'] = trim($atts['src']); - - if ('' === $image['src']) { - return false; - } - - if ($this->isExcluded($image['src'], $this->getExcludedSrc())) { - return false; - } - - // Don't apply LazyLoad on images from WP Retina x2. - if (function_exists('wr2x_picture_rewrite')) { - if (wr2x_get_retina(trailingslashit(ABSPATH) . wr2x_get_pathinfo_from_image_src(trim($image['src'], '"')))) { - return false; - } - } - - return $image; - } - - /** - * Checks if the provided string matches with the provided excluded patterns - * - * @param string $string String to check. - * @param array $excluded_values Patterns to match against. - * @return boolean - */ - public function isExcluded($string, $excluded_values) - { - if (! is_array($excluded_values)) { - (array) $excluded_values; - } - - if (empty($excluded_values)) { - return false; - } - - foreach ($excluded_values as $excluded_value) { - if (strpos($string, $excluded_value) !== false) { - return true; - } - } - - return false; - } - - /** - * Returns the list of excluded attributes - * - * @return array - */ - public function getExcludedAttributes() - { - /** - * Filters the attributes used to prevent lazylad from being applied - * - * @since 1.0 - * @author Remy Perona - * - * @param array $excluded_attributes An array of excluded attributes. - */ - return apply_filters( - 'rocket_lazyload_excluded_attributes', - [ - 'data-src=', - 'data-no-lazy=', - 'data-lazy-original=', - 'data-lazy-src=', - 'data-lazysrc=', - 'data-lazyload=', - 'data-bgposition=', - 'data-envira-src=', - 'fullurl=', - 'lazy-slider-img=', - 'data-srcset=', - 'class="ls-l', - 'class="ls-bg', - 'soliloquy-image', - 'loading="eager"', - 'swatch-img', - 'data-height-percentage', - 'data-large_image', - 'avia-bg-style-fixed', - 'data-skip-lazy', - 'skip-lazy', - ] - ); - } - - /** - * Returns the list of excluded src - * - * @return array - */ - public function getExcludedSrc() - { - /** - * Filters the src used to prevent lazylad from being applied - * - * @since 1.0 - * @author Remy Perona - * - * @param array $excluded_src An array of excluded src. - */ - return apply_filters( - 'rocket_lazyload_excluded_src', - [ - '/wpcf7_captcha/', - 'timthumb.php?src', - 'woocommerce/assets/images/placeholder.png', - ] - ); - } - - /** - * Replaces the original image by the lazyload one - * - * @param array $image Array of matches elements. - * @return string - */ - private function replaceImage($image) - { - $width = 0; - $height = 0; - - if (preg_match('@[\s"\']width\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts)) { - $width = absint($atts['width']); - } - - if (preg_match('@[\s"\']height\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts)) { - $height = absint($atts['height']); - } - - $placeholder_atts = preg_replace('@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', ' src="' . $this->getPlaceholder($width, $height) . '"', $image['atts']); - - $image_lazyload = str_replace($image['atts'], $placeholder_atts . ' data-lazy-src="' . $image['src'] . '"', $image[0]); - - /** - * Filters the use of native lazyload - * - * @since 2.5 - * @param bool $use_native True to enable native lazyload usage. - */ - if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload) && apply_filters('rocket_use_native_lazyload', false)) { - $image_lazyload = str_replace('' . $element . ''; - } - - /** - * Applies lazyload on srcset and sizes attributes - * - * @param string $html HTML image tag. - * @return string - */ - public function lazyloadResponsiveAttributes($html) - { - $html = preg_replace('/[\s|"|\'](srcset)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html); - $html = preg_replace('/[\s|"|\'](sizes)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html); - - return $html; - } - - /** - * Finds patterns matching smiley and call the callback method to replace them with the image - * - * @param string $text Content to search in. - * @return string - */ - public function convertSmilies($text) - { - global $wp_smiliessearch; - - if (! get_option('use_smilies') || empty($wp_smiliessearch)) { - return $text; - } - - $output = ''; - // HTML loop taken from texturize function, could possible be consolidated. - $textarr = preg_split('/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between. - $stop = count($textarr);// loop stuff. - - // Ignore proessing of specific tags. - $tags_to_ignore = 'code|pre|style|script|textarea'; - $ignore_block_element = ''; - - for ($i = 0; $i < $stop; $i++) { - $content = $textarr[ $i ]; - - // If we're in an ignore block, wait until we find its closing tag. - if ('' === $ignore_block_element && preg_match('/^<(' . $tags_to_ignore . ')>/', $content, $matches)) { - $ignore_block_element = $matches[1]; - } - - // If it's not a tag and not in ignore block. - if ('' === $ignore_block_element && strlen($content) > 0 && '<' !== $content[0]) { - $content = preg_replace_callback($wp_smiliessearch, [$this, 'translateSmiley'], $content); - } - - // did we exit ignore block. - if ('' !== $ignore_block_element && '' === $content) { - $ignore_block_element = ''; - } - - $output .= $content; - } - - return $output; - } - - /** - * Replace matches by smiley image, lazyloaded - * - * @param array $matches Array of matches. - * @return string - */ - private function translateSmiley($matches) - { - global $wpsmiliestrans; - - if (count($matches) === 0) { - return ''; - } - - $smiley = trim(reset($matches)); - $img = $wpsmiliestrans[ $smiley ]; - - $matches = []; - $ext = preg_match('/\.([^.]+)$/', $img, $matches) ? strtolower($matches[1]) : false; - $image_exts = ['jpg', 'jpeg', 'jpe', 'gif', 'png']; - - // Don't convert smilies that aren't images - they're probably emoji. - if (! in_array($ext, $image_exts, true)) { - return $img; - } - - /** - * Filter the Smiley image URL before it's used in the image element. - * - * @since 2.9.0 - * - * @param string $smiley_url URL for the smiley image. - * @param string $img Filename for the smiley image. - * @param string $site_url Site URL, as returned by site_url(). - */ - $src_url = apply_filters('smilies_src', includes_url("images/smilies/$img"), $img, site_url()); - - // Don't LazyLoad if process is stopped for these reasons. - if (is_feed() || is_preview()) { - return sprintf(' %s ', esc_url($src_url), esc_attr($smiley)); - } - - return sprintf(' %s ', $this->getPlaceholder(), esc_url($src_url), esc_attr($smiley)); - } - - /** - * Returns the placeholder for the src attribute - * - * @since 1.2 - * @author Remy Perona - * - * @param int $width Width of the placeholder image. Default 0. - * @param int $height Height of the placeholder image. Default 0. - * @return string - */ - public function getPlaceholder($width = 0, $height = 0) - { - $width = 0 === $width ? 0 : absint($width); - $height = 0 === $height ? 0 : absint($height); - - $placeholder = str_replace(' ', '%20', "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 $width $height'%3E%3C/svg%3E"); - /** - * Filter the image lazyLoad placeholder on src attribute - * - * @since 1.1 - * - * @param string $placeholder Placeholder that will be printed. - * @param int $width Placeholder width. - * @param int $height Placeholder height. - */ - return apply_filters('rocket_lazyload_placeholder', $placeholder, $width, $height); - } +class Image { + + /** + * Finds the images to be lazyloaded and call the callback method to replace them. + * + * @param string $html Original HTML. + * @param string $buffer Content to parse. + * @return string + */ + public function lazyloadImages( $html, $buffer ) { + if ( ! preg_match_all( '#\s.+)\s?/?>#iUs', $buffer, $images, PREG_SET_ORDER ) ) { + return $html; + } + + $images = array_unique( $images, SORT_REGULAR ); + + foreach ( $images as $image ) { + $image = $this->canLazyload( $image ); + + if ( ! $image ) { + continue; + } + + $image_lazyload = $this->replaceImage( $image ); + $image_lazyload .= $this->noscript( $image[0] ); + $html = str_replace( $image[0], $image_lazyload, $html ); + + unset( $image_lazyload ); + } + + return $html; + } + + /** + * Applies lazyload on background images defined in style attributes + * + * @param string $html Original HTML. + * @param string $buffer Content to parse. + * @return string + */ + public function lazyloadBackgroundImages( $html, $buffer ) { + if ( ! preg_match_all( '#<(?div|figure|section|span|li)\s+(?[^>]+[\'"\s])?style\s*=\s*([\'"])(?.*?)\3(?[^>]*)>#is', $buffer, $elements, PREG_SET_ORDER ) ) { + return $html; + } + + foreach ( $elements as $element ) { + if ( $this->isExcluded( $element['before'] . $element['after'], $this->getExcludedAttributes() ) ) { + continue; + } + + if ( ! preg_match( '#background-image\s*:\s*(?\s*url\s*\((?[^)]+)\))\s*;?#is', $element['styles'], $url ) ) { + continue; + } + + $url['url'] = trim( $url['url'], '\'" ' ); + + if ( $this->isExcluded( $url['url'], $this->getExcludedSrc() ) ) { + continue; + } + + $lazy_bg = $this->addLazyCLass( $element[0] ); + $lazy_bg = str_replace( $url[0], '', $lazy_bg ); + $lazy_bg = str_replace( '<' . $element['tag'], '<' . $element['tag'] . ' data-bg="url(' . esc_attr( $url['url'] ) . ')"', $lazy_bg ); + + $html = str_replace( $element[0], $lazy_bg, $html ); + unset( $lazy_bg ); + } + + return $html; + } + + /** + * Add the identifier class to the element + * + * @param string $element Element to add the class to. + * @return string + */ + private function addLazyClass( $element ) { + if ( preg_match( '#class=["\']?(?[^"\'>]*)["\']?#is', $element, $class ) ) { + if ( empty( $class['classes'] ) ) { + return str_replace( $class[0], 'class="rocket-lazyload"', $element ); + } + + $classes = str_replace( $class['classes'], $class['classes'] . ' rocket-lazyload', $class[0] ); + + return str_replace( $class[0], $classes, $element ); + } + + return preg_replace( '#<(img|div|figure|section|li|span)([^>]*)>#is', '<\1 class="rocket-lazyload"\2>', $element ); + } + + /** + * Applies lazyload on picture elements found in the HTML. + * + * @param string $html Original HTML. + * @param string $buffer Content to parse. + * @return string + */ + public function lazyloadPictures( $html, $buffer ) { + if ( ! preg_match_all( '#(?.*)#iUs', $buffer, $pictures, PREG_SET_ORDER ) ) { + return $html; + } + + $pictures = array_unique( $pictures, SORT_REGULAR ); + $excluded = array_merge( $this->getExcludedAttributes(), $this->getExcludedSrc() ); + + foreach ( $pictures as $picture ) { + if ( $this->isExcluded( $picture[0], $excluded ) ) { + continue; + } + + if ( preg_match_all( '#\s.+)>#iUs', $picture['sources'], $sources, PREG_SET_ORDER ) ) { + $sources = array_unique( $sources, SORT_REGULAR ); + + $lazy_sources = 0; + + foreach ( $sources as $source ) { + $lazyload_srcset = preg_replace( '/([\s"\'])srcset/i', '\1data-lazy-srcset', $source[0] ); + $html = str_replace( $source[0], $lazyload_srcset, $html ); + + unset( $lazyload_srcset ); + $lazy_sources++; + } + } + + if ( 0 === $lazy_sources ) { + continue; + } + + if ( ! preg_match( '#\s.+)\s?/?>#iUs', $picture[0], $img ) ) { + continue; + } + + $img = $this->canLazyload( $img ); + + if ( ! $img ) { + continue; + } + + $img_lazy = $this->replaceImage( $img ); + $html = str_replace( $img[0], $img_lazy, $html ); + + unset( $img_lazy ); + } + + return $html; + } + + /** + * Checks if the image can be lazyloaded + * + * @param Array $image Array of image data coming from Regex. + * @return bool|Array + */ + private function canLazyload( $image ) { + if ( $this->isExcluded( $image['atts'], $this->getExcludedAttributes() ) ) { + return false; + } + + // Given the previous regex pattern, $image['atts'] starts with a whitespace character. + if ( ! preg_match( '@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts ) ) { + return false; + } + + $image['src'] = trim( $atts['src'] ); + + if ( '' === $image['src'] ) { + return false; + } + + if ( $this->isExcluded( $image['src'], $this->getExcludedSrc() ) ) { + return false; + } + + // Don't apply LazyLoad on images from WP Retina x2. + if ( function_exists( 'wr2x_picture_rewrite' ) ) { + if ( wr2x_get_retina( trailingslashit( ABSPATH ) . wr2x_get_pathinfo_from_image_src( trim( $image['src'], '"' ) ) ) ) { + return false; + } + } + + return $image; + } + + /** + * Checks if the provided string matches with the provided excluded patterns + * + * @param string $string String to check. + * @param array $excluded_values Patterns to match against. + * @return boolean + */ + public function isExcluded( $string, $excluded_values ) { + if ( ! is_array( $excluded_values ) ) { + (array) $excluded_values; + } + + if ( empty( $excluded_values ) ) { + return false; + } + + foreach ( $excluded_values as $excluded_value ) { + if ( strpos( $string, $excluded_value ) !== false ) { + return true; + } + } + + return false; + } + + /** + * Returns the list of excluded attributes + * + * @return array + */ + public function getExcludedAttributes() { + /** + * Filters the attributes used to prevent lazylad from being applied + * + * @since 1.0 + * @author Remy Perona + * + * @param array $excluded_attributes An array of excluded attributes. + */ + return apply_filters( + 'rocket_lazyload_excluded_attributes', + [ + 'data-src=', + 'data-no-lazy=', + 'data-lazy-original=', + 'data-lazy-src=', + 'data-lazysrc=', + 'data-lazyload=', + 'data-bgposition=', + 'data-envira-src=', + 'fullurl=', + 'lazy-slider-img=', + 'data-srcset=', + 'class="ls-l', + 'class="ls-bg', + 'soliloquy-image', + 'loading="eager"', + 'swatch-img', + 'data-height-percentage', + 'data-large_image', + 'avia-bg-style-fixed', + 'data-skip-lazy', + 'skip-lazy', + ] + ); + } + + /** + * Returns the list of excluded src + * + * @return array + */ + public function getExcludedSrc() { + /** + * Filters the src used to prevent lazylad from being applied + * + * @since 1.0 + * @author Remy Perona + * + * @param array $excluded_src An array of excluded src. + */ + return apply_filters( + 'rocket_lazyload_excluded_src', + [ + '/wpcf7_captcha/', + 'timthumb.php?src', + 'woocommerce/assets/images/placeholder.png', + ] + ); + } + + /** + * Replaces the original image by the lazyload one + * + * @param array $image Array of matches elements. + * @return string + */ + private function replaceImage( $image ) { + $width = 0; + $height = 0; + + if ( preg_match( '@[\s"\']width\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts ) ) { + $width = absint( $atts['width'] ); + } + + if ( preg_match( '@[\s"\']height\s*=\s*(\'|")(?.*)\1@iUs', $image['atts'], $atts ) ) { + $height = absint( $atts['height'] ); + } + + $placeholder_atts = preg_replace( '@\ssrc\s*=\s*(\'|")(?.*)\1@iUs', ' src="' . $this->getPlaceholder( $width, $height ) . '"', $image['atts'] ); + + $image_lazyload = str_replace( $image['atts'], $placeholder_atts . ' data-lazy-src="' . $image['src'] . '"', $image[0] ); + + if ( ! preg_match( '@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $image_lazyload ) && apply_filters( 'rocket_use_native_lazyload', false ) ) { + $image_lazyload = str_replace( '' . $element . ''; + } + + /** + * Applies lazyload on srcset and sizes attributes + * + * @param string $html HTML image tag. + * @return string + */ + public function lazyloadResponsiveAttributes( $html ) { + $html = preg_replace( '/[\s|"|\'](srcset)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html ); + $html = preg_replace( '/[\s|"|\'](sizes)\s*=\s*("|\')([^"|\']+)\2/i', ' data-lazy-$1=$2$3$2', $html ); + + return $html; + } + + /** + * Finds patterns matching smiley and call the callback method to replace them with the image + * + * @param string $text Content to search in. + * @return string + */ + public function convertSmilies( $text ) { + global $wp_smiliessearch; + + if ( ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) { + return $text; + } + + $output = ''; + // HTML loop taken from texturize function, could possible be consolidated. + $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between. + $stop = count( $textarr );// loop stuff. + + // Ignore proessing of specific tags. + $tags_to_ignore = 'code|pre|style|script|textarea'; + $ignore_block_element = ''; + + for ( $i = 0; $i < $stop; $i++ ) { + $content = $textarr[ $i ]; + + // If we're in an ignore block, wait until we find its closing tag. + if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) { + $ignore_block_element = $matches[1]; + } + + // If it's not a tag and not in ignore block. + if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) { + $content = preg_replace_callback( $wp_smiliessearch, [ $this, 'translateSmiley' ], $content ); + } + + // did we exit ignore block. + if ( '' !== $ignore_block_element && '' === $content ) { + $ignore_block_element = ''; + } + + $output .= $content; + } + + return $output; + } + + /** + * Replace matches by smiley image, lazyloaded + * + * @param array $matches Array of matches. + * @return string + */ + private function translateSmiley( $matches ) { + global $wpsmiliestrans; + + if ( count( $matches ) === 0 ) { + return ''; + } + + $smiley = trim( reset( $matches ) ); + $img = $wpsmiliestrans[ $smiley ]; + + $matches = []; + $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false; + $image_exts = [ 'jpg', 'jpeg', 'jpe', 'gif', 'png' ]; + + // Don't convert smilies that aren't images - they're probably emoji. + if ( ! in_array( $ext, $image_exts, true ) ) { + return $img; + } + + /** + * Filter the Smiley image URL before it's used in the image element. + * + * @since 2.9.0 + * + * @param string $smiley_url URL for the smiley image. + * @param string $img Filename for the smiley image. + * @param string $site_url Site URL, as returned by site_url(). + */ + $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() ); + + // Don't LazyLoad if process is stopped for these reasons. + if ( is_feed() || is_preview() ) { + return sprintf( ' %s ', esc_url( $src_url ), esc_attr( $smiley ) ); + } + + return sprintf( ' %s ', $this->getPlaceholder(), esc_url( $src_url ), esc_attr( $smiley ) ); + } + + /** + * Returns the placeholder for the src attribute + * + * @since 1.2 + * @author Remy Perona + * + * @param int $width Width of the placeholder image. Default 0. + * @param int $height Height of the placeholder image. Default 0. + * @return string + */ + public function getPlaceholder( $width = 0, $height = 0 ) { + $width = 0 === $width ? 0 : absint( $width ); + $height = 0 === $height ? 0 : absint( $height ); + + $placeholder = str_replace( ' ', '%20', "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 $width $height'%3E%3C/svg%3E" ); + /** + * Filter the image lazyLoad placeholder on src attribute + * + * @since 1.1 + * + * @param string $placeholder Placeholder that will be printed. + * @param int $width Placeholder width. + * @param int $height Placeholder height. + */ + return apply_filters( 'rocket_lazyload_placeholder', $placeholder, $width, $height ); + } } diff --git a/src/Plugin.php b/src/Plugin.php index c9538cf..d7483b4 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -17,81 +17,85 @@ * @since 2.0 * @author Remy Perona */ -class Plugin -{ - /** - * Is the plugin loaded - * - * @since 2.0 - * @author Remy Perona - * - * @var boolean - */ - private $loaded = false; +class Plugin { - /** - * Checks if the plugin is loaded - * - * @since 2.0 - * @author Remy Perona - * - * @return boolean - */ - private function isLoaded() - { - return $this->loaded; - } + /** + * Is the plugin loaded + * + * @since 2.0 + * @author Remy Perona + * + * @var boolean + */ + private $loaded = false; - /** - * Loads the plugin in WordPress - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function load() - { - if ($this->isLoaded()) { - return; - } + /** + * Checks if the plugin is loaded + * + * @since 2.0 + * @author Remy Perona + * + * @return boolean + */ + private function isLoaded() { + return $this->loaded; + } - $container = new Container(); + /** + * Loads the plugin in WordPress + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function load() { + if ( $this->isLoaded() ) { + return; + } - $container->add('template_path', \ROCKET_LL_PATH . 'views/'); - $container->add('plugin_basename', \ROCKET_LL_BASENAME); + $container = new Container(); - $container->add('options', function () { - return new Options('rocket_lazyload'); - }); + $container->add( 'template_path', \ROCKET_LL_PATH . 'views/' ); + $container->add( 'plugin_basename', \ROCKET_LL_BASENAME ); - $container->add('event_manager', function () { - return new EventManager(); - }); + $container->add( + 'options', + function () { + return new Options( 'rocket_lazyload' ); + } + ); - $service_providers = [ - 'RocketLazyLoadPlugin\ServiceProvider\OptionServiceProvider', - 'RocketLazyLoadPlugin\ServiceProvider\AdminServiceProvider', - 'RocketLazyLoadPlugin\ServiceProvider\ImagifyNoticeServiceProvider', - 'RocketLazyLoadPlugin\ServiceProvider\LazyloadServiceProvider', - 'RocketLazyLoadPlugin\ServiceProvider\SubscribersServiceProvider', - ]; + $container->add( + 'event_manager', + function () { + return new EventManager(); + } + ); - foreach ($service_providers as $service) { - $container->addServiceProvider($service); - } + $service_providers = [ + 'RocketLazyLoadPlugin\ServiceProvider\OptionServiceProvider', + 'RocketLazyLoadPlugin\ServiceProvider\AdminServiceProvider', + 'RocketLazyLoadPlugin\ServiceProvider\ImagifyNoticeServiceProvider', + 'RocketLazyLoadPlugin\ServiceProvider\LazyloadServiceProvider', + 'RocketLazyLoadPlugin\ServiceProvider\SubscribersServiceProvider', + ]; - $subscribers = [ - 'RocketLazyLoadPlugin\Subscriber\ThirdParty\AMPSubscriber', - 'RocketLazyLoadPlugin\Subscriber\AdminPageSubscriber', - 'RocketLazyLoadPlugin\Subscriber\ImagifyNoticeSubscriber', - 'RocketLazyLoadPlugin\Subscriber\LazyloadSubscriber', - ]; + foreach ( $service_providers as $service ) { + $container->addServiceProvider( $service ); + } - foreach ($subscribers as $subscriber) { - $container->get('event_manager')->addSubscriber($container->get($subscriber)); - } + $subscribers = [ + 'RocketLazyLoadPlugin\Subscriber\ThirdParty\AMPSubscriber', + 'RocketLazyLoadPlugin\Subscriber\AdminPageSubscriber', + 'RocketLazyLoadPlugin\Subscriber\ImagifyNoticeSubscriber', + 'RocketLazyLoadPlugin\Subscriber\LazyloadSubscriber', + ]; - $this->loaded = true; - } + foreach ( $subscribers as $subscriber ) { + $container->get( 'event_manager' )->addSubscriber( $container->get( $subscriber ) ); + } + + $this->loaded = true; + } } diff --git a/src/Subscriber/AdminPageSubscriber.php b/src/Subscriber/AdminPageSubscriber.php index c4f08c6..85b330b 100644 --- a/src/Subscriber/AdminPageSubscriber.php +++ b/src/Subscriber/AdminPageSubscriber.php @@ -7,7 +7,7 @@ namespace RocketLazyLoadPlugin\Subscriber; -defined('ABSPATH') || die('Cheatin\' uh?'); +defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); use RocketLazyLoadPlugin\EventManagement\SubscriberInterface; use RocketLazyLoadPlugin\Admin\AdminPage; @@ -18,125 +18,121 @@ * @since 2.0 * @author Remy Perona */ -class AdminPageSubscriber implements SubscriberInterface -{ - /** - * AdminPage instance - * - * @since 2.0 - * @author Remy Perona - * - * @var AdminPage - */ - private $page; +class AdminPageSubscriber implements SubscriberInterface { - /** - * Plugin basename - * - * @since 2.0 - * @author Remy Perona - * - * @var string - */ - private $plugin_basename; + /** + * AdminPage instance + * + * @since 2.0 + * @author Remy Perona + * + * @var AdminPage + */ + private $page; - /** - * Constructor - * - * @since 2.0 - * @author Remy Perona - * - * @param AdminPage $page AdminPage instance. - * @param string $plugin_basename Plugin basename. - */ - public function __construct(AdminPage $page, $plugin_basename) - { - $this->page = $page; - $this->plugin_basename = $plugin_basename; - } + /** + * Plugin basename + * + * @since 2.0 + * @author Remy Perona + * + * @var string + */ + private $plugin_basename; - /** - * @inheritDoc - */ - public function getSubscribedEvents() - { - return [ - 'admin_init' => 'configure', - 'admin_menu' => 'addAdminPage', - 'plugin_action_links_' . $this->plugin_basename => 'addPluginPageLink', - 'admin_enqueue_scripts' => 'enqueueAdminStyle', - ]; - } + /** + * Constructor + * + * @since 2.0 + * @author Remy Perona + * + * @param AdminPage $page AdminPage instance. + * @param string $plugin_basename Plugin basename. + */ + public function __construct( AdminPage $page, $plugin_basename ) { + $this->page = $page; + $this->plugin_basename = $plugin_basename; + } - /** - * Registers the plugin settings in WordPress - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function configure() - { - $this->page->configure(); - } + /** + * Returns an array of events that this subscriber wants to listen to. + * + * @return array + */ + public function getSubscribedEvents() { + return [ + 'admin_init' => 'configure', + 'admin_menu' => 'addAdminPage', + 'plugin_action_links_' . $this->plugin_basename => 'addPluginPageLink', + 'admin_enqueue_scripts' => 'enqueueAdminStyle', + ]; + } - /** - * Adds the admin page to the settings menu - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function addAdminPage() - { - add_options_page( - $this->page->getPageTitle(), - $this->page->getMenuTitle(), - $this->page->getCapability(), - $this->page->getSlug(), - [ $this->page, 'renderPage' ] - ); - } + /** + * Registers the plugin settings in WordPress + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function configure() { + $this->page->configure(); + } - /** - * Adds a link to the plugin settings on the plugins page - * - * @since 2.0 - * @author Remy Perona - * - * @param array $actions Actions for the plugin. - * @return array - */ - public function addPluginPageLink($actions) - { - array_unshift( - $actions, - sprintf( - '%s', - admin_url('options-general.php?page=' . $this->page->getSlug()), - __('Settings', 'rocket-lazy-load') - ) - ); + /** + * Adds the admin page to the settings menu + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function addAdminPage() { + add_options_page( + $this->page->getPageTitle(), + $this->page->getMenuTitle(), + $this->page->getCapability(), + $this->page->getSlug(), + [ $this->page, 'renderPage' ] + ); + } - return $actions; - } + /** + * Adds a link to the plugin settings on the plugins page + * + * @since 2.0 + * @author Remy Perona + * + * @param array $actions Actions for the plugin. + * @return array + */ + public function addPluginPageLink( $actions ) { + array_unshift( + $actions, + sprintf( + '%s', + admin_url( 'options-general.php?page=' . $this->page->getSlug() ), + __( 'Settings', 'rocket-lazy-load' ) + ) + ); - /** - * Enqueue the css for the option page - * - * @since 2.0 - * @author Remy Perona - * - * @param string $hook_suffix Current page hook. - */ - public function enqueueAdminStyle($hook_suffix) - { - if ('settings_page_rocket_lazyload' !== $hook_suffix) { - return; - } - - wp_enqueue_style('rocket-lazyload', \ROCKET_LL_ASSETS_URL . 'css/admin.css', null, \ROCKET_LL_VERSION); - } + return $actions; + } + + /** + * Enqueue the css for the option page + * + * @since 2.0 + * @author Remy Perona + * + * @param string $hook_suffix Current page hook. + */ + public function enqueueAdminStyle( $hook_suffix ) { + if ( 'settings_page_rocket_lazyload' !== $hook_suffix ) { + return; + } + + wp_enqueue_style( 'rocket-lazyload', \ROCKET_LL_ASSETS_URL . 'css/admin.css', null, \ROCKET_LL_VERSION ); + } } diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index 93f16fc..ae3a2de 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -19,431 +19,419 @@ * @since 2.0 * @author Remy Perona */ -class LazyloadSubscriber implements SubscriberInterface -{ - /** - * OptionArray instance - * - * @since 2.0 - * @author Remy Perona - * - * @var OptionArray - */ - private $option_array; - - /** - * Assets instance - * - * @since 2.0 - * @author Remy Perona - * - * @var Assets - */ - private $assets; - - /** - * Image instance - * - * @since 2.0 - * @author Remy Perona - * - * @var Image - */ - private $image; - - /** - * Iframe instance - * - * @since 2.0 - * @author Remy Perona - * - * @var Iframe - */ - private $iframe; - - /** - * Constructor - * - * @since 2.0 - * @author Remy Perona - * - * @param OptionArray $option_array OptionArray instance. - * @param Assets $assets Assets instance. - * @param Image $image Image instance. - * @param Iframe $iframe Iframe instance. - */ - public function __construct(OptionArray $option_array, Assets $assets, Image $image, Iframe $iframe) - { - $this->option_array = $option_array; - $this->assets = $assets; - $this->image = $image; - $this->iframe = $iframe; - } - - /** - * @inheritDoc - */ - public function getSubscribedEvents() - { - return [ - 'wp_footer' => [ - [ 'insertLazyloadScript', \ROCKET_LL_INT_MAX ], - ['insertYoutubeThumbnailScript', \ROCKET_LL_INT_MAX ], - ], - 'wp_head' => ['insertNoJSStyle', \ROCKET_LL_INT_MAX ], - 'wp_enqueue_scripts' => ['insertYoutubeThumbnailStyle', \ROCKET_LL_INT_MAX], - 'template_redirect' => ['lazyload', 2], - 'rocket_lazyload_html' => 'lazyloadResponsive', - 'init' => 'lazyloadSmilies', - ]; - } - - /** - * Inserts the lazyload script in the footer - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function insertLazyloadScript() - { - if (! $this->option_array->get('images') && ! $this->option_array->get('iframes')) { - return; - } - - if (! $this->shouldLazyload()) { - return; - } - - /** - * Filters the threshold at which lazyload is triggered - * - * @since 1.2 - * @author Remy Perona - * - * @param int $threshold Threshold value. - */ - $threshold = apply_filters('rocket_lazyload_threshold', 300); - - /** - * Filters the use of the polyfill for intersectionObserver - * - * @since 3.3 - * @author Remy Perona - * - * @param bool $polyfill True to use the polyfill, false otherwise. - */ - $polyfill = apply_filters('rocket_lazyload_polyfill', false); - - $script_args = [ - 'base_url' => ROCKET_LL_FRONT_JS_URL, - 'version' => '12.0', - 'polyfill' => $polyfill, - ]; - - $inline_args = [ - 'threshold' => $threshold, - ]; - - /** - * Filters the use of native lazyload - * - * @since 2.3.3 - * @param bool $use_native True to enable native lazyload usage. - */ - if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals - $inline_args['options'] = [ - 'use_native' => 'true', - ]; - } - - if ($this->option_array->get('images') || $this->option_array->get('iframes')) { - // This filter is documented in src/Subscriber/LazyloadSubscriber.php. - if (apply_filters('rocket_use_native_lazyload', false)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals - $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; - $inline_args['elements']['loading'] = '[loading=lazy]'; - } - } - - if ($this->option_array->get('images')) { - $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; - $inline_args['elements']['image'] = 'img[data-lazy-src]'; - $inline_args['elements']['background_image'] = '.rocket-lazyload'; - } - - if ($this->option_array->get('iframes')) { - $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; - $inline_args['elements']['iframe'] = 'iframe[data-lazy-src]'; - } - - /** - * Filters the arguments array for the lazyload script options - * - * @since 2.0 - * @author Remy Perona - * - * @param array $inline_args Arguments used for the lazyload script options. - */ - $inline_args = apply_filters('rocket_lazyload_script_args', $inline_args); - - echo ''; - $this->assets->insertLazyloadScript($script_args); - } - - /** - * Inserts the Youtube thumbnail script in the footer - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function insertYoutubeThumbnailScript() - { - if (! $this->option_array->get('youtube')) { - return; - } - - if (! $this->shouldLazyload()) { - return; - } - - /** - * Filters the resolution of the YouTube thumbnail - * - * @since 1.4.8 - * @author Arun Basil Lal - * - * @param string $thumbnail_resolution The resolution of the thumbnail. Accepted values: default, mqdefault, sddefault, hqdefault, maxresdefault - */ - $thumbnail_resolution = apply_filters('rocket_lazyload_youtube_thumbnail_resolution', 'hqdefault'); - - $this->assets->insertYoutubeThumbnailScript( - [ - 'resolution' => $thumbnail_resolution, - 'lazy_image' => (bool) $this->option_array->get('images'), - ] - ); - } - - /** - * Inserts the no JS CSS compatibility in the header - * - * @since 2.0.3 - * @author Remy Perona - * - * @return void - */ - public function insertNoJSStyle() - { - if (! $this->shouldLazyload()) { - return; - } - - $this->assets->insertNoJSCSS(); - } - - /** - * Inserts the Youtube thumbnail CSS in the header - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function insertYoutubeThumbnailStyle() - { - if (! $this->option_array->get('youtube')) { - return; - } - - if (! $this->shouldLazyload()) { - return; - } - - $this->assets->insertYoutubeThumbnailCSS( - [ - 'base_url' => ROCKET_LL_ASSETS_URL, - 'responsive_embeds' => current_theme_supports('responsive-embeds'), - ] - ); - } - - /** - * Checks if lazyload should be applied - * - * @since 2.0 - * @author Remy Perona - * - * @return bool - */ - private function shouldLazyload() - { - if (is_admin() || is_feed() || is_preview() || ( defined('REST_REQUEST') && REST_REQUEST ) || ( defined('DONOTLAZYLOAD') && DONOTLAZYLOAD )) { - return false; - } - - if ($this->isPageBuilder()) { - return false; - } - - /** - * Filters the lazyload application - * - * @since 2.0 - * @author Remy Perona - * - * @param bool $do_rocket_lazyload True to apply lazyload, false otherwise. - */ - if (! apply_filters('do_rocket_lazyload', true)) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals - return false; - } - - return true; - } - - /** - * Checks if current page is a page builder editor. - * - * @since 2.2.2 - * @author Remy Perona - * - * @return bool - */ - private function isPageBuilder() - { - // Exclude Page Builders editors. - $excluded_parameters = [ - 'fl_builder', - 'et_fb', - 'ct_builder', - ]; - - foreach ($excluded_parameters as $excluded) { - if (isset($_GET[ $excluded ])) { - return true; - } - } - - return false; - } - - /** - * Gets the content to lazyload - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function lazyload() - { - if (! $this->shouldLazyload()) { - return; - } - - ob_start([$this, 'lazyloadBuffer']); - } - - /** - * Applies lazyload on the provided content - * - * @since 2.0 - * @author Remy Perona - * - * @param string $html HTML content. - * @return string - */ - public function lazyloadBuffer($html) - { - $buffer = $this->ignoreScripts($html); - $buffer = $this->ignoreNoscripts($buffer); - - if ($this->option_array->get('images')) { - $html = $this->image->lazyloadImages($html, $buffer); - $html = $this->image->lazyloadPictures($html, $buffer); - $html = $this->image->lazyloadBackgroundImages($html, $buffer); - } - - if ($this->option_array->get('iframes')) { - $args = [ - 'youtube' => $this->option_array->get('youtube'), - ]; - - $html = $this->iframe->lazyloadIframes($html, $buffer, $args); - } - - return $html; - } - - /** - * Applies lazyload on responsive images attributes srcset and sizes - * - * @since 2.0 - * @author Remy Perona - * - * @param string $html Image HTML. - * @return string - */ - public function lazyloadResponsive($html) - { - return $this->image->lazyloadResponsiveAttributes($html); - } - - /** - * Applies lazyload on WordPress smilies - * - * @since 2.0 - * @author Remy Perona - * - * @return void - */ - public function lazyloadSmilies() - { - if (! $this->shouldLazyload()) { - return; - } - - if (! $this->option_array->get('images')) { - return; - } - - $filters = [ - 'the_content' => 10, - 'the_excerpt' => 10, - 'comment_text' => 20, - ]; - - foreach ($filters as $filter => $prio) { - if (! has_filter($filter)) { - continue; - } - - remove_filter($filter, 'convert_smilies', $prio); - add_filter($filter, [$this->image, 'convertSmilies'], $prio); - } - } - - /** - * Remove inline scripts from the HTML to parse - * - * @param string $html HTML content. - * @return string - */ - private function ignoreScripts($html) - { - return preg_replace('/]*)>(?:.+)?<\/script>/Umsi', '', $html); - } - - /** - * Remove noscript tags from the HTML to parse - * - * @param string $html HTML content. - * @return string - */ - private function ignoreNoscripts($html) - { - return preg_replace('##Umsi', '', $html); - } +class LazyloadSubscriber implements SubscriberInterface { + + /** + * OptionArray instance + * + * @since 2.0 + * @author Remy Perona + * + * @var OptionArray + */ + private $option_array; + + /** + * Assets instance + * + * @since 2.0 + * @author Remy Perona + * + * @var Assets + */ + private $assets; + + /** + * Image instance + * + * @since 2.0 + * @author Remy Perona + * + * @var Image + */ + private $image; + + /** + * Iframe instance + * + * @since 2.0 + * @author Remy Perona + * + * @var Iframe + */ + private $iframe; + + /** + * Constructor + * + * @since 2.0 + * @author Remy Perona + * + * @param OptionArray $option_array OptionArray instance. + * @param Assets $assets Assets instance. + * @param Image $image Image instance. + * @param Iframe $iframe Iframe instance. + */ + public function __construct( OptionArray $option_array, Assets $assets, Image $image, Iframe $iframe ) { + $this->option_array = $option_array; + $this->assets = $assets; + $this->image = $image; + $this->iframe = $iframe; + } + + /** + * Returns an array of events that this subscriber wants to listen to. + * + * @return array + */ + public function getSubscribedEvents() { + return [ + 'wp_footer' => [ + [ 'insertLazyloadScript', \ROCKET_LL_INT_MAX ], + [ 'insertYoutubeThumbnailScript', \ROCKET_LL_INT_MAX ], + ], + 'wp_head' => [ 'insertNoJSStyle', \ROCKET_LL_INT_MAX ], + 'wp_enqueue_scripts' => [ 'insertYoutubeThumbnailStyle', \ROCKET_LL_INT_MAX ], + 'template_redirect' => [ 'lazyload', 2 ], + 'rocket_lazyload_html' => 'lazyloadResponsive', + 'init' => 'lazyloadSmilies', + ]; + } + + /** + * Inserts the lazyload script in the footer + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function insertLazyloadScript() { + if ( ! $this->option_array->get( 'images' ) && ! $this->option_array->get( 'iframes' ) ) { + return; + } + + if ( ! $this->shouldLazyload() ) { + return; + } + + /** + * Filters the threshold at which lazyload is triggered + * + * @since 1.2 + * @author Remy Perona + * + * @param int $threshold Threshold value. + */ + $threshold = apply_filters( 'rocket_lazyload_threshold', 300 ); + + /** + * Filters the use of the polyfill for intersectionObserver + * + * @since 3.3 + * @author Remy Perona + * + * @param bool $polyfill True to use the polyfill, false otherwise. + */ + $polyfill = apply_filters( 'rocket_lazyload_polyfill', false ); + + $script_args = [ + 'base_url' => ROCKET_LL_FRONT_JS_URL, + 'version' => '12.0', + 'polyfill' => $polyfill, + ]; + + $inline_args = [ + 'threshold' => $threshold, + ]; + + /** + * Filters the use of native lazyload + * + * @since 2.3.3 + * @param bool $use_native True to enable native lazyload usage. + */ + if ( apply_filters( 'rocket_use_native_lazyload', false ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $inline_args['options'] = [ + 'use_native' => 'true', + ]; + } + + if ( $this->option_array->get( 'images' ) || $this->option_array->get( 'iframes' ) ) { + // This filter is documented in src/Subscriber/LazyloadSubscriber.php. + if ( apply_filters( 'rocket_use_native_lazyload', false ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; + $inline_args['elements']['loading'] = '[loading=lazy]'; + } + } + + if ( $this->option_array->get( 'images' ) ) { + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; + $inline_args['elements']['image'] = 'img[data-lazy-src]'; + $inline_args['elements']['background_image'] = '.rocket-lazyload'; + } + + if ( $this->option_array->get( 'iframes' ) ) { + $inline_args['elements'] = isset( $inline_args['elements'] ) ? $inline_args['elements'] : []; + $inline_args['elements']['iframe'] = 'iframe[data-lazy-src]'; + } + + /** + * Filters the arguments array for the lazyload script options + * + * @since 2.0 + * @author Remy Perona + * + * @param array $inline_args Arguments used for the lazyload script options. + */ + $inline_args = apply_filters( 'rocket_lazyload_script_args', $inline_args ); + + echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + $this->assets->insertLazyloadScript( $script_args ); + } + + /** + * Inserts the Youtube thumbnail script in the footer + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function insertYoutubeThumbnailScript() { + if ( ! $this->option_array->get( 'youtube' ) ) { + return; + } + + if ( ! $this->shouldLazyload() ) { + return; + } + + /** + * Filters the resolution of the YouTube thumbnail + * + * @since 1.4.8 + * @author Arun Basil Lal + * + * @param string $thumbnail_resolution The resolution of the thumbnail. Accepted values: default, mqdefault, sddefault, hqdefault, maxresdefault + */ + $thumbnail_resolution = apply_filters( 'rocket_lazyload_youtube_thumbnail_resolution', 'hqdefault' ); + + $this->assets->insertYoutubeThumbnailScript( + [ + 'resolution' => $thumbnail_resolution, + 'lazy_image' => (bool) $this->option_array->get( 'images' ), + ] + ); + } + + /** + * Inserts the no JS CSS compatibility in the header + * + * @since 2.0.3 + * @author Remy Perona + * + * @return void + */ + public function insertNoJSStyle() { + if ( ! $this->shouldLazyload() ) { + return; + } + + $this->assets->insertNoJSCSS(); + } + + /** + * Inserts the Youtube thumbnail CSS in the header + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function insertYoutubeThumbnailStyle() { + if ( ! $this->option_array->get( 'youtube' ) ) { + return; + } + + if ( ! $this->shouldLazyload() ) { + return; + } + + $this->assets->insertYoutubeThumbnailCSS( + [ + 'base_url' => ROCKET_LL_ASSETS_URL, + 'responsive_embeds' => current_theme_supports( 'responsive-embeds' ), + ] + ); + } + + /** + * Checks if lazyload should be applied + * + * @since 2.0 + * @author Remy Perona + * + * @return bool + */ + private function shouldLazyload() { + if ( is_admin() || is_feed() || is_preview() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'DONOTLAZYLOAD' ) && DONOTLAZYLOAD ) ) { + return false; + } + + if ( $this->isPageBuilder() ) { + return false; + } + + /** + * Filters the lazyload application + * + * @since 2.0 + * @author Remy Perona + * + * @param bool $do_rocket_lazyload True to apply lazyload, false otherwise. + */ + if ( ! apply_filters( 'do_rocket_lazyload', true ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + return false; + } + + return true; + } + + /** + * Checks if current page is a page builder editor. + * + * @since 2.2.2 + * @author Remy Perona + * + * @return bool + */ + private function isPageBuilder() { + // Exclude Page Builders editors. + $excluded_parameters = [ + 'fl_builder', + 'et_fb', + 'ct_builder', + ]; + + foreach ( $excluded_parameters as $excluded ) { + if ( isset( $_GET[ $excluded ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return true; + } + } + + return false; + } + + /** + * Gets the content to lazyload + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function lazyload() { + if ( ! $this->shouldLazyload() ) { + return; + } + + ob_start( [ $this, 'lazyloadBuffer' ] ); + } + + /** + * Applies lazyload on the provided content + * + * @since 2.0 + * @author Remy Perona + * + * @param string $html HTML content. + * @return string + */ + public function lazyloadBuffer( $html ) { + $buffer = $this->ignoreScripts( $html ); + $buffer = $this->ignoreNoscripts( $buffer ); + + if ( $this->option_array->get( 'images' ) ) { + $html = $this->image->lazyloadImages( $html, $buffer ); + $html = $this->image->lazyloadPictures( $html, $buffer ); + $html = $this->image->lazyloadBackgroundImages( $html, $buffer ); + } + + if ( $this->option_array->get( 'iframes' ) ) { + $args = [ + 'youtube' => $this->option_array->get( 'youtube' ), + ]; + + $html = $this->iframe->lazyloadIframes( $html, $buffer, $args ); + } + + return $html; + } + + /** + * Applies lazyload on responsive images attributes srcset and sizes + * + * @since 2.0 + * @author Remy Perona + * + * @param string $html Image HTML. + * @return string + */ + public function lazyloadResponsive( $html ) { + return $this->image->lazyloadResponsiveAttributes( $html ); + } + + /** + * Applies lazyload on WordPress smilies + * + * @since 2.0 + * @author Remy Perona + * + * @return void + */ + public function lazyloadSmilies() { + if ( ! $this->shouldLazyload() ) { + return; + } + + if ( ! $this->option_array->get( 'images' ) ) { + return; + } + + $filters = [ + 'the_content' => 10, + 'the_excerpt' => 10, + 'comment_text' => 20, + ]; + + foreach ( $filters as $filter => $prio ) { + if ( ! has_filter( $filter ) ) { + continue; + } + + remove_filter( $filter, 'convert_smilies', $prio ); + add_filter( $filter, [ $this->image, 'convertSmilies' ], $prio ); + } + } + + /** + * Remove inline scripts from the HTML to parse + * + * @param string $html HTML content. + * @return string + */ + private function ignoreScripts( $html ) { + return preg_replace( '/]*)>(?:.+)?<\/script>/Umsi', '', $html ); + } + + /** + * Remove noscript tags from the HTML to parse + * + * @param string $html HTML content. + * @return string + */ + private function ignoreNoscripts( $html ) { + return preg_replace( '##Umsi', '', $html ); + } } diff --git a/src/Subscriber/ThirdParty/AMPSubscriber.php b/src/Subscriber/ThirdParty/AMPSubscriber.php index bb27a0e..f42f217 100644 --- a/src/Subscriber/ThirdParty/AMPSubscriber.php +++ b/src/Subscriber/ThirdParty/AMPSubscriber.php @@ -10,7 +10,7 @@ use RocketLazyLoadPlugin\EventManagement\EventManager; use RocketLazyLoadPlugin\EventManagement\EventManagerAwareSubscriberInterface; -defined('ABSPATH') || die('Cheatin\' uh?'); +defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); /** * Manages compatibility with the AMP plugin @@ -18,61 +18,64 @@ * @since 2.0 * @author Remy Perona */ -class AMPSubscriber implements EventManagerAwareSubscriberInterface -{ - /** - * @var EventManager - */ - protected $event_manager; +class AMPSubscriber implements EventManagerAwareSubscriberInterface { - /** - * @inheritDoc - */ - public function getSubscribedEvents() - { - return [ - 'wp' => 'disableIfAMP', - ]; - } + /** + * EventManager instance + * + * @var EventManager + */ + protected $event_manager; - /** - * {@inheritdoc} - */ - public function setEventManager(EventManager $event_manager) - { - $this->event_manager = $event_manager; - } + /** + * Returns an array of events that this subscriber wants to listen to. + * + * @return array + */ + public function getSubscribedEvents() { + return [ + 'wp' => 'disableIfAMP', + ]; + } - /** - * Disable if on AMP page - * - * @since 2.0.2 - * @author Remy Perona - * - * @return void - */ - public function disableIfAMP() - { - if ($this->isAmpEndpoint()) { - $this->event_manager->addCallback('do_rocket_lazyload', '__return_false'); - $this->event_manager->addCallback('do_rocket_lazyload_iframes', '__return_false'); - } - } + /** + * Set the WordPress event manager for the subscriber. + * + * @param EventManager $event_manager EventManager instance. + * @return void + */ + public function setEventManager( EventManager $event_manager ) { + $this->event_manager = $event_manager; + } - /** - * Checks if current page uses AMP - * - * @since 2.0 - * @author Remy Perona - * - * @return boolean - */ - private function isAmpEndpoint() - { - if (function_exists('is_amp_endpoint') && \is_amp_endpoint()) { - return true; - } + /** + * Disable if on AMP page + * + * @since 2.0.2 + * @author Remy Perona + * + * @return void + */ + public function disableIfAMP() { + if ( $this->isAmpEndpoint() ) { + $this->event_manager->addCallback( 'do_rocket_lazyload', '__return_false' ); + $this->event_manager->addCallback( 'do_rocket_lazyload_iframes', '__return_false' ); + } + } - return false; - } + /** + * Checks if current page uses AMP + * + * @since 2.0 + * @author Remy Perona + * + * @return boolean + */ + private function isAmpEndpoint() { + if ( function_exists( 'is_amp_endpoint' ) && \is_amp_endpoint() ) { + return true; + } + + return false; + } } From f9c81e2ab2d426aa2186110c08aaf7bc1a630ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 May 2020 11:55:50 -0400 Subject: [PATCH 15/17] update changelog --- readme.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.txt b/readme.txt index 8ad8ffd..c36b5bc 100644 --- a/readme.txt +++ b/readme.txt @@ -95,6 +95,7 @@ The element must have the class `rocket-lazyload`, and a `data-bg` attribute, wh = 2.3.3 = Enhancement: Add data-skip-lazy and skip-lazy class to exclusions list as part of the interoperability initiative between lazyload plugins Enhancement: Use native lazyload only if filter `rocket_use_native_lazyload` is true +Enhancement: Apply lazyload on background images set on `figure` elements Bugfix: Correctly add the rocket-lazyload class when class attribute is empty on an element with a background image Bugfix: Correctly replace YouTube iframe with preview image when using relative protocol Bugfix: Preserve youtube-nocookie.com during LazyLoad From d3e263c891a786bdc4a571c872b36fc150d323d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 May 2020 11:56:16 -0400 Subject: [PATCH 16/17] remove unnecessary docblocks & define checks --- src/Plugin.php | 5 ----- src/Subscriber/AdminPageSubscriber.php | 15 ++++----------- src/Subscriber/LazyloadSubscriber.php | 5 ----- src/Subscriber/ThirdParty/AMPSubscriber.php | 9 +-------- 4 files changed, 5 insertions(+), 29 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index d7483b4..49bba68 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,9 +1,4 @@ 'configure', - 'admin_menu' => 'addAdminPage', - 'plugin_action_links_' . $this->plugin_basename => 'addPluginPageLink', - 'admin_enqueue_scripts' => 'enqueueAdminStyle', + 'admin_init' => 'configure', + 'admin_menu' => 'addAdminPage', + "plugin_action_links_{$this->plugin_basename}" => 'addPluginPageLink', + 'admin_enqueue_scripts' => 'enqueueAdminStyle', ]; } diff --git a/src/Subscriber/LazyloadSubscriber.php b/src/Subscriber/LazyloadSubscriber.php index ae3a2de..7d60fc3 100644 --- a/src/Subscriber/LazyloadSubscriber.php +++ b/src/Subscriber/LazyloadSubscriber.php @@ -1,9 +1,4 @@ Date: Thu, 22 Oct 2020 16:48:56 -0400 Subject: [PATCH 17/17] Update readme.txt content (PR #120) --- readme.txt | 18 ++++++++++-------- rocket-lazy-load.php | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/readme.txt b/readme.txt index c36b5bc..ecade54 100644 --- a/readme.txt +++ b/readme.txt @@ -1,4 +1,4 @@ -=== Lazy Load - Optimize Images === +=== Lazy Load === Contributors: wp_rocket, wp_media Tags: lazyload, lazy load, images, iframes, thumbnail, thumbnails, smiley, smilies, avatar, gravatar, youtube Requires at least: 4.7 @@ -6,19 +6,21 @@ Tested up to: 5.3 Requires PHP: 5.6 Stable tag: 2.3.3 -Lazy Load your images and iframes, replace Youtube videos by a preview thumbnail. +Lazy load your images and iframes, replace Youtube videos by a preview thumbnail. == Description == -Lazy Load by WP Rocket displays images and/or iframes on a page only when they are visible to the user. This reduces the number of HTTP requests mechanism and improves the loading time. +Lazy Load displays images and/or iframes on a page only when they are visible to the user. This reduces the number of HTTP requests mechanism and improves the loading time. -This plugin works on thumbnails, all images in a post content or in a widget text, avatars, smilies and iframes. No JavaScript library such as jQuery is used and the script weight is less than 10KB. +You can lazy load thumbnails, all images in a post content or in a widget text, avatars, smilies and iframes. No JavaScript library such as jQuery is used and the script weight is less than 10KB. You can also replace Youtube iframes by a preview thumbnail to further speed up the loading time of your website. +Turn on lazy load to optimize your images and make your website faster! + = Dependencies = -Lazyload script: [https://github.com/verlok/lazyload](https://github.com/verlok/lazyload) +Lazy load script: [https://github.com/verlok/lazyload](https://github.com/verlok/lazyload) = Related Plugins = * [Imagify](https://imagify.io/?utm_source=wordpress.org&utm_medium=referral&utm_campaign=LazyLoadPlugin): Best Image Optimizer to speed up your website with lighter images. @@ -79,13 +81,13 @@ add_filter( 'rocket_lazyload_threshold', 'rocket_lazyload_custom_threshold' ); Some plugins are not compatible without lazy loading. Please open a support thread, and we will see how we can solve the issue by excluding lazy loading for this plugin. -= How can I lazyload a background-image? = += How can I lazy load a background-image? = -The plugin will automatically lazyload background-images set with a `style` attribute to a `div` element: +The plugin will automatically lazy load background-images set with a `style` attribute to a `div` element: `
` -You can also apply it manually. The element you want to apply lazyload on must have this specific markup: +You can also apply it manually. The element you want to apply lazy load on must have this specific markup: `
` diff --git a/rocket-lazy-load.php b/rocket-lazy-load.php index c9d46fe..ed60d2b 100644 --- a/rocket-lazy-load.php +++ b/rocket-lazy-load.php @@ -1,6 +1,6 @@