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

Improve plugin compatibility with HealthCheck - PageCache #6042

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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;
}
}
3 changes: 3 additions & 0 deletions inc/Engine/HealthCheck/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ServiceProvider extends AbstractServiceProvider {
*/
protected $provides = [
'health_check',
'health_check_page_cache',
'action_scheduler_check',
];

Expand All @@ -37,6 +38,8 @@ public function register(): void {
$this->getContainer()->addShared( 'health_check', HealthCheck::class )
->addArgument( $this->getContainer()->get( 'options' ) )
->addTag( 'admin_subscriber' );
$this->getContainer()->addShared( 'health_check_page_cache', PageCache::class )
->addTag( 'common_subscriber' );
$this->getContainer()->addShared( 'action_scheduler_check', ActionSchedulerCheck::class )
->addTag( 'common_subscriber' );
}
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ private function init_common_subscribers() {
'atf_cron_subscriber',
'saas_admin_subscriber',
'warmup_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',
],
];
47 changes: 47 additions & 0 deletions tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

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

use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Settings\Page;
use WP_Rocket\Engine\HealthCheck\PageCache;
use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Engine\HealthCheck\PageCache::page_cache_useragent
*
* @group HealthCheck
*/
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