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

Close #6863 Add termly compatibility #7198

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ private function init_common_subscribers() {
'performance_hints_admin_subscriber',
'lrc_frontend_subscriber',
'taxonomy_subscriber',
'termly_subscriber',
];

$host_type = HostResolver::get_host_service();
Expand Down
45 changes: 45 additions & 0 deletions inc/ThirdParty/Plugins/Cookie/Termly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\ThirdParty\Plugins\Cookie;

use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Compatibility class for Termly.
*/
class Termly implements Subscriber_Interface {
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! defined( 'TERMLY_VERSION' ) ) {
return [];
}

return [
'rocket_exclude_defer_js' => 'exclude_termly_defer_and_delay_js',
'rocket_delay_js_exclusions' => 'exclude_termly_defer_and_delay_js',
];
}

/**
* Defer and delay Termly Resources
*
* @param array $exclude_delay_js Array of JS to be excluded.
*
* @return array
*/
public function exclude_termly_defer_and_delay_js( array $exclude_delay_js ): array {
$auto_block = get_option( 'termly_display_auto_blocker', 'off' );
if ( 'on' !== $auto_block ) {
return $exclude_delay_js;
}

$exclude_delay_js[] = 'app.termly.io/resource-blocker/(.*)';

return $exclude_delay_js;
}
}
3 changes: 3 additions & 0 deletions inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WP_Rocket\Subscriber\Third_Party\Plugins\SyntaxHighlighter_Subscriber;
use WP_Rocket\ThirdParty\Plugins\Ads\Adthrive;
use WP_Rocket\ThirdParty\Plugins\ConvertPlug;
use WP_Rocket\ThirdParty\Plugins\Cookie\Termly;
use WP_Rocket\ThirdParty\Plugins\Ecommerce\BigCommerce;
use WP_Rocket\ThirdParty\Plugins\Ecommerce\WooCommerceSubscriber;
use WP_Rocket\ThirdParty\Plugins\I18n\TranslatePress;
Expand Down Expand Up @@ -98,6 +99,7 @@ class ServiceProvider extends AbstractServiceProvider {
'wpgeotargeting',
'weglot',
'contactform7',
'termly_subscriber',
];

