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

252 lines 6.9 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 public const NOTIFICATION_ID = 'wpseo-reindex';
33
34 /**
35 * The Yoast notification center.
36 *
37 * @var Yoast_Notification_Center
38 */
39 protected $notification_center;
40
41 /**
42 * The product helper.
43 *
44 * @var Product_Helper
45 */
46 protected $product_helper;
47
48 /**
49 * The current page helper.
50 *
51 * @var Current_Page_Helper
52 */
53 protected $page_helper;
54
55 /**
56 * The short link helper.
57 *
58 * @var Short_Link_Helper
59 */
60 protected $short_link_helper;
61
62 /**
63 * The notification helper.
64 *
65 * @var Notification_Helper
66 */
67 protected $notification_helper;
68
69 /**
70 * The indexing helper.
71 *
72 * @var Indexing_Helper
73 */
74 protected $indexing_helper;
75
76 /**
77 * The Addon Manager.
78 *
79 * @var WPSEO_Addon_Manager
80 */
81 protected $addon_manager;
82
83 /**
84 * The Environment Helper.
85 *
86 * @var Environment_Helper
87 */
88 protected $environment_helper;
89
90 /**
91 * Indexing_Notification_Integration constructor.
92 *
93 * @param Yoast_Notification_Center $notification_center The notification center.
94 * @param Product_Helper $product_helper The product helper.
95 * @param Current_Page_Helper $page_helper The current page helper.
96 * @param Short_Link_Helper $short_link_helper The short link helper.
97 * @param Notification_Helper $notification_helper The notification helper.
98 * @param Indexing_Helper $indexing_helper The indexing helper.
99 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
100 * @param Environment_Helper $environment_helper The environment helper.
101 */
102 public function __construct(
103 Yoast_Notification_Center $notification_center,
104 Product_Helper $product_helper,
105 Current_Page_Helper $page_helper,
106 Short_Link_Helper $short_link_helper,
107 Notification_Helper $notification_helper,
108 Indexing_Helper $indexing_helper,
109 WPSEO_Addon_Manager $addon_manager,
110 Environment_Helper $environment_helper
111 ) {
112 $this->notification_center = $notification_center;
113 $this->product_helper = $product_helper;
114 $this->page_helper = $page_helper;
115 $this->short_link_helper = $short_link_helper;
116 $this->notification_helper = $notification_helper;
117 $this->indexing_helper = $indexing_helper;
118 $this->addon_manager = $addon_manager;
119 $this->environment_helper = $environment_helper;
120 }
121
122 /**
123 * Initializes the integration.
124 *
125 * Adds hooks and jobs to cleanup or add the notification when necessary.
126 *
127 * @return void
128 */
129 public function register_hooks() {
130 if ( $this->page_helper->get_current_yoast_seo_page() === 'wpseo_dashboard' ) {
131 \add_action( 'admin_init', [ $this, 'maybe_cleanup_notification' ] );
132 }
133
134 if ( $this->indexing_helper->has_reason() ) {
135 \add_action( 'admin_init', [ $this, 'maybe_create_notification' ] );
136 }
137
138 \add_action( self::NOTIFICATION_ID, [ $this, 'maybe_create_notification' ] );
139 }
140
141 /**
142 * Returns the conditionals based on which this loadable should be active.
143 *
144 * @return array The conditionals.
145 */
146 public static function get_conditionals() {
147 return [
148 Admin_Conditional::class,
149 Not_Admin_Ajax_Conditional::class,
150 User_Can_Manage_Wpseo_Options_Conditional::class,
151 ];
152 }
153
154 /**
155 * Checks whether the notification should be shown and adds
156 * it to the notification center if this is the case.
157 *
158 * @return void
159 */
160 public function maybe_create_notification() {
161 if (
162 ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID )
163 && $this->should_show_notification()
164 ) {
165 $notification = $this->notification();
166 $this->notification_helper->restore_notification( $notification );
167 $this->notification_center->add_notification( $notification );
168 }
169 }
170
171 /**
172 * Checks whether the notification should not be shown anymore and removes
173 * it from the notification center if this is the case.
174 *
175 * @return void
176 */
177 public function maybe_cleanup_notification() {
178 $notification = $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID );
179
180 if ( $notification === null ) {
181 return;
182 }
183
184 if ( $this->should_show_notification() ) {
185 return;
186 }
187
188 $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
189 }
190
191 /**
192 * Checks whether the notification should be shown.
193 *
194 * @return bool If the notification should be shown.
195 */
196 protected function should_show_notification() {
197 if ( ! $this->environment_helper->is_production_mode() ) {
198 return false;
199 }
200 // Don't show a notification if the indexing has already been started earlier.
201 if ( $this->indexing_helper->get_started() > 0 ) {
202 return false;
203 }
204
205 // We're about to perform expensive queries, let's inform.
206 \add_filter( 'wpseo_unindexed_count_queries_ran', '__return_true' );
207
208 // Never show a notification when nothing should be indexed.
209 return $this->indexing_helper->get_limited_filtered_unindexed_count( 1 ) > 0;
210 }
211
212 /**
213 * Returns an instance of the notification.
214 *
215 * @return Yoast_Notification The notification to show.
216 */
217 protected function notification() {
218 $reason = $this->indexing_helper->get_reason();
219
220 $presenter = $this->get_presenter( $reason );
221
222 return new Yoast_Notification(
223 $presenter,
224 [
225 'type' => Yoast_Notification::WARNING,
226 'id' => self::NOTIFICATION_ID,
227 'capabilities' => 'wpseo_manage_options',
228 'priority' => 0.8,
229 ],
230 );
231 }
232
233 /**
234 * Gets the presenter to use to show the notification.
235 *
236 * @param string $reason The reason for the notification.
237 *
238 * @return Indexing_Failed_Notification_Presenter|Indexing_Notification_Presenter
239 */
240 protected function get_presenter( $reason ) {
241 if ( $reason === Indexing_Reasons::REASON_INDEXING_FAILED ) {
242 $presenter = new Indexing_Failed_Notification_Presenter( $this->product_helper, $this->short_link_helper, $this->addon_manager );
243 }
244 else {
245 $total_unindexed = $this->indexing_helper->get_filtered_unindexed_count();
246 $presenter = new Indexing_Notification_Presenter( $this->short_link_helper, $total_unindexed, $reason );
247 }
248
249 return $presenter;
250 }
251 }
252