PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / ajax / class-yoast-plugin-conflict-ajax.php

class-yoast-plugin-conflict-ajax.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at admin/ajax/class-yoast-plugin-conflict-ajax.php

131 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return void
38 */
39 public function dismiss_notice() {
40 check_ajax_referer( 'dismiss-plugin-conflict' );
41
42 if ( ! isset( $_POST['data'] ) || ! is_array( $_POST['data'] ) ) {
43 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: WPSEO_Utils::format_json_encode is considered safe.
44 wp_die( WPSEO_Utils::format_json_encode( [] ) );
45 }
46
47 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: $conflict_data is getting sanitized later.
48 $conflict_data = wp_unslash( $_POST['data'] );
49
50 $conflict_data = [
51 'section' => sanitize_text_field( $conflict_data['section'] ),
52 'plugins' => sanitize_text_field( $conflict_data['plugins'] ),
53 ];
54
55 $this->dismissed_conflicts = $this->get_dismissed_conflicts( $conflict_data['section'] );
56
57 $this->compare_plugins( $conflict_data['plugins'] );
58
59 $this->save_dismissed_conflicts( $conflict_data['section'] );
60
61 wp_die( 'true' );
62 }
63
64 /**
65 * Getting the user option from the database.
66 *
67 * @return bool|array
68 */
69 private function get_dismissed_option() {
70 return get_user_meta( get_current_user_id(), $this->option_name, true );
71 }
72
73 /**
74 * Getting the dismissed conflicts from the database
75 *
76 * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap).
77 *
78 * @return array
79 */
80 private function get_dismissed_conflicts( $plugin_section ) {
81 $dismissed_conflicts = $this->get_dismissed_option();
82
83 if ( is_array( $dismissed_conflicts ) && array_key_exists( $plugin_section, $dismissed_conflicts ) ) {
84 return $dismissed_conflicts[ $plugin_section ];
85 }
86
87 return [];
88 }
89
90 /**
91 * Storing the conflicting plugins as an user option in the database.
92 *
93 * @param string $plugin_section Plugin conflict type (such as Open Graph or sitemap).
94 *
95 * @return void
96 */
97 private function save_dismissed_conflicts( $plugin_section ) {
98 $dismissed_conflicts = $this->get_dismissed_option();
99
100 $dismissed_conflicts[ $plugin_section ] = $this->dismissed_conflicts;
101
102 update_user_meta( get_current_user_id(), $this->option_name, $dismissed_conflicts );
103 }
104
105 /**
106 * Loop through the plugins to compare them with the already stored dismissed plugin conflicts.
107 *
108 * @param array $posted_plugins Plugin set to check.
109 *
110 * @return void
111 */
112 public function compare_plugins( array $posted_plugins ) {
113 foreach ( $posted_plugins as $posted_plugin ) {
114 $this->compare_plugin( $posted_plugin );
115 }
116 }
117
118 /**
119 * Check if plugin is already dismissed, if not store it in the array that will be saved later.
120 *
121 * @param string $posted_plugin Plugin to check against dismissed conflicts.
122 *
123 * @return void
124 */
125 private function compare_plugin( $posted_plugin ) {
126 if ( ! in_array( $posted_plugin, $this->dismissed_conflicts, true ) ) {
127 $this->dismissed_conflicts[] = $posted_plugin;
128 }
129 }
130 }
131