PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.22
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.22
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.2.22, at minify/class-wp-optimize-minify-commands.php

354 lines 10.2 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 * Get minify file data.
280 *
281 * @param string $filename
282 * @return array
283 */
284 private function get_file_data($filename) {
285 $log = false;
286 $data = array();
287 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
288 $cache_dir = $cache_path['cachedir'];
289 $file = $cache_dir.'/'.$filename;
290
291 if (file_exists($file.'.json')) {
292 $log = json_decode(file_get_contents($file.'.json'));
293 $data['filename'] = $filename;
294 $data['log'] = $log;
295 }
296
297 return $data;
298 }
299
300 /**
301 * Check minify file exists and delete it.
302 *
303 * @param string $file
304 */
305 private function check_and_delete($file) {
306 if (file_exists($file)) {
307 unlink($file);
308 }
309 }
310
311 /**
312 * Get transient file.
313 *
314 * @param string $key
315 * @param string $value
316 * @param string $filename
317 * @return string
318 */
319 private function get_transient_file($key, $value, $filename) {
320 $url = $value->url;
321 $file_url = site_url().$url;
322 $file_ext = $this->get_file_extension($filename);
323
324 $href = WP_Optimize_Minify_Functions::get_hurl($file_url);
325 $transient_key = $file_ext.'-'.hash('adler32', $key.$href).'.'.$file_ext;
326 return $transient_key.'.transient';
327 }
328
329 /**
330 * Get file extension.
331 *
332 * @param string $filename
333 * @return string
334 */
335 private function get_file_extension($filename) {
336 $file_info = pathinfo($filename);
337 return $file_info['extension'];
338 }
339
340 /**
341 * Check for valid filename.
342 * 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.
343 *
344 * @param string $filename
345 * @return bool
346 */
347 private function is_valid_filename($filename) {
348 if (preg_match('/^[a-zA-Z0-9.-]*$/', $filename)) {
349 return true;
350 }
351 return false;
352 }
353 }
354