PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.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
← All changes | minify/class-wp-optimize-minify-commands.php +126 -21 3.2.22 → 4.7.0 View file →
@@ -15,10 +15,9 @@
15 15 * @param array $data - The $_POST data
16 16 * @return array
17 17 */
18 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;
19 + $stamp = $data['stamp'] ?? 0;
21 20 $files = WP_Optimize_Minify_Cache_Functions::get_cached_files($stamp, false);
22 21 $files['js'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['js']);
23 22 $files['css'] = array_map(array('WP_Optimize_Minify_Cache_Functions', 'format_file_logs'), $files['css']);
24 23 return $files;
@@ -30,9 +29,8 @@
30 29 *
31 30 * @return array
32 31 */
33 32 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 33 WP_Optimize_Minify_Cache_Functions::purge();
36 34 WP_Optimize_Minify_Cache_Functions::cache_increment();
37 35 $others = WP_Optimize_Minify_Cache_Functions::purge_others();
38 36 $files = $this->get_minify_cached_files();
@@ -52,9 +50,8 @@
52 50 *
53 51 * @return array
54 52 */
55 53 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 54 WP_Optimize_Minify_Cache_Functions::cache_increment();
58 55 $files = $this->get_minify_cached_files();
59 56 return array(
60 57 'success' => true,
@@ -67,10 +64,8 @@
67 64 *
68 65 * @return array
69 66 */
70 67 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 68 if (!WP_Optimize()->get_minify()->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) {
74 69 return array(
75 70 'error' => __('You do not have permission to purge the cache', 'wp-optimize')
76 71 );
@@ -75,9 +70,9 @@
75 70 'error' => __('You do not have permission to purge the cache', 'wp-optimize')
76 71 );
77 72 }
78 73
79 - // deletes temp files and old caches incase CRON isn't working
74 + // deletes temp files and old caches in case CRON isn't working
80 75 WP_Optimize_Minify_Cache_Functions::cache_increment();
81 76 if (wp_optimize_minify_config()->always_purge_everything()) {
82 77 WP_Optimize_Minify_Cache_Functions::purge();
83 78 $state = array();
@@ -92,9 +87,9 @@
92 87 $notice = array_merge(array(esc_html__('All caches from WP-Optimize Minify have been purged.', 'wp-optimize')), $others);
93 88
94 89 $notice = WP_Optimize_Minify_Functions::apply_strip_tags_for_messages_array($notice, '');
95 90
96 - $notice = json_encode($notice); // encode
91 + $notice = wp_json_encode($notice); // encode
97 92
98 93 return array(
99 94 'result' => 'caches cleared',
100 95 'others' => implode("\n", $others),
@@ -111,9 +106,8 @@
111 106 * @param string $filename
112 107 * @return array
113 108 */
114 109 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 110 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 111
118 112 $response = array(
119 113 'result' => __('Cache file was not found.', 'wp-optimize'),
@@ -146,9 +140,9 @@
146 140
147 141 /**
148 142 * Fetch and remove the temp and minify js/css cache files.
149 143 *
150 - * @param Array $data file data
144 + * @param array $data file data
151 145 */
152 146 public function fetch_and_remove_temp_minify_cache_files($data) {
153 147 $filename = $data['filename'];
154 148 $log = $data['log'];
@@ -183,9 +177,9 @@
183 177 $new_data[$key] = true;
184 178 } elseif ('false' === $value) {
185 179 $new_data[$key] = false;
186 180 } else {
187 - $new_data[$key] = $value;
181 + $new_data[$key] = is_string($value) ? trim($value) : $value;
188 182 }
189 183 }
190 184
191 185 if (isset($data['minify_advanced_tab'])) {
@@ -215,20 +209,78 @@
215 209 }
216 210 $purged = $this->purge_minify_cache();
217 211 return array(
218 212 'success' => true,
219 - 'files' => isset($purged['files']) ? $purged['files'] : array(),
213 + 'files' => $purged['files'] ?? array(),
220 214 );
221 215 }
222 216
223 217 /**
224 - * Hide the information notice for the current user
218 + * Toggle (hide or show) the information notice for the current user.
225 219 *
226 - * @return array
220 + * @param array $params {type:(js|css|minify), merging_enabled:(true|false), force_hide:(true|false)}
221 + * @return array {success:(true|false), hide:(true|false), message:(string)}
227 222 */
228 - public function hide_minify_notice() {
223 + public function toggle_minify_notice($params) {
224 + if (empty($params['type'])) {
225 + return array(
226 + 'success' => false,
227 + 'message' => __('The request did not include a notice type.', 'wp-optimize')
228 + );
229 + }
230 +
231 + $notice_types = array(
232 + 'js' => 'wpo_hide_js_merging_notice',
233 + 'css' => 'wpo_hide_css_merging_notice',
234 + 'minify' => 'wpo-hide-minify-information-notice'
235 + );
236 +
237 + if (!isset($notice_types[$params['type']])) {
238 + return array(
239 + 'success' => false,
240 + 'message' => __('The specified notice type is invalid.', 'wp-optimize')
241 + );
242 + }
243 +
244 + $current_user_id = get_current_user_id();
245 + if (!$current_user_id) {
246 + return array(
247 + 'success' => false,
248 + 'message' => __('Invalid user context.', 'wp-optimize')
249 + );
250 + }
251 +
252 + $meta_key = $notice_types[$params['type']];
253 + $should_hide_notice = isset($params['force_hide']) ? filter_var($params['force_hide'], FILTER_VALIDATE_BOOLEAN) : true;
254 + if (!$should_hide_notice) {
255 + $merging_enabled = isset($params['merging_enabled']) ? filter_var($params['merging_enabled'], FILTER_VALIDATE_BOOLEAN) : true;
256 + $is_http1 = WP_Optimize_Utils::is_request_protocol_http1();
257 + $should_hide_notice = (($is_http1 && $merging_enabled) || (!$is_http1 && !$merging_enabled));
258 + }
259 +
260 + $updated = update_user_meta($current_user_id, $meta_key, $should_hide_notice);
261 + $success = true;
262 +
263 + if (false === $updated) {
264 + /**
265 + * Function update_user_meta() returns false when:
266 + * the value is unchanged or the update failed.
267 + *
268 + * We verify by reading the stored value. If it matches the intended value,
269 + * treat it as a success; otherwise, treat it as a failure.
270 + */
271 + $existing = get_user_meta($current_user_id, $meta_key, true);
272 +
273 + /**
274 + * Loose comparison (==) is intentional because user meta-values are
275 + * stored as strings ('1', '0'), not booleans.
276 + */
277 + $success = ($existing == $should_hide_notice);
278 + }
279 +
229 280 return array(
230 - 'success' => update_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true)
281 + 'success' => $success,
282 + 'hide' => $should_hide_notice,
231 283 );
232 284 }
233 285
234 286 /**
@@ -249,9 +301,9 @@
249 301
250 302 /**
251 303 * Run minify preload action.
252 304 *
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.
305 + * @return array|true[]|null - Doesn't return anything if run() is successful (Run() prints a JSON object and closed browser connection) or an array if failed.
254 306 */
255 307 public function run_minify_preload() {
256 308 return WP_Optimize_Minify_Preloader::instance()->run('manual');
257 309 }
@@ -258,9 +310,9 @@
258 310
259 311 /**
260 312 * Cancel minify preload action.
261 313 *
262 - * @return array
314 + * @return mixed
263 315 */
264 316 public function cancel_minify_preload() {
265 317 WP_Optimize_Minify_Preloader::instance()->cancel_preload();
266 318 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
@@ -268,9 +320,9 @@
268 320
269 321 /**
270 322 * Get status of minify preload.
271 323 *
272 - * @return array
324 + * @return mixed
273 325 */
274 326 public function get_minify_preload_status() {
275 327 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
276 328 }
@@ -275,8 +327,61 @@
275 327 return WP_Optimize_Minify_Preloader::instance()->get_status_info();
276 328 }
277 329
278 330 /**
331 + * Returns the combined json of all available meta.json files
332 + *
333 + * @return array
334 + */
335 + public function get_minify_meta_files() {
336 + $enabled = wp_optimize_minify_config()->get('enabled');
337 + if (!$enabled) return array(
338 + 'success' => false,
339 + 'error' => __('Minify not enabled', 'wp-optimize'),
340 + );
341 + $combined_metas = array(
342 + 'meta_logs' => array()
343 + );
344 +
345 + // loop through wpo-minify cache directory and get the meta.json files, combine into a single json file
346 + if (is_dir(WPO_CACHE_MIN_FILES_DIR) && wp_is_writable(dirname(WPO_CACHE_MIN_FILES_DIR))) {
347 + if ($handle = opendir(WPO_CACHE_MIN_FILES_DIR)) {
348 + while (false !== ($d = readdir($handle))) {
349 + if (0 === strcmp($d, '.') || 0 === strcmp($d, '..') || !is_numeric($d)) {
350 + continue;
351 + }
352 + $cache_min_folder = WPO_CACHE_MIN_FILES_DIR.'/'.$d;
353 + if ($cache_min_folder_handle = opendir($cache_min_folder)) {
354 + while (false !== ($maybe_file = readdir($cache_min_folder_handle))) {
355 + if (0 === strcmp($maybe_file, '.') || 0 === strcmp($maybe_file, '..')) {
356 + continue;
357 + }
358 + $maybe_file_path = $cache_min_folder . '/' . $maybe_file;
359 + if (is_file($maybe_file_path) && 'meta.json' === basename($maybe_file_path)) {
360 + $combined_metas['meta_logs'][$d] = json_decode(file_get_contents($maybe_file_path));
361 + }
362 + }
363 + closedir($cache_min_folder_handle);
364 + }
365 + }
366 + closedir($handle);
367 + }
368 + }
369 +
370 + if (0 === count($combined_metas['meta_logs'])) {
371 + return array(
372 + 'success' => false,
373 + 'error' => __('No file was found', 'wp-optimize'),
374 + );
375 + } else {
376 + return array(
377 + 'success' => true,
378 + 'combined_metas' => $combined_metas
379 + );
380 + }
381 + }
382 +
383 + /**
279 384 * Get minify file data.
280 385 *
281 386 * @param string $filename
282 387 * @return array
@@ -303,9 +408,9 @@
303 408 * @param string $file
304 409 */
305 410 private function check_and_delete($file) {
306 411 if (file_exists($file)) {
307 - unlink($file);
412 + wp_delete_file($file);
308 413 }
309 414 }
310 415
311 416 /**
@@ -311,9 +416,9 @@
311 416 /**
312 417 * Get transient file.
313 418 *
314 419 * @param string $key
315 - * @param string $value
420 + * @param mixed $value
316 421 * @param string $filename
317 422 * @return string
318 423 */
319 424 private function get_transient_file($key, $value, $filename) {