Autoptimize.php
4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?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;
/**
* 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.
*
* @since 3.10.4
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_notices' => [
[ 'warn_when_js_aggregation_and_delay_js_active' ],
[ 'warn_when_aggregate_inline_css_and_cpcss_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 ( ! $this->can_notify() ) {
return;
}
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
if ( ! (
'on' === get_option( 'autoptimize_js' )
&&
'on' === get_option( 'autoptimize_js_aggregate' )
) || 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 = sprintf(
/* Translators: %1$s is an opening <strong> tag; %2$s is a closing </strong> tag */
__(
'%1$sWP Rocket: %2$sWe have detected that Autoptimize\'s JavaScript Aggregation feature is enabled. WP Rocket\'s Delay JavaScript Execution will not be applied to the file it creates. We suggest disabling %1$sJavaScript Aggregation%2$s to take full advantage of Delay JavaScript Execution.',
'rocket'
),
'<strong>',
'</strong>'
);
rocket_notice_html(
[
'status' => 'info',
'message' => $message,
'dismissible' => '',
'dismiss_button' => __FUNCTION__,
]
);
}
/**
* Add an admin warning notice when CPCSS and Aggregate Inline CSS are both activated.
*
* @since 3.10.4
*/
public function warn_when_aggregate_inline_css_and_cpcss_active() {
if ( ! $this->can_notify() ) {
return;
}
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
if ( ! (
'on' === get_option( 'autoptimize_css' )
&&
'on' === get_option( 'autoptimize_css_aggregate' )
&&
'on' === get_option( 'autoptimize_css_include_inline' )
)
||
false === (bool) $this->options->get( 'async_css' )
) {
if ( ! is_array( $boxes ) ) {
return;
}
$this->remove_warning_dismissal( $boxes, __FUNCTION__ );
return;
}
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
return;
}
$message = sprintf(
/* Translators: %1$s is an opening <strong> tag; %2$s is a closing </strong> tag */
__(
'%1$sWP Rocket: %2$sWe have detected that Autoptimize\'s Aggregate Inline CSS feature is enabled. WP Rocket\'s Load CSS Asynchronously will not work correctly. We suggest disabling %1$sAggregate Inline CSS%2$s to take full advantage of Load CSS Asynchronously Execution.',
'rocket'
),
'<strong>',
'</strong>'
);
rocket_notice_html(
[
'status' => 'info',
'message' => $message,
'dismissible' => '',
'dismiss_button' => __FUNCTION__,
]
);
}
/**
* Whether this compatibility can use notifications.
*
* @return bool
*/
private function can_notify() {
if ( 'settings_page_wprocket' !== get_current_screen()->id ) {
return false;
}
return rocket_get_constant( 'AUTOPTIMIZE_PLUGIN_VERSION', false )
&&
current_user_can( 'rocket_manage_options' );
}
/**
* 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 );
}
}