Skip to content

Commit

Permalink
Closes #6934: Beacon script is injected to logged-in users (#6998)
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Perona <[email protected]>
  • Loading branch information
Miraeld and remyperona authored Oct 4, 2024
1 parent dc941a7 commit 21c9f02
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions inc/Engine/Common/PerformanceHints/Frontend/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function maybe_apply_optimizations( string $html ): string {
return $html;
}

if ( is_user_logged_in() && $this->options->get( 'cache_logged_user', 0 ) ) {
return $html;
}

global $wp;

$url = untrailingslashit( home_url( add_query_arg( [], $wp->request ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
],
'expected' => $html_output_with_preload,
],
'shoudNotAddBeaconToPageWhenLoggedInAndUserCacheEnabled' => [
'config' => [
'html' => $html_input,
'atf' => [],
'lrc' => [],
'is_logged_in' => true,
'user_cache_enabled' => 1,
],
'expected' => $html_output,
],
'shouldNotAddBeaconToPageWhenPerformanceHintsFailed' => [
'config' => [
'html' => $html_input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*/
class Test_MaybeApplyOptimizations extends FilesystemTestCase {
protected $path_to_test_data = '/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php';

protected $config;
private $cached_user = false;
private $user_id = 0;

public static function set_up_before_class() {
parent::set_up_before_class();
Expand All @@ -39,8 +40,13 @@ public function set_up() {
public function tear_down() {
unset( $_GET );
remove_filter( 'rocket_performance_hints_optimization_delay', [ $this, 'add_delay' ] );

remove_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] );
$this->restoreWpHook( 'rocket_buffer' );

if ( $this->user_id > 0 ) {
wp_delete_user( $this->user_id );
}

parent::tear_down();
}

Expand All @@ -49,6 +55,7 @@ public function tear_down() {
*/
public function testShouldReturnAsExpected( $config, $expected ) {
$this->config = $config;
$this->cached_user = $config['user_cache_enabled'] ?? false;

if ( isset( $config['query_string'] ) ) {
$_GET[ $config['query_string'] ] = 1;
Expand All @@ -67,6 +74,14 @@ public function testShouldReturnAsExpected( $config, $expected ) {

Functions\when( 'wp_create_nonce' )->justReturn( '96ac96b69e' );

if ( isset( $config['is_logged_in'] ) ) {
$this->user_id = self::factory()->user->create( [ 'role' => 'editor' ] );
wp_set_current_user( $this->user_id );
}

// Override cache_logged_user option for this specific scenario.
add_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] );

$this->assertSame(
$expected,
apply_filters( 'rocket_buffer', $config['html'] )
Expand All @@ -76,4 +91,8 @@ public function testShouldReturnAsExpected( $config, $expected ) {
public function add_delay() {
return $this->config['filter_delay'];
}

public function get_cache_user() {
return $this->cached_user;
}
}

0 comments on commit 21c9f02

Please sign in to comment.