| 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 |
$message = array( |
| 40 |
__('The minification cache was deleted.', 'wp-optimize'), |
| 41 |
strip_tags($others, '<strong>'), |
| 42 |
); |
| 43 |
$message = array_filter($message); |
| 44 |
return array( |
| 45 |
'success' => true, |
| 46 |
'message' => implode("\n", $message), |
| 47 |
'files' => $files |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Forces a new Cache to be built |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function minify_increment_cache() { |
| 57 |
if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize')); |
| 58 |
WP_Optimize_Minify_Cache_Functions::cache_increment(); |
| 59 |
$files = $this->get_minify_cached_files(); |
| 60 |
return array( |
| 61 |
'success' => true, |
| 62 |
'files' => $files |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Purge the cache |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function purge_minify_cache() { |
| 72 |
if (!WPO_MINIFY_PHP_VERSION_MET) return array('error' => __('WP-Optimize Minify requires a higher PHP version', 'wp-optimize')); |
| 73 |
if (!WP_Optimize()->can_purge_the_cache()) return array('error' => __('You do not have permission to purge the cache', 'wp-optimize')); |
| 74 |
|
| 75 |
// deletes temp files and old caches incase CRON isn't working |
| 76 |
WP_Optimize_Minify_Cache_Functions::cache_increment(); |
| 77 |
if (wp_optimize_minify_config()->always_purge_everything()) { |
| 78 |
WP_Optimize_Minify_Cache_Functions::purge(); |
| 79 |
$state = array(); |
| 80 |
$old = array(); |
| 81 |
} else { |
| 82 |
$state = WP_Optimize_Minify_Cache_Functions::purge_temp_files(); |
| 83 |
$old = WP_Optimize_Minify_Cache_Functions::purge_old(); |
| 84 |
} |
| 85 |
$others = WP_Optimize_Minify_Cache_Functions::purge_others(); |
| 86 |
$files = $this->get_minify_cached_files(); |
| 87 |
|
| 88 |
$notice = array( |
| 89 |
__('All caches from WP-Optimize Minify have been purged.', 'wp-optimize'), |
| 90 |
strip_tags($others, '<strong>'), |
| 91 |
); |
| 92 |
$notice = array_filter($notice); |
| 93 |
$notice = json_encode($notice); // encode |
| 94 |
|
| 95 |
return array( |
| 96 |
'result' => 'caches cleared', |
| 97 |
'others' => $others, |
| 98 |
'state' => $state, |
| 99 |
'message' => $notice, |
| 100 |
'old' => $old, |
| 101 |
'files' => $files |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Save options to the config |
| 107 |
* |
| 108 |
* @param array $data |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function save_minify_settings($data) { |
| 112 |
$new_data = array(); |
| 113 |
foreach ($data as $key => $value) { |
| 114 |
if ('true' === $value) { |
| 115 |
$new_data[$key] = true; |
| 116 |
} elseif ('false' === $value) { |
| 117 |
$new_data[$key] = false; |
| 118 |
} else { |
| 119 |
$new_data[$key] = $value; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (isset($data['minify_advanced_tab'])) { |
| 124 |
// Make sure that empty settings are still saved |
| 125 |
if (!isset($new_data['ignore_list'])) $new_data['ignore_list'] = array(); |
| 126 |
if (!isset($new_data['blacklist'])) $new_data['blacklist'] = array(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Filters the data before saving it |
| 131 |
* |
| 132 |
* @param array $new_data - The original data |
| 133 |
* @return array The data, altered or not |
| 134 |
*/ |
| 135 |
$new_data = apply_filters('wpo_save_minify_settings', $new_data); |
| 136 |
|
| 137 |
if (!class_exists('WP_Optimize_Minify_Config')) return array( |
| 138 |
'success' => false, |
| 139 |
'message' => "WP_Optimize_Minify_Config class doesn't exist", |
| 140 |
); |
| 141 |
$working = wp_optimize_minify_config()->update($new_data); |
| 142 |
if (!$working) { |
| 143 |
return array( |
| 144 |
'success' => false, |
| 145 |
'error' => 'failed to save minify settings' |
| 146 |
); |
| 147 |
} |
| 148 |
$purged = $this->purge_minify_cache(); |
| 149 |
return array( |
| 150 |
'success' => true, |
| 151 |
'files' => $purged['files'] |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Hide the information notice for the current user |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function hide_minify_notice() { |
| 161 |
return array( |
| 162 |
'success' => update_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the current status |
| 168 |
* |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function get_status() { |
| 172 |
$config = wp_optimize_minify_config()->get(); |
| 173 |
return array( |
| 174 |
'enabled' => $config['enabled'], |
| 175 |
'js' => $config['enable_js'], |
| 176 |
'css' => $config['enable_css'], |
| 177 |
'html' => $config['html_minification'], |
| 178 |
'stats' => $this->get_minify_cached_files() |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Run minify preload action. |
| 184 |
* |
| 185 |
* @return void|array - Doesn't return anything if run() is successfull (Run() prints a JSON object and closed browser connection) or an array if failed. |
| 186 |
*/ |
| 187 |
public function run_minify_preload() { |
| 188 |
return WP_Optimize_Minify_Preloader::instance()->run('manual'); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Cancel minify preload action. |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
public function cancel_minify_preload() { |
| 197 |
WP_Optimize_Minify_Preloader::instance()->cancel_preload(); |
| 198 |
return WP_Optimize_Minify_Preloader::instance()->get_status_info(); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get status of minify preload. |
| 203 |
* |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function get_minify_preload_status() { |
| 207 |
return WP_Optimize_Minify_Preloader::instance()->get_status_info(); |
| 208 |
} |
| 209 |
} |
| 210 |
|