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