| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
class WP_Optimize_Minify_Admin { |
| 5 |
|
| 6 |
private $wp_version_required = '4.5'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialize, add actions and filters |
| 10 |
* |
| 11 |
* @return void |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
if (WPO_MINIFY_PHP_VERSION_MET) { |
| 15 |
// exclude processing for editors and administrators (fix editors) |
| 16 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'check_permissions_admin_notices')); |
| 17 |
} |
| 18 |
|
| 19 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'admin_notices_activation_errors')); |
| 20 |
|
| 21 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 22 |
|
| 23 |
// This function runs when WordPress updates or installs/remove something. Forces new cache |
| 24 |
add_action('upgrader_process_complete', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment')); |
| 25 |
// This function runs when an active theme or plugin is updated |
| 26 |
add_action('wpo_active_plugin_or_theme_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 27 |
add_action('upgrader_overwrote_package', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 28 |
add_action('after_switch_theme', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment')); |
| 29 |
add_action('updraftcentral_version_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 30 |
add_action('elementor/editor/after_save', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 31 |
add_action('fusion_cache_reset_after', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 32 |
// Output asset preload placeholder, replaced by premium |
| 33 |
add_action('wpo_minify_settings_tabs', array($this, 'output_assets_preload_placeholder'), 10, 1); |
| 34 |
|
| 35 |
add_action('wp_optimize_register_admin_content', array($this, 'register_content')); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register the content |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function register_content() { |
| 44 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'output_status'), 20); |
| 45 |
add_action('wp_optimize_admin_page_wpo_minify_settings', array($this, 'output_settings'), 20); |
| 46 |
add_action('wp_optimize_admin_page_wpo_minify_advanced', array($this, 'output_advanced'), 20); |
| 47 |
add_action('wp_optimize_admin_page_wpo_minify_font', array($this, 'output_font_settings'), 20); |
| 48 |
add_action('wp_optimize_admin_page_wpo_minify_css', array($this, 'output_css_settings'), 20); |
| 49 |
add_action('wp_optimize_admin_page_wpo_minify_js', array($this, 'output_js_settings'), 20); |
| 50 |
add_action('wp_optimize_admin_page_wpo_minify_preload', array($this, 'output_preload_settings'), 20); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Load scripts for controlling the admin pages |
| 55 |
* |
| 56 |
* @param string $hook |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function admin_enqueue_scripts($hook) { |
| 60 |
$enqueue_version = (defined('WP_DEBUG') && WP_DEBUG) ? WPO_VERSION.'.'.time() : WPO_VERSION; |
| 61 |
$min_or_not_internal = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '-'. str_replace('.', '-', WPO_VERSION). '.min'; |
| 62 |
if (preg_match('/wp\-optimize/i', $hook)) { |
| 63 |
wp_enqueue_script('wp-optimize-min-js', WPO_PLUGIN_URL.'js/minify' . $min_or_not_internal . '.js', array('jquery', 'wp-optimize-admin-js'), $enqueue_version); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Conditionally runs upon the WP action admin_notices to display errors |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function admin_notices_activation_errors() { |
| 73 |
global $wp_version; |
| 74 |
include ABSPATH . WPINC . '/version.php'; |
| 75 |
$errors = array(); |
| 76 |
|
| 77 |
if (!WPO_MINIFY_PHP_VERSION_MET) { |
| 78 |
$errors[] = sprintf(__('WP-Optimize Minify requires PHP 5.4 or higher. You’re using version %s.', 'wp-optimize'), PHP_VERSION); |
| 79 |
} |
| 80 |
|
| 81 |
if (!extension_loaded('mbstring')) { |
| 82 |
$errors[] = __('WP-Optimize Minify requires the PHP mbstring module to be installed on the server; please ask your web hosting company for advice on how to enable it on your server.', 'wp-optimize'); |
| 83 |
} |
| 84 |
|
| 85 |
if (version_compare($wp_version, $this->wp_version_required, '<')) { |
| 86 |
$errors[] = sprintf(__('WP-Optimize Minify requires WordPress version %s or higher. You’re using version %s.', 'wp-optimize'), $this->wp_version_required, $wp_version); |
| 87 |
} |
| 88 |
|
| 89 |
foreach ($errors as $error) { |
| 90 |
?> |
| 91 |
<div class="notice notice-error wpo-warning"> |
| 92 |
<p><?php echo $error; ?></p> |
| 93 |
</div> |
| 94 |
<?php |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Display an admin notice if the user has inadequate filesystem permissions |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function check_permissions_admin_notices() { |
| 104 |
// get cache path |
| 105 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 106 |
$cache_dir = $cache_path['cachedir']; |
| 107 |
if (is_dir($cache_dir) && !is_writable($cache_dir)) { |
| 108 |
$chmod = substr(sprintf('%o', fileperms($cache_dir)), -4); |
| 109 |
?> |
| 110 |
<div class="notice notice-error wpo-warning"> |
| 111 |
<p> |
| 112 |
<?php printf(__('WP-Optimize Minify needs write permissions on the folder %s.', 'wp-optimize'), "<strong>".htmlspecialchars($cache_dir)."</strpmg>"); ?> |
| 113 |
</p> |
| 114 |
</div> |
| 115 |
<div class="notice notice-error wpo-warning"> |
| 116 |
<p> |
| 117 |
<?php printf(__('The current permissions for WP-Optimize Minify are chmod %s.', 'wp-optimize'), "<strong>$chmod</strong>"); ?> |
| 118 |
</p> |
| 119 |
</div> |
| 120 |
<div class="notice notice-error wpo-warning"> |
| 121 |
<p> |
| 122 |
<?php |
| 123 |
printf(__('If you need something more than %s for it to work, then your server is probably misconfigured.', 'wp-optimize'), '<strong>775</strong>'); |
| 124 |
echo " "; |
| 125 |
_e('Please contact your hosting provider.', 'wp-optimize'); |
| 126 |
?> |
| 127 |
</p> |
| 128 |
</div> |
| 129 |
<?php |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Minify - Outputs the status tab |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function output_status() { |
| 139 |
if (!class_exists('WP_Optimize_Detect_Minify_Plugins')) { |
| 140 |
require_once(WP_OPTIMIZE_MINIFY_DIR.'/class-wp-optimize-detect-minify-plugins.php'); |
| 141 |
} |
| 142 |
$this->found_incompatible_plugins = WP_Optimize_Detect_Minify_Plugins::get_instance()->get_active_minify_plugins(); |
| 143 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 144 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 145 |
WP_Optimize()->include_template( |
| 146 |
'minify/status-tab.php', |
| 147 |
false, |
| 148 |
array( |
| 149 |
'wpo_minify_options' => $wpo_minify_options, |
| 150 |
'show_information_notice' => !get_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true), |
| 151 |
'cache_dir' => $cache_path['cachedir'], |
| 152 |
'can_purge_the_cache' => WP_Optimize()->can_purge_the_cache(), |
| 153 |
'active_minify_plugins' => apply_filters('wpo_minify_found_incompatible_plugins', $this->found_incompatible_plugins), |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Minify - Outputs the font settings tab |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function output_font_settings() { |
| 164 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 165 |
WP_Optimize()->include_template( |
| 166 |
'minify/font-settings-tab.php', |
| 167 |
false, |
| 168 |
array( |
| 169 |
'wpo_minify_options' => $wpo_minify_options |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Minify - Outputs the CSS settings tab |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function output_css_settings() { |
| 180 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 181 |
WP_Optimize()->include_template( |
| 182 |
'minify/css-settings-tab.php', |
| 183 |
false, |
| 184 |
array( |
| 185 |
'wpo_minify_options' => $wpo_minify_options |
| 186 |
) |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Minify - Outputs the JS settings tab |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function output_js_settings() { |
| 196 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 197 |
WP_Optimize()->include_template( |
| 198 |
'minify/js-settings-tab.php', |
| 199 |
false, |
| 200 |
array( |
| 201 |
'wpo_minify_options' => $wpo_minify_options |
| 202 |
) |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Minify - Outputs the settings tab |
| 208 |
* |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function output_settings() { |
| 212 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 213 |
$url = parse_url(get_home_url()); |
| 214 |
WP_Optimize()->include_template( |
| 215 |
'minify/settings-tab.php', |
| 216 |
false, |
| 217 |
array( |
| 218 |
'wpo_minify_options' => $wpo_minify_options, |
| 219 |
'default_protocol' => $url['scheme'] |
| 220 |
) |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Minify - Outputs the settings tab |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public function output_assets_preload_placeholder($wpo_minify_options) { |
| 230 |
WP_Optimize()->include_template( |
| 231 |
'minify/asset-preload.php', |
| 232 |
false, |
| 233 |
array( |
| 234 |
'wpo_minify_options' => $wpo_minify_options |
| 235 |
) |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Minify - Outputs the preload tab |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
public function output_preload_settings() { |
| 245 |
$wpo_minify_preloader = WP_Optimize_Minify_Preloader::instance(); |
| 246 |
$is_running = $wpo_minify_preloader->is_running(); |
| 247 |
$status = $wpo_minify_preloader->get_status_info(); |
| 248 |
if (!class_exists('WPO_Cache_Config')) require_once(WPO_PLUGIN_MAIN_PATH . '/cache/class-wpo-cache-config.php'); |
| 249 |
$cache_config = WPO_Cache_Config::instance(); |
| 250 |
WP_Optimize()->include_template( |
| 251 |
'minify/preload-tab.php', |
| 252 |
false, |
| 253 |
array( |
| 254 |
'is_cache_enabled' => $cache_config->get_option('enable_page_caching'), |
| 255 |
'is_running' => $is_running, |
| 256 |
'status_message' => isset($status['message']) ? $status['message'] : '', |
| 257 |
) |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Minify - Outputs the advanced tab |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public function output_advanced() { |
| 267 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 268 |
$files = false; |
| 269 |
if (apply_filters('wpo_minify_status_show_files_on_load', true) && WPO_MINIFY_PHP_VERSION_MET) { |
| 270 |
$files = WP_Optimize_Minify_Cache_Functions::get_cached_files(); |
| 271 |
} |
| 272 |
|
| 273 |
// WP_Optimize_Minify_Functions is only loaded when Minify is active |
| 274 |
if (class_exists('WP_Optimize_Minify_Functions')) { |
| 275 |
$default_ignore = WP_Optimize_Minify_Functions::get_default_ignore(); |
| 276 |
$default_ie_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist(); |
| 277 |
} else { |
| 278 |
$default_ignore = array(); |
| 279 |
$default_ie_blacklist = array(); |
| 280 |
} |
| 281 |
|
| 282 |
WP_Optimize()->include_template( |
| 283 |
'minify/advanced-tab.php', |
| 284 |
false, |
| 285 |
array( |
| 286 |
'wpo_minify_options' => $wpo_minify_options, |
| 287 |
'files' => $files, |
| 288 |
'default_ignore' => $default_ignore, |
| 289 |
'default_ie_blacklist' => $default_ie_blacklist |
| 290 |
) |
| 291 |
); |
| 292 |
} |
| 293 |
} |
| 294 |
|