| @@ -27,8 +27,15 @@ | ||
| 27 | 27 | */ |
| 28 | 28 | protected $filepath = null; |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | + * Critical CSS base object | |
| 32 | + * | |
| 33 | + * @var object | |
| 34 | + */ | |
| 35 | + protected $_criticalcss = null; | |
| 36 | + | |
| 37 | + /** | |
| 31 | 38 | * Constructor. |
| 32 | 39 | * |
| 33 | 40 | * @param string $version Version. |
| 34 | 41 | * @param string $filepath Filepath. Needed for activation/deactivation/uninstall hooks. |
| @@ -49,25 +56,33 @@ | ||
| 49 | 56 | } |
| 50 | 57 | |
| 51 | 58 | protected function add_hooks() |
| 52 | 59 | { |
| 53 | - add_action( 'plugins_loaded', array( $this, 'setup' ) ); | |
| 60 | + if ( ! defined( 'AUTOPTIMIZE_SETUP_INITHOOK' ) ) { | |
| 61 | + define( 'AUTOPTIMIZE_SETUP_INITHOOK', 'plugins_loaded' ); | |
| 62 | + } | |
| 54 | 63 | |
| 64 | + add_action( AUTOPTIMIZE_SETUP_INITHOOK, array( $this, 'setup' ) ); | |
| 65 | + add_action( AUTOPTIMIZE_SETUP_INITHOOK, array( $this, 'hook_page_cache_purge' ) ); | |
| 66 | + | |
| 55 | 67 | add_action( 'autoptimize_setup_done', array( $this, 'version_upgrades_check' ) ); |
| 56 | 68 | add_action( 'autoptimize_setup_done', array( $this, 'check_cache_and_run' ) ); |
| 57 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_ao_extra' ) ); | |
| 58 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_partners_tab' ) ); | |
| 69 | + add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_ao_compat' ), 10 ); | |
| 70 | + add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_ao_extra' ), 15 ); | |
| 71 | + add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_admin_only_trinkets' ), 20 ); | |
| 72 | + add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_criticalcss' ), 11 ); | |
| 73 | + add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_notfound_fallback' ), 10 ); | |
| 59 | 74 | |
| 60 | 75 | add_action( 'init', array( $this, 'load_textdomain' ) ); |
| 61 | - add_action( 'plugins_loaded', array( $this, 'hook_page_cache_purge' ) ); | |
| 62 | - add_action( 'admin_init', array( 'PAnD', 'init' ) ); | |
| 63 | 76 | |
| 64 | - register_activation_hook( $this->filepath, array( $this, 'on_activate' ) ); | |
| 65 | - } | |
| 77 | + if ( is_multisite() && is_admin() ) { | |
| 78 | + // Only if multisite and if in admin we want to check if we need to save options on network level. | |
| 79 | + add_action( 'init', 'autoptimizeOptionWrapper::check_multisite_on_saving_options' ); | |
| 80 | + } | |
| 66 | 81 | |
| 67 | - public function on_activate() | |
| 68 | - { | |
| 82 | + // register uninstall & deactivation hooks. | |
| 69 | 83 | register_uninstall_hook( $this->filepath, 'autoptimizeMain::on_uninstall' ); |
| 84 | + register_deactivation_hook( $this->filepath, 'autoptimizeMain::on_deactivation' ); | |
| 70 | 85 | } |
| 71 | 86 | |
| 72 | 87 | public function load_textdomain() |
| 73 | 88 | { |
| @@ -76,9 +91,9 @@ | ||
| 76 | 91 | |
| 77 | 92 | public function setup() |
| 78 | 93 | { |
| 79 | 94 | // Do we gzip in php when caching or is the webserver doing it? |
| 80 | - define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) get_option( 'autoptimize_cache_nogzip' ) ); | |
| 95 | + define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) autoptimizeOptionWrapper::get_option( 'autoptimize_cache_nogzip' ) ); | |
| 81 | 96 | |
| 82 | 97 | // These can be overridden by specifying them in wp-config.php or such. |
| 83 | 98 | if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_NAME' ) ) { |
| 84 | 99 | define( 'AUTOPTIMIZE_WP_CONTENT_NAME', '/' . wp_basename( WP_CONTENT_DIR ) ); |
| @@ -103,9 +118,9 @@ | ||
| 103 | 118 | define( 'AUTOPTIMIZE_WP_SITE_URL', site_url() ); |
| 104 | 119 | } |
| 105 | 120 | } |
| 106 | 121 | if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_URL' ) ) { |
| 107 | - if ( function_exists( 'domain_mapping_siteurl' ) ) { | |
| 122 | + if ( function_exists( 'get_original_url' ) ) { | |
| 108 | 123 | define( 'AUTOPTIMIZE_WP_CONTENT_URL', str_replace( get_original_url( AUTOPTIMIZE_WP_SITE_URL ), AUTOPTIMIZE_WP_SITE_URL, content_url() ) ); |
| 109 | 124 | } else { |
| 110 | 125 | define( 'AUTOPTIMIZE_WP_CONTENT_URL', content_url() ); |
| 111 | 126 | } |
| @@ -131,9 +146,9 @@ | ||
| 131 | 146 | // Multibyte-capable string replacements are available with a filter. |
| 132 | 147 | // Also requires 'mbstring' extension. |
| 133 | 148 | $with_mbstring = apply_filters( 'autoptimize_filter_main_use_mbstring', false ); |
| 134 | 149 | if ( $with_mbstring ) { |
| 135 | - autoptimizeUtils::mbstring_available( \extensions_loaded( 'mbstring' ) ); | |
| 150 | + autoptimizeUtils::mbstring_available( \extension_loaded( 'mbstring' ) ); | |
| 136 | 151 | } else { |
| 137 | 152 | autoptimizeUtils::mbstring_available( false ); |
| 138 | 153 | } |
| 139 | 154 | |
| @@ -153,26 +168,37 @@ | ||
| 153 | 168 | public function check_cache_and_run() |
| 154 | 169 | { |
| 155 | 170 | if ( autoptimizeCache::cacheavail() ) { |
| 156 | 171 | $conf = autoptimizeConfig::instance(); |
| 157 | - if ( $conf->get( 'autoptimize_html' ) || $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) ) { | |
| 158 | - // Hook into WordPress frontend. | |
| 159 | - if ( defined( 'AUTOPTIMIZE_INIT_EARLIER' ) ) { | |
| 160 | - add_action( | |
| 161 | - 'init', | |
| 162 | - array( $this, 'start_buffering' ), | |
| 163 | - self::INIT_EARLIER_PRIORITY | |
| 164 | - ); | |
| 165 | - } else { | |
| 166 | - if ( ! defined( 'AUTOPTIMIZE_HOOK_INTO' ) ) { | |
| 167 | - define( 'AUTOPTIMIZE_HOOK_INTO', 'template_redirect' ); | |
| 172 | + if ( $conf->get( 'autoptimize_html' ) || $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) || autoptimizeImages::imgopt_active() || autoptimizeImages::should_lazyload_wrapper() ) { | |
| 173 | + if ( ! defined( 'AUTOPTIMIZE_NOBUFFER_OPTIMIZE' ) ) { | |
| 174 | + // Hook into WordPress frontend. | |
| 175 | + if ( defined( 'AUTOPTIMIZE_INIT_EARLIER' ) ) { | |
| 176 | + add_action( | |
| 177 | + 'init', | |
| 178 | + array( $this, 'start_buffering' ), | |
| 179 | + self::INIT_EARLIER_PRIORITY | |
| 180 | + ); | |
| 181 | + } else { | |
| 182 | + if ( ! defined( 'AUTOPTIMIZE_HOOK_INTO' ) ) { | |
| 183 | + define( 'AUTOPTIMIZE_HOOK_INTO', 'template_redirect' ); | |
| 184 | + } | |
| 185 | + add_action( | |
| 186 | + constant( 'AUTOPTIMIZE_HOOK_INTO' ), | |
| 187 | + array( $this, 'start_buffering' ), | |
| 188 | + self::DEFAULT_HOOK_PRIORITY | |
| 189 | + ); | |
| 168 | 190 | } |
| 169 | - add_action( | |
| 170 | - constant( 'AUTOPTIMIZE_HOOK_INTO' ), | |
| 171 | - array( $this, 'start_buffering' ), | |
| 172 | - self::DEFAULT_HOOK_PRIORITY | |
| 173 | - ); | |
| 174 | 191 | } |
| 192 | + | |
| 193 | + // And disable Jetpack's site accelerator if JS or CSS opt. are active. | |
| 194 | + if ( class_exists( 'Jetpack' ) && apply_filters( 'autoptimize_filter_main_disable_jetpack_cdn', true ) && ( $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) ) ) { | |
| 195 | + add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' ); | |
| 196 | + } | |
| 197 | + | |
| 198 | + // Add "no cache found" notice. | |
| 199 | + add_action( 'admin_notices', 'autoptimizeMain::notice_nopagecache', 99 ); | |
| 200 | + add_action( 'admin_notices', 'autoptimizeMain::notice_potential_conflict', 99 ); | |
| 175 | 201 | } |
| 176 | 202 | } else { |
| 177 | 203 | add_action( 'admin_notices', 'autoptimizeMain::notice_cache_unavailable' ); |
| 178 | 204 | } |
| @@ -180,8 +206,10 @@ | ||
| 180 | 206 | |
| 181 | 207 | public function maybe_run_ao_extra() |
| 182 | 208 | { |
| 183 | 209 | if ( apply_filters( 'autoptimize_filter_extra_activate', true ) ) { |
| 210 | + $ao_imgopt = new autoptimizeImages(); | |
| 211 | + $ao_imgopt->run(); | |
| 184 | 212 | $ao_extra = new autoptimizeExtra(); |
| 185 | 213 | $ao_extra->run(); |
| 186 | 214 | |
| 187 | 215 | // And show the imgopt notice. |
| @@ -188,16 +216,60 @@ | ||
| 188 | 216 | add_action( 'admin_notices', 'autoptimizeMain::notice_plug_imgopt' ); |
| 189 | 217 | } |
| 190 | 218 | } |
| 191 | 219 | |
| 192 | - public function maybe_run_partners_tab() | |
| 220 | + public function maybe_run_admin_only_trinkets() | |
| 193 | 221 | { |
| 194 | - // Loads partners tab code if in admin (and not in admin-ajax.php)! | |
| 222 | + // Loads partners tab and exit survey code if in admin (and not in admin-ajax.php)! | |
| 195 | 223 | if ( autoptimizeConfig::is_admin_and_not_ajax() ) { |
| 196 | 224 | new autoptimizePartners(); |
| 225 | + new autoptimizeExitSurvey(); | |
| 197 | 226 | } |
| 198 | 227 | } |
| 199 | 228 | |
| 229 | + public function criticalcss() | |
| 230 | + { | |
| 231 | + if ( apply_filters( 'autoptimize_filter_criticalcss_active', true ) && ! autoptimizeUtils::is_plugin_active( 'autoptimize-criticalcss/ao_criticss_aas.php' ) ) { | |
| 232 | + return $this->_criticalcss; | |
| 233 | + } else { | |
| 234 | + return false; | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + public function maybe_run_criticalcss() | |
| 239 | + { | |
| 240 | + // Loads criticalcss if the filter returns true & old power-up is not active. | |
| 241 | + if ( apply_filters( 'autoptimize_filter_criticalcss_active', true ) && ! autoptimizeUtils::is_plugin_active( 'autoptimize-criticalcss/ao_criticss_aas.php' ) ) { | |
| 242 | + $this->_criticalcss = new autoptimizeCriticalCSSBase(); | |
| 243 | + $this->_criticalcss->setup(); | |
| 244 | + $this->_criticalcss->load_requires(); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + public function maybe_run_notfound_fallback() | |
| 249 | + { | |
| 250 | + if ( autoptimizeCache::do_fallback() ) { | |
| 251 | + add_action( 'template_redirect', array( 'autoptimizeCache', 'wordpress_notfound_fallback' ) ); | |
| 252 | + } | |
| 253 | + } | |
| 254 | + | |
| 255 | + public function maybe_run_ao_compat() | |
| 256 | + { | |
| 257 | + // Condtionally loads the compatibility-class to ensure more out-of-the-box compatibility with big players. | |
| 258 | + $_run_compat = true; | |
| 259 | + | |
| 260 | + if ( autoptimizeOptionWrapper::get_option( 'autoptimize_installed_before_compatibility', false ) ) { | |
| 261 | + // If AO was already running before Compatibility logic was added, don't run compat by default | |
| 262 | + // because it can be assumed everything works and we want to avoid (perf) regressions that | |
| 263 | + // could occur due to compatibility code. | |
| 264 | + $_run_compat = false; | |
| 265 | + } | |
| 266 | + | |
| 267 | + if ( apply_filters( 'autoptimize_filter_init_compatibility', $_run_compat ) ) { | |
| 268 | + new autoptimizeCompatibility(); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 200 | 272 | public function hook_page_cache_purge() |
| 201 | 273 | { |
| 202 | 274 | // hook into a collection of page cache purge actions if filter allows. |
| 203 | 275 | if ( apply_filters( 'autoptimize_filter_main_hookpagecachepurge', true ) ) { |
| @@ -206,12 +278,15 @@ | ||
| 206 | 278 | 'hyper_cache_purged', // Stefano confirmed this will be added. |
| 207 | 279 | 'w3tc_flush_posts', // exits. |
| 208 | 280 | 'w3tc_flush_all', // exists. |
| 209 | 281 | 'ce_action_cache_cleared', // Sven confirmed this will be added. |
| 282 | + 'aoce_action_cache_cleared', // Some other cache enabler. | |
| 210 | 283 | 'comet_cache_wipe_cache', // still to be confirmed by Raam. |
| 211 | 284 | 'wp_cache_cleared', // cfr. https://github.com/Automattic/wp-super-cache/pull/537. |
| 212 | 285 | 'wpfc_delete_cache', // Emre confirmed this will be added this. |
| 213 | 286 | 'swift_performance_after_clear_all_cache', // swift perf. yeah! |
| 287 | + 'wpo_cache_flush', // wp-optimize. | |
| 288 | + 'rt_nginx_helper_after_fastcgi_purge_all', // nginx helper. | |
| 214 | 289 | ); |
| 215 | 290 | $page_cache_purge_actions = apply_filters( 'autoptimize_filter_main_pagecachepurgeactions', $page_cache_purge_actions ); |
| 216 | 291 | foreach ( $page_cache_purge_actions as $purge_action ) { |
| 217 | 292 | add_action( $purge_action, 'autoptimizeCache::clearall_actionless' ); |
| @@ -267,9 +342,9 @@ | ||
| 267 | 342 | * @param bool $doing_tests Allows overriding the optimization of only |
| 268 | 343 | * deciding once per request (for use in tests). |
| 269 | 344 | * @return bool |
| 270 | 345 | */ |
| 271 | - public function should_buffer( $doing_tests = false ) | |
| 346 | + public static function should_buffer( $doing_tests = false ) | |
| 272 | 347 | { |
| 273 | 348 | static $do_buffering = null; |
| 274 | 349 | |
| 275 | 350 | // Only check once in case we're called multiple times by others but |
| @@ -299,16 +374,16 @@ | ||
| 299 | 374 | } |
| 300 | 375 | } |
| 301 | 376 | |
| 302 | 377 | // If setting says not to optimize logged in user and user is logged in... |
| 303 | - if ( 'on' !== get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) { | |
| 378 | + if ( false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) { | |
| 304 | 379 | $ao_noptimize = true; |
| 305 | 380 | } |
| 306 | 381 | |
| 307 | 382 | // If setting says not to optimize cart/checkout. |
| 308 | - if ( 'on' !== get_option( 'autoptimize_optimize_checkout', 'on' ) ) { | |
| 383 | + if ( false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_optimize_checkout', 'off' ) ) { | |
| 309 | 384 | // Checking for woocommerce, easy digital downloads and wp ecommerce... |
| 310 | - foreach ( array( 'is_checkout', 'is_cart', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout' ) as $func ) { | |
| 385 | + foreach ( array( 'is_checkout', 'is_cart', 'is_account_page', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout' ) as $func ) { | |
| 311 | 386 | if ( function_exists( $func ) && $func() ) { |
| 312 | 387 | $ao_noptimize = true; |
| 313 | 388 | break; |
| 314 | 389 | } |
| @@ -314,9 +389,38 @@ | ||
| 314 | 389 | } |
| 315 | 390 | } |
| 316 | 391 | } |
| 317 | 392 | |
| 318 | - // Allows blocking of autoptimization on your own terms regardless of above decisions. | |
| 393 | + // Misc. querystring paramaters that will stop AO from doing optimizations (pagebuilders + | |
| 394 | + // 2 generic parameters that could/ should become standard between optimization plugins?). | |
| 395 | + if ( false === $ao_noptimize ) { | |
| 396 | + $_qs_showstoppers = array( 'no_cache', 'no_optimize', 'tve', 'elementor-preview', 'fl_builder', 'vc_action', 'et_fb', 'bt-beaverbuildertheme', 'ct_builder', 'fb-edit', 'siteorigin_panels_live_editor', 'preview', 'td_action' ); | |
| 397 | + | |
| 398 | + // doing Jonathan a quick favor to allow correct unused CSS generation ;-) . | |
| 399 | + if ( apply_filters( 'autoptimize_filter_main_showstoppers_do_wp_rocket_a_favor', true ) ) { | |
| 400 | + $_qs_showstoppers[] = 'nowprocket'; | |
| 401 | + } | |
| 402 | + | |
| 403 | + foreach ( $_qs_showstoppers as $_showstopper ) { | |
| 404 | + if ( array_key_exists( $_showstopper, $_GET ) ) { | |
| 405 | + $ao_noptimize = true; | |
| 406 | + break; | |
| 407 | + } | |
| 408 | + } | |
| 409 | + } | |
| 410 | + | |
| 411 | + // Also honor PageSpeed=off parameter as used by mod_pagespeed, in use by some pagebuilders, | |
| 412 | + // see https://www.modpagespeed.com/doc/experiment#ModPagespeed for info on that. | |
| 413 | + if ( false === $ao_noptimize && array_key_exists( 'PageSpeed', $_GET ) && 'off' === $_GET['PageSpeed'] ) { | |
| 414 | + $ao_noptimize = true; | |
| 415 | + } | |
| 416 | + | |
| 417 | + // If page/ post check post_meta to see if optimize is off. | |
| 418 | + if ( false === autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_optimize' ) ) { | |
| 419 | + $ao_noptimize = true; | |
| 420 | + } | |
| 421 | + | |
| 422 | + // And finally allows blocking of autoptimization on your own terms regardless of above decisions. | |
| 319 | 423 | $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); |
| 320 | 424 | |
| 321 | 425 | // Check for site being previewed in the Customizer (available since WP 4.0). |
| 322 | 426 | $is_customize_preview = false; |
| @@ -330,9 +434,9 @@ | ||
| 330 | 434 | * NOTE: Tests throw a notice here due to is_feed() being called |
| 331 | 435 | * while the main query hasn't been ran yet. Thats why we use |
| 332 | 436 | * AUTOPTIMIZE_INIT_EARLIER in tests. |
| 333 | 437 | */ |
| 334 | - $do_buffering = ( ! is_admin() && ! is_feed() && ! $ao_noptimize && ! $is_customize_preview ); | |
| 438 | + $do_buffering = ( ! is_admin() && ! is_feed() && ! is_embed() && ! $ao_noptimize && ! $is_customize_preview ); | |
| 335 | 439 | } |
| 336 | 440 | |
| 337 | 441 | return $do_buffering; |
| 338 | 442 | } |
| @@ -349,10 +453,11 @@ | ||
| 349 | 453 | // Defaults to true. |
| 350 | 454 | $valid = true; |
| 351 | 455 | |
| 352 | 456 | $has_no_html_tag = ( false === stripos( $content, '<html' ) ); |
| 353 | - $has_xsl_stylesheet = ( false !== stripos( $content, '<xsl:stylesheet' ) ); | |
| 354 | - $has_html5_doctype = ( preg_match( '/^<!DOCTYPE.+html>/i', $content ) > 0 ); | |
| 457 | + $has_xsl_stylesheet = ( false !== stripos( $content, '<xsl:stylesheet' ) || false !== stripos( $content, '<?xml-stylesheet' ) ); | |
| 458 | + $has_html5_doctype = ( preg_match( '/^<!DOCTYPE.+html>/i', ltrim( $content ) ) > 0 ); | |
| 459 | + $has_noptimize_page = ( false !== stripos( $content, '<!-- noptimize-page -->' ) ); | |
| 355 | 460 | |
| 356 | 461 | if ( $has_no_html_tag ) { |
| 357 | 462 | // Can't be valid amp markup without an html tag preceding it. |
| 358 | 463 | $is_amp_markup = false; |
| @@ -360,9 +465,9 @@ | ||
| 360 | 465 | $is_amp_markup = self::is_amp_markup( $content ); |
| 361 | 466 | } |
| 362 | 467 | |
| 363 | 468 | // If it's not html, or if it's amp or contains xsl stylesheets we don't touch it. |
| 364 | - if ( $has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet ) { | |
| 469 | + if ( $has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet || $has_noptimize_page ) { | |
| 365 | 470 | $valid = false; |
| 366 | 471 | } |
| 367 | 472 | |
| 368 | 473 | return $valid; |
| @@ -377,11 +482,31 @@ | ||
| 377 | 482 | * @return bool |
| 378 | 483 | */ |
| 379 | 484 | public static function is_amp_markup( $content ) |
| 380 | 485 | { |
| 381 | - $is_amp_markup = preg_match( '/<html[^>]*(?:amp|âš¡)/i', $content ); | |
| 486 | + // Short-circuit if the page is already AMP from the start. | |
| 487 | + if ( | |
| 488 | + preg_match( | |
| 489 | + sprintf( | |
| 490 | + '#^(?:<!.*?>|\s+)*+<html(?=\s)[^>]*?\s(%1$s|%2$s|%3$s)(\s|=|>)#is', | |
| 491 | + 'amp', | |
| 492 | + "\xE2\x9A\xA1", // From \AmpProject\Attribute::AMP_EMOJI. | |
| 493 | + "\xE2\x9A\xA1\xEF\xB8\x8F" // From \AmpProject\Attribute::AMP_EMOJI_ALT, per https://github.com/ampproject/amphtml/issues/25990. | |
| 494 | + ), | |
| 495 | + $content | |
| 496 | + ) | |
| 497 | + ) { | |
| 498 | + return true; | |
| 499 | + } | |
| 382 | 500 | |
| 383 | - return (bool) $is_amp_markup; | |
| 501 | + // Or else short-circuit if the AMP plugin will be processing the output to be an AMP page. | |
| 502 | + if ( function_exists( 'amp_is_request' ) ) { | |
| 503 | + return amp_is_request(); // For AMP plugin v2.0+. | |
| 504 | + } elseif ( function_exists( 'is_amp_endpoint' ) ) { | |
| 505 | + return is_amp_endpoint(); // For older/other AMP plugins (still supported in 2.0 as an alias). | |
| 506 | + } | |
| 507 | + | |
| 508 | + return false; | |
| 384 | 509 | } |
| 385 | 510 | |
| 386 | 511 | /** |
| 387 | 512 | * Processes/optimizes the output-buffered content and returns it. |
| @@ -413,30 +538,35 @@ | ||
| 413 | 538 | } |
| 414 | 539 | |
| 415 | 540 | $classoptions = array( |
| 416 | 541 | 'autoptimizeScripts' => array( |
| 417 | - 'aggregate' => $conf->get( 'autoptimize_js_aggregate' ), | |
| 418 | - 'justhead' => $conf->get( 'autoptimize_js_justhead' ), | |
| 419 | - 'forcehead' => $conf->get( 'autoptimize_js_forcehead' ), | |
| 420 | - 'trycatch' => $conf->get( 'autoptimize_js_trycatch' ), | |
| 421 | - 'js_exclude' => $conf->get( 'autoptimize_js_exclude' ), | |
| 422 | - 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), | |
| 423 | - 'include_inline' => $conf->get( 'autoptimize_js_include_inline' ), | |
| 542 | + 'aggregate' => $conf->get( 'autoptimize_js_aggregate' ), | |
| 543 | + 'defer_not_aggregate' => $conf->get( 'autoptimize_js_defer_not_aggregate' ), | |
| 544 | + 'defer_inline' => $conf->get( 'autoptimize_js_defer_inline' ), | |
| 545 | + 'justhead' => $conf->get( 'autoptimize_js_justhead' ), | |
| 546 | + 'forcehead' => $conf->get( 'autoptimize_js_forcehead' ), | |
| 547 | + 'trycatch' => $conf->get( 'autoptimize_js_trycatch' ), | |
| 548 | + 'js_exclude' => $conf->get( 'autoptimize_js_exclude' ), | |
| 549 | + 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), | |
| 550 | + 'include_inline' => $conf->get( 'autoptimize_js_include_inline' ), | |
| 551 | + 'minify_excluded' => $conf->get( 'autoptimize_minify_excluded' ), | |
| 424 | 552 | ), |
| 425 | 553 | 'autoptimizeStyles' => array( |
| 426 | - 'aggregate' => $conf->get( 'autoptimize_css_aggregate' ), | |
| 427 | - 'justhead' => $conf->get( 'autoptimize_css_justhead' ), | |
| 428 | - 'datauris' => $conf->get( 'autoptimize_css_datauris' ), | |
| 429 | - 'defer' => $conf->get( 'autoptimize_css_defer' ), | |
| 430 | - 'defer_inline' => $conf->get( 'autoptimize_css_defer_inline' ), | |
| 431 | - 'inline' => $conf->get( 'autoptimize_css_inline' ), | |
| 432 | - 'css_exclude' => $conf->get( 'autoptimize_css_exclude' ), | |
| 433 | - 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), | |
| 434 | - 'include_inline' => $conf->get( 'autoptimize_css_include_inline' ), | |
| 435 | - 'nogooglefont' => $conf->get( 'autoptimize_css_nogooglefont' ), | |
| 554 | + 'aggregate' => $conf->get( 'autoptimize_css_aggregate' ), | |
| 555 | + 'justhead' => $conf->get( 'autoptimize_css_justhead' ), | |
| 556 | + 'datauris' => $conf->get( 'autoptimize_css_datauris' ), | |
| 557 | + 'defer' => $conf->get( 'autoptimize_css_defer' ), | |
| 558 | + 'defer_inline' => $conf->get( 'autoptimize_css_defer_inline' ), | |
| 559 | + 'inline' => $conf->get( 'autoptimize_css_inline' ), | |
| 560 | + 'css_exclude' => $conf->get( 'autoptimize_css_exclude' ), | |
| 561 | + 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), | |
| 562 | + 'include_inline' => $conf->get( 'autoptimize_css_include_inline' ), | |
| 563 | + 'nogooglefont' => $conf->get( 'autoptimize_css_nogooglefont' ), | |
| 564 | + 'minify_excluded' => $conf->get( 'autoptimize_minify_excluded' ), | |
| 436 | 565 | ), |
| 437 | 566 | 'autoptimizeHTML' => array( |
| 438 | - 'keepcomments' => $conf->get( 'autoptimize_html_keepcomments' ), | |
| 567 | + 'keepcomments' => $conf->get( 'autoptimize_html_keepcomments' ), | |
| 568 | + 'minify_inline' => $conf->get( 'autoptimize_html_minify_inline' ), | |
| 439 | 569 | ), |
| 440 | 570 | ); |
| 441 | 571 | |
| 442 | 572 | $content = apply_filters( 'autoptimize_filter_html_before_minify', $content ); |
| @@ -456,12 +586,33 @@ | ||
| 456 | 586 | |
| 457 | 587 | return $content; |
| 458 | 588 | } |
| 459 | 589 | |
| 590 | + public static function autoptimize_nobuffer_optimize( $html_in ) { | |
| 591 | + $html_out = $html_in; | |
| 592 | + | |
| 593 | + if ( apply_filters( 'autoptimize_filter_speedupper', true ) ) { | |
| 594 | + $ao_speedupper = new autoptimizeSpeedupper(); | |
| 595 | + } | |
| 596 | + | |
| 597 | + $self = new self( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); | |
| 598 | + if ( $self->should_buffer() ) { | |
| 599 | + $html_out = $self->end_buffering( $html_in ); | |
| 600 | + } | |
| 601 | + return $html_out; | |
| 602 | + } | |
| 603 | + | |
| 460 | 604 | public static function on_uninstall() |
| 461 | 605 | { |
| 606 | + // clear the cache. | |
| 462 | 607 | autoptimizeCache::clearall(); |
| 463 | 608 | |
| 609 | + // remove postmeta if active. | |
| 610 | + if ( autoptimizeConfig::is_ao_meta_settings_active() ) { | |
| 611 | + delete_post_meta_by_key( 'ao_post_optimize' ); | |
| 612 | + } | |
| 613 | + | |
| 614 | + // remove all options. | |
| 464 | 615 | $delete_options = array( |
| 465 | 616 | 'autoptimize_cache_clean', |
| 466 | 617 | 'autoptimize_cache_nogzip', |
| 467 | 618 | 'autoptimize_css', |
| @@ -473,10 +624,15 @@ | ||
| 473 | 624 | 'autoptimize_css_inline', |
| 474 | 625 | 'autoptimize_css_exclude', |
| 475 | 626 | 'autoptimize_html', |
| 476 | 627 | 'autoptimize_html_keepcomments', |
| 628 | + 'autoptimize_html_minify_inline', | |
| 629 | + 'autoptimize_enable_site_config', | |
| 630 | + 'autoptimize_enable_meta_ao_settings', | |
| 477 | 631 | 'autoptimize_js', |
| 478 | 632 | 'autoptimize_js_aggregate', |
| 633 | + 'autoptimize_js_defer_not_aggregate', | |
| 634 | + 'autoptimize_js_defer_inline', | |
| 479 | 635 | 'autoptimize_js_exclude', |
| 480 | 636 | 'autoptimize_js_forcehead', |
| 481 | 637 | 'autoptimize_js_justhead', |
| 482 | 638 | 'autoptimize_js_trycatch', |
| @@ -491,8 +647,29 @@ | ||
| 491 | 647 | 'autoptimize_extra_settings', |
| 492 | 648 | 'autoptimize_service_availablity', |
| 493 | 649 | 'autoptimize_imgopt_provider_stat', |
| 494 | 650 | 'autoptimize_imgopt_launched', |
| 651 | + 'autoptimize_imgopt_settings', | |
| 652 | + 'autoptimize_minify_excluded', | |
| 653 | + 'autoptimize_cache_fallback', | |
| 654 | + 'autoptimize_ccss_rules', | |
| 655 | + 'autoptimize_ccss_additional', | |
| 656 | + 'autoptimize_ccss_queue', | |
| 657 | + 'autoptimize_ccss_viewport', | |
| 658 | + 'autoptimize_ccss_finclude', | |
| 659 | + 'autoptimize_ccss_rlimit', | |
| 660 | + 'autoptimize_ccss_rtimelimit', | |
| 661 | + 'autoptimize_ccss_noptimize', | |
| 662 | + 'autoptimize_ccss_debug', | |
| 663 | + 'autoptimize_ccss_key', | |
| 664 | + 'autoptimize_ccss_keyst', | |
| 665 | + 'autoptimize_ccss_version', | |
| 666 | + 'autoptimize_ccss_loggedin', | |
| 667 | + 'autoptimize_ccss_forcepath', | |
| 668 | + 'autoptimize_ccss_deferjquery', | |
| 669 | + 'autoptimize_ccss_domain', | |
| 670 | + 'autoptimize_ccss_unloadccss', | |
| 671 | + 'autoptimize_installed_before_compatibility', | |
| 495 | 672 | ); |
| 496 | 673 | |
| 497 | 674 | if ( ! is_multisite() ) { |
| 498 | 675 | foreach ( $delete_options as $del_opt ) { |
| @@ -497,8 +674,9 @@ | ||
| 497 | 674 | if ( ! is_multisite() ) { |
| 498 | 675 | foreach ( $delete_options as $del_opt ) { |
| 499 | 676 | delete_option( $del_opt ); |
| 500 | 677 | } |
| 678 | + autoptimizeMain::remove_cronjobs(); | |
| 501 | 679 | } else { |
| 502 | 680 | global $wpdb; |
| 503 | 681 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
| 504 | 682 | $original_blog_id = get_current_blog_id(); |
| @@ -506,17 +684,54 @@ | ||
| 506 | 684 | switch_to_blog( $blog_id ); |
| 507 | 685 | foreach ( $delete_options as $del_opt ) { |
| 508 | 686 | delete_option( $del_opt ); |
| 509 | 687 | } |
| 688 | + autoptimizeMain::remove_cronjobs(); | |
| 510 | 689 | } |
| 511 | 690 | switch_to_blog( $original_blog_id ); |
| 512 | 691 | } |
| 513 | 692 | |
| 514 | - if ( wp_get_schedule( 'ao_cachechecker' ) ) { | |
| 515 | - wp_clear_scheduled_hook( 'ao_cachechecker' ); | |
| 693 | + // Remove AO CCSS cached files and directory. | |
| 694 | + $ao_ccss_dir = WP_CONTENT_DIR . '/uploads/ao_ccss/'; | |
| 695 | + if ( file_exists( $ao_ccss_dir ) && is_dir( $ao_ccss_dir ) ) { | |
| 696 | + // fixme: should check for subdirs when in multisite and remove contents of those as well. | |
| 697 | + array_map( 'unlink', glob( $ao_ccss_dir . '*.{css,html,json,log,zip,lock}', GLOB_BRACE ) ); | |
| 698 | + rmdir( $ao_ccss_dir ); | |
| 516 | 699 | } |
| 700 | + | |
| 701 | + // Remove 404-handler (although that should have been removed in clearall already). | |
| 702 | + $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php'; | |
| 703 | + if ( file_exists( $_fallback_php ) ) { | |
| 704 | + unlink( $_fallback_php ); | |
| 705 | + } | |
| 517 | 706 | } |
| 518 | 707 | |
| 708 | + public static function on_deactivation() | |
| 709 | + { | |
| 710 | + if ( is_multisite() && is_network_admin() ) { | |
| 711 | + global $wpdb; | |
| 712 | + $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); | |
| 713 | + $original_blog_id = get_current_blog_id(); | |
| 714 | + foreach ( $blog_ids as $blog_id ) { | |
| 715 | + switch_to_blog( $blog_id ); | |
| 716 | + autoptimizeMain::remove_cronjobs(); | |
| 717 | + } | |
| 718 | + switch_to_blog( $original_blog_id ); | |
| 719 | + } else { | |
| 720 | + autoptimizeMain::remove_cronjobs(); | |
| 721 | + } | |
| 722 | + autoptimizeCache::clearall(); | |
| 723 | + } | |
| 724 | + | |
| 725 | + public static function remove_cronjobs() { | |
| 726 | + // Remove scheduled events. | |
| 727 | + foreach ( array( 'ao_cachechecker', 'ao_ccss_queue', 'ao_ccss_maintenance', 'ao_ccss_keychecker' ) as $_event ) { | |
| 728 | + if ( wp_get_schedule( $_event ) ) { | |
| 729 | + wp_clear_scheduled_hook( $_event ); | |
| 730 | + } | |
| 731 | + } | |
| 732 | + } | |
| 733 | + | |
| 519 | 734 | public static function notice_cache_unavailable() |
| 520 | 735 | { |
| 521 | 736 | echo '<div class="error"><p>'; |
| 522 | 737 | // Translators: %s is the cache directory location. |
| @@ -526,9 +741,9 @@ | ||
| 526 | 741 | |
| 527 | 742 | public static function notice_installed() |
| 528 | 743 | { |
| 529 | 744 | echo '<div class="updated"><p>'; |
| 530 | - _e( 'Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); | |
| 745 | + printf( __( 'Thank you for installing and activating Autoptimize. Please configure it under %1$sSettings -> Autoptimize%2$s to start improving your site\'s performance.', 'autoptimize' ), '<a href="options-general.php?page=autoptimize">', '</a>' ); | |
| 531 | 746 | echo '</p></div>'; |
| 532 | 747 | } |
| 533 | 748 | |
| 534 | 749 | public static function notice_updated() |
| @@ -540,23 +755,61 @@ | ||
| 540 | 755 | |
| 541 | 756 | public static function notice_plug_imgopt() |
| 542 | 757 | { |
| 543 | 758 | // Translators: the URL added points to the Autopmize Extra settings. |
| 544 | - $_ao_imgopt_plug_notice = sprintf( __( 'Did you know Autoptimize includes on-the-fly image optimization and CDN via ShortPixel? Check out the %1$sAutoptimize Extra settings%2$s to activate this option.', 'autoptimize' ), '<a href="options-general.php?page=autoptimize_extra">', '</a>' ); | |
| 759 | + $_ao_imgopt_plug_notice = sprintf( __( 'Did you know Autoptimize that Autoptimize offers on-the-fly image optimization (with support for WebP and AVIF) and CDN via ShortPixel? Check out the %1$sAutoptimize Image settings%2$s to enable this option.', 'autoptimize' ), '<a href="options-general.php?page=autoptimize_imgopt">', '</a>' ); | |
| 545 | 760 | $_ao_imgopt_plug_notice = apply_filters( 'autoptimize_filter_main_imgopt_plug_notice', $_ao_imgopt_plug_notice ); |
| 546 | - $_ao_imgopt_launch_ok = autoptimizeExtra::imgopt_launch_ok_wrapper(); | |
| 761 | + $_ao_imgopt_launch_ok = autoptimizeImages::launch_ok_wrapper(); | |
| 547 | 762 | $_ao_imgopt_plug_dismissible = 'ao-img-opt-plug-123'; |
| 763 | + $_ao_imgopt_active = autoptimizeImages::imgopt_active(); | |
| 764 | + $_is_ao_settings_page = autoptimizeUtils::is_ao_settings(); | |
| 548 | 765 | |
| 549 | - // check if AO is optimizing images already. | |
| 550 | - $_ao_imgopt_active = false; | |
| 551 | - $_ao_extra_options = get_option( 'autoptimize_extra_settings', '' ); | |
| 552 | - if ( is_array( $_ao_extra_options ) && array_key_exists( 'autoptimize_extra_checkbox_field_5', $_ao_extra_options ) && ! empty( $_ao_extra_options['autoptimize_extra_checkbox_field_5'] ) ) { | |
| 553 | - $_ao_imgopt_active = true; | |
| 554 | - } | |
| 555 | - | |
| 556 | - if ( '' !== $_ao_imgopt_plug_notice && ! $_ao_imgopt_active && $_ao_imgopt_launch_ok && PAnD::is_admin_notice_active( $_ao_imgopt_plug_dismissible ) ) { | |
| 766 | + if ( current_user_can( 'manage_options' ) && ! defined( 'AO_PRO_VERSION' ) && $_is_ao_settings_page && '' !== $_ao_imgopt_plug_notice && ! $_ao_imgopt_active && $_ao_imgopt_launch_ok && PAnD::is_admin_notice_active( $_ao_imgopt_plug_dismissible ) ) { | |
| 557 | 767 | echo '<div class="notice notice-info is-dismissible" data-dismissible="' . $_ao_imgopt_plug_dismissible . '"><p>'; |
| 558 | 768 | echo $_ao_imgopt_plug_notice; |
| 559 | 769 | echo '</p></div>'; |
| 770 | + } | |
| 771 | + } | |
| 772 | + | |
| 773 | + public static function notice_nopagecache() | |
| 774 | + { | |
| 775 | + /* | |
| 776 | + * Autoptimize does not do page caching (yet) but not everyone knows, so below logic tries to find out if page caching is available and if not show a notice on the AO Settings pages. | |
| 777 | + * | |
| 778 | + * uses helper function in autoptimizeUtils.php | |
| 779 | + */ | |
| 780 | + $_ao_nopagecache_notice = __( 'It looks like your site might not have <strong>page caching</strong> which is a <strong>must-have for performance</strong>. If you are sure you have a page cache, you can close this notice, but when in doubt check with your host if they offer this or install a page caching plugin like for example', 'autoptimize' ); | |
| 781 | + $_ao_pagecache_install_url = network_admin_url() . 'plugin-install.php?tab=search&type=term&s='; | |
| 782 | + $_ao_nopagecache_notice .= ' <a href="' . $_ao_pagecache_install_url . 'wp+super+cache' . '">WP Super Cache</a>, <a href="' . $_ao_pagecache_install_url . 'keycdn+cache+enabler' . '">KeyCDN Cache Enabler</a>, ...'; | |
| 783 | + $_ao_nopagecache_dismissible = 'ao-nopagecache-forever'; // the notice is only shown once and will not re-appear when dismissed. | |
| 784 | + $_is_ao_settings_page = autoptimizeUtils::is_ao_settings(); | |
| 785 | + | |
| 786 | + if ( current_user_can( 'manage_options' ) && $_is_ao_settings_page && PAnD::is_admin_notice_active( $_ao_nopagecache_dismissible ) && true === apply_filters( 'autoptimize_filter_main_show_pagecache_notice', true ) ) { | |
| 787 | + if ( false === autoptimizeUtils::find_pagecache() ) { | |
| 788 | + echo '<div class="notice notice-info is-dismissible" data-dismissible="' . $_ao_nopagecache_dismissible . '"><p>'; | |
| 789 | + echo $_ao_nopagecache_notice; | |
| 790 | + echo '</p></div>'; | |
| 791 | + } | |
| 792 | + } | |
| 793 | + } | |
| 794 | + | |
| 795 | + public static function notice_potential_conflict() | |
| 796 | + { | |
| 797 | + /* | |
| 798 | + * Using other plugins to do CSS/ JS optimization can cause unexpected and hard to troubleshoot issues, warn users who seem to be in that situation. | |
| 799 | + */ | |
| 800 | + // Translators: the sentence will be finished with the name of the offending plugin and a final stop. | |
| 801 | + $_ao_potential_conflict_notice = __( 'It looks like you have <strong>another plugin also doing CSS and/ or JS optimization</strong>, which can result in hard to troubleshoot <strong>conflicts</strong>. For this reason it is recommended to disable this functionality in', 'autoptimize' ) . ' '; | |
| 802 | + $_ao_potential_conflict_dismissible = 'ao-potential-conflict-forever'; // the notice is only shown once and will not re-appear when dismissed. | |
| 803 | + $_is_ao_settings_page = autoptimizeUtils::is_ao_settings(); | |
| 804 | + | |
| 805 | + if ( current_user_can( 'manage_options' ) && $_is_ao_settings_page && PAnD::is_admin_notice_active( $_ao_potential_conflict_dismissible ) && true === apply_filters( 'autoptimize_filter_main_show_potential_conclict_notice', true ) ) { | |
| 806 | + $_potential_conflicts = autoptimizeUtils::find_potential_conflicts(); | |
| 807 | + if ( false !== $_potential_conflicts ) { | |
| 808 | + $_ao_potential_conflict_notice .= '<strong>' . $_potential_conflicts . '</strong>.'; | |
| 809 | + echo '<div class="notice notice-info is-dismissible" data-dismissible="' . $_ao_potential_conflict_dismissible . '"><p>'; | |
| 810 | + echo $_ao_potential_conflict_notice; | |
| 811 | + echo '</p></div>'; | |
| 812 | + } | |
| 560 | 813 | } |
| 561 | 814 | } |
| 562 | 815 | } |