| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
define('WP_OPTIMIZE_MINIFY_VERSION', '2.6.5'); |
| 5 |
define('WP_OPTIMIZE_MINIFY_DIR', dirname(__FILE__)); |
| 6 |
if (!defined('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED')) define('WP_OPTIMIZE_SHOW_MINIFY_ADVANCED', false); |
| 7 |
|
| 8 |
class WP_Optimize_Minify { |
| 9 |
|
| 10 |
/** |
| 11 |
* Minify commands object |
| 12 |
* |
| 13 |
* @var WP_Optimize_Minify_Commands |
| 14 |
*/ |
| 15 |
public $minify_commands; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
private $enabled; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor - Initialize actions and filters |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
|
| 29 |
$this->minify_commands = new WP_Optimize_Minify_Commands(); |
| 30 |
|
| 31 |
if (!class_exists('WP_Optimize_Minify_Config')) { |
| 32 |
include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| 33 |
} |
| 34 |
|
| 35 |
$this->enabled = wp_optimize_minify_config()->is_enabled(); |
| 36 |
|
| 37 |
$this->load_admin(); |
| 38 |
|
| 39 |
// Don't run the rest if PHP requirement isn't met |
| 40 |
if (!WPO_MINIFY_PHP_VERSION_MET) return; |
| 41 |
|
| 42 |
add_filter('wpo_cache_admin_bar_menu_items', array($this, 'admin_bar_menu'), 30, 1); |
| 43 |
|
| 44 |
if (WP_Optimize::is_premium()) { |
| 45 |
$this->load_premium(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Directory that stores the cache, including gzipped files and mobile specific cache |
| 50 |
*/ |
| 51 |
if (!defined('WPO_CACHE_MIN_FILES_DIR')) define('WPO_CACHE_MIN_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-minify'); |
| 52 |
if (!defined('WPO_CACHE_MIN_FILES_URL')) define('WPO_CACHE_MIN_FILES_URL', untrailingslashit(WP_CONTENT_URL).'/cache/wpo-minify'); |
| 53 |
|
| 54 |
if (!class_exists('WP_Optimize_Minify_Cache_Functions')) { |
| 55 |
include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-cache-functions.php'; |
| 56 |
} |
| 57 |
|
| 58 |
$this->load_frontend(); |
| 59 |
|
| 60 |
// cron job to delete old wpo_min cache |
| 61 |
add_action('wpo_minify_purge_old_cache', array('WP_Optimize_Minify_Cache_Functions', 'purge_old')); |
| 62 |
|
| 63 |
if ($this->enabled) { |
| 64 |
$this->schedule_purge_old_cache_event(); |
| 65 |
} |
| 66 |
|
| 67 |
// Handle minify cache purging. |
| 68 |
add_action('wp_loaded', array($this, 'handle_purge_minify_cache')); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns singleton instance object |
| 74 |
* |
| 75 |
* @return WP_Optimize_Minify Returns `WP_Optimize_Minify` object |
| 76 |
*/ |
| 77 |
public static function instance() { |
| 78 |
static $_instance = null; |
| 79 |
if (null === $_instance) { |
| 80 |
$_instance = new self(); |
| 81 |
} |
| 82 |
return $_instance; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Admin toolbar processing |
| 87 |
* |
| 88 |
* @param array $menu_items |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function admin_bar_menu($menu_items) { |
| 92 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 93 |
|
| 94 |
if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options') || !($wpo_minify_options['enable_css'] || $wpo_minify_options['enable_js'])) return $menu_items; |
| 95 |
|
| 96 |
$act_url = remove_query_arg('wpo_minify_cache_purged'); |
| 97 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 98 |
$cache_size_info = '<h4>'.__('Minify cache', 'wp-optimize').'</h4><span><span class="label">'.__('Cache size:', 'wp-optimize').'</span> <span class="stats">'.esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats($cache_path['cachedir'])).'</span></span>'; |
| 99 |
|
| 100 |
$menu_items[] = array( |
| 101 |
'id' => 'wpo_minify_cache_stats', |
| 102 |
'title' => $cache_size_info, |
| 103 |
'meta' => array( |
| 104 |
'class' => 'wpo-cache-stats', |
| 105 |
), |
| 106 |
'parent' => 'wpo_purge_cache', |
| 107 |
); |
| 108 |
|
| 109 |
$menu_items[] = array( |
| 110 |
'parent' => 'wpo_purge_cache', |
| 111 |
'id' => 'purge_minify_cache', |
| 112 |
'title' => __('Purge minify cache', 'wp-optimize'), |
| 113 |
'href' => add_query_arg('_wpo_purge_minify_cache', wp_create_nonce('wpo_purge_minify_cache'), $act_url), |
| 114 |
); |
| 115 |
return $menu_items; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Check if purge single page action sent and purge cache. |
| 120 |
*/ |
| 121 |
public function handle_purge_minify_cache() { |
| 122 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 123 |
if (!$wpo_minify_options['enabled'] || !current_user_can('manage_options')) return; |
| 124 |
|
| 125 |
if (isset($_GET['wpo_minify_cache_purged'])) { |
| 126 |
if (is_admin()) { |
| 127 |
add_action('admin_notices', array($this, 'notice_purge_minify_cache_success')); |
| 128 |
return; |
| 129 |
} else { |
| 130 |
$message = __('Minify cache purged', 'wp-optimize'); |
| 131 |
printf('<script>alert("%s");</script>', $message); |
| 132 |
return; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (!isset($_GET['_wpo_purge_minify_cache'])) return; |
| 137 |
|
| 138 |
if (wp_verify_nonce($_GET['_wpo_purge_minify_cache'], 'wpo_purge_minify_cache')) { |
| 139 |
$success = false; |
| 140 |
|
| 141 |
// Purge minify |
| 142 |
$results = $this->minify_commands->purge_minify_cache(); |
| 143 |
if ("caches cleared" == $results['result']) $success = true; |
| 144 |
|
| 145 |
// remove nonce from url and reload page. |
| 146 |
wp_redirect(add_query_arg('wpo_minify_cache_purged', $success, remove_query_arg('_wpo_purge_minify_cache'))); |
| 147 |
exit; |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Load the admin class |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
private function load_admin() { |
| 158 |
if (!is_admin()) return; |
| 159 |
new WP_Optimize_Minify_Admin(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Load the frontend class |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
private function load_frontend() { |
| 168 |
if ($this->enabled) { |
| 169 |
new WP_Optimize_Minify_Front_End(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Load the premium class |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
private function load_premium() { |
| 179 |
new WP_Optimize_Minify_Premium(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Run during activation |
| 184 |
* Increment cache first as it will save files to that dir |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function plugin_activate() { |
| 189 |
// increment cache time |
| 190 |
if (class_exists('WP_Optimize_Minify_Cache_Functions')) { |
| 191 |
WP_Optimize_Minify_Cache_Functions::cache_increment(); |
| 192 |
} |
| 193 |
|
| 194 |
// old cache purge event cron |
| 195 |
wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); |
| 196 |
$this->schedule_purge_old_cache_event(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* If the WP cron event for scheduling purging of the minify cache does not exist, then create it |
| 201 |
*/ |
| 202 |
private function schedule_purge_old_cache_event() { |
| 203 |
if (!wp_next_scheduled('wpo_minify_purge_old_cache')) { |
| 204 |
wp_schedule_event(time() + 43200, 'daily', 'wpo_minify_purge_old_cache'); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Run during plugin deactivation |
| 210 |
* |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
public function plugin_deactivate() { |
| 214 |
if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; |
| 215 |
if (class_exists('WP_Optimize_Minify_Cache_Functions') && WP_Optimize()->get_page_cache()->should_purge) { |
| 216 |
WP_Optimize_Minify_Cache_Functions::purge_temp_files(); |
| 217 |
WP_Optimize_Minify_Cache_Functions::purge_old(); |
| 218 |
WP_Optimize_Minify_Cache_Functions::purge_others(); |
| 219 |
} |
| 220 |
|
| 221 |
// old cache purge event cron |
| 222 |
wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Run during plugin uninstall |
| 227 |
* |
| 228 |
* @return void |
| 229 |
*/ |
| 230 |
public function plugin_uninstall() { |
| 231 |
if (defined('WPO_MINIFY_PHP_VERSION_MET') && !WPO_MINIFY_PHP_VERSION_MET) return; |
| 232 |
// remove options from DB |
| 233 |
if (!function_exists('wp_optimize_minify_config')) { |
| 234 |
include WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-minify-config.php'; |
| 235 |
} |
| 236 |
wp_optimize_minify_config()->purge(); |
| 237 |
// remove minified files |
| 238 |
if (class_exists('WP_Optimize_Minify_Cache_Functions')) { |
| 239 |
WP_Optimize_Minify_Cache_Functions::purge(); |
| 240 |
WP_Optimize_Minify_Cache_Functions::purge_others(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Shows success notice for purge minify cache |
| 246 |
*/ |
| 247 |
public function notice_purge_minify_cache_success() { |
| 248 |
$this->show_notice(__('The minify cache was successfully purged.', 'wp-optimize'), 'success'); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Show notification in WordPress admin. |
| 253 |
* |
| 254 |
* @param string $message HTML (no further escaping is performed) |
| 255 |
* @param string $type error, warning, success, or info |
| 256 |
*/ |
| 257 |
public function show_notice($message, $type) { |
| 258 |
global $current_screen; |
| 259 |
|
| 260 |
if ($current_screen && is_callable(array($current_screen, 'is_block_editor')) && $current_screen->is_block_editor()) : |
| 261 |
?> |
| 262 |
<script> |
| 263 |
window.addEventListener('load', function() { |
| 264 |
(function(wp) { |
| 265 |
if (window.wp && wp.hasOwnProperty('data') && 'function' == typeof wp.data.dispatch) { |
| 266 |
wp.data.dispatch('core/notices').createNotice( |
| 267 |
'<?php echo $type; ?>', |
| 268 |
'<?php echo $message; ?>', |
| 269 |
{ |
| 270 |
isDismissible: true, |
| 271 |
} |
| 272 |
); |
| 273 |
} |
| 274 |
})(window.wp); |
| 275 |
}); |
| 276 |
</script> |
| 277 |
<?php else : ?> |
| 278 |
<div class="notice wpo-notice notice-<?php echo $type; ?> is-dismissible"> |
| 279 |
<p><?php echo $message; ?></p> |
| 280 |
</div> |
| 281 |
<?php |
| 282 |
endif; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Check if current user can purge cache. |
| 287 |
* |
| 288 |
* @return bool |
| 289 |
*/ |
| 290 |
public function can_purge_cache() { |
| 291 |
$required_capability = is_multisite() ? 'manage_network_options' : 'manage_options'; |
| 292 |
|
| 293 |
if (WP_Optimize::is_premium()) { |
| 294 |
return current_user_can($required_capability) || WP_Optimize_Premium()->can_purge_the_cache(); |
| 295 |
} else { |
| 296 |
return current_user_can($required_capability); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|