PluginProbe
Autoptimize / 3.0.0
Autoptimize v3.0.0
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeCompatibility.php

autoptimizeCompatibility.php in Autoptimize 3.0.0, at classes/autoptimizeCompatibility.php

118 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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