PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.6
28.6 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 All 130 releases
wordpress-seo / admin / ajax / class-yoast-dismissable-notice.php

class-yoast-dismissable-notice.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.6, at admin/ajax/class-yoast-dismissable-notice.php

96 lines 2.0 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 * This class will catch the request to dismiss the target notice (set by notice_name)
10 * and will store the dismiss status as an user meta in the database.
11 */
12 class Yoast_Dismissable_Notice_Ajax {
13
14 /**
15 * Notice type toggle value for user notices.
16 *
17 * @var string
18 */
19 public const FOR_USER = 'user_meta';
20
21 /**
22 * Notice type toggle value for network notices.
23 *
24 * @var string
25 */
26 public const FOR_NETWORK = 'site_option';
27
28 /**
29 * Notice type toggle value for site notices.
30 *
31 * @var string
32 */
33 public const FOR_SITE = 'option';
34
35 /**
36 * Name of the notice that will be dismissed.
37 *
38 * @var string
39 */
40 private $notice_name;
41
42 /**
43 * The type of the current notice.
44 *
45 * @var string
46 */
47 private $notice_type;
48
49 /**
50 * Initialize the hooks for the AJAX request.
51 *
52 * @param string $notice_name The name for the hook to catch the notice.
53 * @param string $notice_type The notice type.
54 */
55 public function __construct( $notice_name, $notice_type = self::FOR_USER ) {
56 $this->notice_name = $notice_name;
57 $this->notice_type = $notice_type;
58
59 add_action( 'wp_ajax_wpseo_dismiss_' . $notice_name, [ $this, 'dismiss_notice' ] );
60 }
61
62 /**
63 * Handles the dismiss notice request.
64 *
65 * @return void
66 */
67 public function dismiss_notice() {
68 check_ajax_referer( 'wpseo-dismiss-' . $this->notice_name );
69
70 $this->save_dismissed();
71
72 wp_die( 'true' );
73 }
74
75 /**
76 * Storing the dismissed value in the database. The target location is based on the set notification type.
77 *
78 * @return void
79 */
80 private function save_dismissed() {
81 if ( $this->notice_type === self::FOR_SITE ) {
82 update_option( 'wpseo_dismiss_' . $this->notice_name, 1 );
83
84 return;
85 }
86
87 if ( $this->notice_type === self::FOR_NETWORK ) {
88 update_site_option( 'wpseo_dismiss_' . $this->notice_name, 1 );
89
90 return;
91 }
92
93 update_user_meta( get_current_user_id(), 'wpseo_dismiss_' . $this->notice_name, 1 );
94 }
95 }
96