| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
class WP_Optimize_Minify_Admin { |
| 5 |
|
| 6 |
/** |
| 7 |
* Initialize, add actions and filters |
| 8 |
* |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
public function __construct() { |
| 12 |
// exclude processing for editors and administrators (fix editors) |
| 13 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'check_permissions_admin_notices')); |
| 14 |
|
| 15 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'admin_notices_activation_error')); |
| 16 |
|
| 17 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 18 |
|
| 19 |
// This function runs when WordPress updates or installs/remove something. Forces new cache |
| 20 |
add_action('upgrader_process_complete', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment')); |
| 21 |
// This function runs when an active theme or plugin is updated |
| 22 |
add_action('wpo_active_plugin_or_theme_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 23 |
add_action('upgrader_overwrote_package', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 24 |
add_action('after_switch_theme', array('WP_Optimize_Minify_Cache_Functions', 'cache_increment')); |
| 25 |
add_action('updraftcentral_version_updated', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 26 |
add_action('elementor/editor/after_save', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 27 |
add_action('fusion_cache_reset_after', array('WP_Optimize_Minify_Cache_Functions', 'reset')); |
| 28 |
// Output asset preload placeholder, replaced by premium |
| 29 |
add_action('wpo_minify_settings_tabs', array($this, 'output_assets_preload_placeholder')); |
| 30 |
|
| 31 |
add_action('wp_optimize_register_admin_content', array($this, 'register_content')); |
| 32 |
add_action('wpo_after_minify_javascript_options', array($this, 'output_capo_js_settings')); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Register the content |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function register_content() { |
| 41 |
add_action('wp_optimize_admin_page_wpo_minify_status', array($this, 'output_status'), 20); |
| 42 |
add_action('wp_optimize_admin_page_wpo_minify_settings', array($this, 'output_settings'), 20); |
| 43 |
add_action('wp_optimize_admin_page_wpo_minify_advanced', array($this, 'output_advanced'), 20); |
| 44 |
add_action('wp_optimize_admin_page_wpo_minify_font', array($this, 'output_font_settings'), 20); |
| 45 |
add_action('wp_optimize_admin_page_wpo_minify_analytics', array($this, 'output_analytics_settings'), 20); |
| 46 |
add_action('wp_optimize_admin_page_wpo_minify_css', array($this, 'output_css_settings'), 20); |
| 47 |
add_action('wp_optimize_admin_page_wpo_minify_js', array($this, 'output_js_settings'), 20); |
| 48 |
add_action('wp_optimize_admin_page_wpo_minify_preload', array($this, 'output_preload_settings'), 20); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Output Capo JS settings |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function output_capo_js_settings() { |
| 57 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 58 |
$extract = array( |
| 59 |
'enable_capo_js' => $wpo_minify_options['enable_capo_js'] ?? false, |
| 60 |
); |
| 61 |
WP_Optimize()->include_template('minify/capo-js.php', false, $extract); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Load scripts for controlling the admin pages |
| 66 |
* |
| 67 |
* @param string $hook |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function admin_enqueue_scripts($hook) { |
| 71 |
$enqueue_version = WP_Optimize()->get_enqueue_version(); |
| 72 |
$min_or_not_internal = WP_Optimize()->get_min_or_not_internal_string(); |
| 73 |
if (preg_match('/wp\-optimize/i', $hook)) { |
| 74 |
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, true); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Conditionally runs upon the WP action admin_notices to display error |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function admin_notices_activation_error() { |
| 84 |
if (!extension_loaded('mbstring')) { |
| 85 |
echo '<div class="notice notice-error wpo-warning">'; |
| 86 |
echo '<p>' . esc_html__('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') . '</p>'; |
| 87 |
echo '</div>'; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Display an admin notice if the user has inadequate filesystem permissions |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function check_permissions_admin_notices() { |
| 97 |
// get cache path |
| 98 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 99 |
$cache_dir = $cache_path['cachedir']; |
| 100 |
if (is_dir($cache_dir) && !wp_is_writable($cache_dir)) { |
| 101 |
$chmod = substr(sprintf('%o', fileperms($cache_dir)), -4); |
| 102 |
?> |
| 103 |
<div class="notice notice-error wpo-warning"> |
| 104 |
<p> |
| 105 |
<?php |
| 106 |
// translators: %s is a directory |
| 107 |
echo wp_kses_post(sprintf(__('WP-Optimize Minify needs write permissions on the folder %s.', 'wp-optimize'), "<strong>". esc_html($cache_dir)."</strong>")); |
| 108 |
?> |
| 109 |
</p> |
| 110 |
</div> |
| 111 |
<div class="notice notice-error wpo-warning"> |
| 112 |
<p> |
| 113 |
<?php |
| 114 |
// translators: %s is a file permissions code |
| 115 |
echo wp_kses_post(sprintf(__('The current permissions for WP-Optimize Minify are chmod %s.', 'wp-optimize'), "<strong>" . esc_html($chmod) . "</strong>")); |
| 116 |
?> |
| 117 |
</p> |
| 118 |
</div> |
| 119 |
<div class="notice notice-error wpo-warning"> |
| 120 |
<p> |
| 121 |
<?php |
| 122 |
// translators: %s is a file permissions code |
| 123 |
echo wp_kses_post(sprintf(__('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 |
esc_html_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 |
$found_incompatible_plugins = WP_Optimize_Detect_Minify_Plugins::get_instance()->get_active_minify_plugins(); |
| 140 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 141 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 142 |
WP_Optimize()->include_template( |
| 143 |
'minify/status-tab.php', |
| 144 |
false, |
| 145 |
array( |
| 146 |
'wpo_minify_options' => $wpo_minify_options, |
| 147 |
'show_information_notice' => !get_user_meta(get_current_user_id(), 'wpo-hide-minify-information-notice', true), |
| 148 |
'cache_dir' => $cache_path['cachedir'], |
| 149 |
'can_purge_the_cache' => WP_Optimize()->get_minify()->can_purge_cache(), |
| 150 |
'active_minify_plugins' => apply_filters('wpo_minify_found_incompatible_plugins', $found_incompatible_plugins), |
| 151 |
) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Minify - Outputs the font settings tab |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function output_font_settings() { |
| 161 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 162 |
|
| 163 |
WP_Optimize()->include_template( |
| 164 |
'minify/font-settings-tab.php', |
| 165 |
false, |
| 166 |
array( |
| 167 |
'wpo_minify_options' => $wpo_minify_options, |
| 168 |
'fonts_cache_size' => WP_Optimize()::is_premium() ? WP_Optimize_Host_Google_Fonts::instance()->get_cache_stats() : '', |
| 169 |
) |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Minify - Outputs the analytics settings tab |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function output_analytics_settings() { |
| 179 |
$config = wp_optimize_minify_config()->get(); |
| 180 |
$id = $config['tracking_id'] ?? ''; |
| 181 |
$method = $config['analytics_method'] ?? ''; |
| 182 |
$is_enabled = $config['enable_analytics'] ?? false; |
| 183 |
|
| 184 |
WP_Optimize()->include_template( |
| 185 |
'minify/analytics-settings-tab.php', |
| 186 |
false, |
| 187 |
array( |
| 188 |
'id' => $id, |
| 189 |
'method'=> $method, |
| 190 |
'is_enabled'=> $is_enabled |
| 191 |
) |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Minify - Get the output settings for the CSS or JS settings tabs |
| 197 |
* |
| 198 |
* @param string $type - css or js |
| 199 |
* |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
private function get_output_setting($type) { |
| 203 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 204 |
$is_http1 = WP_Optimize_Utils::is_request_protocol_http1(); |
| 205 |
$protocol = $is_http1 ? 'HTTP/1.x' : 'HTTP/2/3'; |
| 206 |
|
| 207 |
$merge_enabled = !empty($wpo_minify_options['enable_merging_of_'.$type]); |
| 208 |
|
| 209 |
// Determine if the notice should be shown |
| 210 |
$should_show_notice = (($is_http1 && !$merge_enabled) || (!$is_http1 && $merge_enabled)); |
| 211 |
$user_dismissed = get_user_meta(get_current_user_id(), 'wpo_hide_'.$type.'_merging_notice', true); |
| 212 |
|
| 213 |
// Only show if a user hasn't dismissed yet |
| 214 |
$show_section_notice = $should_show_notice && !$user_dismissed; |
| 215 |
$is_enabled_minification = $wpo_minify_options['enable_'.$type.'_minification']; |
| 216 |
|
| 217 |
$label = 'js' === $type ? 'JavaScript' : 'CSS'; |
| 218 |
|
| 219 |
// Notice text |
| 220 |
$notice_text = sprintf( |
| 221 |
/* translators: 1: File type (CSS/JS), 2: HTTP protocol */ |
| 222 |
__('Merging %1$s files may improve performance on HTTP/1.1, but could reduce speed on HTTP/2/3.<br><b>Note:</b> Your site is currently using %2$s.', 'wp-optimize'), |
| 223 |
$label, |
| 224 |
$protocol |
| 225 |
); |
| 226 |
|
| 227 |
// Tooltip parts |
| 228 |
$tooltip_parts = array( |
| 229 |
sprintf( |
| 230 |
/* translators: %s: File type (CSS/JS) */ |
| 231 |
__('Combines multiple %s files into one to reduce HTTP requests.', 'wp-optimize'), |
| 232 |
$label |
| 233 |
), |
| 234 |
__('May improve performance on HTTP/1.1, but could reduce speed on HTTP/2/3.', 'wp-optimize'), |
| 235 |
__('Disable this option if your site experiences issues.', 'wp-optimize'), |
| 236 |
sprintf( |
| 237 |
/* translators: %s: HTTP protocol */ |
| 238 |
__('Note: Your site is currently using %s.', 'wp-optimize'), |
| 239 |
$protocol |
| 240 |
), |
| 241 |
); |
| 242 |
$tooltip = implode(' ', $tooltip_parts); |
| 243 |
|
| 244 |
return array( |
| 245 |
'protocol' => $protocol, |
| 246 |
'show_section_notice' => $show_section_notice, |
| 247 |
'wpo_minify_options' => $wpo_minify_options, |
| 248 |
'is_enabled_minification' => $is_enabled_minification, |
| 249 |
'notice_text' => $notice_text, |
| 250 |
'tooltip' => $tooltip, |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Minify - Outputs the CSS settings tab |
| 256 |
* |
| 257 |
* @return void |
| 258 |
*/ |
| 259 |
public function output_css_settings() { |
| 260 |
$template_args = $this->get_output_setting('css'); |
| 261 |
$template_args['show_unused_css'] = !WP_Optimize::is_premium() || (WP_Optimize::is_premium() && $template_args['is_enabled_minification']); |
| 262 |
$template_args['show_unused_css_advertise'] = !WP_Optimize::is_premium(); |
| 263 |
$template_args['is_page_cache_enabled'] = WPO_Cache_Config::instance()->get_option('enable_page_caching'); |
| 264 |
|
| 265 |
WP_Optimize()->include_template( |
| 266 |
'minify/css-settings-tab.php', |
| 267 |
false, |
| 268 |
$template_args |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Minify - Outputs the JS settings tab |
| 274 |
* |
| 275 |
* @return void |
| 276 |
*/ |
| 277 |
public function output_js_settings() { |
| 278 |
$template_args = $this->get_output_setting('js'); |
| 279 |
WP_Optimize()->include_template( |
| 280 |
'minify/js-settings-tab.php', |
| 281 |
false, |
| 282 |
$template_args |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Minify - Outputs the settings tab |
| 288 |
* |
| 289 |
* @return void |
| 290 |
*/ |
| 291 |
public function output_settings() { |
| 292 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 293 |
$url = wp_parse_url(get_home_url()); |
| 294 |
WP_Optimize()->include_template( |
| 295 |
'minify/settings-tab.php', |
| 296 |
false, |
| 297 |
array( |
| 298 |
'wpo_minify_options' => $wpo_minify_options, |
| 299 |
'default_protocol' => $url['scheme'] |
| 300 |
) |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Minify - Outputs the settings tab |
| 306 |
* |
| 307 |
* @return void |
| 308 |
*/ |
| 309 |
public function output_assets_preload_placeholder($wpo_minify_options) { |
| 310 |
WP_Optimize()->include_template( |
| 311 |
'minify/asset-preload.php', |
| 312 |
false, |
| 313 |
array( |
| 314 |
'wpo_minify_options' => $wpo_minify_options |
| 315 |
) |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Minify - Outputs the preload tab |
| 321 |
* |
| 322 |
* @return void |
| 323 |
*/ |
| 324 |
public function output_preload_settings() { |
| 325 |
$wpo_minify_preloader = WP_Optimize_Minify_Preloader::instance(); |
| 326 |
$is_running = $wpo_minify_preloader->is_running(); |
| 327 |
$status = $wpo_minify_preloader->get_status_info(); |
| 328 |
$cache_config = WPO_Cache_Config::instance(); |
| 329 |
WP_Optimize()->include_template( |
| 330 |
'minify/preload-tab.php', |
| 331 |
false, |
| 332 |
array( |
| 333 |
'is_cache_enabled' => $cache_config->get_option('enable_page_caching'), |
| 334 |
'is_running' => $is_running, |
| 335 |
'status_message' => $status['message'] ?? '', |
| 336 |
) |
| 337 |
); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Minify - Outputs the advanced tab |
| 342 |
* |
| 343 |
* @return void |
| 344 |
*/ |
| 345 |
public function output_advanced() { |
| 346 |
$wpo_minify_options = wp_optimize_minify_config()->get(); |
| 347 |
$files = false; |
| 348 |
if (apply_filters('wpo_minify_status_show_files_on_load', true)) { |
| 349 |
$files = WP_Optimize_Minify_Cache_Functions::get_cached_files(); |
| 350 |
} |
| 351 |
|
| 352 |
// WP_Optimize_Minify_Functions is only loaded when Minify is active |
| 353 |
if (class_exists('WP_Optimize_Minify_Functions')) { |
| 354 |
$default_ignore = WP_Optimize_Minify_Functions::get_default_ignore(); |
| 355 |
$default_ie_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist(); |
| 356 |
} else { |
| 357 |
$default_ignore = array(); |
| 358 |
$default_ie_blacklist = array(); |
| 359 |
} |
| 360 |
|
| 361 |
WP_Optimize()->include_template( |
| 362 |
'minify/advanced-tab.php', |
| 363 |
false, |
| 364 |
array( |
| 365 |
'wpo_minify_options' => $wpo_minify_options, |
| 366 |
'files' => $files, |
| 367 |
'default_ignore' => $default_ignore, |
| 368 |
'default_ie_blacklist' => $default_ie_blacklist |
| 369 |
) |
| 370 |
); |
| 371 |
} |
| 372 |
} |
| 373 |
|