| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Ajax |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Yoast_Plugin_Conflict_Ajax. |
| 10 |
*/ |
| 11 |
class Yoast_Plugin_Conflict_Ajax { |
| 12 |
|
| 13 |
/** |
| 14 |
* Option identifier where dismissed conflicts are stored. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $option_name = 'wpseo_dismissed_conflicts'; |
| 19 |
|
| 20 |
/** |
| 21 |
* List of notification identifiers that have been dismissed. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $dismissed_conflicts = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize the hooks for the AJAX request. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
add_action( 'wp_ajax_wpseo_dismiss_plugin_conflict', [ $this, 'dismiss_notice' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Handles the dismiss notice request. |
| 36 |
*/ |
| 37 |
public function dismiss_notice() { |
| 38 |
check_ajax_referer( 'dismiss-plugin-conflict' ); |
| 39 |
|
| 40 |
$conflict_data = filter_input( INPUT_POST, 'data', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 41 |
|
| 42 |
$this->dismissed_conflicts = $this->get_dismissed_conflicts( $conflict_data['section'] ); |
| 43 |
|
| 44 |
$this->compare_plugins( $conflict_data['plugins'] ); |
| 45 |
|
| 46 |
$this->save_dismissed_conflicts( $conflict_data['section'] ); |
| 47 |
|
| 48 |
wp_die( 'true' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Getting the user option from the database. |
| 53 |
* |
| 54 |
* @return bool|array |
| 55 |
*/ |
| 56 |
private function get_dismissed_option() { |
| 57 |
return get_user_meta( get_current_user_id(), $this->option_name, true ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Getting the dismissed conflicts from the database |
| 62 |
* |
| 63 |
* @param string $plugin_section Type of conflict group (such as Open Graph or sitemap). |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
private function get_dismissed_conflicts( $plugin_section ) { |
| 68 |
$dismissed_conflicts = $this->get_dismissed_option(); |
| 69 |
|
| 70 |
if ( is_array( $dismissed_conflicts ) && array_key_exists( $plugin_section, $dismissed_conflicts ) ) { |
| 71 |
return $dismissed_conflicts[ $plugin_section ]; |
| 72 |
} |
| 73 |
|
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Storing the conflicting plugins as an user option in the database. |
| 79 |
* |
| 80 |
* @param string $plugin_section Plugin conflict type (such as Open Graph or sitemap). |
| 81 |
*/ |
| 82 |
private function save_dismissed_conflicts( $plugin_section ) { |
| 83 |
$dismissed_conflicts = $this->get_dismissed_option(); |
| 84 |
|
| 85 |
$dismissed_conflicts[ $plugin_section ] = $this->dismissed_conflicts; |
| 86 |
|
| 87 |
update_user_meta( get_current_user_id(), $this->option_name, $dismissed_conflicts ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Loop through the plugins to compare them with the already stored dismissed plugin conflicts. |
| 92 |
* |
| 93 |
* @param array $posted_plugins Plugin set to check. |
| 94 |
*/ |
| 95 |
public function compare_plugins( array $posted_plugins ) { |
| 96 |
foreach ( $posted_plugins as $posted_plugin ) { |
| 97 |
$this->compare_plugin( $posted_plugin ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if plugin is already dismissed, if not store it in the array that will be saved later. |
| 103 |
* |
| 104 |
* @param string $posted_plugin Plugin to check against dismissed conflicts. |
| 105 |
*/ |
| 106 |
private function compare_plugin( $posted_plugin ) { |
| 107 |
if ( ! in_array( $posted_plugin, $this->dismissed_conflicts, true ) ) { |
| 108 |
$this->dismissed_conflicts[] = $posted_plugin; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|