Skip to content

Commit

Permalink
Closes #6944 Add LRC hashes when using LRC query string (#6955)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuLamiot authored Sep 11, 2024
2 parents b2ae01f + 7c2274b commit be278fc
Show file tree
Hide file tree
Showing 17 changed files with 474 additions and 65 deletions.
59 changes: 54 additions & 5 deletions inc/Engine/Common/PerformanceHints/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace WP_Rocket\Engine\Common\PerformanceHints\Frontend;

use WP_Rocket\Buffer\Tests;
use WP_Rocket\Event_Management\Subscriber_Interface;


class Subscriber implements Subscriber_Interface {

/**
Expand All @@ -15,13 +15,22 @@ class Subscriber implements Subscriber_Interface {
*/
private $processor;

/**
* Buffer tests to run against current page, to decide if we can start the buffer or not.
*
* @var Tests
*/
private $buffer_tests;

/**
* Instantiate the class
*
* @param Processor $processor Processor Instance.
* @param Tests $buffer_tests Buffer tests instance.
*/
public function __construct( Processor $processor ) {
$this->processor = $processor;
public function __construct( Processor $processor, Tests $buffer_tests ) {
$this->processor = $processor;
$this->buffer_tests = $buffer_tests;
}

/**
Expand All @@ -31,8 +40,9 @@ public function __construct( Processor $processor ) {
*/
public static function get_subscribed_events(): array {
return [
'rocket_buffer' => [ 'maybe_apply_optimizations', 17 ],
'rocket_critical_image_saas_visit_buffer' => [ 'maybe_apply_optimizations', 17 ],
'rocket_buffer' => [ 'maybe_apply_optimizations', 17 ],
'rocket_performance_hints_buffer' => [ 'maybe_apply_optimizations', 17 ],
'template_redirect' => [ 'start_performance_hints_buffer', 3 ],
];
}

Expand All @@ -44,6 +54,45 @@ public static function get_subscribed_events(): array {
* @return string
*/
public function maybe_apply_optimizations( $html ): string {
if ( ! isset( $_GET['wpr_imagedimensions'] ) && isset( $_GET['wpr_lazyrendercontent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $html;
}

return $this->processor->maybe_apply_optimizations( $html );
}

/**
* Start performance hints buffer
*
* @return void
*/
public function start_performance_hints_buffer() {
if ( ! isset( $_GET['wpr_imagedimensions'] ) && ! isset( $_GET['wpr_lazyrendercontent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}

if ( ! $this->buffer_tests->can_process_any_buffer() ) {
return;
}

ob_start( [ $this, 'performance_hints_buffer' ] );
}

/**
* Update images that have no width/height with real dimensions for the SaaS
*
* @param string $buffer Page HTML content.
*
* @return string Page HTML content after update.
*/
public function performance_hints_buffer( $buffer ) {
/**
* Filters the buffer content for performance hints.
*
* @since 3.17
*
* @param $buffer Page HTML content.
*/
return wpm_apply_filters_typed( 'string', 'rocket_performance_hints_buffer', $buffer );
}
}
11 changes: 10 additions & 1 deletion inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace WP_Rocket\Engine\Common\PerformanceHints;

use WP_Rocket\Buffer\{Config, Tests};
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Common\PerformanceHints\Admin\{
Controller as AdminController,
Expand All @@ -20,7 +21,6 @@
Subscriber as WarmUpSubscriber,
Queue
};
use WP_Rocket\Engine\Optimization\LazyRenderContent\Context\Context as LRCContext;

class ServiceProvider extends AbstractServiceProvider {
/**
Expand All @@ -33,6 +33,8 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $provides = [
'config',
'tests',
'ajax_processor',
'performance_hints_ajax_subscriber',
'frontend_processor',
Expand Down Expand Up @@ -100,10 +102,17 @@ public function register(): void {
]
);

$this->getContainer()->add( 'config', Config::class )
->addArgument( [ 'config_dir_path' => rocket_get_constant( 'WP_ROCKET_CONFIG_PATH' ) ] );

$this->getContainer()->add( 'tests', Tests::class )
->addArgument( $this->getContainer()->get( 'config' ) );

$this->getContainer()->addShared( 'performance_hints_frontend_subscriber', FrontendSubscriber::class )
->addArguments(
[
$this->getContainer()->get( 'frontend_processor' ),
$this->getContainer()->get( 'tests' ),
]
);

Expand Down
46 changes: 9 additions & 37 deletions inc/Engine/Media/ImageDimensions/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WP_Rocket\Engine\Media\ImageDimensions;

use WP_Rocket\Buffer\Tests;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
Expand All @@ -19,22 +18,13 @@ class Subscriber implements Subscriber_Interface {
*/
private $dimensions;

/**
* Buffer tests to run against current page, to decide if we can start the buffer or not.
*
* @var Tests
*/
private $buffer_tests;

/**
* Subscriber constructor.
*
* @param ImageDimensions $dimensions Images dimensions class that handles all business logic.
* @param Tests $buffer_tests Buffer tests instance.
*/
public function __construct( ImageDimensions $dimensions, Tests $buffer_tests ) {
$this->dimensions = $dimensions;
$this->buffer_tests = $buffer_tests;
public function __construct( ImageDimensions $dimensions ) {
$this->dimensions = $dimensions;
}

/**
Expand All @@ -44,9 +34,8 @@ public function __construct( ImageDimensions $dimensions, Tests $buffer_tests )
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [ 'specify_image_dimensions', 17 ],
'template_redirect' => [ 'start_image_dimensions_buffer', 3 ],
'rocket_critical_image_saas_visit_buffer' => 'specify_image_dimensions',
'rocket_buffer' => [ 'specify_image_dimensions', 17 ],
'rocket_performance_hints_buffer' => 'image_dimensions_query_string',
];
}

Expand All @@ -66,36 +55,19 @@ public function specify_image_dimensions( $buffer ) {
}

/**
* Update images that have no width/height with real dimensions for the SaaS
* Add image dimensions if the query string is in the URL.
*
* @param string $buffer Page HTML content.
*
* @return string Page HTML content after update.
* @return string
*/
public function prepare_critical_image_saas_visit( $buffer ) {
if ( ! isset( $_GET['wpr_imagedimensions'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $buffer;
}

return apply_filters( 'rocket_critical_image_saas_visit_buffer', $buffer );
}

/**
* Start image dimensions buffer to add
*
* @return void
*/
public function start_image_dimensions_buffer() {
public function image_dimensions_query_string( $buffer ): string {
if ( empty( $_GET['wpr_imagedimensions'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}

if ( ! $this->buffer_tests->can_process_any_buffer() ) {
return;
return $buffer;
}

add_filter( 'rocket_specify_image_dimensions', '__return_true' );

ob_start( [ $this, 'prepare_critical_image_saas_visit' ] );
return $this->dimensions->specify_image_dimensions( $buffer );
}
}
15 changes: 15 additions & 0 deletions inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ private function add_css( $html ) {
return $result;
}

/**
* Add hashes to the HTML elements if allowed
*
* @param string $html The HTML content.
*
* @return string
*/
public function add_hashes_when_allowed( $html ) {
if ( ! $this->context->is_allowed() ) {
return $html;
}

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

/**
* Add hashes to the HTML elements
*
Expand Down
15 changes: 13 additions & 2 deletions inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ public function __construct( Controller $controller ) {
*/
public static function get_subscribed_events(): array {
return [
'rocket_buffer' => [ 'add_hashes', 16 ],
'rocket_critical_image_saas_visit_buffer' => [ 'add_hashes', 16 ],
'rocket_buffer' => [ 'add_hashes_when_allowed', 16 ],
'rocket_performance_hints_buffer' => [ 'add_hashes', 16 ],
];
}

/**
* Add hashes to the HTML elements if allowed
*
* @param string $html The HTML content.
*
* @return string
*/
public function add_hashes_when_allowed( $html ) {
return $this->controller->add_hashes_when_allowed( $html );
}

/**
* Add hashes to the HTML elements
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@

return [
'test_data' => [
'shouldReturnOriginalWhenQueryString' => [
'config' => [
'query_string' => 'wpr_lazyrendercontent',
'html' => $html_input,
'atf' => [
'row' => null,
],
'lrc' => [
'row' => null,
],
],
'expected' => $html_input,
],
'shouldAddBeaconToPage' => [
'config' => [
'html' => $html_input,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
return [
'shouldStartBufferWithWprImagedimensions' => [
'config' => [
'wpr_imagedimensions' => '1',
],
'expected' => 1,
],
'shouldStartBufferWithWprLazyrendercontent' => [
'config' => [
'wpr_lazyrendercontent' => '1',
],
'expected' => 1,
],
'shouldNotStartBufferWithNoRelevantGETParams' => [
'config' => [
'unrelated_param' => '1',
],
'expected' => 0,
],
'shouldNotStartBufferWithEmptyGETParams' => [
'config' => [],
'expected' => 0,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [
'testShouldReturnSameWhenNoQueryString' => [
'config' => [
'query_string' => '',
],
'html' => '<img src="image.jpg">',
'expected' => '<img src="image.jpg">',
],
'testShouldReturnUpdated' => [
'config' => [
'query_string' => 'wpr_imagedimensions',
],
'html' => '<img src="image.jpg">',
'expected' => '<img src="image.jpg" width="100" height="100">',
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

return [
'test_data' => [
'shouldNotAddHashesWhenNotAllowed' => [
'config' => [
'filter' => false,
'row' => [
'url' => 'http://example.org/',
'is_mobile' => 0,
'below_the_fold' => json_encode(
[
'93548b90aa8f4989f7198144479055dc',
'7b16eca0652d4703f83ba63e304f2030',
'737184bbad8e65d0172a89cc68a46107',
'8a4ef50742cf3456f9db6425e16930dc',
]
),
'status' => 'completed',
],
'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php' ),
],
'expected' => [
'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php' ),
],
],
'shouldAddHashes' => [
'config' => [
'filter' => true,
'row' => [
'url' => 'http://example.org/',
'is_mobile' => 0,
'below_the_fold' => json_encode(
[
'93548b90aa8f4989f7198144479055dc',
'7b16eca0652d4703f83ba63e304f2030',
'737184bbad8e65d0172a89cc68a46107',
'8a4ef50742cf3456f9db6425e16930dc',
]
),
'status' => 'completed',
],
'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php' ),
],
'expected' => [
'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected_allowed.php' ),
],
],
],
];
Loading

0 comments on commit be278fc

Please sign in to comment.