PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.3.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.3.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / minify / class-wp-optimize-minify-commands.php

class-wp-optimize-minify-commands.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.3.0, at minify/class-wp-optimize-minify-commands.php

407 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 if (!class_exists('WP_Optimize_Minify_Config')) require_once(dirname(__FILE__) . '/class-wp-optimize-minify-config.php');
5 if (!class_exists('WP_Optimize_Minify_Preloader')) require_once(dirname(__FILE__) . '/class-wpo-minify-preloader.php');
6
7 /**
8 * All cache commands that are intended to be available for calling from any sort of control interface (e.g. wp-admin, UpdraftCentral) go in here. All public methods should either return the data to be returned, or a WP_Error with associated error code, message and error data.
9 */
10 class WP_Optimize_Minify_Commands {
11
12 /**
13 * List all cache files
14 *
15 * @param array $data - The $_POST data
16 * @return array
17 */
18 public function get_minify_cached_files($data = array()) {
19 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
20 $stamp = isset($data['stamp']) ? $data['stamp'] : 0;
21 $files = WP_Optimize_Minify_Cache_Functions::get_cached_files($stamp, false);
22 $files['js'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['js']);
23 $files['css'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['css']);
24 return $files;
25 }
26
27 /**
28 * Removes the entire cache dir.
29 * Use with caution, as cached html may still reference those files.
30 *
31 * @return array
32 */
33 public function purge_all_minify_cache() {
34 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
35 WP_Optimize_Minify_Cache_Functions::purge();
36 WP_Optimize_Minify_Cache_Functions::cache_increment();
37 $others = WP_Optimize_Minify_Cache_Functions::purge_others();
38 $files = $this->get_minify_cached_files();
39
40 $message = array_merge(array(esc_html__('The minification cache was deleted.', 'wp-optimize')), $others);
41 $message = WP_Optimize_Minify_Functions::apply_strip_tags_for_messages_array($message, '');
42
43 return array(
44 'success' => true,
45 'message' => implode("\n", $message),
46 'files' => $files
47 );
48 }
49
50 /**
51 * Forces a new Cache to be built
52 *
53 * @return array
54 */
55 public function minify_increment_cache() {
56 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
57 WP_Optimize_Minify_Cache_Functions::cache_increment();
58 $files = $this->get_minify_cached_files();
59 return array(
60 'success' => true,
61 'files' => $files
62 );
63 }
64
65 /**
66 * Purge the cache
67 *
68 * @return array
69 */
70 public function purge_minify_cache() {
71 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'));
72
73 if (!WP_Optimize()->get_minify()->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) {
74 return array(
75 'error' => __('You do not have permission to purge the cache', 'wp-optimize')
76 );
77 }
78
79 // deletes temp files and old caches incase CRON isn't working
80 WP_Optimize_Minify_Cache_Functions::cache_increment();
81 if (wp_optimize_minify_config()->always_purge_everything()) {
82 WP_Optimize_Minify_Cache_Functions::purge();
83 $state = array();
84 $old = array();
85 } else {
86 $state = WP_Optimize_Minify_Cache_Functions::purge_temp_files();
87 $old = WP_Optimize_Minify_Cache_Functions::purge_old();
88 }
89 $others = WP_Optimize_Minify_Cache_Functions::purge_others();
90 $files = $this->get_minify_cached_files();
91
92 $notice = array_merge(array(esc_html__('All caches from WP-Optimize Minify have been purged.', 'wp-optimize')), $others);
93
94 $notice = WP_Optimize_Minify_Functions::apply_strip_tags_for_messages_array($notice, '');
95
96 $notice = json_encode($notice); // encode
97
98 return array(
99 'result' => 'caches cleared',
100 'others' => implode("\n", $others),
101 'state' => $state,
102 'message' => $notice,
103 'old' => $old,
104 'files' => $files
105 );
106 }
107
108 /**
109 * Delete minify cache file
110 *
111 * @param string $filename
112 * @return array
113 */
114 public function delete_minify_cache_file($filename) {
115 if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize'), 'result' => '', 'files' => '');
116 if (!WP_Optimize()->get_minify()->can_purge_cache()) return array('error' => __('You do not have permission to purge the cache', 'wp-optimize'), 'result' => '', 'files' => '');
117
118 $response = array(
119 'result' => __('Cache file was not found.', 'wp-optimize'),
120 'files' => '',
121 'error' => '',
122 );
123
124 if (empty($filename)) return $response;
125
126 $is_valid_filename = $this->is_valid_filename($filename);
127
128 if (!$is_valid_filename) {
129 return $response;
130 } else {
131 $filename = pathinfo($filename, PATHINFO_BASENAME);
132
133 $data = $this->get_file_data($filename);
134
135 if (empty($data)) return $response;
136 $this->fetch_and_remove_temp_minify_cache_files($data);
137
138 $files = $this->get_minify_cached_files();
139 return array(
140 'result' => __('Cache deleted.', 'wp-optimize'),
141 'files' => $files,
142 'error' => '',
143 );
144 }
145 }
146
147 /**
148 * Fetch and remove the temp and minify js/css cache files.
149 *
150 * @param Array $data file data
151 */
152 public function fetch_and_remove_temp_minify_cache_files($data) {
153 $filename = $data['filename'];
154 $log = $data['log'];
155
156 // get cache directories and urls
157 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
158 $tmp_dir = $cache_path['tmpdir'];
159 $cache_dir = $cache_path['cachedir'];
160
161 $ext_to_delete = array('', '.json', '.gz');
162 foreach ($ext_to_delete as $ext) {
163 $this->check_and_delete($cache_dir . '/' . $filename . $ext);
164 }
165
166 if (empty($log->files)) return;
167 foreach ($log->files as $key => $value) {
168 $transient_file = $this->get_transient_file($key, $value, $filename);
169 $this->check_and_delete($tmp_dir.'/'.$transient_file);
170 }
171 }
172
173 /**
174 * Save options to the config
175 *
176 * @param array $data
177 * @return array
178 */
179 public function save_minify_settings($data) {
180 $new_data = array();
181 foreach ($data as $key => $value) {
182 if ('true' === $value) {
183 $new_data[$key] = true;
184 } elseif ('false' === $value) {
185 $new_data[$key] = false;
186 } else {
187 $new_data[$key] = $value;
188 }
189 }
190
191 if (isset($data['minify_advanced_tab'])) {
192 // Make sure that empty settings are still saved
193 if (!isset($new_data['ignore_list'])) $new_data['ignore_list'] = array();
194 if (!isset($new_data['blacklist'])) $new_data['blacklist'] = array();
195 }
196
197 /**
198 * Filters the data before saving it
199 *
200 * @param array $new_data - The original data
201 * @return array The data, altered or not
202 */
203 $new_data = apply_filters('wpo_save_minify_settings', $new_data);
204
205 if (!class_exists('WP_Optimize_Minify_Config')) return array(
206 'success' => false,
207 'message' => "WP_Optimize_Minify_Config class doesn't exist",
208 );
209 $working = wp_optimize_minify_config()->update($new_data);
210 if (!$working) {
211 return array(
212 'success' => false,
213 'error' => 'failed to save minify settings'
214 );
215 }
216 $purged = $this->purge_minify_cache();
217 return array(
218 'success' => true,
219 'files' => isset($purged['files']) ? $purged['files'] : array(),
220 );
221 }
222
223 /**
224 * Hide the information notice for the current user
225 *
226 * @return array
227 */
228 public function hide_minify_notice() {
229 return array(
230 'success' => update_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true)
231 );
232 }
233
234 /**
235 * Get the current status
236 *
237 * @return array
238 */
239 public function get_status() {
240 $config = wp_optimize_minify_config()->get();
241 return array(
242 'enabled' => $config['enabled'],
243 'js' => $config['enable_js'],
244 'css' => $config['enable_css'],
245 'html' => $config['html_minification'],
246 'stats' => $this->get_minify_cached_files()
247 );
248 }
249
250 /**
251 * Run minify preload action.
252 *
253 * @return void|array - Doesn't return anything if run() is successful (Run() prints a JSON object and closed browser connection) or an array if failed.
254 */
255 public function run_minify_preload() {
256 return WP_Optimize_Minify_Preloader::instance()->run('manual');
257 }
258
259 /**
260 * Cancel minify preload action.
261 *
262 * @return array
263 */
264 public function cancel_minify_preload() {
265 WP_Optimize_Minify_Preloader::instance()->cancel_preload();
266 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
267 }
268
269 /**
270 * Get status of minify preload.
271 *
272 * @return array
273 */
274 public function get_minify_preload_status() {
275 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
276 }
277
278 /**
279 * Returns the combined json of all available meta.json files
280 *
281 * @return array
282 */
283 public function get_minify_meta_files() {
284 $enabled = wp_optimize_minify_config()->get('enabled');
285 if (!$enabled) return array(
286 'success' => false,
287 'error' => __('Minify not enabled', 'wp-optimize'),
288 );
289 $combined_metas = array(
290 'meta_logs' => array()
291 );
292
293 // loop through wpo-minify cache directory and get the meta.json files, combine into a single json file
294 if (is_dir(WPO_CACHE_MIN_FILES_DIR) && is_writable(dirname(WPO_CACHE_MIN_FILES_DIR))) {
295 if ($handle = opendir(WPO_CACHE_MIN_FILES_DIR)) {
296 while (false !== ($d = readdir($handle))) {
297 if (0 === strcmp($d, '.') || 0 === strcmp($d, '..') || !is_numeric($d)) {
298 continue;
299 }
300 $cache_min_folder = WPO_CACHE_MIN_FILES_DIR.'/'.$d;
301 if ($cache_min_folder_handle = opendir($cache_min_folder)) {
302 while (false !== ($maybe_file = readdir($cache_min_folder_handle))) {
303 if (0 === strcmp($maybe_file, '.') || 0 === strcmp($maybe_file, '..')) {
304 continue;
305 }
306 $maybe_file_path = $cache_min_folder . '/' . $maybe_file;
307 if (is_file($maybe_file_path) && 'meta.json' === basename($maybe_file_path)) {
308 $combined_metas['meta_logs'][$d] = json_decode(file_get_contents($maybe_file_path));
309 }
310 }
311 closedir($cache_min_folder_handle);
312 }
313 }
314 closedir($handle);
315 }
316 }
317
318 if (0 === count($combined_metas['meta_logs'])) {
319 return array(
320 'success' => false,
321 'error' => __('No file was found', 'wp-optimize'),
322 );
323 } else {
324 return array(
325 'success' => true,
326 'combined_metas' => $combined_metas
327 );
328 }
329 }
330
331 /**
332 * Get minify file data.
333 *
334 * @param string $filename
335 * @return array
336 */
337 private function get_file_data($filename) {
338 $log = false;
339 $data = array();
340 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
341 $cache_dir = $cache_path['cachedir'];
342 $file = $cache_dir.'/'.$filename;
343
344 if (file_exists($file.'.json')) {
345 $log = json_decode(file_get_contents($file.'.json'));
346 $data['filename'] = $filename;
347 $data['log'] = $log;
348 }
349
350 return $data;
351 }
352
353 /**
354 * Check minify file exists and delete it.
355 *
356 * @param string $file
357 */
358 private function check_and_delete($file) {
359 if (file_exists($file)) {
360 unlink($file);
361 }
362 }
363
364 /**
365 * Get transient file.
366 *
367 * @param string $key
368 * @param string $value
369 * @param string $filename
370 * @return string
371 */
372 private function get_transient_file($key, $value, $filename) {
373 $url = $value->url;
374 $file_url = site_url().$url;
375 $file_ext = $this->get_file_extension($filename);
376
377 $href = WP_Optimize_Minify_Functions::get_hurl($file_url);
378 $transient_key = $file_ext.'-'.hash('adler32', $key.$href).'.'.$file_ext;
379 return $transient_key.'.transient';
380 }
381
382 /**
383 * Get file extension.
384 *
385 * @param string $filename
386 * @return string
387 */
388 private function get_file_extension($filename) {
389 $file_info = pathinfo($filename);
390 return $file_info['extension'];
391 }
392
393 /**
394 * Check for valid filename.
395 * For file name pattern see here, the function 'process_header_css' in the file 'class-wp-optimize-minify-front-end.php' where the file name is constructed. You can find' WP_Optimize_Minify_Print::write_combined_asset' and go to the passed $file variable where it's defined or constructed.
396 *
397 * @param string $filename
398 * @return bool
399 */
400 private function is_valid_filename($filename) {
401 if (preg_match('/^[a-zA-Z0-9.-]*$/', $filename)) {
402 return true;
403 }
404 return false;
405 }
406 }
407