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 / src / alerts / application / ping-other-admins / ping-other-admins-alert.php

ping-other-admins-alert.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.6, at src/alerts/application/ping-other-admins/ping-other-admins-alert.php

184 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\Alerts\Application\Ping_Other_Admins;
5
6 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7 use Yoast\WP\SEO\Helpers\Options_Helper;
8 use Yoast\WP\SEO\Helpers\Product_Helper;
9 use Yoast\WP\SEO\Helpers\Short_Link_Helper;
10 use Yoast\WP\SEO\Helpers\User_Helper;
11 use Yoast\WP\SEO\Integrations\Integration_Interface;
12 use Yoast_Notification;
13 use Yoast_Notification_Center;
14
15 /**
16 * Ping_Other_Admins_Alert class.
17 */
18 class Ping_Other_Admins_Alert implements Integration_Interface {
19
20 public const NOTIFICATION_ID = 'wpseo-ping-other-admins';
21
22 /**
23 * The notifications center.
24 *
25 * @var Yoast_Notification_Center
26 */
27 private $notification_center;
28
29 /**
30 * The short link helper.
31 *
32 * @var Short_Link_Helper
33 */
34 private $short_link_helper;
35
36 /**
37 * The product helper.
38 *
39 * @var Product_Helper
40 */
41 private $product_helper;
42
43 /**
44 * The options helper.
45 *
46 * @var Options_Helper
47 */
48 private $options_helper;
49
50 /**
51 * The user helper.
52 *
53 * @var User_Helper
54 */
55 private $user_helper;
56
57 /**
58 * Ping_Other_Admins_Alert constructor.
59 *
60 * @param Yoast_Notification_Center $notification_center The notification center.
61 * @param Short_Link_Helper $short_link_helper The short link helper.
62 * @param Product_Helper $product_helper The product helper.
63 * @param Options_Helper $options_helper The options helper.
64 * @param User_Helper $user_helper The user helper.
65 */
66 public function __construct(
67 Yoast_Notification_Center $notification_center,
68 Short_Link_Helper $short_link_helper,
69 Product_Helper $product_helper,
70 Options_Helper $options_helper,
71 User_Helper $user_helper
72 ) {
73 $this->notification_center = $notification_center;
74 $this->short_link_helper = $short_link_helper;
75 $this->product_helper = $product_helper;
76 $this->options_helper = $options_helper;
77 $this->user_helper = $user_helper;
78 }
79
80 /**
81 * Returns the conditionals based on which this loadable should be active.
82 *
83 * @return array<string>
84 */
85 public static function get_conditionals() {
86 return [ Admin_Conditional::class ];
87 }
88
89 /**
90 * Initializes the integration.
91 *
92 * @return void
93 */
94 public function register_hooks() {
95 \add_action( 'admin_init', [ $this, 'add_notifications' ] );
96 }
97
98 /**
99 * Adds notification when user has not installed Yoast SEO themselves and has not resolved the notification yet.
100 *
101 * @return void
102 */
103 public function add_notifications() {
104 if ( $this->has_user_installed_yoast() ) {
105 $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
106 return;
107 }
108
109 if ( $this->has_notification_been_resolved() ) {
110 $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
111 return;
112 }
113
114 $notification = $this->get_ping_other_admins_notification();
115
116 $this->notification_center->add_notification( $notification );
117 }
118
119 /**
120 * Returns whether user has installed Yoast SEO themselves.
121 *
122 * @return bool Whether the user has installed Yoast SEO themselves.
123 */
124 private function has_user_installed_yoast(): bool {
125 $first_activated_by = $this->options_helper->get( 'first_activated_by', 0 );
126
127 if ( $first_activated_by === 0 ) {
128 return true; // We cannot be sure, so we assume they did.
129 }
130
131 if ( \get_current_user_id() === $first_activated_by ) {
132 return true;
133 }
134
135 return false;
136 }
137
138 /**
139 * Returns whether the alert has been resolved before.
140 *
141 * @return bool Whether the alert has been resolved before.
142 */
143 private function has_notification_been_resolved(): bool {
144 return $this->user_helper->get_meta( \get_current_user_id(), self::NOTIFICATION_ID . '_resolved', true ) === '1';
145 }
146
147 /**
148 * Build the ping-other-admins notification.
149 *
150 * @return Yoast_Notification The ping-other-admins notification.
151 */
152 private function get_ping_other_admins_notification(): Yoast_Notification {
153 $message = $this->get_message();
154
155 return new Yoast_Notification(
156 $message,
157 [
158 'id' => self::NOTIFICATION_ID,
159 'type' => Yoast_Notification::WARNING,
160 'capabilities' => [ 'wpseo_manage_options' ],
161 'priority' => 20,
162 'resolve_nonce' => \wp_create_nonce( 'wpseo-resolve-alert-nonce' ),
163 ],
164 );
165 }
166
167 /**
168 * Returns the notification as an HTML string.
169 *
170 * @return string The HTML string representation of the notification.
171 */
172 private function get_message() {
173 $message = \sprintf(
174 /* translators: %1$s expands to "Yoast SEO". */
175 \esc_html__( 'Looks like you’re new here. %1$s makes it easy to optimize your website for search engines. Want to keep your site healthy and easier to find? Sign up below to receive practical emails to get you started!', 'wordpress-seo' ),
176 'Yoast SEO',
177 );
178
179 $notification_text = '<p>' . $message . '</p>';
180
181 return $notification_text;
182 }
183 }
184