indexable-taxonomy-change-watcher.php
5.77 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
<?php
namespace Yoast\WP\SEO\Integrations\Watchers;
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action;
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
use Yoast\WP\SEO\Config\Indexing_Reasons;
use Yoast\WP\SEO\Helpers\Indexing_Helper;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
use Yoast\WP\SEO\Integrations\Cleanup_Integration;
use Yoast\WP\SEO\Integrations\Integration_Interface;
use Yoast_Notification;
use Yoast_Notification_Center;
/**
* Taxonomy watcher.
*
* Responds to changes in taxonomies public availability.
*/
class Indexable_Taxonomy_Change_Watcher implements Integration_Interface {
/**
* The indexing helper.
*
* @var Indexing_Helper
*/
protected $indexing_helper;
/**
* Holds the Options_Helper instance.
*
* @var Options_Helper
*/
private $options;
/**
* Holds the Taxonomy_Helper instance.
*
* @var Taxonomy_Helper
*/
private $taxonomy_helper;
/**
* The notifications center.
*
* @var Yoast_Notification_Center
*/
private $notification_center;
/**
* Returns the conditionals based on which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Not_Admin_Ajax_Conditional::class, Admin_Conditional::class, Migrations_Conditional::class ];
}
/**
* Indexable_Taxonomy_Change_Watcher constructor.
*
* @param Indexing_Helper $indexing_helper The indexing helper.
* @param Options_Helper $options The options helper.
* @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
* @param Yoast_Notification_Center $notification_center The notification center.
*/
public function __construct(
Indexing_Helper $indexing_helper,
Options_Helper $options,
Taxonomy_Helper $taxonomy_helper,
Yoast_Notification_Center $notification_center
) {
$this->indexing_helper = $indexing_helper;
$this->options = $options;
$this->taxonomy_helper = $taxonomy_helper;
$this->notification_center = $notification_center;
}
/**
* Initializes the integration.
*
* This is the place to register hooks and filters.
*
* @return void
*/
public function register_hooks() {
\add_action( 'admin_init', [ $this, 'check_taxonomy_public_availability' ] );
}
/**
* Checks if one or more taxonomies change visibility.
*
* @return void
*/
public function check_taxonomy_public_availability() {
// We have to make sure this is just a plain http request, no ajax/REST.
if ( \wp_is_json_request() ) {
return;
}
$public_taxonomies = \array_keys( $this->taxonomy_helper->get_public_taxonomies() );
$last_known_public_taxonomies = $this->options->get( 'last_known_public_taxonomies', [] );
// Initializing the option on the first run.
if ( empty( $last_known_public_taxonomies ) ) {
$this->options->set( 'last_known_public_taxonomies', $public_taxonomies );
return;
}
// We look for new public taxonomies.
$newly_made_public_taxonomies = \array_diff( $public_taxonomies, $last_known_public_taxonomies );
// We look fortaxonomies that from public have been made private.
$newly_made_non_public_taxonomies = \array_diff( $last_known_public_taxonomies, $public_taxonomies );
// Nothing to be done if no changes has been made to taxonomies.
if ( empty( $newly_made_public_taxonomies ) && ( empty( $newly_made_non_public_taxonomies ) ) ) {
return;
}
// Update the list of last known public taxonomies in the database.
$this->options->set( 'last_known_public_taxonomies', $public_taxonomies );
// There are new taxonomies that have been made public.
if ( ! empty( $newly_made_public_taxonomies ) ) {
// Force a notification requesting to start the SEO data optimization.
\delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
\delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_LIMITED_COUNT_TRANSIENT );
$this->indexing_helper->set_reason( Indexing_Reasons::REASON_TAXONOMY_MADE_PUBLIC );
$this->maybe_add_notification();
}
// There are taxonomies that have been made private.
if ( ! empty( $newly_made_non_public_taxonomies ) ) {
// Schedule a cron job to remove all the terms whose taxonomy has been made private.
$cleanup_not_yet_scheduled = ! \wp_next_scheduled( Cleanup_Integration::START_HOOK );
if ( $cleanup_not_yet_scheduled ) {
\wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK );
}
}
}
/**
* Decides if a notification should be added in the notification center.
*
* @return void
*/
private function maybe_add_notification() {
$notification = $this->notification_center->get_notification_by_id( 'taxonomies-made-public' );
if ( \is_null( $notification ) ) {
$this->add_notification();
}
}
/**
* Adds a notification to be shown on the next page request since posts are updated in an ajax request.
*
* @return void
*/
private function add_notification() {
$message = \sprintf(
/* translators: 1: Opening tag of the link to the Search appearance settings page, 2: Link closing tag. */
\esc_html__( 'It looks like you\'ve added a new taxonomy to your website. We recommend that you review your %1$sSettings%2$s under Categories & tags.', 'wordpress-seo' ),
'<a href="' . \esc_url( \admin_url( 'admin.php?page=wpseo_page_settings' ) ) . '">',
'</a>'
);
$notification = new Yoast_Notification(
$message,
[
'type' => Yoast_Notification::WARNING,
'id' => 'taxonomies-made-public',
'capabilities' => 'wpseo_manage_options',
'priority' => 0.8,
]
);
$this->notification_center->add_notification( $notification );
}
}