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

Added a fix for the notice not displaying when cloudflare and APO are enabled #5976

Merged
merged 5 commits into from Jun 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions inc/Addon/Cloudflare/Subscriber.php
Expand Up @@ -79,6 +79,7 @@ public static function get_subscribed_events() {
],
'rocket_buffer' => [ 'protocol_rewrite', PHP_INT_MAX ],
'wp_calculate_image_srcset' => [ 'protocol_rewrite_srcset', PHP_INT_MAX ],
'rocket_cdn_helper_addons' => 'add_cdn_helper_message',
];
}

Expand Down Expand Up @@ -693,4 +694,15 @@ private function can_protocol_rewrite(): bool {
apply_filters( 'do_rocket_protocol_rewrite', false ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
);
}

/**
* Add the helper message on the CDN settings.
*
* @param string[] $addons Name from the addon that requires the helper message.
* @return string[]
*/
public function add_cdn_helper_message( array $addons ): array {
$addons[] = 'Cloudflare';
return $addons;
}
}
17 changes: 17 additions & 0 deletions inc/Addon/Sucuri/Subscriber.php
Expand Up @@ -48,6 +48,7 @@ public static function get_subscribed_events() {
'after_rocket_clean_files' => 'maybe_clean_firewall_cache',
'admin_post_rocket_purge_sucuri' => 'do_admin_post_rocket_purge_sucuri',
'admin_notices' => 'maybe_print_notice',
'rocket_cdn_helper_addons' => 'add_cdn_helper_message',
];
}

Expand Down Expand Up @@ -339,6 +340,22 @@ private function request_api( $params = [] ) {
return $data;
}

/**
* Add the helper message on the CDN settings.
*
* @param string[] $addons Name from the addon that requires the helper message.
* @return string[]
*/
public function add_cdn_helper_message( array $addons ): array {

if ( ! $this->options->get( 'sucury_waf_cache_sync', false ) ) {
return $addons;
}

$addons[] = 'Sucuri';
return $addons;
}

/**
* An i18n-friendly alternative to the built-in PHP method `http_build_query()`.
*
Expand Down
18 changes: 12 additions & 6 deletions inc/Engine/Admin/Settings/Page.php
Expand Up @@ -1625,16 +1625,22 @@ private function cdn_section() {
);

$maybe_display_cdn_helper = '';
$addons = [];

if ( get_rocket_option( 'do_cloudflare' ) ) {
$addons[] = 'Cloudflare';
}
/**
* Name from addons requiring the helper message.
*
* @param string[] addons.
*
* @return string []
*/
$addons = apply_filters( 'rocket_cdn_helper_addons', [] );

if ( get_rocket_option( 'sucury_waf_cache_sync' ) ) {
$addons[] = 'Sucuri';
if ( ! is_array( $addons ) ) {
$addons = [];
}

$addons = array_unique( $addons );

if ( ! empty( $addons ) ) {
$maybe_display_cdn_helper = wp_sprintf(
// translators: %1$s = opening em tag, %2$l = list of add-on name(s), %3$s = closing em tag.
Expand Down
15 changes: 15 additions & 0 deletions inc/ThirdParty/Plugins/CDN/Cloudflare.php
Expand Up @@ -75,6 +75,7 @@ public static function get_subscribed_events() {
'rocket_rucss_complete_job_status' => 'purge_cloudflare_after_usedcss',
'rocket_rucss_after_clearing_usedcss' => 'purge_cloudflare_after_usedcss',
'admin_post_rocket_enable_separate_mobile_cache' => 'enable_separate_mobile_cache',
'rocket_cdn_helper_addons' => 'add_cdn_helper_message',
];
}

Expand Down Expand Up @@ -449,4 +450,18 @@ private function is_apo_enabled(): bool {

return (bool) $is_apo_enabled['value'];
}

/**
* Add the helper message on the CDN settings.
*
* @param string[] $addons Name from the addon that requires the helper message.
* @return string[]
*/
public function add_cdn_helper_message( array $addons ): array {
if ( ! $this->is_plugin_active() ) {
return $addons;
}
$addons[] = 'Cloudflare';
return $addons;
}
}
@@ -0,0 +1,12 @@
<?php
return [
'shouldReturnMessage' => [
'config' => [
'addons' => [],
],
'expected' => [
'Cloudflare'
]
],

];
21 changes: 21 additions & 0 deletions tests/Fixtures/inc/Addon/Sucuri/Subscriber/addCdnHelperMessage.php
@@ -0,0 +1,21 @@
<?php
return [
'addonEnabledShouldAddSucuri' => [
'config' => [
'addons' => [],
'is_enabled' => true,
],
'expected' => [
'Sucuri'
]
],
'addonDisabledShouldReturnSame' => [
'config' => [
'addons' => [],
'is_enabled' => false,
],
'expected' => [

]
],
];
@@ -0,0 +1,27 @@
<?php
return [
'pluginEnabledShouldAddCloudflare' => [
'config' => [
'addons' => [],
'plugin_enabled' => true,
'cf_email' => 'email',
'cf_key' => 'key',
'cf_domain' => 'domain',
],
'expected' => [
'Cloudflare'
]
],
'pluginDisabledShouldReturnSame' => [
'config' => [
'addons' => [],
'plugin_enabled' => false,
'cf_email' => 'email',
'cf_key' => 'key',
'cf_domain' => 'domain',
],
'expected' => [

]
]
];
@@ -0,0 +1,20 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\Addon\Cloudflare\Subscriber;

use WP_Rocket\Tests\Integration\TestCase;

/**
* @covers \WP_Rocket\Addon\Cloudflare\Subscriber::add_cdn_helper_message
* @group Cloudflare
*/
class Test_addCdnHelperMessage extends TestCase {

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
$this->assertSame($expected, apply_filters('rocket_cdn_helper_addons', $config['addons']));
}
}
@@ -0,0 +1,37 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\Addon\Sucuri\Subscriber;

