Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions inc/Engine/HealthCheck/PageCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace WP_Rocket\Engine\HealthCheck;

use WP_Rocket\Event_Management\Subscriber_Interface;

class PageCache implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'http_headers_useragent' => [ 'page_cache_useragent', 10, 2 ],
];
}

/**
* Pass plugin header to skip test "mandatory cookie".
*
* @param string $user_agent WordPress user agent string.
* @param string $url The request URL.
* @return string
*/
public function page_cache_useragent( $user_agent, $url = null ) {

Check warning on line 26 in inc/Engine/HealthCheck/PageCache.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/HealthCheck/PageCache.php#L26

Avoid unused parameters such as '$url'.
$uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) );
if (
strpos( $uri, 'wp-site-health' ) !== false &&
strpos( $uri, 'page-cache' ) !== false
) {
$user_agent = 'WP Rocket';
}

return $user_agent;
}
}
2 changes: 2 additions & 0 deletions inc/Engine/HealthCheck/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ServiceProvider extends AbstractServiceProvider {
*/
protected $provides = [
'health_check',
'health_check_page_cache',
'action_scheduler_check',
];

Expand All @@ -38,6 +39,7 @@ public function provides( string $id ): bool {
public function register(): void {
$this->getContainer()->addShared( 'health_check', HealthCheck::class )
->addArgument( 'options' );
$this->getContainer()->addShared( 'health_check_page_cache', PageCache::class );
$this->getContainer()->addShared( 'action_scheduler_check', ActionSchedulerCheck::class );
}
}
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ private function init_common_subscribers() {
'media_fonts_frontend_subscriber',
'media_fonts_admin_subscriber',
'media_fonts_clean_subscriber',
'health_check_page_cache',
];

$host_type = HostResolver::get_host_service();
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return [
'null' => [
'request_uri' => null,
'user_agent_default' => 'WordPress',
'user_agent_expected' => 'WordPress',
],
'default' => [
'request_uri' => '/wp-json/wp-site-health/v1/tests/https-status?_locale=user',
'user_agent_default' => 'WordPress',
'user_agent_expected' => 'WordPress',
],
'plugin' => [
'request_uri' => '/wp-json/wp-site-health/v1/tests/page-cache?_locale=user',
'user_agent_default' => 'WP Rocket',
'user_agent_expected' => 'WP Rocket',
],
];
44 changes: 44 additions & 0 deletions tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\HealthCheck\PageCache;

use Brain\Monkey\Functions;
use WP_Rocket\Engine\HealthCheck\PageCache;
use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Engine\HealthCheck\PageCache::page_cache_useragent
*
* @group zzz
*/
class Test_UserAgent extends TestCase {
private $health;

public function setUp(): void {
parent::setUp();

$this->health = new PageCache();

Functions\when( 'sanitize_text_field' )->alias(
function ( $value ) {
return $value;
}
);

Functions\when( 'wp_unslash' )->alias(
function ( $value ) {
return stripslashes( $value );
}
);
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $request_uri, $user_agent_default, $user_agent_expected ) {
$_SERVER['REQUEST_URI'] = $request_uri;

$user_agent = $this->health->page_cache_useragent( $user_agent_default );
$this->assertSame( $user_agent_expected, $user_agent );
}
}
Loading