Hummingbird.php
6.84 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace WP_Rocket\ThirdParty\Plugins\Optimization;
use WP_Hummingbird_Settings;
use WP_Hummingbird_Utils;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Hummingbird compatibility class
*/
class Hummingbird 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 wants to listen to.
*
* @since 3.3.3
* @author Remy Perona
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_notices' => 'warning_notice',
];
}
/**
* Display a notice if conflicting options are active
*
* @since 3.3.3
* @author Remy Perona
*
* @return void
*/
public function warning_notice() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( ! is_plugin_active( 'hummingbird-performance/wp-hummingbird.php' ) && ! is_plugin_active( 'wp-hummingbird/wp-hummingbird.php' ) ) {
return;
}
$this->check_cache();
$this->check_assets();
$this->check_browser_caching();
$this->check_gzip();
$this->check_emoji();
if ( 0 === count( $this->errors ) ) {
return;
}
// Translators: %s = Plugin name.
$message = '<p>' . sprintf( _nx( 'Please deactivate the following %s option which conflicts with WP Rocket features:', 'Please deactivate the following %s options which conflict with WP Rocket features:', count( $this->errors ), 'Hummingbird notice', 'rocket' ), 'Hummingbird' ) . '</p>';
$message .= '<ul>';
foreach ( $this->errors as $error ) {
$message .= '<li>' . $error . '</li>';
}
$message .= '</ul>';
rocket_notice_html(
[
'status' => 'error',
'message' => $message,
]
);
}
/**
* Checks if the Hummingbird Utils class exists
*
* @since 3.3.3
* @author Remy Perona
*
* @return boolean
*/
private function is_utils_available() {
if ( ! class_exists( 'WP_Hummingbird_Utils' ) ) {
return false;
}
if ( ! method_exists( 'WP_Hummingbird_Utils', 'get_module' ) ) {
return false;
}
return true;
}
/**
* Checks if the Hummingbird settings class exists
*
* @since 3.3.3
* @author Remy Perona
*
* @return boolean
*/
private function is_settings_available() {
if ( ! class_exists( 'WP_Hummingbird_Settings' ) ) {
return false;
}
if ( ! method_exists( 'WP_Hummingbird_Settings', 'get_setting' ) ) {
return false;
}
return true;
}
/**
* Checks if Hummingbird and WP Rocket disable emoji options are active at the same time
*
* @since 3.3.3
* @author Remy Perona
*
* @return bool
*/
private function check_emoji() {
if ( ! $this->is_settings_available() ) {
return false;
}
if ( $this->options->get( 'emoji' ) && WP_Hummingbird_Settings::get_setting( 'emoji', 'advanced' ) ) {
// Translators: %1$s = Plugin name, %2$s = <em>, %3$s = </em>.
$this->errors[] = sprintf( _x( '%1$s %2$sdisable emoji%3$s conflicts with WP Rockets %2$sdisable emoji%3$s', 'Hummingbird notice', 'rocket' ), 'Hummingbird', '<em>', '</em>' );
return true;
}
return false;
}
/**
* Checks if Hummingbird Gzip rules are in the htaccess file.
*
* @since 3.3.3
* @author Remy Perona
*
* @return bool
*/
private function check_gzip() {
if ( ! $this->is_utils_available() ) {
return false;
}
$gzip = WP_Hummingbird_Utils::get_module( 'gzip' );
if ( ! $gzip instanceof \WP_Hummingbird_Module_GZip ) {
return false;
}
if ( ! method_exists( $gzip, 'is_htaccess_written' ) ) {
return false;
}
if ( ! method_exists( $gzip, 'get_server_type' ) ) {
return false;
}
if ( $gzip::is_htaccess_written( 'gzip' ) && 'apache' === $gzip::get_server_type() ) {
// Translators: %1$s = Plugin name, %2$s = <em>, %3$s = </em>.
$this->errors[] = sprintf( _x( '%1$s %2$sGZIP compression%3$s conflicts with WP Rocket %2$sGZIP compression%3$s', 'Hummingbird notice', 'rocket' ), 'Hummingbird', '<em>', '</em>' );
return true;
}
return false;
}
/**
* Checks if Hummingbird browser caching rules are in the htaccess file
*
* @since 3.3.3
* @author Remy Perona
*
* @return bool
*/
private function check_browser_caching() {
if ( ! $this->is_utils_available() ) {
return false;
}
$caching = WP_Hummingbird_Utils::get_module( 'caching' );
if ( ! $caching instanceof \WP_Hummingbird_Module_Caching ) {
return false;
}
if ( ! method_exists( $caching, 'is_htaccess_written' ) ) {
return false;
}
if ( ! method_exists( $caching, 'get_server_type' ) ) {
return false;
}
if ( $caching::is_htaccess_written( 'caching' ) && 'apache' === $caching::get_server_type() ) {
// Translators: %1$s = Plugin name, %2$s = <em>, %3$s = </em>.
$this->errors[] = sprintf( _x( '%1$s %2$sbrowser caching%3$s conflicts with WP Rocket %2$sbrowser caching%3$s', 'Hummingbird notice', 'rocket' ), 'Hummingbird', '<em>', '</em>' );
return true;
}
return false;
}
/**
* Checks if Hummingbird Cache is active
*
* @since 3.3.3
* @author Remy Perona
*
* @return bool
*/
private function check_cache() {
if ( ! $this->is_utils_available() ) {
return false;
}
$cache = WP_Hummingbird_Utils::get_module( 'page_cache' );
if ( ! $cache instanceof \WP_Hummingbird_Module_Page_Cache ) {
return false;
}
if ( ! method_exists( $cache, 'is_active' ) ) {
return false;
}
if ( $cache->is_active() ) {
// Translators: %1$s = Plugin name, %2$s = <em>, %3$s = </em>.
$this->errors[] = sprintf( _x( '%1$s %2$spage caching%3$s conflicts with WP Rocket %2$spage caching%3$s', 'Hummingbird notice', 'rocket' ), 'Hummingbird', '<em>', '</em>' );
return true;
}
return false;
}
/**
* Checks if Hummingbird Assets optimization is active
*
* Checks against WP Rocket Minify CSS, Minify JS and Defer JS options.
*
* @since 3.3.3
* @author Remy Perona
*
* @return bool
*/
private function check_assets() {
if ( ! $this->is_utils_available() ) {
return false;
}
$minify = WP_Hummingbird_Utils::get_module( 'minify' );
if ( ! $minify instanceof \WP_Hummingbird_Module_Minify ) {
return false;
}
if ( ! method_exists( $minify, 'is_active' ) ) {
return false;
}
if ( $minify->is_active() && ( $this->options->get( 'minify_css' ) || $this->options->get( 'minify_js' ) || $this->options->get( 'defer_all_js' ) ) ) {
// Translators: %1$s = Plugin name, %2$s = <em>, %3$s = </em>.
$this->errors[] = sprintf( _x( '%1$s %2$sasset optimization%3$s conflicts with WP Rocket %2$sfile optimization%3$s', 'Hummingbird notice', 'rocket' ), 'Hummingbird', '<em>', '</em>' );
return true;
}
return false;
}
}