| 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 |
// 'plugin-slug' => 'plugin-folder/plugin-file.php' |
| 55 |
]; |
| 56 |
|
| 57 |
return apply_filters( 'optml_conflicting_defined_plugins', $plugins ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get a list of active conflicting plugins. |
| 62 |
* |
| 63 |
* @since 3.8.0 |
| 64 |
* @access private |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
private function get_active_plugins() { |
| 68 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 69 |
|
| 70 |
$conflicting_plugins = $this->defined_plugins(); |
| 71 |
$conflicting_plugins = array_filter( $conflicting_plugins, 'is_plugin_active' ); |
| 72 |
return apply_filters( 'optml_conflicting_active_plugins', $conflicting_plugins ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get a list of dismissed conflicting plugins. |
| 77 |
* |
| 78 |
* @since 3.8.0 |
| 79 |
* @access private |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
private function get_dismissed_notices() { |
| 83 |
$dismissed_conflicts = get_option( $this->option_main, '{}' ); |
| 84 |
$dismissed_conflicts = json_decode( $dismissed_conflicts, true ); |
| 85 |
|
| 86 |
if ( empty( $dismissed_conflicts ) ) { |
| 87 |
return []; |
| 88 |
} |
| 89 |
|
| 90 |
return $dismissed_conflicts; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get a list of undismissed conflicting plugins. |
| 95 |
* |
| 96 |
* @since 3.8.0 |
| 97 |
* @access private |
| 98 |
* @param boolean $show_dismissed Also show the dismissed plugins. |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function get_conflicting_plugins( $show_dismissed = false ) { |
| 102 |
$conflicting_plugins = $this->get_active_plugins(); |
| 103 |
|
| 104 |
if ( true === $show_dismissed ) { |
| 105 |
return $conflicting_plugins; |
| 106 |
} |
| 107 |
|
| 108 |
$dismissed_conflicts = $this->get_dismissed_notices(); |
| 109 |
|
| 110 |
$conflicting_plugins = array_diff_key( $conflicting_plugins, $dismissed_conflicts ); |
| 111 |
|
| 112 |
return $conflicting_plugins; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Checks if there are any conflicting plugins. |
| 117 |
* |
| 118 |
* @since 3.8.0 |
| 119 |
* @access public |
| 120 |
* @return boolean |
| 121 |
*/ |
| 122 |
public function has_conflicting_plugins() { |
| 123 |
$conflicting_plugins = $this->get_conflicting_plugins(); |
| 124 |
|
| 125 |
return ! empty( $conflicting_plugins ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Dismiss conflicting plugins. |
| 130 |
* |
| 131 |
* @since 3.8.0 |
| 132 |
* @access public |
| 133 |
*/ |
| 134 |
public function dismiss_conflicting_plugins() { |
| 135 |
$options = get_option( $this->option_main, '{}' ); |
| 136 |
$options = json_decode( $options, true ); |
| 137 |
|
| 138 |
if ( empty( $options ) ) { |
| 139 |
$options = []; |
| 140 |
} |
| 141 |
|
| 142 |
$conflicting_plugins = $this->get_conflicting_plugins(); |
| 143 |
|
| 144 |
foreach ( $conflicting_plugins as $slug => $file ) { |
| 145 |
$conflicting_plugins[ $slug ] = true; |
| 146 |
} |
| 147 |
|
| 148 |
$options = array_merge( $options, $conflicting_plugins ); |
| 149 |
|
| 150 |
update_option( $this->option_main, wp_json_encode( $options ) ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Check if we should show the notice. |
| 155 |
* |
| 156 |
* @since 3.8.0 |
| 157 |
* @access public |
| 158 |
* @return bool Should show? |
| 159 |
*/ |
| 160 |
public function should_show_notice() { |
| 161 |
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
if ( is_network_admin() ) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
$current_screen = get_current_screen(); |
| 174 |
|
| 175 |
if ( empty( $current_screen ) ) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
if ( ! $this->has_conflicting_plugins() ) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Filter conflicting plugins. |
| 188 |
* It will appear at wp-admin/plugins.php?optimole_conflicts |
| 189 |
* |
| 190 |
* @since 3.8.0 |
| 191 |
* @access public |
| 192 |
* @param array $plugins List of plugins. |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
public function filter_conflicting_plugins( $plugins ) { |
| 196 |
if ( ! isset( $_GET['optimole_conflicts'] ) ) { |
| 197 |
return $plugins; |
| 198 |
} |
| 199 |
|
| 200 |
$allowed_plugins = $this->get_conflicting_plugins( true ); |
| 201 |
|
| 202 |
$filtered_plugins = []; |
| 203 |
|
| 204 |
foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 205 |
if ( in_array( $plugin_file, $allowed_plugins, true ) ) { |
| 206 |
$filtered_plugins[ $plugin_file ] = $plugin_data; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return $filtered_plugins; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Update the option value using AJAX |
| 215 |
* |
| 216 |
* @since 3.8.0 |
| 217 |
* @access public |
| 218 |
*/ |
| 219 |
public function dismiss_notice() { |
| 220 |
if ( ! isset( $_POST['nonce'] ) ) { |
| 221 |
$response = [ |
| 222 |
'success' => false, |
| 223 |
'message' => 'Missing nonce or value.', |
| 224 |
]; |
| 225 |
wp_send_json( $response ); |
| 226 |
} |
| 227 |
|
| 228 |
$nonce = sanitize_text_field( $_POST['nonce'] ); |
| 229 |
|
| 230 |
if ( ! wp_verify_nonce( $nonce, 'optml_dismiss_conflict_notice' ) ) { |
| 231 |
$response = [ |
| 232 |
'success' => false, |
| 233 |
'message' => 'Invalid nonce.', |
| 234 |
]; |
| 235 |
wp_send_json( $response ); |
| 236 |
} |
| 237 |
|
| 238 |
$this->dismiss_conflicting_plugins(); |
| 239 |
|
| 240 |
$response = [ |
| 241 |
'success' => true, |
| 242 |
]; |
| 243 |
|
| 244 |
wp_send_json( $response ); |
| 245 |
} |
| 246 |
} |
| 247 |
|