-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunique-title-checker.php
More file actions
341 lines (297 loc) · 8.44 KB
/
unique-title-checker.php
File metadata and controls
341 lines (297 loc) · 8.44 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* Unique Title Checker
*
* @package unique-title-checker
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: Unique Title Checker
* Plugin URI: https://github.com/2ndkauboy/unique-title-checker
* Description: Checks if the title of a post, page or custom post type is unique and warn the editor if not
* Version: 2.1.0
* Author: Bernhard Kau
* Author URI: http://kau-boys.de
* Text Domain: unique-title-checker
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0
*/
add_action(
'plugins_loaded',
array( Unique_Title_Checker::get_instance(), 'plugin_setup' )
);
/**
* Class Unique_Title_Checker.
*/
class Unique_Title_Checker {
/**
* Plugin instance.
*
* @see get_instance()
* @var object
*/
protected static $instance = null;
/**
* URL to this plugin's directory.
*
* @var string
*/
public $plugin_url = '';
/**
* Path to this plugin's directory.
*
* @var string
*/
public $plugin_path = '';
/**
* The nonce action.
*
* @var string
*/
public $nonce_action = 'unique_title_check_nonce';
/**
* The AJAX nonce.
*
* @var string
*/
public $ajax_nonce = '';
/**
* The `post_title` to be checked
*
* @var string
*/
public $post_title = '';
/**
* Access this plugin’s working instance.
*
* @wp-hook plugins_loaded
*
* @return object The current instance of this class.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
*
* @return void
*/
public function plugin_setup() {
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
// Enqueue the main JavaScript file.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Add the AJAX callback function.
add_action( 'wp_ajax_unique_title_check', array( $this, 'unique_title_check' ) );
// Check the uniqueness when a post is edited.
add_action( 'admin_notices', array( $this, 'uniqueness_admin_notice' ) );
// Generate the AJAX nonce.
$this->ajax_nonce = wp_create_nonce( $this->nonce_action );
}
/**
* Constructor.
* Intentionally left empty and public.
*
* @see plugin_setup()
*/
public function __construct() {
}
/**
* Add the JavaScript files to the admin pages.
*
* @wp-hook admin_enqueue_scripts
*
* @param string $hook The current admin page script.
*
* @return void
*/
public function enqueue_scripts( $hook ) {
// Only enable it on new posts/pages/CPTs.
if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
return;
}
// Enqueue the script.
$current_screen = get_current_screen();
if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
wp_enqueue_script(
'unique_title_checker',
plugins_url( 'js/unique-title-checker-block-editor.js', __FILE__ ),
array( 'jquery', 'wp-data', 'wp-notices' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/unique-title-checker-block-editor.js' ),
true
);
} else {
wp_enqueue_script(
'unique_title_checker',
plugins_url( 'js/unique-title-checker.js', __FILE__ ),
array( 'jquery' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/unique-title-checker.js' ),
true
);
}
$plugin_options = array(
'nonce' => $this->ajax_nonce,
'only_unique_error' => apply_filters( 'unique_title_checker_only_unique_error', false ),
);
// Add the nonce to the form.
wp_localize_script( 'unique_title_checker', 'unique_title_checker', $plugin_options );
wp_enqueue_script( 'unique_title_checker' );
}
/**
* Check the uniqueness and return the result.
*
* @wp-hook wp_ajax_(action)
*
* @return void
*/
public function unique_title_check() {
// Verify the ajax request.
check_ajax_referer( $this->nonce_action, 'ajax_nonce' );
$args = wp_parse_args(
$_REQUEST, // WPCS: input var OK.
array(
'action',
'ajax_nonce',
'post_id',
'post_type',
'post_title',
)
);
$response = $this->check_uniqueness( $args );
echo wp_json_encode( $response );
die();
}
/**
* Show an initial warning if the title of a saved post is not unique.
*
* @wp-hook admin_notices
*
* @return void
*/
public function uniqueness_admin_notice() {
global $post, $pagenow;
// Don't show an initial warning on a new post.
if ( 'post.php' !== $pagenow ) {
return;
}
// Show no warning, when the title is empty.
if ( empty( $post->post_title ) ) {
return;
}
// phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
// Build the necessary args for the initial uniqueness check.
$args = array(
'post__not_in' => array( $post->ID ),
'post_type' => $post->post_type,
'post_title' => $post->post_title,
);
// phpcs:enable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
$response = $this->check_uniqueness( $args );
// Don't show a message on init, if title is unique.
if ( 'error' !== $response['status'] ) {
return;
}
printf(
'<div id="unique-title-message" class="%s"><p>%s</p></div>',
esc_attr( $response['status'] ),
esc_html( $response['message'] )
);
}
/**
* Check for the uniqueness of the post.
*
* @param array|string $args The WP_QUERY arguments array or query string.
*
* @return array The status and message for the response
*/
public function check_uniqueness( $args ) {
// Use the posts_where hook to add the filter for the post_title, as it is not available through WP_Query args.
add_filter( 'posts_where', array( $this, 'post_title_where' ), 10, 1 );
// phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
// Use the `post_id` parameter for an excluding post filter.
if ( isset( $args['post_id'] ) ) {
$args['post__not_in'][] = $args['post_id'];
unset( $args['post_id'] );
}
// Make sure `post__not_in` is an array.
if ( ! is_array( $args['post__not_in'] ) ) {
$args['post__not_in'] = array( $args['post__not_in'] );
}
// phpcs:enable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
// Providing a filter to overwrite the search arguments.
$args = apply_filters( 'unique_title_checker_arguments', $args );
$post_type_object = get_post_type_object( $args['post_type'] );
if ( $post_type_object ) {
$post_type_singular_name = $post_type_object->labels->singular_name;
$post_type_name = $post_type_object->labels->name;
} else {
$post_type_singular_name = esc_html__( 'post', 'unique-title-checker' );
$post_type_name = esc_html__( 'posts', 'unique-title-checker' );
}
// Set the `post_title` to be checked.
$this->post_title = $args['post_title'];
$query = new WP_Query( $args );
$posts_count = $query->post_count;
if ( empty( $posts_count ) ) {
$response = array(
'message' => esc_html__( 'The chosen title is unique.', 'unique-title-checker' ),
'status' => 'updated',
);
} else {
if ( 1 === $posts_count ) {
$message = esc_html(
sprintf(
// Translators: %1$s: `post_type` singular name.
__( 'There is one %1$s with the same title!', 'unique-title-checker' ),
$post_type_singular_name
)
);
} else {
// phpcs:disable WordPress.WP.I18n.MismatchedPlaceholders
$message = esc_html(
sprintf(
// Translators: %1$d: posts count, %2$s: `post_type` singular name, %3$s: `post_type` plural name.
_n(
'There is %1$d %2$s with the same title!',
'There are %1$d other %3$s with the same title!',
$posts_count,
'unique-title-checker'
),
$posts_count,
$post_type_singular_name,
$post_type_name
)
);
// phpcs:enable WordPress.WP.I18n.MismatchedPlaceholders
}
$response = array(
'message' => $message,
'status' => 'error',
);
}
// Remove filter for post_title.
remove_filter( 'posts_where', array( $this, 'post_title_where' ), 10 );
return $response;
}
/**
* Add the filter for the post_title to the WHERE clause
*
* @wp-hook wp_ajax_(action)
*
* @global wpdb $wpdb The database object
*
* @param string $where The WHERE clause.
*
* @return string The new WHERE clause.
*/
public function post_title_where( $where ) {
global $wpdb;
return $where . " AND $wpdb->posts.post_title = '" . esc_sql( $this->post_title ) . "'";
}
}