-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 4472 - Display notice when DelayJS is activated with Autoptimize …
…Aggregate JS already in effect (#4501) * Add tests and method for AO agg-js and wpr delayjs * Fix param order in docblock * Use add_settings_error instead of transient * Fix docblock order * Remove dead transient_errrors code * Remove extra spacing * Use matching priority in test * Remove using void thing. * Move warning method to new Compat Subscriber * Reconfigure for new story conditions * Add Autoptimize class to container * Remove dead code from DelayJs Subscriber * Move test to ThirdParty * Check failing test This will display the notice as expected, but the integration test will not run in isolation. Other functions from preload still run on admin_notice. * Fix phpcs * Revise tests and make pass * Fix open/close strong tags * Fix strong tags in test * Remove extra dismiss icon
- Loading branch information
Showing
6 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace WP_Rocket\ThirdParty\Plugins\Optimization; | ||
|
||
use WP_Rocket\Admin\Options_Data; | ||
use WP_Rocket\Event_Management\Subscriber_Interface; | ||
|
||
class Autoptimize implements Subscriber_Interface { | ||
/** | ||
* WP Rocket Options instance | ||
* | ||
* @var Options_Data | ||
*/ | ||
private $options; | ||
|
||
/** | ||
* Array containing the errors | ||
* | ||
* @var array | ||
*/ | ||
private $errors = []; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Options_Data $options WP Rocket Options instance. | ||
*/ | ||
public function __construct( Options_Data $options ) { | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* Return an array of events that this subscriber listens to. | ||
* | ||
* @return array | ||
* @since 3.10.4 | ||
*/ | ||
public static function get_subscribed_events() { | ||
return [ | ||
'admin_notices' => [ 'warn_when_js_aggregation_and_delay_js_active' ], | ||
]; | ||
} | ||
|
||
/** | ||
* Add an admin warning notice when Delay JS and JS Aggregation are both activated. | ||
* | ||
* @since 3.10.4 | ||
*/ | ||
public function warn_when_js_aggregation_and_delay_js_active() { | ||
if ( ! rocket_get_constant( 'AUTOPTIMIZE_PLUGIN_VERSION', false ) ) { | ||
return; | ||
} | ||
|
||
if ( ! current_user_can( 'rocket_manage_options' ) ) { | ||
return; | ||
} | ||
|
||
$autoptimize_aggregate_js_setting = get_option( 'autoptimize_js_aggregate' ); | ||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true ); | ||
|
||
if ( 'on' !== $autoptimize_aggregate_js_setting || false === (bool) $this->options->get( 'delay_js' ) ) { | ||
if ( ! is_array( $boxes ) ) { | ||
return; | ||
} | ||
|
||
$this->remove_warning_dismissal( $boxes, __FUNCTION__ ); | ||
|
||
return; | ||
} | ||
|
||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) { | ||
return; | ||
} | ||
|
||
$message = '<strong>' . __( | ||
'We have detected that Autoptimize\'s JavaScript Aggregation feature is enabled. The Delay JavaScript Execution will not be applied to the file it creates. We suggest disabling it to take full advantage of Delay JavaScript Execution.', | ||
'rocket' | ||
) . '</strong>'; | ||
|
||
rocket_notice_html( | ||
[ | ||
'status' => 'warning', | ||
'message' => $message, | ||
'dismissible' => '', | ||
'dismiss_button' => __FUNCTION__, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Remove a warning box dismissal. | ||
* | ||
* @param array $boxes The rocket_boxes user meta. | ||
* @param string $name Slug for the box to be removed. | ||
*/ | ||
private function remove_warning_dismissal( $boxes, $name ) { | ||
if ( ! in_array( $name, $boxes, true ) ) { | ||
return; | ||
} | ||
|
||
unset( $boxes[ array_search( $name, $boxes, true ) ] ); | ||
update_user_meta( get_current_user_id(), 'rocket_boxes', $boxes ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...inc/ThirdParty/Plugins/Optimization/Autoptimize/warnWhenJsAggregationAndDelayJsActive.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
$expected_html = <<<HTML | ||
<div class="notice notice-warning "> | ||
<p> | ||
<strong> | ||
We have detected that Autoptimize's JavaScript Aggregation feature is enabled. The Delay JavaScript Execution will not be applied to the file it creates. We suggest disabling it to take full advantage of Delay JavaScript Execution.</strong> | ||
</p> | ||
<p> | ||
<a class="rocket-dismiss" href="http://example.org/wp-admin/admin-post.php?action=rocket_ignore&box=warn_when_js_aggregation_and_delay_js_active&_wpnonce=123456"> | ||
Dismiss this notice.</a></p></div> | ||
HTML; | ||
|
||
return [ | ||
'shouldAddNoticeWhenAutoptimizeAggregateJsOnAndDelayJsActivated' => [ | ||
'config' => [ | ||
'delayJSActive' => true, | ||
'autoptimizeAggregateJSActive' => 'on', | ||
'dismissed' => false, | ||
], | ||
'expected' => $expected_html | ||
], | ||
|
||
'shouldSkipWhenAutoptimizeAggregateJsOffAndDelayJsNotActivated' => [ | ||
'config' => [ | ||
'delayJSActive' => false, | ||
'autoptimizeAggregateJSActive' => 'off', | ||
'dismissed' => false, | ||
], | ||
'expected' => '', | ||
], | ||
|
||
'shouldSkipWhenAutoptimizeAggregateJsOffAndDelayJsActivated' => [ | ||
'config' => [ | ||
'delayJSActive' => true, | ||
'autoptimizeAggregateJSActive' => 'off', | ||
'dismissed' => false, | ||
], | ||
'expected' => '', | ||
], | ||
|
||
'shouldSkipWhenAutoptimizeAggregateJsOnAndDelayJsNotActivated' => [ | ||
'config' => [ | ||
'delayJSActive' => false, | ||
'autoptimizeAggregateJSActive' => 'on', | ||
'dismissed' => false, | ||
], | ||
'expected' => '', | ||
], | ||
|
||
'shouldSkipWhenUserHasDismissedNotice' => [ | ||
'config' => [ | ||
'delayJSActive' => true, | ||
'autoptimizeAggregateJSActive' => 'on', | ||
'dismissed' => true, | ||
], | ||
'expected' => '', | ||
], | ||
|
||
'shouldClearDismissalWhenUserDeactivatesDelayJS' => [ | ||
'config' => [ | ||
'delayJSActive' => false, | ||
'autoptimizeAggregateJSActive' => 'on', | ||
'dismissed' => true, | ||
], | ||
'expected' => '', | ||
], | ||
]; |
92 changes: 92 additions & 0 deletions
92
...inc/ThirdParty/Plugins/Optimization/Autoptimize/warnWhenJsAggregationAndDelayJsActive.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
use Brain\Monkey\Functions; | ||
use WP_Rocket\Tests\Integration\CapTrait; | ||
use WP_Rocket\Tests\Integration\TestCase; | ||
|
||
/** | ||
* @covers WP_Rocket\ThirdParty\Plugins\Optimization\Autoptimize::warn_when_js_aggregation_and_delay_js_active | ||
* | ||
* @group Autoptimize | ||
* @group AdminOnly | ||
* @group ThirdParty | ||
*/ | ||
class Test_WarnWhenJsAggregationAndDelayJsActive extends TestCase { | ||
use CapTrait; | ||
|
||
public static function setUpBeforeClass(): void { | ||
parent::setUpBeforeClass(); | ||
|
||
CapTrait::setAdminCap(); | ||
} | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->unregisterAllCallbacksExcept( | ||
'admin_notices', | ||
'warn_when_js_aggregation_and_delay_js_active' | ||
); | ||
|
||
Functions\expect( 'wp_create_nonce' ) | ||
->with( 'warn_when_js_aggregation_and_delay_js_active' ) | ||
->andReturn( '123456' ); | ||
} | ||
|
||
public function tearDown() { | ||
$this->restoreWpFilter( 'admin_notices' ); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @dataProvider configTestData | ||
*/ | ||
public function testShouldAddExpectedNotice( $config, $expected ) { | ||
$this->constants['AUTOPTIMIZE_PLUGIN_VERSION'] = '1.2.3'; | ||
$this->stubRocketGetConstant(); | ||
|
||
$current_user = static::factory()->user->create( [ 'role' => 'administrator' ] ); | ||
wp_set_current_user( $current_user ); | ||
|
||
update_option( 'autoptimize_js_aggregate', $config['autoptimizeAggregateJSActive'] ); | ||
add_filter( 'pre_get_rocket_option_delay_js', function () use ( $config ) { | ||
return $config[ 'delayJSActive' ]; | ||
} ); | ||
|
||
if ( $config['dismissed'] ) { | ||
update_user_meta( | ||
$current_user, | ||
'rocket_boxes', | ||
[ 'warn_when_js_aggregation_and_delay_js_active' ] | ||
); | ||
} | ||
|
||
ob_start(); | ||
do_action( 'admin_notices' ); | ||
$notices = ob_get_clean(); | ||
$notices = empty( $notices ) ? $notices : $this->format_the_html( $notices ); | ||
|
||
$this->assertSame( $this->format_the_html( $expected ), $notices ); | ||
|
||
$boxes = get_user_meta( $current_user, 'rocket_boxes', true ); | ||
|
||
if ( $config[ 'dismissed' ] | ||
&& | ||
$config[ 'delayJSActive'] | ||
&& | ||
$config[ 'autoptimizeAggregateJSActive' ] | ||
) { | ||
$this->assertContains( 'warn_when_js_aggregation_and_delay_js_active', $boxes ); | ||
} | ||
|
||
if ( | ||
$config[ 'dismissed' ] | ||
&& | ||
( ! $config[ 'delayJSActive' ] || 'off' === $config[ 'autoptimizeAggregateJSActive' ] ) | ||
) { | ||
$this->assertNotContains('warn_when_js_aggregation_and_delay_js_active', $boxes ); | ||
} | ||
} | ||
} |