class-yoast-plugin-conflict-ajax.php 2.8 KB

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