| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: WP Search Suggest |
| 4 |
* Plugin URI: http://en.obenland.it/wp-search-suggest/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-search-suggest |
| 5 |
* Description: Provides title suggestions while typing a search query, using the built-in jQuery suggest script. |
| 6 |
* Version: 8 |
| 7 |
* Author: Konstantin Obenland |
| 8 |
* Author URI: http://en.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-search-suggest |
| 9 |
* Text Domain: wp-search-suggest |
| 10 |
* Domain Path: /lang |
| 11 |
* License: GNU General Public License v2 or later |
| 12 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
* |
| 14 |
* @package wp-search-suggest |
| 15 |
*/ |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers the script and stylesheet. |
| 19 |
* |
| 20 |
* The scripts and stylesheets can easily be deregistered by calling |
| 21 |
* <code>wp_deregister_script( 'wp-search-suggest' );</code> or |
| 22 |
* <code>wp_deregister_style( 'wp-search-suggest' );</code> on the init hook. |
| 23 |
*/ |
| 24 |
function wpss_init() { |
| 25 |
$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ), 'plugin' ); |
| 26 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : ''; |
| 27 |
|
| 28 |
wp_register_script( 'wp-search-suggest', plugins_url( "js/wpss-search-suggest$suffix.js", __FILE__ ), array( 'suggest' ), $plugin_data['Version'], true ); |
| 29 |
wp_localize_script( |
| 30 |
'wp-search-suggest', |
| 31 |
'wpss_options', |
| 32 |
array( |
| 33 |
'url' => admin_url( 'admin-ajax.php' ), |
| 34 |
'nonce' => wp_create_nonce( 'wpss-post-url' ), |
| 35 |
'ajaxurl' => add_query_arg( |
| 36 |
array( |
| 37 |
'action' => 'wp-search-suggest', |
| 38 |
'_wpnonce' => wp_create_nonce( 'wp-search-suggest' ), |
| 39 |
), |
| 40 |
admin_url( 'admin-ajax.php' ) |
| 41 |
), |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
wp_register_style( 'wp-search-suggest', plugins_url( "css/wpss-search-suggest$suffix.css", __FILE__ ), array(), $plugin_data['Version'] ); |
| 46 |
} |
| 47 |
add_action( 'init', 'wpss_init', 9 ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Enqueues the script and style. |
| 51 |
*/ |
| 52 |
function wpss_enqueue_scripts() { |
| 53 |
wp_enqueue_script( 'wp-search-suggest' ); |
| 54 |
wp_enqueue_style( 'wp-search-suggest' ); |
| 55 |
} |
| 56 |
add_action( 'wp_enqueue_scripts', 'wpss_enqueue_scripts' ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Handles the AJAX request for the search term. |
| 60 |
*/ |
| 61 |
function wpss_ajax_response() { |
| 62 |
check_ajax_referer( 'wp-search-suggest' ); |
| 63 |
|
| 64 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 65 |
$s = trim( stripslashes( $_GET['q'] ) ); |
| 66 |
|
| 67 |
$query_args = apply_filters( |
| 68 |
'wpss_search_query_args', |
| 69 |
array( |
| 70 |
's' => $s, |
| 71 |
'post_status' => 'publish', |
| 72 |
), |
| 73 |
$s |
| 74 |
); |
| 75 |
|
| 76 |
$query = new WP_Query( $query_args ); |
| 77 |
|
| 78 |
if ( $query->posts ) { |
| 79 |
$results = apply_filters( 'wpss_search_results', wp_list_pluck( $query->posts, 'post_title' ), $query ); |
| 80 |
echo wp_kses_post( join( "\n", $results ) ); |
| 81 |
} |
| 82 |
|
| 83 |
wp_die(); |
| 84 |
} |
| 85 |
add_action( 'wp_ajax_wp-search-suggest', 'wpss_ajax_response' ); |
| 86 |
add_action( 'wp_ajax_nopriv_wp-search-suggest', 'wpss_ajax_response' ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Handles the AJAX request for a specific title. |
| 90 |
*/ |
| 91 |
function wpss_post_url() { |
| 92 |
check_ajax_referer( 'wpss-post-url' ); |
| 93 |
|
| 94 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 95 |
$post = wpss_get_post_id_from_title( trim( stripslashes( $_REQUEST['title'] ) ) ); |
| 96 |
|
| 97 |
if ( $post ) { |
| 98 |
echo esc_url( get_permalink( $post ) ); |
| 99 |
} |
| 100 |
|
| 101 |
wp_die(); |
| 102 |
} |
| 103 |
add_action( 'wp_ajax_wpss-post-url', 'wpss_post_url' ); |
| 104 |
add_action( 'wp_ajax_nopriv_wpss-post-url', 'wpss_post_url' ); |
| 105 |
|
| 106 |
/** |
| 107 |
* Examines a title and tries to determine the post ID it represents.\ |
| 108 |
* |
| 109 |
* @param string $title Post title to check. |
| 110 |
* @return int Post ID or 0 on failure. |
| 111 |
*/ |
| 112 |
function wpss_get_post_id_from_title( $title ) { |
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$post_id = wp_cache_get( 'wpss_post_title' . $title, 'post' ); |
| 116 |
|
| 117 |
if ( false === $post_id ) { |
| 118 |
$post_id = $wpdb->get_var( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 119 |
$wpdb->prepare( |
| 120 |
"SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_status = 'publish' LIMIT 1", |
| 121 |
$title |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
wp_cache_set( 'wpss_post_title' . $title, $post_id, 'post' ); |
| 126 |
} |
| 127 |
|
| 128 |
return absint( $post_id ); |
| 129 |
} |
| 130 |
|