use WP_Rocket\Tests\Integration\TestCase;

/**
* @covers \WP_Rocket\Addon\Sucuri\Subscriber::add_cdn_helper_message
*/
class Test_addCdnHelperMessage extends TestCase {

public function set_up()
{
add_filter( 'pre_get_rocket_option_sucury_waf_cache_sync', [ $this, 'sucury_waf_cache_sync'] );
parent::set_up();
}

public function tear_down()
{
remove_filter( 'pre_get_rocket_option_sucury_waf_cache_sync', [ $this, 'sucury_waf_cache_sync'] );
parent::tear_down();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
$this->config = $config;

$this->assertSame($expected, apply_filters('rocket_cdn_helper_addons', $config['addons']));
}

public function sucury_waf_cache_sync() {
return $this->config['is_enabled'];
}
}
@@ -0,0 +1,61 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Plugins\CDN\Cloudflare;

use WP_Rocket\Tests\Integration\TestCase;

/**
* @covers \WP_Rocket\ThirdParty\Plugins\CDN\Cloudflare::add_cdn_helper_message
*/
class Test_addCdnHelperMessage extends TestCase {

protected $config;

public function set_up()
{
parent::set_up();
add_filter('pre_option_active_plugins', [$this, 'plugin_enabled']);
add_filter('pre_option_cloudflare_api_email', [$this, 'cloudflare_api_email']);
add_filter('pre_option_cloudflare_api_key', [$this, 'cloudflare_api_key']);
add_filter('pre_option_cloudflare_cached_domain_name', [$this, 'cloudflare_cached_domain_name']);
}

public function tear_down()
{
remove_filter('pre_option_active_plugins', [$this, 'plugin_enabled']);
remove_filter('pre_option_cloudflare_api_email', [$this, 'cloudflare_api_email']);
remove_filter('pre_option_cloudflare_api_key', [$this, 'cloudflare_api_key']);
remove_filter('pre_option_cloudflare_cached_domain_name', [$this, 'cloudflare_cached_domain_name']);
parent::tear_down();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
$this->config = $config;
$this->assertSame($expected, apply_filters('rocket_cdn_helper_addons', $config['addons']));
}

public function plugin_enabled($plugins) {
if(! $this->config['plugin_enabled']) {
return $plugins;
}
$plugins []= 'cloudflare/cloudflare.php';

Check warning on line 45 in tests/Integration/inc/ThirdParty/Plugins/CDN/Cloudflare/addCdnHelperMessage.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.1 on ubuntu-latest.

Automatic conversion of false to array is deprecated

Check warning on line 45 in tests/Integration/inc/ThirdParty/Plugins/CDN/Cloudflare/addCdnHelperMessage.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.2 on ubuntu-latest.

Automatic conversion of false to array is deprecated

return $plugins;
}

public function cloudflare_api_email() {
return $this->config['cf_email'];
}

public function cloudflare_api_key() {
return $this->config['cf_key'];
}

public function cloudflare_cached_domain_name() {
return $this->config['cf_domain'];
}
}
63 changes: 63 additions & 0 deletions tests/Unit/inc/Addon/Cloudflare/Subscriber/addCdnHelperMessage.php
@@ -0,0 +1,63 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Addon\Cloudflare\Subscriber;

use Mockery;
use WP_Rocket\Addon\Cloudflare\Subscriber;
use WP_Rocket\Addon\Cloudflare\Cloudflare;
use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
use WPMedia\Cloudflare\Auth\AuthFactoryInterface;


use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Addon\Cloudflare\Subscriber::add_cdn_helper_message
*/
class Test_addCdnHelperMessage extends TestCase {

/**
* @var Cloudflare
*/
protected $cloudflare;

/**
* @var Options_Data
*/
protected $options;

/**
* @var Options
*/
protected $options_api;

/**
* @var AuthFactoryInterface
*/
protected $auth_factory;

/**
* @var Subscriber
*/
protected $subscriber;

public function set_up() {
parent::set_up();
$this->cloudflare = Mockery::mock(Cloudflare::class);
$this->options = Mockery::mock(Options_Data::class);
$this->options_api = Mockery::mock(Options::class);
$this->auth_factory = Mockery::mock(AuthFactoryInterface::class);

$this->subscriber = new Subscriber($this->cloudflare, $this->options, $this->options_api, $this->auth_factory);
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
$this->assertSame($expected, $this->subscriber->add_cdn_helper_message($config['addons']));

}
}