| 1 |
<?php |
| 2 |
/** |
| 3 |
* Multiple compatibility snippets to ensure important/ stubborn plugins work out of the box. |
| 4 |
* |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class autoptimizeCompatibility |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Constructor. |
| 15 |
* |
| 16 |
*/ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
if ( ! is_admin() && ! defined( 'DOING_CRON' ) ) { |
| 20 |
$this->conf = autoptimizeConfig::instance(); |
| 21 |
$this->run(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Runs multiple compatibility snippets to ensure important plugins work out of the box. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public function run() |
| 30 |
{ |
| 31 |
// Edit with Elementor in frontend admin menu (so for editors/ administrators) needs JS opt. disabled to appear & function. |
| 32 |
if ( defined( 'ELEMENTOR_VERSION' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) && apply_filters( 'autoptimize_filter_compatibility_editelementor_active', true ) ) { |
| 33 |
add_filter( 'autoptimize_filter_js_noptimize', '__return_true' ); |
| 34 |
} |
| 35 |
|
| 36 |
// Revslider; jQuery should not be deferred + exclude all revslider JS. |
| 37 |
if ( defined( 'RS_REVISION' ) && $this->conf->get( 'autoptimize_js' ) && true == $this->inline_js_config_checker() && apply_filters( 'autoptimize_filter_compatibility_revslider_active', true ) ) { |
| 38 |
add_filter( 'autoptimize_filter_js_exclude', function( $js_excl = '', $html = '' ) { |
| 39 |
$revslider_excl = 'revslider, setREVStartSize, window.RSIW, window.RS_MODULES, jquery.min.js'; |
| 40 |
if ( ! empty( $html ) && false !== strpos( $html, '<rs-slides>' ) ) { |
| 41 |
if ( is_array( $js_excl ) ) { |
| 42 |
$js_excl = implode( ',', $js_excl ); |
| 43 |
} |
| 44 |
|
| 45 |
$js_excl .= ',' . $revslider_excl; |
| 46 |
} |
| 47 |
return $js_excl; |
| 48 |
}, 11, 2 ); |
| 49 |
} |
| 50 |
|
| 51 |
// Revslider; remove revslider JS if no slides in HTML for non-logged in users. |
| 52 |
if ( defined( 'RS_REVISION' ) && $this->conf->get( 'autoptimize_js' ) && false === is_user_logged_in() && apply_filters( 'autoptimize_filter_compatibility_revslider_remover_active', true ) ) { |
| 53 |
add_filter( 'autoptimize_filter_js_removables', function( $to_remove = '', $html = '' ) { |
| 54 |
if ( ! empty( $html ) && false === strpos( $html, '<rs-slides>') ) { |
| 55 |
$to_remove .= 'plugins/revslider, setREVStartSize, window.RSIW, window.RS_MODULES'; |
| 56 |
} |
| 57 |
|
| 58 |
return $to_remove; |
| 59 |
}, 11, 2 ); |
| 60 |
} |
| 61 |
|
| 62 |
// Exclude jQuery if inline JS is found that requires jQuery. |
| 63 |
if ( $this->inline_js_config_checker() && false === strpos( $this->conf->get( 'autoptimize_js_exclude' ), 'jquery.min.js' ) && apply_filters( 'autoptimize_filter_compatibility_inline_jquery', true ) ) { |
| 64 |
add_filter( 'autoptimize_filter_js_exclude', function( $js_excl = '', $html = '' ) { |
| 65 |
if ( ! empty( $html ) && preg_match( '/<script[^>]*>[^<]*(jQuery|\$)\([^<]*<\/script>/Usm', $html ) ) { |
| 66 |
if ( is_array( $js_excl ) ) { |
| 67 |
$js_excl = implode( ',', $js_excl ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( false === strpos( $js_excl, 'jquery.min.js' ) ) { |
| 71 |
$js_excl .= ', jquery.min.js'; |
| 72 |
} |
| 73 |
} |
| 74 |
return $js_excl; |
| 75 |
}, 12, 2 ); |
| 76 |
} |
| 77 |
|
| 78 |
// Make JS-based Gutenberg blocks work OOTB. |
| 79 |
if ( $this->inline_js_config_checker() && apply_filters( 'autoptimize_filter_compatibility_gutenberg_js', true ) ) { |
| 80 |
add_filter( 'autoptimize_filter_js_exclude', function( $js_excl = '', $html = '' ) { |
| 81 |
if ( ! empty( $html ) && false !== strpos( $html, 'wp.i18n' ) || false !== strpos( $html, 'wp.apiFetch' ) || false !== strpos( $html, 'window.lodash' ) ) { |
| 82 |
if ( is_array( $js_excl ) ) { |
| 83 |
$js_excl = implode( ',', $js_excl ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( false === strpos( $js_excl, 'jquery.min.js' ) ) { |
| 87 |
$js_excl .= ', jquery.min.js'; |
| 88 |
} |
| 89 |
|
| 90 |
if ( false === strpos( $js_excl, 'wp-includes/js/dist' ) ) { |
| 91 |
$js_excl .= ', wp-includes/js/dist'; |
| 92 |
} |
| 93 |
} |
| 94 |
return $js_excl; |
| 95 |
}, 13, 2 ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function inline_js_config_checker() { |
| 100 |
static $inline_js_flagged = null; |
| 101 |
|
| 102 |
if ( null === $inline_js_flagged ) { |
| 103 |
if ( ( $this->conf->get( 'autoptimize_js_aggregate' ) || apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) && apply_filters( 'autoptimize_js_include_inline', $this->conf->get( 'autoptimize_js_include_inline' ) ) ) { |
| 104 |
// if all files and also inline JS are aggregated we don't have to worry about inline JS. |
| 105 |
$inline_js_flagged = false; |
| 106 |
} else if ( apply_filters( 'autoptimize_filter_js_defer_not_aggregate', $this->conf->get( 'autoptimize_js_defer_not_aggregate' ) ) && apply_filters( 'autoptimize_js_filter_defer_inline', $this->conf->get( 'autoptimize_js_defer_inline' ) ) ) { |
| 107 |
// and when not aggregating but deferring all including inline JS, then all is OK too. |
| 108 |
$inline_js_flagged = false; |
| 109 |
} |
| 110 |
|
| 111 |
// in all other cases we need to pay attention to inline JS requiring src'ed JS to be available. |
| 112 |
$inline_js_flagged = true; |
| 113 |
} |
| 114 |
|
| 115 |
return $inline_js_flagged; |
| 116 |
} |
| 117 |
} |
| 118 |
|