PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.8
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.8
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / conflicts / conflicting_plugins.php

conflicting_plugins.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.8, at inc/conflicts/conflicting_plugins.php

251 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Conflicting Plugins class, documents and displays dashboard notice for conflicting plugins.
4 *
5 * @package \Optimole\Inc\Conflicts
6 * @author Hardeep Asrani <hardeep@optimole.com>
7 */
8
9 /**
10 * Class Optml_Conflicting_Plugins
11 *
12 * @since 3.8.0
13 */
14 class Optml_Conflicting_Plugins {
15
16 /**
17 * Option key.
18 *
19 * @since 3.8.0
20 * @access private
21 * @var string
22 */
23 private $option_main = 'optml_dismissed_plugin_conflicts';
24
25 /**
26 * Optml_Conflicting_Plugins constructor.
27 *
28 * @since 3.8.0
29 * @access public
30 */
31 public function __construct() {
32 add_action( 'wp_ajax_optml_dismiss_conflict_notice', [ $this, 'dismiss_notice' ] );
33 add_filter( 'all_plugins', [ $this, 'filter_conflicting_plugins' ] );
34 }
35
36 /**
37 * Define conflicting plugins.
38 *
39 * @since 3.8.0
40 * @access private
41 * @return array
42 */
43 private function defined_plugins() {
44 $plugins = [
45 'wp-smush' => 'wp-smushit/wp-smush.php',
46 'wp-smush-pro' => 'wp-smush-pro/wp-smush.php',
47 'kraken' => 'kraken-image-optimizer/kraken.php',
48 'tinypng' => 'tiny-compress-images/tiny-compress-images.php',
49 'shortpixel' => 'shortpixel-image-optimiser/wp-shortpixel.php',
50 'ewww' => 'ewww-image-optimizer/ewww-image-optimizer.php',
51 'ewww-cloud' => 'ewww-image-optimizer-cloud/ewww-image-optimizer-cloud.php',
52 'imagerecycle' => 'imagerecycle-pdf-image-compression/wp-image-recycle.php',
53 'imagify' => 'imagify/imagify.php',
54 'litespeed' => 'litespeed-cache/litespeed-cache.php',
55 'autoptimize' => 'autoptimize/autoptimize.php',
56 'perfmatters' => 'perfmatters/perfmatters.php',
57 'jetpack_Photon' => 'jetpack/jetpack.php',
58 // 'plugin-slug' => 'plugin-folder/plugin-file.php'
59 ];
60
61 return apply_filters( 'optml_conflicting_defined_plugins', $plugins );
62 }
63
64 /**
65 * Get a list of active conflicting plugins.
66 *
67 * @since 3.8.0
68 * @access private
69 * @return array
70 */
71 private function get_active_plugins() {
72 require_once ABSPATH . 'wp-admin/includes/plugin.php';
73
74 $conflicting_plugins = $this->defined_plugins();
75 $conflicting_plugins = array_filter( $conflicting_plugins, 'is_plugin_active' );
76 return apply_filters( 'optml_conflicting_active_plugins', $conflicting_plugins );
77 }
78
79 /**
80 * Get a list of dismissed conflicting plugins.
81 *
82 * @since 3.8.0
83 * @access private
84 * @return array
85 */
86 private function get_dismissed_notices() {
87 $dismissed_conflicts = get_option( $this->option_main, '{}' );
88 $dismissed_conflicts = json_decode( $dismissed_conflicts, true );
89
90 if ( empty( $dismissed_conflicts ) ) {
91 return [];
92 }
93
94 return $dismissed_conflicts;
95 }
96
97 /**
98 * Get a list of undismissed conflicting plugins.
99 *
100 * @since 3.8.0
101 * @access private
102 * @param boolean $show_dismissed Also show the dismissed plugins.
103 * @return array
104 */
105 public function get_conflicting_plugins( $show_dismissed = false ) {
106 $conflicting_plugins = $this->get_active_plugins();
107
108 if ( true === $show_dismissed ) {
109 return $conflicting_plugins;
110 }
111
112 $dismissed_conflicts = $this->get_dismissed_notices();
113
114 $conflicting_plugins = array_diff_key( $conflicting_plugins, $dismissed_conflicts );
115
116 return $conflicting_plugins;
117 }
118
119 /**
120 * Checks if there are any conflicting plugins.
121 *
122 * @since 3.8.0
123 * @access public
124 * @return boolean
125 */
126 public function has_conflicting_plugins() {
127 $conflicting_plugins = $this->get_conflicting_plugins();
128
129 return ! empty( $conflicting_plugins );
130 }
131
132 /**
133 * Dismiss conflicting plugins.
134 *
135 * @since 3.8.0
136 * @access public
137 */
138 public function dismiss_conflicting_plugins() {
139 $options = get_option( $this->option_main, '{}' );
140 $options = json_decode( $options, true );
141
142 if ( empty( $options ) ) {
143 $options = [];
144 }
145
146 $conflicting_plugins = $this->get_conflicting_plugins();
147
148 foreach ( $conflicting_plugins as $slug => $file ) {
149 $conflicting_plugins[ $slug ] = true;
150 }
151
152 $options = array_merge( $options, $conflicting_plugins );
153
154 update_option( $this->option_main, wp_json_encode( $options ) );
155 }
156
157 /**
158 * Check if we should show the notice.
159 *
160 * @since 3.8.0
161 * @access public
162 * @return bool Should show?
163 */
164 public function should_show_notice() {
165 if ( wp_doing_ajax() ) {
166 return false;
167 }
168
169 if ( is_network_admin() ) {
170 return false;
171 }
172
173 if ( ! current_user_can( 'manage_options' ) ) {
174 return false;
175 }
176
177 $current_screen = get_current_screen();
178
179 if ( empty( $current_screen ) ) {
180 return false;
181 }
182
183 if ( ! $this->has_conflicting_plugins() ) {
184 return false;
185 }
186
187 return true;
188 }
189
190 /**
191 * Filter conflicting plugins.
192 * It will appear at wp-admin/plugins.php?optimole_conflicts
193 *
194 * @since 3.8.0
195 * @access public
196 * @param array $plugins List of plugins.
197 * @return array
198 */
199 public function filter_conflicting_plugins( $plugins ) {
200 if ( ! isset( $_GET['optimole_conflicts'] ) ) {
201 return $plugins;
202 }
203
204 $allowed_plugins = $this->get_conflicting_plugins( true );
205
206 $filtered_plugins = [];
207
208 foreach ( $plugins as $plugin_file => $plugin_data ) {
209 if ( in_array( $plugin_file, $allowed_plugins, true ) ) {
210 $filtered_plugins[ $plugin_file ] = $plugin_data;
211 }
212 }
213
214 return $filtered_plugins;
215 }
216
217 /**
218 * Update the option value using AJAX
219 *
220 * @since 3.8.0
221 * @access public
222 */
223 public function dismiss_notice() {
224 if ( ! isset( $_POST['nonce'] ) ) {
225 $response = [
226 'success' => false,
227 'message' => 'Missing nonce or value.',
228 ];
229 wp_send_json( $response );
230 }
231
232 $nonce = sanitize_text_field( $_POST['nonce'] );
233
234 if ( ! wp_verify_nonce( $nonce, 'optml_dismiss_conflict_notice' ) ) {
235 $response = [
236 'success' => false,
237 'message' => 'Invalid nonce.',
238 ];
239 wp_send_json( $response );
240 }
241
242 $this->dismiss_conflicting_plugins();
243
244 $response = [
245 'success' => true,
246 ];
247
248 wp_send_json( $response );
249 }
250 }
251