Skip to content

Detect available object caching and show recommended plugin in admin dashboard. #6493

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

Merged
merged 13 commits into from
Sep 10, 2021
Merged
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
81 changes: 80 additions & 1 deletion src/Admin/SiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,62 @@ private function get_persistent_object_cache_learn_more_action() {
public function persistent_object_cache() {
$is_using_object_cache = wp_using_ext_object_cache();

$description = '<p>' . __( 'The AMP plugin performs at its best when persistent object cache is enabled. Object caching is used to more effectively store image dimensions and parsed CSS.', 'amp' ) . '</p>';

if ( ! $is_using_object_cache ) {
$services = $this->get_persistent_object_cache_availability();

$available_services = array_filter(
$services,
static function ( $service ) {
return $service['available'];
}
);

$description .= '<p>';
if ( count( $available_services ) > 0 ) {

$description .= _n(
'During the test, we found the following object caching service may be available on your server:',
'During the test, we found the following object caching services may be available on your server:',
count( $available_services ),
'amp'
);

$description .= ' ' . implode(
', ',
array_map(
static function ( $available_service ) {
return sprintf(
'<a href="%s">%s</a>',
esc_url( $available_service['url'] ),
esc_html( $available_service['name'] )
);
},
$available_services
)
);

$description .= ' ' . _n(
'(link goes to Add Plugins screen).',
'(links go to Add Plugins screen).',
count( $available_services ),
'amp'
);

$description .= ' ';
}

$description .= __( 'Please check with your host for what persistent caching services are available.', 'amp' );
$description .= '</p>';
}

return [
'badge' => [
'label' => $this->get_badge_label(),
'color' => $is_using_object_cache ? 'green' : 'orange',
],
'description' => esc_html__( 'The AMP plugin performs at its best when persistent object cache is enabled. Object caching is used to more effectively store image dimensions and parsed CSS.', 'amp' ),
'description' => wp_kses_post( $description ),
'actions' => $this->get_persistent_object_cache_learn_more_action(),
'test' => 'amp_persistent_object_cache',
'status' => $is_using_object_cache ? 'good' : 'recommended',
Expand All @@ -173,6 +223,35 @@ public function persistent_object_cache() {
];
}

/**
* Return list of available object cache mechanism.
*
* @return array
*/
public function get_persistent_object_cache_availability() {
return [
'redis' => [
'available' => class_exists( 'Redis' ),
'name' => _x( 'Redis', 'persistent object cache service', 'amp' ),
'url' => admin_url( 'plugin-install.php?s=redis%20object%20cache&tab=search&type=term' ),
],
'memcached' => [
'available' => ( class_exists( 'Memcache' ) || class_exists( 'Memcached' ) ),
'name' => _x( 'Memcached', 'persistent object cache service', 'amp' ),
'url' => admin_url( 'plugin-install.php?s=memcached%20object%20cache&tab=search&type=term' ),
],
'apcu' => [
'available' => (
extension_loaded( 'apcu' ) ||
function_exists( 'apc_store' ) ||
function_exists( 'apcu_store' )
),
'name' => _x( 'APCu', 'persistent object cache service', 'amp' ),
'url' => admin_url( 'plugin-install.php?s=apcu%20object%20cache&tab=search&type=term' ),
],
];
}

/**
* Gets the test result data for whether the AMP slug (query var) was defined late.
*
Expand Down
66 changes: 59 additions & 7 deletions tests/php/src/Admin/SiteHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class SiteHealthTest extends TestCase {

use PrivateAccess;

/**
* Whether external object cache is being used.
*
* @var bool
*/
private $was_wp_using_ext_object_cache;

/**
* The tested instance.
*
Expand All @@ -51,6 +58,8 @@ public function setUp() {
remove_post_type_support( $post_type, 'amp' );
}
delete_option( AMP_Options_Manager::OPTION_NAME );

$this->was_wp_using_ext_object_cache = wp_using_ext_object_cache();
}

/**
Expand All @@ -59,8 +68,8 @@ public function setUp() {
* @inheritDoc
*/
public function tearDown() {
unset( $GLOBALS['_wp_using_ext_object_cache'] );
parent::tearDown();
wp_using_ext_object_cache( $this->was_wp_using_ext_object_cache );
}

/**
Expand Down Expand Up @@ -107,16 +116,19 @@ public function test_add_tests() {
}

/**
* Test persistent_object_cache.
* Test get_persistent_object_cache_availability.
*
* @covers ::persistent_object_cache()
* @covers ::get_persistent_object_cache_availability()
*/
public function test_persistent_object_cache() {
public function test_get_persistent_object_cache_availability() {
$data = [
'test' => 'amp_persistent_object_cache',
];

$GLOBALS['_wp_using_ext_object_cache'] = false;
wp_using_ext_object_cache( false );
$output = $this->instance->persistent_object_cache();

$this->assertArraySubset(
array_merge(
$data,
Expand All @@ -128,10 +140,13 @@ public function test_persistent_object_cache() {
],
]
),
$this->instance->persistent_object_cache()
$output
);

$GLOBALS['_wp_using_ext_object_cache'] = true;
$this->assertStringContainsString( 'Please check with your host for what persistent caching services are available.', $output['description'] );

wp_using_ext_object_cache( true );
$output = $this->instance->persistent_object_cache();
$this->assertArraySubset(
array_merge(
$data,
Expand All @@ -144,8 +159,29 @@ public function test_persistent_object_cache() {
],
]
),
$this->instance->persistent_object_cache()
$output
);
$this->assertStringNotContainsString( 'Please check with your host for what persistent caching services are available.', $output['description'] );
}

/**
* @covers ::get_persistent_object_cache_availability()
*/
public function test_persistent_object_cache_with_suggestions() {

$output = $this->instance->get_persistent_object_cache_availability();

$this->assertArrayHasKey( 'redis', $output );
$this->assertIsBool( $output['redis']['available'] );
$this->assertEquals( 'Redis', $output['redis']['name'] );

$this->assertArrayHasKey( 'memcached', $output );
$this->assertIsBool( $output['memcached']['available'] );
$this->assertEquals( 'Memcached', $output['memcached']['name'] );

$this->assertArrayHasKey( 'apcu', $output );
$this->assertIsBool( $output['apcu']['available'] );
$this->assertEquals( 'APCu', $output['apcu']['name'] );
}

/**
Expand Down Expand Up @@ -235,6 +271,7 @@ public function test_icu_version() {
* @covers ::css_transient_caching()
*/
public function test_css_transient_caching() {
wp_using_ext_object_cache( false );
$data = [
'test' => 'amp_css_transient_caching',
];
Expand Down Expand Up @@ -270,6 +307,21 @@ public function test_css_transient_caching() {
),
$this->instance->css_transient_caching()
);

wp_using_ext_object_cache( true );
$this->assertArraySubset(
array_merge(
$data,
[
'status' => 'good',
'badge' => [
'label' => 'AMP',
'color' => 'blue',
],
]
),
$this->instance->css_transient_caching()
);
}

/**
Expand Down