PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.12
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.12
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.12, at inc/conflicts/conflicting_plugins.php

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