class-slug-change-watcher.php
6.83 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
249
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Watchers
*/
/**
* Class WPSEO_Slug_Change_Watcher.
*/
class WPSEO_Slug_Change_Watcher implements WPSEO_WordPress_Integration {
/**
* Registers all hooks to WordPress.
*
* @return void
*/
public function register_hooks() {
// If the current plugin is Yoast SEO Premium, stop registering.
if ( YoastSEO()->helpers->product->is_premium() ) {
return;
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
// Detect a post trash.
add_action( 'wp_trash_post', [ $this, 'detect_post_trash' ] );
// Detect a post delete.
add_action( 'before_delete_post', [ $this, 'detect_post_delete' ] );
// Detects deletion of a term.
add_action( 'delete_term_taxonomy', [ $this, 'detect_term_delete' ] );
}
/**
* Enqueues the quick edit handler.
*
* @return void
*/
public function enqueue_assets() {
global $pagenow;
if ( ! in_array( $pagenow, [ 'edit.php', 'edit-tags.php' ], true ) ) {
return;
}
$asset_manager = new WPSEO_Admin_Asset_Manager();
$asset_manager->enqueue_script( 'quick-edit-handler' );
}
/**
* Shows an message when a post is about to get trashed.
*
* @param int $post_id The current post ID.
*
* @return void
*/
public function detect_post_trash( $post_id ) {
if ( ! $this->is_post_viewable( $post_id ) ) {
return;
}
/* translators: %1$s expands to the translated name of the post type. */
$first_sentence = sprintf( __( 'You just trashed a %1$s.', 'wordpress-seo' ), $this->get_post_type_label( get_post_type( $post_id ) ) );
$message = $this->get_message( $first_sentence );
$this->add_notification( $message );
}
/**
* Shows an message when a post is about to get trashed.
*
* @param int $post_id The current post ID.
*
* @return void
*/
public function detect_post_delete( $post_id ) {
if ( ! $this->is_post_viewable( $post_id ) ) {
return;
}
/* translators: %1$s expands to the translated name of the post type. */
$first_sentence = sprintf( __( 'You just deleted a %1$s.', 'wordpress-seo' ), $this->get_post_type_label( get_post_type( $post_id ) ) );
$message = $this->get_message( $first_sentence );
$this->add_notification( $message );
}
/**
* Shows a message when a term is about to get deleted.
*
* @param int $term_taxonomy_id The term taxonomy ID that will be deleted.
*
* @return void
*/
public function detect_term_delete( $term_taxonomy_id ) {
if ( ! $this->is_term_viewable( $term_taxonomy_id ) ) {
return;
}
$term = \get_term_by( 'term_taxonomy_id', (int) $term_taxonomy_id );
$first_sentence = sprintf(
/* translators: 1: term label */
__( 'You just deleted a %1$s.', 'wordpress-seo' ),
$this->get_taxonomy_label_for_term( $term->term_id )
);
$message = $this->get_message( $first_sentence );
$this->add_notification( $message );
}
/**
* Checks if the post is viewable.
*
* @param string $post_id The post id to check.
*
* @return bool Whether the post is viewable or not.
*/
protected function is_post_viewable( $post_id ) {
$post_type = get_post_type( $post_id );
if ( ! WPSEO_Post_Type::is_post_type_accessible( $post_type ) ) {
return false;
}
$post_status = get_post_status( $post_id );
if ( ! $this->check_visible_post_status( $post_status ) ) {
return false;
}
return true;
}
/**
* Checks if the term is viewable.
*
* @param int $term_taxonomy_id The term taxonomy ID to check.
*
* @return bool Whether the term is viewable or not.
*/
protected function is_term_viewable( $term_taxonomy_id ) {
$term = \get_term_by( 'term_taxonomy_id', (int) $term_taxonomy_id );
if ( ! $term || is_wp_error( $term ) ) {
return false;
}
$taxonomy = get_taxonomy( $term->taxonomy );
if ( ! $taxonomy ) {
return false;
}
return $taxonomy->publicly_queryable || $taxonomy->public;
}
/**
* Gets the taxonomy label to use for a term.
*
* @param int $term_id The term ID.
*
* @return string The taxonomy's singular label.
*/
protected function get_taxonomy_label_for_term( $term_id ) {
$term = get_term( $term_id );
$taxonomy = get_taxonomy( $term->taxonomy );
return $taxonomy->labels->singular_name;
}
/**
* Retrieves the singular post type label.
*
* @param string $post_type Post type to retrieve label from.
*
* @return string The singular post type name.
*/
protected function get_post_type_label( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
// If the post type of this post wasn't registered default back to post.
if ( $post_type_object === null ) {
$post_type_object = get_post_type_object( 'post' );
}
return $post_type_object->labels->singular_name;
}
/**
* Checks whether the given post status is visible or not.
*
* @param string $post_status The post status to check.
*
* @return bool Whether or not the post is visible.
*/
protected function check_visible_post_status( $post_status ) {
$visible_post_statuses = [
'publish',
'static',
'private',
];
return in_array( $post_status, $visible_post_statuses, true );
}
/**
* Returns the message around changed URLs.
*
* @param string $first_sentence The first sentence of the notification.
*
* @return string The full notification.
*/
protected function get_message( $first_sentence ) {
return '<h2>' . __( 'Make sure you don\'t miss out on traffic!', 'wordpress-seo' ) . '</h2>'
. '<p>'
. $first_sentence
. ' ' . __( 'Search engines and other websites can still send traffic to your deleted post.', 'wordpress-seo' )
. ' ' . __( 'You should create a redirect to ensure your visitors do not get a 404 error when they click on the no longer working URL.', 'wordpress-seo' )
/* translators: %s expands to Yoast SEO Premium */
. ' ' . sprintf( __( 'With %s, you can easily create such redirects.', 'wordpress-seo' ), 'Yoast SEO Premium' )
. '</p>'
. '<p><a class="yoast-button-upsell" href="' . WPSEO_Shortlinker::get( 'https://yoa.st/1d0' ) . '" target="_blank">'
/* translators: %s expands to Yoast SEO Premium */
. sprintf( __( 'Get %s', 'wordpress-seo' ), 'Yoast SEO Premium' )
. '<span class="screen-reader-text">' . __( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
. '<span aria-hidden="true" class="yoast-button-upsell__caret"></span>'
. '</a></p>';
}
/**
* Adds a notification to be shown on the next page request since posts are updated in an ajax request.
*
* @param string $message The message to add to the notification.
*
* @return void
*/
protected function add_notification( $message ) {
$notification = new Yoast_Notification(
$message,
[
'type' => 'notice-warning is-dismissible',
'yoast_branding' => true,
]
);
$notification_center = Yoast_Notification_Center::get();
$notification_center->add_notification( $notification );
}
}