/**
Expand Down Expand Up @@ -227,5 +229,6 @@ public function register(): void {
$this->getContainer()->addShared( 'translatepress', TranslatePress::class );
$this->getContainer()->addShared( 'wpgeotargeting', WPGeotargeting::class );
$this->getContainer()->addShared( 'contactform7', ContactForm7::class );
$this->getContainer()->addShared( 'termly_subscriber', Termly::class );
}
}
61 changes: 61 additions & 0 deletions tests/Fixtures/inc/ThirdParty/Plugins/Cookie/deferJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

$html = <<<HTML
<script src="http://example.org/wp-content/plugins/hello-world/script.js" id="hello-script"></script>
<script data-cfasync="false" src="/javascript.js"></script>
<script src="https://app.termly.io/resource-blocker/c3dccd2d-48a5-4b63-9109-2dc7068924df?autoBlock=on"></script>
HTML
;

$expected = <<<HTML
<script src="http://example.org/wp-content/plugins/hello-world/script.js" id="hello-script" data-rocket-defer defer></script>
<script data-cfasync="false" src="/javascript.js" data-rocket-defer defer></script>
<script src="https://app.termly.io/resource-blocker/c3dccd2d-48a5-4b63-9109-2dc7068924df?autoBlock=on" data-rocket-defer defer></script>
HTML
;

$expected_exclusion = <<<HTML
<script src="http://example.org/wp-content/plugins/hello-world/script.js" id="hello-script" data-rocket-defer defer></script>
<script data-cfasync="false" src="/javascript.js" data-rocket-defer defer></script>
<script src="https://app.termly.io/resource-blocker/c3dccd2d-48a5-4b63-9109-2dc7068924df?autoBlock=on"></script>
HTML
;

$exclusions_list = (object) [
'defer_js_external_exclusions' => [
'gist.github.com',
'/api/scripts/lb_cs.js',
],
];

return [
'testShouldReturnUpdatedHTML' => [
'config' => [
'donotrocketoptimize' => false,
'post_meta' => false,
'options' => [
'defer_all_js' => 1,
'exclude_defer_js' => [],
],
'exclusions' => $exclusions_list,
],
'html' => $html,
'expected' => $expected,
],

'testShouldEvaluateRegexPatternInOptions' => [
'config' => [
'donotrocketoptimize' => false,
'post_meta' => false,
'options' => [
'defer_all_js' => 1,
'exclude_defer_js' => [
'app.termly.io/resource-blocker/(.*)',
],
],
'exclusions' => $exclusions_list,
],
'html' => $html,
'expected' => $expected_exclusion,
],
];
20 changes: 20 additions & 0 deletions tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'shouldNotExcludeTermlyResources' => [
'config' => [
'excluded' => [],
'termly_display_auto_blocker' => 'off'
],
'expected' => []
],
'shouldExcludeTermlyResources' => [
'config' => [
'excluded' => [],
'termly_display_auto_blocker' => 'on'
],
'expected' => [
'app.termly.io/resource-blocker/(.*)',
]
]
];
61 changes: 61 additions & 0 deletions tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Plugins\Cookie;

use WP_Rocket\Tests\Integration\ContentTrait;
use WP_Rocket\Tests\Integration\TestCase;

/**
* Test class covering \WP_Rocket\ThirdParty\Plugins\Cookie\Termly::exclude_termly_defer_and_delay_js
*
* @group Termly
*/
class Test_deferJs extends TestCase {


private $defer_js;
private $exclude_defer_js;

public function set_up() {
parent::set_up();

set_current_screen( 'front' );
}

public function tear_down() {
// Re-enable ATF optimization.
remove_filter( 'rocket_above_the_fold_optimization', '__return_false' );

remove_filter( 'pre_get_rocket_option_defer_all_js', [ $this, 'set_defer_js' ] );
remove_filter( 'pre_get_rocket_option_exclude_defer_js', [ $this, 'set_exclude_defer_js' ] );
delete_post_meta( 100, '_rocket_exclude_defer_all_js' );
delete_transient( 'wpr_dynamic_lists' );

parent::tear_down();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $html, $expected ) {
$this->exclude_defer_js = $config['options']['exclude_defer_js'];
$this->defer_js = $config['options']['defer_all_js'];

add_filter( 'pre_get_rocket_option_defer_all_js', [ $this, 'set_defer_js' ] );
add_filter( 'pre_get_rocket_option_exclude_defer_js', [ $this, 'set_exclude_defer_js' ] );

set_transient( 'wpr_dynamic_lists', $config['exclusions'], HOUR_IN_SECONDS );

$this->assertSame(
$expected,
wpm_apply_filters_typed('string', 'rocket_buffer', $html )
);
}

public function set_defer_js() {
return $this->defer_js;
}

public function set_exclude_defer_js() {
return $this->exclude_defer_js;
}
}
61 changes: 61 additions & 0 deletions tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Plugins\Cookie;

use WP_Rocket\Tests\Integration\TestCase;
use WP_Rocket\ThirdParty\Plugins\Cookie\Termly;
use Brain\Monkey\Functions;

/**
* Test class covering \WP_Rocket\ThirdParty\Plugins\Cookies\Termly::clean_domain
*
* @group Plugins
*/
class Test_ExcludeDelayJs extends TestCase {

private $event;
private $subscriber;

protected $path_to_test_data = '/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php';

public function set_up() {
parent::set_up();

if ( ! defined( 'TERMLY_VERSION' ) ) {
define( 'TERMLY_VERSION', '1.0' );
}

$container = wpm_apply_filters_typed('object', 'rocket_container', '' );

$this->event = $container->get( 'event_manager' );
}

public function tear_down() {
$this->event->remove_subscriber( $this->subscriber );

parent::tear_down();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected ) {
$this->subscriber = new Termly();
$this->event->add_subscriber( $this->subscriber );

Functions\expect( 'get_option' )
->twice()
->andReturn( $config['termly_display_auto_blocker'] );

$this->assertSame(
$expected,
wpm_apply_filters_typed( 'array', 'rocket_delay_js_exclusions', $config['excluded'] )
);

$this->assertSame(
$expected,
wpm_apply_filters_typed( 'array', 'rocket_exclude_defer_js', $config['excluded'] )
);
}
}
Loading