PluginProbe
Autoptimize / 3.1.7
Autoptimize v3.1.7
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.1.7, at classes/autoptimizeCompatibility.php

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