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