forked from cedaro/better-internal-link-search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
better-internal-link-search.php
498 lines (430 loc) · 14.8 KB
/
better-internal-link-search.php
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
/**
* Better Internal Link Search
*
* @package BetterInternalLinkSearch
* @author Brady Vercher
* @link https://www.cedaro.com/
* @copyright Copyright (c) 2016 Cedaro, Inc.
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Better Internal Link Search
* Plugin URI: https://wordpress.org/plugins/better-internal-link-search/?utm_source=wordpress-plugin&utm_medium=link&utm_content=better-internal-link-search-plugin-uri&utm_campaign=plugins
* Description: Improve the internal link popup functionality with time saving enhancements and features.
* Version: 1.3.0
* Author: Cedaro
* Author URI: https://www.cedaro.com/?utm_source=wordpress-plugin&utm_medium=link&utm_content=better-internal-link-search-author-uri&utm_campaign=plugins
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: better-internal-link-search
*/
/**
* Set a constant path to the plugin's root directory.
*/
if ( ! defined( 'BETTER_INTERNAL_LINK_SEARCH_DIR' ) )
define( 'BETTER_INTERNAL_LINK_SEARCH_DIR', plugin_dir_path( __FILE__ ) );
/**
* Set a constant URL to the plugin's root directory.
*/
if ( ! defined( 'BETTER_INTERNAL_LINK_SEARCH_URL' ) )
define( 'BETTER_INTERNAL_LINK_SEARCH_URL', plugin_dir_url( __FILE__ ) );
/**
* Load the plugin.
*/
if ( is_admin() ) {
add_action( 'plugins_loaded', array( 'Better_Internal_Link_Search', 'load' ) );
}
/**
* Main plugin class.
*
* @since 1.0.0
*/
class Better_Internal_Link_Search {
/**
* Static variable for storing the search term.
*/
private static $s;
/**
* Hook into actions to modify behavior when needed.
*
* @since 1.0.0
*/
public static function load() {
// Load settings.
include( BETTER_INTERNAL_LINK_SEARCH_DIR . 'includes/settings.php' );
Better_Internal_Link_Search_Settings::load();
// Load post list table typeahead search.
include( BETTER_INTERNAL_LINK_SEARCH_DIR . 'includes/posts-list-table.php' );
Better_Internal_Link_Search_Posts_List_Table::load();
// Replace the default wp-link-ajax action.
if ( isset( $_POST['search'] ) ) {
remove_action( 'wp_ajax_wp-link-ajax', 'wp_link_ajax', 1 );
add_action( 'wp_ajax_wp-link-ajax', array( __CLASS__, 'ajax_get_link_search_results' ), 1 );
}
// Hook it up.
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
// Enqueue Internal Link Manager JavaScript and styles.
add_action( 'wp_enqueue_editor', array( __CLASS__, 'enqueue_editor_assets' ) );
// Upgrade routine.
add_action( 'admin_init', array( __CLASS__, 'upgrade' ) );
}
/**
* Add a filter to limit search results.
*
* The filter is only attached when a request comes from the Pages meta
* box on the Menus screen or from the "Insert/edit link" editor popup.
*
* @since 1.0.0
*/
public static function admin_init() {
add_filter( 'better_internal_link_search_modifier-help', array( __CLASS__, 'search_modifier_help' ), 10, 2 );
// Disable default search modifiers by returning false for this filter.
if ( apply_filters( 'better_internal_link_search_load_default_modifiers', true ) ) {
include ( BETTER_INTERNAL_LINK_SEARCH_DIR . 'includes/search-modifiers.php' );
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) ) {
$actions = array(
'bils-get-link-search-results',
'menu-quick-search',
'wp-link-ajax',
);
if ( in_array( $_POST['action'], $actions ) ) {
add_filter( 'wp_link_query_args', array( __CLASS__, 'unsupress_posts_search_filters' ) );
add_filter( 'posts_search', array( __CLASS__, 'limit_search_to_title' ), 10, 2 );
add_action( 'pre_get_posts', array( __CLASS__, 'set_query_vars' ) );
}
}
}
/**
* Set query vars in pre_get_posts.
*
* Includes scheduled posts in search results and disables paging.
*
* @since 1.1.0
*/
public static function set_query_vars( $query ) {
if ( 'bils-get-link-search-results' == $_POST['action'] || 'wp-link-ajax' == $_POST['action'] ) {
// Scheduled post concept from Evan Solomon's plugin.
// https://wordpress.org/plugins/internal-linking-for-scheduled-posts/
$post_status = (array) $query->get( 'post_status' );
$post_status[] = 'future';
if ( current_user_can( 'read_private_posts' ) ) {
$post_status[] = 'private';
}
$query->set( 'post_status', array_unique( $post_status ) );
// Make sure 'posts_per_page' hasn't been explicitly set by a modifier to allow for paging of local results before overriding it.
if ( ! $query->get( 'posts_per_page' ) ) {
// Paging won't work with multiple data sources and ideally the search term
// should be unique enough that there aren't a ton of matches.
$query->set( 'posts_per_page', -1 );
}
}
}
/**
* Limits search queries to the post title field.
*
* @see wp-includes/query.php
*
* @since 1.0.0
*/
public static function limit_search_to_title( $search, $wp_query ) {
global $wpdb;
if ( empty( $search ) ) {
return $search;
}
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = '';
$searchand = '';
foreach( (array) $q['search_terms'] as $term ) {
$term = esc_sql( self::esc_like( $term ) );
$search.= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}'))";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
}
return $search;
}
/**
* Unsupress posts search filters.
*
* The 'posts_search' filter was disabled for internal link searches in
* https://core.trac.wordpress.org/ticket/35594
*
* @since 1.3.0
*/
public static function unsupress_posts_search_filters( $args ) {
$args['suppress_filters'] = false;
return $args;
}
/**
* Returns search results.
*
* Results returned in a format expected by the internal link manager.
* Doesn't have support for paging.
*
* Multiple filters provided for either adding results or short-circuiting
* the flow at various points.
*
* @since 1.1.0
*/
public static function ajax_get_link_search_results() {
global $wpdb;
check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
if ( isset( $_POST['search'] ) ) {
$results = array();
$s = stripslashes( $_POST['search'] );
$args['s'] = $s;
$args['page'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
$args['per_page'] = 20; // Default for usage in filters, otherwise, it shouldn't do anything.
// Check to see if the request is prepended with a modifier (ex: -wikipedia interrobang, -spotify:artist willie nelson).
if ( 0 === mb_strpos( $s, '-' ) ) {
preg_match( '/-([^\s]+)\s?(.*)?/', $s, $matches );
$s = trim( $matches[2] );
$args['s'] = $s;
$args['modifier'] = explode( ':', trim( $matches[1] ) );
$results = (array) apply_filters( 'better_internal_link_search_modifier-' . $args['modifier'][0], array(), $args );
if ( ! empty( $results ) ) {
echo json_encode( $results );
wp_die();
}
}
// Allow plugins to intercept the request and add their own results or short-circuit execution.
$pre_results = (array) apply_filters( 'pre_better_internal_link_search_results', array(), $args );
if ( ! empty( $pre_results ) ) {
$results = array_merge( $results, $pre_results );
}
// Short-circuit if this is a paged request. The first request should have returned all results.
if ( isset( $_POST['page'] ) && $_POST['page'] > 1 ) {
wp_die( 0 );
}
// Don't continue if the query length is less than three.
if ( strlen( $args['s'] ) < 3 ) {
wp_die( 0 );
}
// @see wp_link_ajax();
require_once(ABSPATH . WPINC . '/class-wp-editor.php');
$posts = _WP_Editors::wp_link_query( $args );
if ( $posts ) {
$future_status_object = get_post_status_object( 'future' );
$private_status_object = get_post_status_object( 'private' );
foreach( $posts as $key => $post ) {
if ( 'future' == get_post_status( $post['ID'] ) ) {
$posts[ $key ]['info'] = $future_status_object->label;
} elseif ( 'private' == get_post_status( $post['ID'] ) ) {
$posts[ $key ]['info'] .= ' (' . $private_status_object->label . ')';
}
}
$results = array_merge( $results, $posts );
}
if ( 'yes' === Better_Internal_Link_Search_Settings::get_settings( 'include_term_results' ) ) {
// Search for matching term archives.
$search = '%' . self::esc_like( $s ) . '%';
$terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.term_id, t.name, tt.taxonomy
FROM $wpdb->terms t
INNER JOIN $wpdb->term_taxonomy tt ON t.term_id=tt.term_id
WHERE t.name LIKE %s
ORDER BY name ASC", $search ) );
if ( $terms ) {
foreach ( $terms as $term ) {
$taxonomy = get_taxonomy( $term->taxonomy );
if ( isset( $taxonomy->query_var ) ) {
$results[] = array(
'title' => trim( esc_html( strip_tags( $term->name ) ) ),
'permalink' => get_term_link( (int) $term->term_id, $term->taxonomy ),
'info' => $taxonomy->labels->singular_name,
);
}
}
}
}
// Allow results to be filtered one last time and attempt to sort them.
if ( ! empty( $results ) ) {
self::$s = $s;
$results = apply_filters( 'better_internal_link_search_results', $results, $args );
if ( apply_filters( 'better_internal_link_search_sort_results', true, $results, $args ) ) {
usort( $results, array( __CLASS__, 'sort_results' ) );
}
}
// Add shortcut results.
$shortcuts = (array) self::get_shortcuts();
if ( ! empty( $shortcuts ) ) {
if ( array_key_exists( $s, $shortcuts ) ) {
array_unshift( $results, $shortcuts[ $s ] );
} elseif ( 'shortcuts' == $s ) {
$results = array_merge( $shortcuts, $results );
}
}
}
if ( ! isset( $results ) || empty( $results ) ) {
wp_die( 0 );
}
echo json_encode( $results );
echo "\n";
wp_die();
}
/**
* Javascript to automatically search for text selected in the editor.
*
* Inserts any text selected in the editor into the search field in the
* "Insert/edit link" popup when the link button in the toolbar is
* clicked. Automatically executes a search request and returns the
* results.
*/
public static function enqueue_editor_assets() {
wp_enqueue_script(
'better-internal-link-search-internal-link-manager',
BETTER_INTERNAL_LINK_SEARCH_URL . 'js/internal-link-manager.js',
array( 'jquery', 'underscore', 'wplink' )
);
wp_localize_script(
'better-internal-link-search-internal-link-manager',
'BilsSettings',
Better_Internal_Link_Search_Settings::get_settings()
);
add_action( 'after_wp_tiny_mce', array( __CLASS__, 'print_editor_styles' ), 1 );
}
/**
* Prints the CSS needed
*/
public static function print_editor_styles() {
?>
<style type="text/css">
#wp-link .item-description{ display: block; clear:both; padding: 3px 0 0 10px;}
</style>
<?php
}
/**
* Internal link shortcuts.
*
* A couple of basic shortcuts for easily linking to the home url and site
* url. Also gives plugins the ability to add more shortcuts.
*
* @since 1.1.0
*/
public static function get_shortcuts() {
$shortcuts = apply_filters( 'better_internal_link_search_shortcuts', array(
'home' => array(
'title' => __( 'Home', 'better-internal-link-search' ),
'permalink' => home_url( '/' ),
),
'siteurl' => array(
'title' => __( 'Site URL', 'better-internal-link-search' ),
'permalink' => site_url( '/' ),
)
) );
if ( ! empty( $shortcuts ) ) {
// Sanitize the shortcuts a bit.
foreach( $shortcuts as $key => $shortcut ) {
if ( empty( $shortcut['title'] ) || empty( $shortcut['permalink'] ) ) {
unset( $shortcuts[ $key ] );
break;
}
if ( empty( $shortcut['info'] ) ) {
$shortcuts[ $key ]['info'] = 'Shortcut';
}
$shortcuts[ $key ]['title'] = trim( esc_html( strip_tags( $shortcut['title'] ) ) );
$shortcuts[ $key ]['info'] = trim( esc_html( strip_tags( $shortcuts[ $key ]['info'] ) ) );
}
}
return $shortcuts;
}
/**
* Custom results sorter.
*
* Attempts to return results in a more natural order. Titles that exactly
* match a search query are returned first, followed by titles that begin
* with the query. Remaining results are sorted alphabetically.
*
* @todo Potentially remove articles (a, an, the) when doing matches.
*
* @since 1.1.0
*/
public static function sort_results( $a, $b ) {
$a_title = mb_strtolower( $a['title'] );
$b_title = mb_strtolower( $b['title'] );
$s = mb_strtolower( self::$s );
if ( $a_title == $b_title ) {
return 0;
}
if ( $s == $a_title ) {
return -1;
} elseif ( $s == $b_title ) {
return 1;
}
$a_strpos = mb_strpos( $a_title, $s );
$b_strpos = mb_strpos( $b_title, $s );
if ( 0 === $a_strpos && 0 === $b_strpos ) {
// Return the shorter title first.
return ( mb_strlen( $a_title ) < mb_strlen( $b_title ) ) ? -1 : 1;
} elseif ( 0 === $a_strpos ) {
return -1;
} elseif ( 0 === $b_strpos ) {
return 1;
}
return strcmp( $a_title, $b_title );
}
/**
* Search modifier help.
*
* Intercepts a request for '-help' and displays any modifiers that have
* been added via the filter.
*
* @since 1.1.0
*/
public static function search_modifier_help( $results, $args ) {
if ( intval( $args['page'] ) > 1 ) {
return array();
}
$results = apply_filters( 'better_internal_link_search_modifier_help', array() );
if ( ! empty( $results ) && ! empty( $args['s'] ) && array_key_exists( $args['s'], $results ) ) {
// If the -help request has a search query, limit the returned results to that modifier.
$results = array( $results[ $args['s'] ] );
}
return $results;
}
/**
* Upgrade plugin settings.
*
* The plugin version is saved as a different option so that updates to
* the settings option don't stomp on it.
*
* @since 1.1.2
*/
public static function upgrade() {
$saved_version = get_option( 'better_internal_link_search_version' );
$update_version = false;
if ( ! $saved_version || version_compare( $saved_version, '1.1.2', '<' ) ) {
$update_version = true;
update_option( 'better_internal_link_search', array( 'automatically_search_selection' => 'yes' ) );
}
if ( ! $saved_version || version_compare( $saved_version, '1.2.7', '<' ) ) {
$update_version = true;
$settings = Better_Internal_Link_Search_Settings::get_settings();
$settings['include_term_results'] = 'yes';
update_option( 'better_internal_link_search', $settings );
}
if ( $update_version ) {
// Update saved version number.
$plugin_data = get_plugin_data( __FILE__ );
update_option( 'better_internal_link_search_version', $plugin_data['Version'] );
}
}
/**
* Escape LIKE special characters.
*
* @since 1.2.8
*
* @see wpdb::esc_like()
* @link https://make.wordpress.org/core/2014/06/20/like_escape-is-deprecated-in-wordpress-4-0/
*/
public static function esc_like( $text ) {
global $wpdb;
if ( method_exists( $wpdb, 'esc_like' ) ) {
return $wpdb->esc_like( $text );
}
return addcslashes( $text, '_%\\' );
}
}