-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathMinify_Plugin_Admin.php
348 lines (306 loc) · 12 KB
/
Minify_Plugin_Admin.php
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* File: Minify_Plugin_Admin.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Minify_Plugin_Admin
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
*/
class Minify_Plugin_Admin {
/**
* Config
*
* @var Config
*/
private $_config = null;
/**
* Constructor method for the Minify_Plugin_Admin class.
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
}
/**
* Initializes the plugin by registering filters and actions.
*
* @return void
*/
public function run() {
add_filter( 'w3tc_save_options', array( $this, 'w3tc_save_options' ) );
$config_labels = new Minify_ConfigLabels();
add_filter( 'w3tc_config_labels', array( $config_labels, 'config_labels' ) );
if ( $this->_config->get_boolean( 'minify.enabled' ) ) {
add_filter( 'w3tc_usage_statistics_summary_from_history', array( $this, 'w3tc_usage_statistics_summary_from_history' ), 10, 2 );
add_filter( 'w3tc_notes', array( $this, 'w3tc_notes' ) );
add_filter( 'w3tc_errors', array( $this, 'w3tc_errors' ) );
}
add_action( 'w3tc_ajax_minify_help', array( $this, 'w3tc_ajax_minify_help' ) );
add_action( 'w3tc_message_action_minify_help', array( $this, 'w3tc_message_action_minify_help' ) );
if ( defined( 'W3TC_DEBUG' ) && W3TC_DEBUG ) {
add_filter( 'w3tc_admin_bar_menu', array( $this, 'w3tc_admin_bar_menu' ) );
}
}
/**
* Adds the Minify cache menu item to the WordPress admin bar.
*
* @param array $menu_items Existing menu items.
*
* @return array Modified menu items.
*/
public function w3tc_admin_bar_menu( $menu_items ) {
$menu_items['90040.minify'] = array(
'id' => 'w3tc_overlay_minify',
'parent' => 'w3tc_debug_overlays',
'title' => __( 'Minify Cache', 'w3-total-cache' ),
'href' => wp_nonce_url( network_admin_url( 'admin.php?page=w3tc_dashboard&w3tc_message_action=minify_help' ), 'w3tc' ),
);
return $menu_items;
}
/**
* Handles the minify help action triggered in admin messages.
*
* @return void
*/
public function w3tc_message_action_minify_help() {
wp_enqueue_script( 'w3tc-minify-help', plugins_url( 'Minify_GeneralPage_View_ShowHelp.js', W3TC_FILE ), array(), W3TC_VERSION, false );
wp_enqueue_script( 'w3tc-minify-help-force', plugins_url( 'Minify_GeneralPage_View_ShowHelpForce.js', W3TC_FILE ), array(), W3TC_VERSION, false );
}
/**
* Processes and saves plugin options.
*
* @param array $data Array containing new and old configuration data.
*
* @return array Processed configuration data.
*/
public function w3tc_save_options( $data ) {
$new_config = $data['new_config'];
$old_config = $data['old_config'];
if ( $new_config->get_boolean( 'minify.enabled' ) && ! $old_config->get_boolean( 'minify.enabled' ) ) {
$state = Dispatcher::config_state();
$state->set( 'minify.hide_minify_help', true );
$state->save();
}
// Schedule purge if enabled.
if ( $new_config->get_boolean( 'minify.enabled' ) && $new_config->get_boolean( 'minify.wp_cron' ) ) {
$new_wp_cron_time = $new_config->get_integer( 'minify.wp_cron_time' );
$old_wp_cron_time = $old_config ? $old_config->get_integer( 'minify.wp_cron_time' ) : -1;
$new_wp_cron_interval = $new_config->get_string( 'minify.wp_cron_interval' );
$old_wp_cron_interval = $old_config ? $old_config->get_string( 'minify.wp_cron_interval' ) : -1;
$schedule_needs_update = $new_wp_cron_time !== $old_wp_cron_time || $new_wp_cron_interval !== $old_wp_cron_interval;
// Clear the scheduled hook if a change in time or interval is detected.
if ( wp_next_scheduled( 'w3tc_minifycache_purge_wpcron' ) && $schedule_needs_update ) {
wp_clear_scheduled_hook( 'w3tc_minifycache_purge_wpcron' );
}
// Schedule if no existing cron event or settings have changed.
if ( ! wp_next_scheduled( 'w3tc_minifycache_purge_wpcron' ) || $schedule_needs_update ) {
$scheduled_timestamp_server = Util_Environment::get_cron_schedule_time( $new_wp_cron_time );
wp_schedule_event( $scheduled_timestamp_server, $new_wp_cron_interval, 'w3tc_minifycache_purge_wpcron' );
}
} elseif ( wp_next_scheduled( 'w3tc_minifycache_purge_wpcron' ) ) {
wp_clear_scheduled_hook( 'w3tc_minifycache_purge_wpcron' );
}
return $data;
}
/**
* Prints admin scripts for the general settings page.
*
* @return void
*/
public static function admin_print_scripts_w3tc_general() {
$state = Dispatcher::config_state();
if ( ! $state->get_boolean( 'minify.hide_minify_help' ) ) {
wp_enqueue_script( 'w3tc-minify-help', plugins_url( 'Minify_GeneralPage_View_ShowHelp.js', W3TC_FILE ), array(), W3TC_VERSION, false );
}
}
/**
* Cleans up expired and unnecessary cache files.
*
* @return void
*/
public function cleanup() {
$w3_cache_file_cleaner_generic = new Cache_File_Cleaner_Generic(
array(
'exclude' => array(
'*.files',
'.htaccess',
'index.html',
),
'cache_dir' => Util_Environment::cache_blog_dir( 'minify' ),
'expire' => $this->_config->get_integer( 'minify.file.gc' ),
'clean_timelimit' => $this->_config->get_integer( 'timelimit.cache_gc' ),
)
);
$w3_cache_file_cleaner_generic->clean();
}
/**
* Collects and returns errors related to Minify configuration or usage.
*
* @param array $errors Existing errors.
*
* @return array Updated list of errors.
*/
public function w3tc_errors( $errors ) {
$c = Dispatcher::config();
$state = Dispatcher::config_state_master();
// Minify error occured.
if ( $state->get_boolean( 'minify.show_note_minify_error' ) ) {
$errors['minify_error_creating'] = sprintf(
// Translators: 1 last minify error, 2 dismiss button.
__(
'Recently an error occurred while creating the CSS / JS minify cache: %1$s. %2$s',
'w3-total-cache'
),
$state->get_string( 'minify.error.last' ),
Util_Ui::button_hide_note2(
array(
'w3tc_default_config_state_master' => 'y',
'key' => 'minify.show_note_minify_error',
'value' => 'false',
)
)
);
}
if ( 'memcached' === $c->get_string( 'minify.engine' ) ) {
$memcached_servers = $c->get_array( 'minify.memcached.servers' );
$memcached_binary_protocol = $c->get_boolean( 'minify.memcached.binary_protocol' );
$memcached_username = $c->get_string( 'minify.memcached.username' );
$memcached_password = $c->get_string( 'minify.memcached.password' );
if (
! Util_Installed::is_memcache_available(
$memcached_servers,
$memcached_binary_protocol,
$memcached_username,
$memcached_password
)
) {
if ( ! isset( $errors['memcache_not_responding.details'] ) ) {
$errors['memcache_not_responding.details'] = array();
}
$errors['memcache_not_responding.details'][] = sprintf(
// Translators: 1 memcache servers.
__(
'Minify: %1$s.',
'w3-total-cache'
),
implode( ', ', $memcached_servers )
);
}
}
return $errors;
}
/**
* Adds notes or messages related to Minify cache actions.
*
* @param array $notes Existing notes.
*
* @return array Updated notes.
*/
public function w3tc_notes( $notes ) {
$state_note = Dispatcher::config_state();
// Show notification when minify needs to be emptied.
if (
$state_note->get_boolean( 'minify.show_note.need_flush' ) &&
! is_network_admin() && // flushing doesnt work in network admin.
! $this->_config->is_preview()
) {
$notes['minify_flush_needed'] = sprintf(
// Translators: 1 empty cache button.
__(
'The setting change(s) made either invalidate the cached data or modify the behavior of the site. %1$s now to provide a consistent user experience.',
'w3-total-cache'
),
Util_Ui::button_link(
__( 'Empty the minify cache', 'w3-total-cache' ),
Util_Ui::url( array( 'w3tc_flush_minify' => 'y' ) )
)
);
}
return $notes;
}
/**
* Handles the AJAX request for displaying Minify help.
*
* @return void
*/
public function w3tc_ajax_minify_help() {
include W3TC_DIR . '/Minify_HelpPopup_View.php';
}
/**
* Summarizes Minify usage statistics from history.
*
* @param array $summary Existing summary data.
* @param array $history Historical usage data.
*
* @return array Updated summary data.
*/
public function w3tc_usage_statistics_summary_from_history( $summary, $history ) {
// memcached servers.
$c = Dispatcher::config();
if ( 'memcached' === $c->get_string( 'minify.engine' ) ) {
$summary['memcached_servers']['minify'] = array(
'servers' => $c->get_array( 'minify.memcached.servers' ),
'username' => $c->get_string( 'minify.memcached.username' ),
'password' => $c->get_string( 'minify.memcached.password' ),
'binary_protocol' => $c->get_boolean( 'minify.memcached.binary_protocol' ),
'name' => __( 'Minification', 'w3-total-cache' ),
);
} elseif ( 'redis' === $c->get_string( 'minify.engine' ) ) {
$summary['redis_servers']['minify'] = array(
'servers' => $c->get_array( 'minify.redis.servers' ),
'username' => $c->get_boolean( 'minify.redis.username' ),
'dbid' => $c->get_integer( 'minify.redis.dbid' ),
'password' => $c->get_string( 'minify.redis.password' ),
'name' => __( 'Minification', 'w3-total-cache' ),
);
}
$e = $this->_config->get_string( 'minify.engine' );
$a = array(
'size_visible' => ( 'file' === $e ),
'requests_visible' => ! ( 'file' === $e && $this->_config->get_boolean( 'minify.rewrite' ) ),
);
if ( ! isset( $summary['period']['timestamp_end'] ) ) {
// summary requested, enough.
$summary['minify'] = $a;
return $summary;
}
// get requests rate stats.
if ( $a['requests_visible'] ) {
// counters.
$requests_total = Util_UsageStatistics::sum( $history, 'minify_requests_total' );
$original_length_css = Util_UsageStatistics::sum( $history, 'minify_original_length_css' );
$output_length_css = Util_UsageStatistics::sum( $history, 'minify_output_length_css' );
$original_length_js = Util_UsageStatistics::sum( $history, 'minify_original_length_js' );
$output_length_js = Util_UsageStatistics::sum( $history, 'minify_output_length_js' );
$a['requests_total'] = Util_UsageStatistics::integer( $requests_total );
$a['requests_per_second'] = Util_UsageStatistics::value_per_period_seconds( $requests_total, $summary );
$a['compression_css'] = Util_UsageStatistics::percent( $original_length_css - $output_length_css, $original_length_css );
$a['compression_js'] = Util_UsageStatistics::percent( $original_length_js - $output_length_js, $original_length_js );
}
if ( $a['size_visible'] ) {
list( $v, $should_count ) = Util_UsageStatistics::get_or_init_size_transient( 'w3tc_ustats_minify_size', $summary );
if ( $should_count ) {
$h = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$stats = $h->get_stats_size( $summary['timeout_time'] );
// build stats to show.
$v['size_used'] = Util_UsageStatistics::bytes_to_size( $stats['css']['output_length'] + $stats['js']['output_length'] );
$v['size_items'] = Util_UsageStatistics::integer( $stats['css']['items'] + $stats['js']['items'] );
$v['size_compression_css'] = Util_UsageStatistics::percent( $stats['css']['original_length'] - $stats['css']['output_length'], $stats['css']['original_length'] );
$v['size_compression_js'] = Util_UsageStatistics::percent( $stats['js']['original_length'] - $stats['js']['output_length'], $stats['js']['original_length'] );
set_transient( 'w3tc_ustats_pagecache_size', $v, 120 );
}
if ( isset( $v['size_used'] ) ) {
$a['size_used'] = $v['size_used'];
$a['size_items'] = empty( $v['size_items'] ) ? null : $v['size_items'];
$a['size_compression_css'] = empty( $v['size_compression_css'] ) ? null : $v['size_compression_css'];
$a['size_compression_js'] = empty( $v['size_compression_js'] ) ? null : $v['size_compression_js'];
}
}
$summary['minify'] = $a;
return $summary;
}
}