forked from obenland/wp-search-suggest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-search-suggest.php
159 lines (129 loc) · 4.42 KB
/
wp-search-suggest.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
<?php
/** wp-search-suggest.php
*
* Plugin Name: WP Search Suggest
* Plugin URI: http://en.obenland.it/wp-search-suggest/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-search-suggest
* Description: Provides title suggestions while typing a search query, using the built in jQuery suggest script.
* Version: 2.0.1
* Author: Konstantin Obenland
* Author URI: http://en.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-search-suggest
* Text Domain: wp-search-suggest
* Domain Path: /lang
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! class_exists( 'Obenland_Wp_Plugins_v301' ) ) {
require_once( 'obenland-wp-plugins.php' );
}
class Obenland_Wp_Search_Suggest extends Obenland_Wp_Plugins_v301 {
///////////////////////////////////////////////////////////////////////////
// METHODS, PUBLIC
///////////////////////////////////////////////////////////////////////////
/**
* Constructor
*
* @author Konstantin Obenland
* @since 1.0 - 16.04.2011
* @access public
*
* @return Obenland_Wp_Search_Suggest
*/
public function __construct() {
parent::__construct( array(
'textdomain' => 'wp-search-suggest',
'plugin_path' => __FILE__,
'donate_link_id' => 'TLX9TH5XRURBA',
) );
$this->hook( 'wp_ajax_wp-search-suggest', 'ajax_response' );
$this->hook( 'wp_ajax_nopriv_wp-search-suggest', 'ajax_response' );
$this->hook( 'wp_ajax_wpss-post-url', 'post_url' );
$this->hook( 'wp_ajax_nopriv_wpss-post-url', 'post_url' );
$this->hook( 'init', 9 ); // Set to 9, so they can easily be deregistered
$this->hook( 'wp_enqueue_scripts' );
}
/**
* Registers the script and stylesheet
*
* The scripts and stylesheets can easily be deregeistered be calling
* <code>wp_deregister_script( 'wp-search-suggest' );</code> or
* <code>wp_deregister_style( 'wp-search-suggest' );</code> on the init
* hook
*
* @author Konstantin Obenland
* @since 1.0 - 16.04.2011
* @access public
*
* @return void
*/
public function init() {
$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ), 'plugin' );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
wp_register_script( $this->textdomain, plugins_url( "js/wpss-search-suggest$suffix.js", __FILE__ ), array( 'suggest' ), $plugin_data['Version'], true );
wp_localize_script( $this->textdomain, 'wpss_options', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wpss-post-url' ),
'ajaxurl' => add_query_arg( array(
'action' => $this->textdomain,
'_wpnonce' => wp_create_nonce( $this->textdomain )
), admin_url( 'admin-ajax.php' ) ),
) );
wp_register_style( $this->textdomain, plugins_url( "css/wpss-search-suggest$suffix.css", __FILE__ ), array(), $plugin_data['Version'] );
}
/**
* Enqueues the script and style
*
* @author Konstantin Obenland
* @since 1.0 - 16.04.2011
* @access public
*
* @return void
*/
public function wp_enqueue_scripts() {
wp_enqueue_script( $this->textdomain );
wp_enqueue_style( $this->textdomain );
}
/**
* Handles the AJAX request for the search term.
*
* @author Konstantin Obenland
* @since 1.0 - 16.04.2011
* @access public
*
* @return void
*/
public function ajax_response() {
check_ajax_referer( $this->textdomain );
$s = trim( stripslashes( $_GET['q'] ) );
$query_args = apply_filters( 'wpss_search_query_args', array(
's' => $s,
'post_status' => 'publish',
), $s );
$query = new WP_Query( $query_args );
if ( $query->posts ) {
$results = apply_filters( 'wpss_search_results', wp_list_pluck( $query->posts, 'post_title' ), $query );
echo join( $results, "\n" );
}
wp_die();
}
/**
* Handles the AJAX request for a specific title.
*
* @author Konstantin Obenland
* @since 2.0.0 - 29.12.2013
* @access public
*
* @return void
*/
public function post_url() {
check_ajax_referer( 'wpss-post-url' );
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s LIMIT 1", trim( stripslashes($_REQUEST['title'] ) ) ) );
if ( $post ) {
echo get_permalink( $post );
}
wp_die();
}
} // End of class Obenland_Wp_Search_Suggest
new Obenland_Wp_Search_Suggest;
/* End of file wp-search-suggest.php */
/* Location: ./wp-content/plugins/wp-search-suggest/wp-search-suggest.php */