PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 / src / integrations / admin / indexing-notification-integration.php

indexing-notification-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at src/integrations/admin/indexing-notification-integration.php

281 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Admin;
4
5 use WPSEO_Addon_Manager;
6 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7 use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
8 use Yoast\WP\SEO\Conditionals\User_Can_Manage_Wpseo_Options_Conditional;
9 use Yoast\WP\SEO\Config\Indexing_Reasons;
10 use Yoast\WP\SEO\Helpers\Current_Page_Helper;
11 use Yoast\WP\SEO\Helpers\Environment_Helper;
12 use Yoast\WP\SEO\Helpers\Indexing_Helper;
13 use Yoast\WP\SEO\Helpers\Notification_Helper;
14 use Yoast\WP\SEO\Helpers\Product_Helper;
15 use Yoast\WP\SEO\Helpers\Short_Link_Helper;
16 use Yoast\WP\SEO\Integrations\Integration_Interface;
17 use Yoast\WP\SEO\Presenters\Admin\Indexing_Failed_Notification_Presenter;
18 use Yoast\WP\SEO\Presenters\Admin\Indexing_Notification_Presenter;
19 use Yoast_Notification;
20 use Yoast_Notification_Center;
21
22 /**
23 * Class Indexing_Notification_Integration.
24 *
25 * @package Yoast\WP\SEO\Integrations\Admin
26 */
27 class Indexing_Notification_Integration implements Integration_Interface {
28
29 /**
30 * The notification ID.
31 */
32 const NOTIFICATION_ID = 'wpseo-reindex';
33
34 /**
35 * Represents the reason that the indexing process failed and should be tried again.
36 *
37 * @deprecated 15.3
38 */
39 const REASON_INDEXING_FAILED = Indexing_Reasons::REASON_INDEXING_FAILED;
40
41 /**
42 * Represents the reason that the permalink settings are changed.
43 *
44 * @deprecated 15.3
45 */
46 const REASON_PERMALINK_SETTINGS = Indexing_Reasons::REASON_PERMALINK_SETTINGS;
47
48 /**
49 * Represents the reason that the category base is changed.
50 *
51 * @deprecated 15.3
52 */
53 const REASON_CATEGORY_BASE_PREFIX = Indexing_Reasons::REASON_CATEGORY_BASE_PREFIX;
54
55 /**
56 * Represents the reason that the tag base is changed.
57 *
58 * @deprecated 15.3
59 */
60 const REASON_TAG_BASE_PREFIX = Indexing_Reasons::REASON_TAG_BASE_PREFIX;
61
62 /**
63 * Represents the reason that the home url option is changed.
64 *
65 * @deprecated 15.3
66 */
67 const REASON_HOME_URL_OPTION = Indexing_Reasons::REASON_HOME_URL_OPTION;
68
69 /**
70 * The Yoast notification center.
71 *
72 * @var Yoast_Notification_Center
73 */
74 protected $notification_center;
75
76 /**
77 * The product helper.
78 *
79 * @var Product_Helper
80 */
81 protected $product_helper;
82
83 /**
84 * The current page helper.
85 *
86 * @var Current_Page_Helper
87 */
88 protected $page_helper;
89
90 /**
91 * The short link helper.
92 *
93 * @var Short_Link_Helper
94 */
95 protected $short_link_helper;
96
97 /**
98 * The notification helper.
99 *
100 * @var Notification_Helper
101 */
102 protected $notification_helper;
103
104 /**
105 * The indexing helper.
106 *
107 * @var Indexing_Helper
108 */
109 protected $indexing_helper;
110
111 /**
112 * The Addon Manager.
113 *
114 * @var WPSEO_Addon_Manager
115 */
116 protected $addon_manager;
117
118 /**
119 * The Environment Helper.
120 *
121 * @var Environment_Helper
122 */
123 protected $environment_helper;
124
125 /**
126 * Indexing_Notification_Integration constructor.
127 *
128 * @param Yoast_Notification_Center $notification_center The notification center.
129 * @param Product_Helper $product_helper The product helper.
130 * @param Current_Page_Helper $page_helper The current page helper.
131 * @param Short_Link_Helper $short_link_helper The short link helper.
132 * @param Notification_Helper $notification_helper The notification helper.
133 * @param Indexing_Helper $indexing_helper The indexing helper.
134 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
135 * @param Environment_Helper $environment_helper The environment helper.
136 */
137 public function __construct(
138 Yoast_Notification_Center $notification_center,
139 Product_Helper $product_helper,
140 Current_Page_Helper $page_helper,
141 Short_Link_Helper $short_link_helper,
142 Notification_Helper $notification_helper,
143 Indexing_Helper $indexing_helper,
144 WPSEO_Addon_Manager $addon_manager,
145 Environment_Helper $environment_helper
146 ) {
147 $this->notification_center = $notification_center;
148 $this->product_helper = $product_helper;
149 $this->page_helper = $page_helper;
150 $this->short_link_helper = $short_link_helper;
151 $this->notification_helper = $notification_helper;
152 $this->indexing_helper = $indexing_helper;
153 $this->addon_manager = $addon_manager;
154 $this->environment_helper = $environment_helper;
155 }
156
157 /**
158 * Initializes the integration.
159 *
160 * Adds hooks and jobs to cleanup or add the notification when necessary.
161 *
162 * @return void
163 */
164 public function register_hooks() {
165 if ( $this->page_helper->get_current_yoast_seo_page() === 'wpseo_dashboard' ) {
166 \add_action( 'admin_init', [ $this, 'maybe_cleanup_notification' ] );
167 }
168
169 if ( $this->indexing_helper->has_reason() ) {
170 \add_action( 'admin_init', [ $this, 'maybe_create_notification' ] );
171 }
172
173 \add_action( self::NOTIFICATION_ID, [ $this, 'maybe_create_notification' ] );
174 }
175
176 /**
177 * Returns the conditionals based on which this loadable should be active.
178 *
179 * @return array The conditionals.
180 */
181 public static function get_conditionals() {
182 return [
183 Admin_Conditional::class,
184 Not_Admin_Ajax_Conditional::class,
185 User_Can_Manage_Wpseo_Options_Conditional::class,
186 ];
187 }
188
189 /**
190 * Checks whether the notification should be shown and adds
191 * it to the notification center if this is the case.
192 */
193 public function maybe_create_notification() {
194 if ( ! $this->should_show_notification() ) {
195 return;
196 }
197
198 if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) {
199 $notification = $this->notification();
200 $this->notification_helper->restore_notification( $notification );
201 $this->notification_center->add_notification( $notification );
202 }
203 }
204
205 /**
206 * Checks whether the notification should not be shown anymore and removes
207 * it from the notification center if this is the case.
208 */
209 public function maybe_cleanup_notification() {
210 $notification = $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID );
211
212 if ( $notification === null ) {
213 return;
214 }
215
216 if ( $this->should_show_notification() ) {
217 return;
218 }
219
220 $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
221 }
222
223 /**
224 * Checks whether the notification should be shown.
225 *
226 * @return bool If the notification should be shown.
227 */
228 protected function should_show_notification() {
229 if ( ! $this->environment_helper->is_production_mode() ) {
230 return false;
231 }
232 // Don't show a notification if the indexing has already been started earlier.
233 if ( $this->indexing_helper->get_started() > 0 ) {
234 return false;
235 }
236
237 // Never show a notification when nothing should be indexed.
238 return $this->indexing_helper->get_limited_filtered_unindexed_count( 1 ) > 0;
239 }
240
241 /**
242 * Returns an instance of the notification.
243 *
244 * @return Yoast_Notification The notification to show.
245 */
246 protected function notification() {
247 $reason = $this->indexing_helper->get_reason();
248
249 $presenter = $this->get_presenter( $reason );
250
251 return new Yoast_Notification(
252 $presenter,
253 [
254 'type' => Yoast_Notification::WARNING,
255 'id' => self::NOTIFICATION_ID,
256 'capabilities' => 'wpseo_manage_options',
257 'priority' => 0.8,
258 ]
259 );
260 }
261
262 /**
263 * Gets the presenter to use to show the notification.
264 *
265 * @param string $reason The reason for the notification.
266 *
267 * @return Indexing_Failed_Notification_Presenter|Indexing_Notification_Presenter
268 */
269 protected function get_presenter( $reason ) {
270 if ( $reason === Indexing_Reasons::REASON_INDEXING_FAILED ) {
271 $presenter = new Indexing_Failed_Notification_Presenter( $this->product_helper, $this->short_link_helper, $this->addon_manager );
272 }
273 else {
274 $total_unindexed = $this->indexing_helper->get_filtered_unindexed_count();
275 $presenter = new Indexing_Notification_Presenter( $this->short_link_helper, $total_unindexed, $reason );
276 }
277
278 return $presenter;
279 }
280 }
281