Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/5987 lazyload css #6129

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8517fa5
Fixed a bug when having two blocks with the same selector
CrochetFeve0251 Aug 28, 2023
df61774
Apply cdn only on internal URLs
CrochetFeve0251 Aug 28, 2023
cb9c8ed
Fix phpcs
CrochetFeve0251 Aug 28, 2023
1aec0db
Fixed the bug with spaces
CrochetFeve0251 Aug 28, 2023
0d5591f
Added fix for tests
CrochetFeve0251 Aug 28, 2023
bcef911
Added fix for the meta classes
CrochetFeve0251 Aug 29, 2023
233f70a
Added a wider selector regex
CrochetFeve0251 Aug 29, 2023
4ffe583
Changed the script to internal
CrochetFeve0251 Aug 29, 2023
589a15d
Added new regex to fix issue with meta queries
CrochetFeve0251 Aug 29, 2023
79527a7
Added fix inline script
CrochetFeve0251 Aug 30, 2023
e1decaf
Added new script to handle dynamic elements
CrochetFeve0251 Aug 30, 2023
ddb9af2
Added working version from the script
CrochetFeve0251 Aug 30, 2023
5ea52ab
Added working version from the script
CrochetFeve0251 Aug 30, 2023
6f3cd1a
Fixed warning on chrome
CrochetFeve0251 Aug 30, 2023
58c7c35
Merge branch 'branch-3.15' into feature/5987-lazyload-css
CrochetFeve0251 Aug 30, 2023
80896b1
Fixed phpcs and js
CrochetFeve0251 Aug 30, 2023
0a320a0
Fixed script
CrochetFeve0251 Aug 31, 2023
7541f70
Fixed script
CrochetFeve0251 Aug 31, 2023
fc4e1bd
Fixed issue with relative urls
CrochetFeve0251 Aug 31, 2023
78bbb1d
Added fix to lazyload
CrochetFeve0251 Aug 31, 2023
447d6a0
Added a fix for pseudo classes
CrochetFeve0251 Aug 31, 2023
a4e1961
Merge branch 'develop' into feature/5987-lazyload-css
CrochetFeve0251 Aug 31, 2023
2053ac9
Added a fix for pseudo classes
CrochetFeve0251 Aug 31, 2023
e09492c
Merge remote-tracking branch 'origin/feature/5987-lazyload-css' into …
CrochetFeve0251 Aug 31, 2023
d9fcb83
Added a fix for ll tests
CrochetFeve0251 Aug 31, 2023
8112def
Added fixed for non existing content
CrochetFeve0251 Aug 31, 2023
2e3a151
Added fix for ll tests
CrochetFeve0251 Sep 1, 2023
57afe85
Fixed tests
CrochetFeve0251 Sep 1, 2023
245b1f5
Fixed phpcs
CrochetFeve0251 Sep 1, 2023
a89316d
Fixed integration test
CrochetFeve0251 Sep 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/js/lazyload-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function rocket_css_lazyload_launch() {
pairs.map(pair => {
if (pair) {
styleElement.innerHTML += pair.style;

if(pair.elements === undefined) {
return;
}

pair.elements.forEach(el => {
el.setAttribute('data-rocket-lazy-bg', 'loaded');
// Stop observing the target element
Expand Down
2 changes: 1 addition & 1 deletion assets/js/lazyload-css.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion assets/js/lazyload-css.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion inc/Engine/Media/Lazyload/CSS/Front/ContentFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
return $this->filesystem->get_contents( $path );
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );

$response = wp_remote_get( $path );

if ( wp_remote_retrieve_response_code( $response ) === 404 ) {
return false;
}

return wp_remote_retrieve_body( $response );
}
}
25 changes: 25 additions & 0 deletions inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,35 @@ public function format( array $data ): array {
* @return string
*/
protected function remove_pseudo_classes( string $selector ): string {

$original_pseudo_elements = [
':before',
':after',
':first-line',
':first-letter',
];

/**
* Pseudo elements to remove from lazyload CSS selector.
*
* @param string[] Pseudo elements to remove.
*/
$pseudo_elements_to_remove = apply_filters( 'rocket_lazyload_css_ignored_pseudo_elements', $original_pseudo_elements );

if ( ! is_array( $original_pseudo_elements ) ) {
$pseudo_elements_to_remove = $original_pseudo_elements;
}

$result = preg_replace( '/::[\w\-]+/', '', $selector );

foreach ( $pseudo_elements_to_remove as $element ) {
$result = str_replace( $element, '', $result );
}

if ( ! $result ) {
return 'body';
}

return (string) $result;
}
}
5 changes: 4 additions & 1 deletion inc/Engine/Media/Lazyload/CSS/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ protected function generate_content( string $content ): LazyloadedContent {
*/
protected function load_existing_mapping( string $url ) {
$content = $this->cache->get( $this->get_mapping_file_url( $url ) );
$urls = json_decode( $content, true );
if ( ! $content ) {
return [];
}
$urls = json_decode( $content, true );
if ( ! $urls ) {
return [];
}
Expand Down
2 changes: 2 additions & 0 deletions inc/Engine/Media/Lazyload/CanLazyloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function should_lazyload() {
rocket_get_constant( 'DONOTLAZYLOAD', false )
||
rocket_get_constant( 'DONOTROCKETOPTIMIZE', false )
||
rocket_get_constant( 'DONOTCACHEPAGE', false )
) {
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions src/js/global/lazyload-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function rocket_css_lazyload_launch() {
const usable_pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs;



const styleElement = document.querySelector('#wpr-lazyload-bg');

const threshold = rocket_lazyload_css_data.threshold || 300;
Expand All @@ -14,6 +15,11 @@ function rocket_css_lazyload_launch() {
pairs.map(pair => {
if (pair) {
styleElement.innerHTML += pair.style;

if(pair.elements === undefined) {
return;
}

pair.elements.forEach(el => {
el.setAttribute('data-rocket-lazy-bg', 'loaded');
// Stop observing the target element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
'path' => '/my/file',
'is_url' => false,
'content' => 'content',
'destination' => '/path/new'
'destination' => '/path/new',
'code' => 200
],
'expected' => 'content'
],
Expand All @@ -16,7 +17,8 @@
'content' => 'content',
'response' => 'response',
'body' => 'body',
'destination' => '/path/new'
'destination' => '/path/new',
'code' => 200
],
'expected' => 'body'
],
Expand All @@ -27,7 +29,8 @@
'content' => 'content',
'response' => 'response',
'body' => null,
'destination' => '/path/new'
'destination' => '/path/new',
'code' => 200
],
'expected' => false
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
'hash' => 'a122ad12df3',
'selector' => '::after',
'url' => 'http://example.org/test',
],
[
'hash' => 'a122ad12df3',
'selector' => ':after',
'url' => 'http://example.org/test',
],
[
'hash' => 'a122ad12df3',
'selector' => ':last',
'url' => 'http://example.org/test',
]
],
],
Expand All @@ -58,6 +68,14 @@
[
'selector' => 'body',
'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}'
],
[
'selector' => 'body',
'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}'
],
[
'selector' => ':last',
'style' => ':root{--wpr-bg-a122ad12df3: http://example.org/test;}'
]
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@
'integration' => '',
],
],
'testShouldReturnNothingWhenPageNotCached' => [
'config' => [
'is_not_cached_page' => false,
'options' => [
'lazyload' => 1,
'lazyload_iframes' => 0,
],
],
'expected' => [
'unit' => [
'inline_script' => '',
'script' => '',
'result' => '',
],
'integration' => '',
],
],
'testShouldReturnNothingWhenIsNotRocketOptimize' => [
'config' => [
'is_rocket_optimize' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function testShouldInsertLazyloadScript( $config, $expected ) {
$is_rest_request = isset( $config['is_rest_request'] ) ? $config['is_rest_request'] : false;
$is_lazy_load = isset( $config['is_lazy_load'] ) ? $config['is_lazy_load'] : true;
$is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true;
$is_not_cached_page = isset( $config['is_not_cached_page'] ) ? $config['is_not_cached_page'] : true;

set_current_screen( $is_admin ? 'settings_page_wprocket' : 'front' );

Expand All @@ -87,6 +88,7 @@ public function testShouldInsertLazyloadScript( $config, $expected ) {
$this->constants['REST_REQUEST'] = $is_rest_request;
$this->constants['DONOTLAZYLOAD'] = ! $is_lazy_load;
$this->donotrocketoptimize = ! $is_rocket_optimize;
$this->is_not_cached_page = ! $is_not_cached_page;
$this->constants['WP_ROCKET_ASSETS_JS_URL'] = 'http://example.org/wp-content/plugins/wp-rocket/assets/';

// wp-media/rocket-lazyload-common uses the constant for determining whether to set as .min.js.
Expand Down
4 changes: 4 additions & 0 deletions tests/StubTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait StubTrait {
protected $wp_rocket_advanced_cache = true;
protected $disable_wp_cron = false;
protected $donotrocketoptimize = null;
protected $is_not_cached_page = null;
protected $white_label = false;
protected $white_label_footprint = null;
protected $plugin_name = 'WP Rocket';
Expand Down Expand Up @@ -70,6 +71,9 @@ protected function getConstant( $constant_name, $default = null ) {
case 'DONOTROCKETOPTIMIZE' :
return $this->donotrocketoptimize;

case 'DONOTCACHEPAGE' :
return $this->is_not_cached_page;

case 'FS_CHMOD_DIR':
return 0777;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function configure_url($config) {
return;
}
Functions\expect('wp_remote_get')->with($config['path'])->andReturn($config['response']);
Functions\expect('wp_remote_retrieve_response_code')->with($config['response'])->andReturn($config['code']);
Functions\expect('wp_remote_retrieve_body')->with($config['response'])->andReturn($config['body']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,33 @@ public function testShouldInsertLazyloadScript( $config, $expected ) {
$is_rest_request = isset( $config['is_rest_request'] ) ? $config['is_rest_request'] : false;
$is_lazy_load = isset( $config['is_lazy_load'] ) ? $config['is_lazy_load'] : true;
$is_rocket_optimize = isset( $config['is_rocket_optimize'] ) ? $config['is_rocket_optimize'] : true;
$is_not_cached_page = isset( $config['is_not_cached_page'] ) ? $config['is_not_cached_page'] : true;

Functions\when( 'is_admin' )->justReturn( $is_admin );
Functions\when( 'is_feed' )->justReturn( $is_feed );
Functions\when( 'is_preview' )->justReturn( $is_preview );
Functions\when( 'is_search' )->justReturn( $is_search );
Functions\expect( 'rocket_get_constant' )
->with('REST_REQUEST', 'DONOTLAZYLOAD', 'DONOTROCKETOPTIMIZE', 'WP_ROCKET_ASSETS_JS_URL')
->andReturn( $is_rest_request, !$is_lazy_load, !$is_rocket_optimize, 'http://example.org/wp-content/plugins/wp-rocket/assets/' );
Functions\when('rocket_get_constant')->alias(function ($name, $default = false) use ($is_rest_request, $is_lazy_load, $is_rocket_optimize, $is_not_cached_page) {
if($name == 'REST_REQUEST') {
return $is_rest_request;
}
if($name == 'DONOTLAZYLOAD') {
return !$is_lazy_load;
}
if($name == 'DONOTROCKETOPTIMIZE') {
return !$is_rocket_optimize;
}
if($name == 'WP_ROCKET_ASSETS_JS_URL') {
return !$is_rocket_optimize;
}
if($name == 'SCRIPT_DEBUG') {
return true;
}
if($name == 'DONOTCACHEPAGE') {
return !$is_not_cached_page;
}
return $default;
});

foreach ( $options as $key => $value ) {
$this->options->shouldReceive( 'get' )
Expand Down
Loading