Skip to content

Commit

Permalink
Merge branch 'feature/lrc' into fix/6891-3.17-all-hashes-are-saved-to…
Browse files Browse the repository at this point in the history
…-db-for-lrc
  • Loading branch information
jeawhanlee committed Aug 23, 2024
2 parents 9c643ce + c6b849e commit 4b868b9
Show file tree
Hide file tree
Showing 22 changed files with 358 additions and 19,236 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_wprocket_latest_general_coverage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit/Integration General tests Latest version
name: Unit/Integration General tests Latest version with coverage

on:
pull_request:
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"

- name: Test
run: composer run-tests-general
run: composer run-tests-coverage

- name: Code Coverage Report
run: composer report-code-coverage
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ composer.lock
vfs:/
/tests/Integration/.phpunit.result.cache
/tests/Unit/.phpunit.result.cache

package-lock.json
29 changes: 18 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@
}
},
"scripts": {
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist --coverage-php tests/report/unit.cov",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,TranslatePress,WPGeotargeting,Weglot,Pressidium,PerformanceHints --coverage-php tests/report/integration.cov",
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist",
"test-unit-coverage": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist --coverage-php tests/report/unit.cov",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,TranslatePress,WPGeotargeting,Weglot,Pressidium,PerformanceHints",
"test-integration-coverage": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,TranslatePress,WPGeotargeting,Weglot,Pressidium,PerformanceHints --coverage-php tests/report/integration.cov",
"test-integration-adminonly": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly",
"test-integration-adminonly-coverage": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly --coverage-php tests/report/unit.cov",
"test-integration-performancehints": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group PerformanceHints",
"test-integration-performancehints-coverage": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group PerformanceHints --coverage-php tests/report/unit.cov",
"test-integration-bb": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group BeaverBuilder",
"test-integration-cloudflare": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Cloudflare",
"test-integration-cloudflareadmin": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group CloudflareAdmin",
Expand Down Expand Up @@ -167,20 +171,23 @@
"test-integration-rapidload": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group RapidLoad",
"test-integration-proisp": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group ProIsp",
"test-integration-wp-geotargeting": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group WPGeotargeting",
"test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress",
"test-integration-weglot": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Weglot",
"test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium",
"test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress",
"test-integration-weglot": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Weglot",
"test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium",
"run-tests": [
"@run-tests-general",
"@test-unit",
"@test-integration",
"@test-integration-adminonly",
"@test-integration-performancehints",
"@run-tests-integration-specific"
],
"run-tests-general": [
"@test-unit",
"@test-integration"
"run-tests-coverage": [
"@test-unit-coverage",
"@test-integration-coverage",
"@test-integration-adminonly-coverage",
"@test-integration-performancehints-coverage"
],
"run-tests-integration-specific": [
"@test-integration-adminonly",
"@test-integration-performancehints",
"@test-integration-cloudflare",
"@test-integration-cloudflareadmin",
"@test-integration-bb",
Expand Down
147 changes: 97 additions & 50 deletions inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Database\Queries\LazyRenderContent as LRCQuery;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend\Processor\Processor;
use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend\Processor\Processor;

class Controller implements ControllerInterface {
/**
Expand All @@ -25,52 +23,96 @@ class Controller implements ControllerInterface {
private $context;

/**
* LRCQuery instance
* Constructor
*
* @var LRCQuery
* @param Processor $processor Processor instance.
* @param ContextInterface $context Context instance.
*/
private $query;
public function __construct( Processor $processor, ContextInterface $context ) {
$this->processor = $processor;
$this->context = $context;
}

/**
* Options instance
* Applies optimization.
*
* @param string $html HTML content.
* @param object $row Database Row.
*
* @var Options_Data
* @return string
*/
private $options;
public function optimize( string $html, $row ): string {
if ( ! $row->has_lrc() ) {
return $html;
}

$hashes = json_decode( $row->below_the_fold );

if ( null === $hashes || ! is_array( $hashes ) ) {
return $html;
}

$result = preg_replace( '/data-rocket-location-hash="(?:' . implode( '|', $hashes ) . ')"/i', 'data-wpr-lazyrender="1"', $html, -1, $count );

if (
null === $result
||
0 === $count
) {
return $html;
}

$html = $result;
$html = $this->remove_hashes( $html );

return $this->add_css( $html );
}

/**
* Constructor
* Remove hashes from the HTML content.
*
* @param Processor $processor Processor instance.
* @param ContextInterface $context Context instance.
* @param LRCQuery $query Query instance.
* @param Options_Data $options Options instance.
* @param string $html The HTML content.
*
* @return string
*/
public function __construct( Processor $processor, ContextInterface $context, LRCQuery $query, Options_Data $options ) {
$this->processor = $processor;
$this->context = $context;
$this->query = $query;
$this->options = $options;
private function remove_hashes( $html ) {
$result = preg_replace( '/data-rocket-location-hash="(?:.*)"/i', '', $html );

if ( null === $result ) {
return $html;
}

return $result;
}

/**
* Add hashes to the HTML elements
* Add CSS to the HTML content.
*
* @param string $html The HTML content.
*
* @return string
*/
public function add_hashes( $html ) {
global $wp;
private function add_css( $html ) {
$css = '<style>[data-wpr-lazyrender] {content-visibility: auto;}</style>';

if ( ! $this->context->is_allowed() ) {
$result = preg_replace( '/<\/head>/i', $css . '</head>', $html, 1 );

if ( null === $result ) {
return $html;
}

$url = untrailingslashit( home_url( add_query_arg( [], $wp->request ) ) );
$is_mobile = $this->is_mobile();
return $result;
}

if ( $this->query->get_row( $url, $is_mobile ) ) {
/**
* Add hashes to the HTML elements
*
* @param string $html The HTML content.
*
* @return string
*/
public function add_hashes( $html ) {
if ( ! $this->context->is_allowed() ) {
return $html;
}

Expand All @@ -89,38 +131,43 @@ public function add_hashes( $html ) {
}

/**
* Apply LRC Optimzation to content.
*
* @param string $html HTML content.
* @param object $row Database Row.
*
* @return string
*/
public function optimize( string $html, $row ): string {
return $html;
}

/**
* Add custom data for the LRC optimization.
* Add custom data like the List of elements to be considered for optimization.
*
* @param array $data Array of data passed in beacon.
*
* @return array
*/
public function add_custom_data( array $data ): array {
$elements = [
'div',
'main',
'footer',
'section',
'article',
'header',
];

/**
* Filters the array of elements
*
* @since 3.17
*
* @param array $formats Array of elements
*/
$elements = wpm_apply_filters_typed( 'array', 'rocket_lrc_elements', $elements );

$data['lrc_elements'] = implode( ', ', $elements );
$data['status']['lrc'] = $this->context->is_allowed();

return $data;
}
/**
* Filters the LRC threshold
*
* @since 3.17
*
* @param int $threshold The LRC threshold value.
*/
$data['lrc_threshold'] = wpm_apply_filters_typed( 'integer', 'rocket_lrc_threshold', 1800 );

/**
* Determines if the page is mobile and separate cache for mobile files is enabled.
*
* @return bool
*/
private function is_mobile(): bool {
return $this->options->get( 'cache_mobile', 0 )
&& $this->options->get( 'do_caching_mobile_files', 0 )
&& wp_is_mobile();
return $data;
}
}
8 changes: 0 additions & 8 deletions inc/Engine/Optimization/LazyRenderContent/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ public function provides( string $id ): bool {
*/
public function register(): void {
$this->getContainer()->add( 'lrc_context', Context::class );

$this->getContainer()->addShared( 'lrc_table', LRCTable::class );

$this->getContainer()->add( 'lrc_query', LRCQuery::class );

$this->getContainer()->addShared( 'lrc_table', LRCTable::class );

$this->getContainer()->add( 'lrc_query', LRCQuery::class );

$this->getContainer()->add( 'lrc_ajax_controller', AJAXController::class )
Expand All @@ -72,8 +66,6 @@ public function register(): void {
[
$this->getContainer()->get( 'lrc_frontend_processor' ),
$this->getContainer()->get( 'lrc_context' ),
$this->getContainer()->get( 'lrc_query' ),
$this->getContainer()->get( 'options' ),
]
);
$this->getContainer()->addShared( 'lrc_frontend_subscriber', FrontSubscriber::class )
Expand Down
Loading

0 comments on commit 4b868b9

Please sign in to comment.