Version Description
Download this release
Release Info
Developer | dancameron |
Plugin | Search Everything |
Version | 3.0 |
Comparing to | |
See all releases |
Version 3.0
- search_everything3.0.1.php +383 -0
- search_everything3.php +383 -0
search_everything3.0.1.php
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Search Everything
|
4 |
+
Plugin URI: http://dancameron.org/wordpress/
|
5 |
+
Description: Adds search functionality with little setup. Including options to search pages, attachments, drafts, comments and custom fields (metadata).
|
6 |
+
Heavy props to <a href="http://kinrowan.net">Cori Schlegel</a> for making the admin options panel and the additional search capabilities. Thanks to <a href="http://alexking.org">Alex King</a> amongst <a href="http://blog.saddey.net">ot</a>h<a href="http://www.reaper-x.com">ers</a> for the WP 2.1 compatibility
|
7 |
+
Version: 3.02
|
8 |
+
Author: Dan Cameron
|
9 |
+
Author URI: http://dancameron.org
|
10 |
+
*/
|
11 |
+
|
12 |
+
/*
|
13 |
+
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
14 |
+
|
15 |
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
16 |
+
*/
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
//add filters based upon option settings
|
22 |
+
|
23 |
+
//logging
|
24 |
+
$logging = 0;
|
25 |
+
|
26 |
+
function SE3_log($msg) {
|
27 |
+
global $logging;
|
28 |
+
if ($logging) {
|
29 |
+
$fp = fopen("logfile.log","a+");
|
30 |
+
$date = date("Y-m-d H:i:s ");
|
31 |
+
$source = "search_everything_2 plugin: ";
|
32 |
+
fwrite($fp, "\n\n".$date."\n".$source."\n".$msg);
|
33 |
+
fclose($fp);
|
34 |
+
}
|
35 |
+
return true;
|
36 |
+
}
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
//add filters based upon option settings
|
41 |
+
if ("true" == get_option('SE3_use_page_search')) {
|
42 |
+
add_filter('posts_where', 'SE3_search_pages');
|
43 |
+
SE3_log("searching pages");
|
44 |
+
}
|
45 |
+
|
46 |
+
if ("true" == get_option('SE3_use_comment_search')) {
|
47 |
+
add_filter('posts_where', 'SE3_search_comments');
|
48 |
+
add_filter('posts_join', 'SE3_comments_join');
|
49 |
+
SE3_log("searching comments");
|
50 |
+
}
|
51 |
+
|
52 |
+
if ("true" == get_option('SE3_use_draft_search')) {
|
53 |
+
add_filter('posts_where', 'SE3_search_draft_posts');
|
54 |
+
SE3_log("searching drafts");
|
55 |
+
}
|
56 |
+
|
57 |
+
if ("true" == get_option('SE3_use_attachment_search')) {
|
58 |
+
add_filter('posts_where', 'SE3_search_attachments');
|
59 |
+
SE3_log("searching attachments");
|
60 |
+
}
|
61 |
+
|
62 |
+
if ("true" == get_option('SE3_use_metadata_search')) {
|
63 |
+
add_filter('posts_where', 'SE3_search_metadata');
|
64 |
+
add_filter('posts_join', 'SE3_search_metadata_join');
|
65 |
+
SE3_log("searching metadata");
|
66 |
+
}
|
67 |
+
|
68 |
+
//Duplicate fix provided by Tiago.Pocinho
|
69 |
+
add_filter('posts_request', 'SE3_distinct');
|
70 |
+
function SE3_distinct($query){
|
71 |
+
global $wp_query;
|
72 |
+
if (!empty($wp_query->query_vars['s'])) {
|
73 |
+
if (strstr($where, 'DISTINCT')) {}
|
74 |
+
else {
|
75 |
+
$query = str_replace('SELECT', 'SELECT DISTINCT', $query);
|
76 |
+
}
|
77 |
+
}
|
78 |
+
return $query;
|
79 |
+
}
|
80 |
+
|
81 |
+
|
82 |
+
//search pages
|
83 |
+
function SE3_search_pages($where) {
|
84 |
+
global $wp_query;
|
85 |
+
if (!empty($wp_query->query_vars['s'])) {
|
86 |
+
$where = str_replace('"', '\'', $where);
|
87 |
+
if (strstr($where, 'post_type')) { // >= v 2.1
|
88 |
+
$where = str_replace('post_type = \'post\' AND ', '', $where);
|
89 |
+
}
|
90 |
+
else { // < v 2.1
|
91 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'static\'', $where);
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
SE3_log("pages where: ".$where);
|
96 |
+
return $where;
|
97 |
+
}
|
98 |
+
|
99 |
+
//search drafts
|
100 |
+
function SE3_search_draft_posts($where) {
|
101 |
+
global $wp_query;
|
102 |
+
if (!empty($wp_query->query_vars['s'])) {
|
103 |
+
$where = str_replace('"', '\'', $where);
|
104 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'draft\'', $where);
|
105 |
+
}
|
106 |
+
|
107 |
+
SE3_log("drafts where: ".$where);
|
108 |
+
return $where;
|
109 |
+
}
|
110 |
+
|
111 |
+
//search attachments
|
112 |
+
function SE3_search_attachments($where) {
|
113 |
+
global $wp_query;
|
114 |
+
if (!empty($wp_query->query_vars['s'])) {
|
115 |
+
$where = str_replace('"', '\'', $where);
|
116 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'attachment\'', $where);
|
117 |
+
$where = str_replace('AND post_status != \'attachment\'','',$where);
|
118 |
+
}
|
119 |
+
|
120 |
+
SE3_log("attachments where: ".$where);
|
121 |
+
return $where;
|
122 |
+
}
|
123 |
+
|
124 |
+
//search comments
|
125 |
+
function SE3_search_comments($where) {
|
126 |
+
global $wp_query;
|
127 |
+
if (!empty($wp_query->query_vars['s'])) {
|
128 |
+
$where .= " OR (comment_content LIKE '%" . $wp_query->query_vars['s'] . "%') ";
|
129 |
+
}
|
130 |
+
|
131 |
+
SE3_log("comments where: ".$where);
|
132 |
+
|
133 |
+
return $where;
|
134 |
+
}
|
135 |
+
|
136 |
+
//join for searching comments
|
137 |
+
function SE3_comments_join($join) {
|
138 |
+
global $wp_query, $wpdb;
|
139 |
+
|
140 |
+
if (!empty($wp_query->query_vars['s'])) {
|
141 |
+
|
142 |
+
if ('true' == get_option('SE3_approved_comments_only')) {
|
143 |
+
$comment_approved = " AND comment_approved = '1'";
|
144 |
+
} else {
|
145 |
+
$comment_approved = '';
|
146 |
+
}
|
147 |
+
|
148 |
+
$join .= "LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID " . $comment_approved . ") ";
|
149 |
+
}
|
150 |
+
SE3_log("comments join: ".$join);
|
151 |
+
return $join;
|
152 |
+
}
|
153 |
+
|
154 |
+
//search metadata
|
155 |
+
function SE3_search_metadata($where) {
|
156 |
+
global $wp_query;
|
157 |
+
if (!empty($wp_query->query_vars['s'])) {
|
158 |
+
$where .= " OR meta_value LIKE '%" . $wp_query->query_vars['s'] . "%' ";
|
159 |
+
}
|
160 |
+
|
161 |
+
SE3_log("metadata where: ".$where);
|
162 |
+
|
163 |
+
return $where;
|
164 |
+
}
|
165 |
+
|
166 |
+
//join for searching metadata
|
167 |
+
function SE3_search_metadata_join($join) {
|
168 |
+
global $wp_query, $wpdb;
|
169 |
+
|
170 |
+
if (!empty($wp_query->query_vars['s'])) {
|
171 |
+
|
172 |
+
$join .= "LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
|
173 |
+
}
|
174 |
+
SE3_log("metadata join: ".$join);
|
175 |
+
return $join;
|
176 |
+
}
|
177 |
+
|
178 |
+
|
179 |
+
//build admin interface
|
180 |
+
function SE3_option_page() {
|
181 |
+
|
182 |
+
global $wpdb, $table_prefix;
|
183 |
+
|
184 |
+
if ( isset($_POST['SE3_update_options']) ) {
|
185 |
+
|
186 |
+
$errs = array();
|
187 |
+
|
188 |
+
if ( !empty($_POST['search_pages']) ) {
|
189 |
+
update_option('SE3_use_page_search', "true");
|
190 |
+
} else {
|
191 |
+
update_option('SE3_use_page_search', "false");
|
192 |
+
}
|
193 |
+
|
194 |
+
if ( !empty($_POST['search_comments']) ) {
|
195 |
+
update_option('SE3_use_comment_search', "true");
|
196 |
+
} else {
|
197 |
+
update_option('SE3_use_comment_search', "false");
|
198 |
+
}
|
199 |
+
|
200 |
+
if ( !empty($_POST['appvd_comments']) ) {
|
201 |
+
update_option('SE3_approved_comments_only', "true");
|
202 |
+
} else {
|
203 |
+
update_option('SE3_approved_comments_only', "false");
|
204 |
+
}
|
205 |
+
|
206 |
+
if ( !empty($_POST['search_drafts']) ) {
|
207 |
+
update_option('SE3_use_draft_search', "true");
|
208 |
+
} else {
|
209 |
+
update_option('SE3_use_draft_search', "false");
|
210 |
+
}
|
211 |
+
|
212 |
+
if ( !empty($_POST['search_attachments']) ) {
|
213 |
+
update_option('SE3_use_attachment_search', "true");
|
214 |
+
} else {
|
215 |
+
update_option('SE3_use_attachment_search', "false");
|
216 |
+
}
|
217 |
+
|
218 |
+
if ( !empty($_POST['search_metadata']) ) {
|
219 |
+
update_option('SE3_use_metadata_search', "true");
|
220 |
+
} else {
|
221 |
+
update_option('SE3_use_metadata_search', "false");
|
222 |
+
}
|
223 |
+
|
224 |
+
if ( empty($errs) ) {
|
225 |
+
echo '<div id="message" class="updated fade"><p>Options updated!</p></div>';
|
226 |
+
} else {
|
227 |
+
echo '<div id="message" class="error fade"><ul>';
|
228 |
+
foreach ( $errs as $name => $msg ) {
|
229 |
+
echo '<li>'.wptexturize($msg).'</li>';
|
230 |
+
}
|
231 |
+
echo '</ul></div>';
|
232 |
+
}
|
233 |
+
} // End if update
|
234 |
+
|
235 |
+
//set up option checkbox values
|
236 |
+
if ('true' == get_option('SE3_use_page_search')) {
|
237 |
+
$page_search = 'checked="true"';
|
238 |
+
} else {
|
239 |
+
$page_search = '';
|
240 |
+
}
|
241 |
+
|
242 |
+
if ('true' == get_option('SE3_use_comment_search')) {
|
243 |
+
$comment_search = 'checked="true"';
|
244 |
+
} else {
|
245 |
+
$comment_search = '';
|
246 |
+
}
|
247 |
+
|
248 |
+
if ('true' == get_option('SE3_approved_comments_only')) {
|
249 |
+
$appvd_comment = 'checked="true"';
|
250 |
+
} else {
|
251 |
+
$appvd_comment = '';
|
252 |
+
}
|
253 |
+
|
254 |
+
if ('true' == get_option('SE3_use_draft_search')) {
|
255 |
+
$draft_search = 'checked="true"';
|
256 |
+
} else {
|
257 |
+
$draft_search = '';
|
258 |
+
}
|
259 |
+
|
260 |
+
if ('true' == get_option('SE3_use_attachment_search')) {
|
261 |
+
$attachment_search = 'checked="true"';
|
262 |
+
} else {
|
263 |
+
$attachment_search = '';
|
264 |
+
}
|
265 |
+
|
266 |
+
if ('true' == get_option('SE3_use_metadata_search')) {
|
267 |
+
$metadata_search = 'checked="true"';
|
268 |
+
} else {
|
269 |
+
$metadata_search = '';
|
270 |
+
}
|
271 |
+
|
272 |
+
?>
|
273 |
+
|
274 |
+
<div style="width:75%;" class="wrap" id="SE3_options_panel">
|
275 |
+
<h2>Search Everything</h2>
|
276 |
+
<p>The options selected below will be used in every search query on this site; in addition to the built-in post search.</p>
|
277 |
+
<div id="searchform">
|
278 |
+
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>">
|
279 |
+
<div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
|
280 |
+
<input type="submit" id="searchsubmit" value="Test Search" />
|
281 |
+
</div>
|
282 |
+
</form>
|
283 |
+
</div>
|
284 |
+
|
285 |
+
|
286 |
+
|
287 |
+
<form method="post">
|
288 |
+
|
289 |
+
<table id="search_options" cell-spacing="2" cell-padding="2">
|
290 |
+
<tr>
|
291 |
+
<td class="col1"><input type="checkbox" name="search_pages" value="<?php echo get_option('SE3_use_page_search'); ?>" <?php echo $page_search; ?> /></td>
|
292 |
+
<td class="col2">Search Every Page</td>
|
293 |
+
</tr>
|
294 |
+
<tr>
|
295 |
+
<td class="col1"><input type="checkbox" name="search_comments" value="<?php echo get_option('SE3_use_comment_search'); ?>" <?php echo $comment_search; ?> /></td>
|
296 |
+
<td class="col2">Search Every Comment</td>
|
297 |
+
</tr>
|
298 |
+
<tr class="child_option">
|
299 |
+
<td> </td>
|
300 |
+
<td>
|
301 |
+
<table>
|
302 |
+
<tr>
|
303 |
+
<td class="col1"><input type="checkbox" name="appvd_comments" value="<?php echo get_option('SE3_approved_comments_only'); ?>" <?php echo $appvd_comment; ?> /></td>
|
304 |
+
<td class="col2">Search only Approved comments only?</td>
|
305 |
+
</tr>
|
306 |
+
</table>
|
307 |
+
</td>
|
308 |
+
</tr>
|
309 |
+
<tr>
|
310 |
+
<td class="col1"><input type="checkbox" name="search_drafts" value="<?php echo get_option('SE3_use_draft_search'); ?>" <?php echo $draft_search; ?> /></td>
|
311 |
+
<td class="col2">Search Every Draft</td>
|
312 |
+
</tr>
|
313 |
+
<tr>
|
314 |
+
<td class="col1"><input type="checkbox" name="search_attachments" value="<?php echo get_option('SE3_use_attachment_search'); ?>" <?php echo $attachment_search; ?> /></td>
|
315 |
+
<td class="col2">Search Every Attachment Title and Description</td>
|
316 |
+
</tr>
|
317 |
+
<tr>
|
318 |
+
<td class="col1"><input type="checkbox" name="search_metadata" value="<?php echo get_option('SE3_use_metadata_search'); ?>" <?php echo $metadata_search; ?> /></td>
|
319 |
+
<td class="col2">Search Custom Fields (Metadata)</td>
|
320 |
+
</tr>
|
321 |
+
</table>
|
322 |
+
|
323 |
+
<p class="submit">
|
324 |
+
<input type="submit" name="SE3_update_options" value="Save"/>
|
325 |
+
</p>You may have to update your options twice before it sticks.
|
326 |
+
</form>
|
327 |
+
|
328 |
+
</div>
|
329 |
+
|
330 |
+
<?php
|
331 |
+
} //end SE3_option_page
|
332 |
+
|
333 |
+
function SE3_add_options_panel() {
|
334 |
+
add_options_page('Search Everything', 'Search Everything', 'edit_plugins', 'SE3_options_page', 'SE3_option_page');
|
335 |
+
}
|
336 |
+
add_action('admin_menu', 'SE3_add_options_panel');
|
337 |
+
|
338 |
+
//styling options page
|
339 |
+
function SE3_options_style() {
|
340 |
+
?>
|
341 |
+
<style type="text/css">
|
342 |
+
|
343 |
+
table#search_options {
|
344 |
+
table-layout: auto;
|
345 |
+
}
|
346 |
+
|
347 |
+
|
348 |
+
#search_options td.col1, #search_options th.col1 {
|
349 |
+
width: 30px;
|
350 |
+
text-align: left;
|
351 |
+
}
|
352 |
+
|
353 |
+
#search_options td.col2, #search_options th.col2 {
|
354 |
+
width: 220px;
|
355 |
+
margin-left: -15px;
|
356 |
+
text-align: left;
|
357 |
+
}
|
358 |
+
|
359 |
+
#search_options tr.child_option {
|
360 |
+
margin-left: 15px;
|
361 |
+
margin-top: -3px;
|
362 |
+
}
|
363 |
+
|
364 |
+
#SE3_options_panel p.submit {
|
365 |
+
text-align: left;
|
366 |
+
}
|
367 |
+
|
368 |
+
div#searchform div {
|
369 |
+
margin-left: auto;
|
370 |
+
margin-right: auto;
|
371 |
+
margin-top: 5px;
|
372 |
+
margin-bottom: 5px;
|
373 |
+
}
|
374 |
+
|
375 |
+
</style>
|
376 |
+
|
377 |
+
<?php
|
378 |
+
}
|
379 |
+
|
380 |
+
|
381 |
+
add_action('admin_head', 'SE3_options_style');
|
382 |
+
|
383 |
+
?>
|
search_everything3.php
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Search Everything
|
4 |
+
Plugin URI: http://dancameron.org/wordpress/
|
5 |
+
Description: Adds search functionality with little setup. Including options to search pages, attachments, drafts, comments and custom fields (metadata).
|
6 |
+
Heavy props to <a href="http://kinrowan.net">Cori Schlegel</a> for making the admin options panel and the additional search capabilities. Thanks to <a href="http://alexking.org">Alex King</a> amongst <a href="http://blog.saddey.net">ot</a>h<a href="http://www.reaper-x.com">ers</a> for the WP 2.1 compatibility
|
7 |
+
Version: 3.01
|
8 |
+
Author: Dan Cameron
|
9 |
+
Author URI: http://dancameron.org
|
10 |
+
*/
|
11 |
+
|
12 |
+
/*
|
13 |
+
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
14 |
+
|
15 |
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
16 |
+
*/
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
//add filters based upon option settings
|
22 |
+
|
23 |
+
//logging
|
24 |
+
$logging = 0;
|
25 |
+
|
26 |
+
function SE3_log($msg) {
|
27 |
+
global $logging;
|
28 |
+
if ($logging) {
|
29 |
+
$fp = fopen("logfile.log","a+");
|
30 |
+
$date = date("Y-m-d H:i:s ");
|
31 |
+
$source = "search_everything_2 plugin: ";
|
32 |
+
fwrite($fp, "\n\n".$date."\n".$source."\n".$msg);
|
33 |
+
fclose($fp);
|
34 |
+
}
|
35 |
+
return true;
|
36 |
+
}
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
//add filters based upon option settings
|
41 |
+
if ("true" == get_option('SE3_use_page_search')) {
|
42 |
+
add_filter('posts_where', 'SE3_search_pages');
|
43 |
+
SE3_log("searching pages");
|
44 |
+
}
|
45 |
+
|
46 |
+
if ("true" == get_option('SE3_use_comment_search')) {
|
47 |
+
add_filter('posts_where', 'SE3_search_comments');
|
48 |
+
add_filter('posts_join', 'SE3_comments_join');
|
49 |
+
SE3_log("searching comments");
|
50 |
+
}
|
51 |
+
|
52 |
+
if ("true" == get_option('SE3_use_draft_search')) {
|
53 |
+
add_filter('posts_where', 'SE3_search_draft_posts');
|
54 |
+
SE3_log("searching drafts");
|
55 |
+
}
|
56 |
+
|
57 |
+
if ("true" == get_option('SE3_use_attachment_search')) {
|
58 |
+
add_filter('posts_where', 'SE3_search_attachments');
|
59 |
+
SE3_log("searching attachments");
|
60 |
+
}
|
61 |
+
|
62 |
+
if ("true" == get_option('SE3_use_metadata_search')) {
|
63 |
+
add_filter('posts_where', 'SE3_search_metadata');
|
64 |
+
add_filter('posts_join', 'SE3_search_metadata_join');
|
65 |
+
SE3_log("searching metadata");
|
66 |
+
}
|
67 |
+
|
68 |
+
//Duplicate fix provided by Tiago.Pocinho
|
69 |
+
add_filter('posts_request', 'SE3_distinct');
|
70 |
+
function SE3_distinct($query){
|
71 |
+
global $wp_query;
|
72 |
+
if (!empty($wp_query->query_vars['s'])) {
|
73 |
+
if (strstr($where, 'DISTINCT')) {}
|
74 |
+
else {
|
75 |
+
$query = str_replace('SELECT', 'SELECT DISTINCT', $query);
|
76 |
+
}
|
77 |
+
}
|
78 |
+
return $query;
|
79 |
+
}
|
80 |
+
|
81 |
+
|
82 |
+
//search pages
|
83 |
+
function SE3_search_pages($where) {
|
84 |
+
global $wp_query;
|
85 |
+
if (!empty($wp_query->query_vars['s'])) {
|
86 |
+
$where = str_replace('"', '\'', $where);
|
87 |
+
if (strstr($where, 'post_type')) { // >= v 2.1
|
88 |
+
$where = str_replace('post_type = \'post\' AND ', '', $where);
|
89 |
+
}
|
90 |
+
else { // < v 2.1
|
91 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'static\'', $where);
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
SE3_log("pages where: ".$where);
|
96 |
+
return $where;
|
97 |
+
}
|
98 |
+
|
99 |
+
//search drafts
|
100 |
+
function SE3_search_draft_posts($where) {
|
101 |
+
global $wp_query;
|
102 |
+
if (!empty($wp_query->query_vars['s'])) {
|
103 |
+
$where = str_replace('"', '\'', $where);
|
104 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'draft\'', $where);
|
105 |
+
}
|
106 |
+
|
107 |
+
SE3_log("drafts where: ".$where);
|
108 |
+
return $where;
|
109 |
+
}
|
110 |
+
|
111 |
+
//search attachments
|
112 |
+
function SE3_search_attachments($where) {
|
113 |
+
global $wp_query;
|
114 |
+
if (!empty($wp_query->query_vars['s'])) {
|
115 |
+
$where = str_replace('"', '\'', $where);
|
116 |
+
$where = str_replace(' AND (post_status = \'publish\'', ' AND (post_status = \'publish\' or post_status = \'attachment\'', $where);
|
117 |
+
$where = str_replace('AND post_status != \'attachment\'','',$where);
|
118 |
+
}
|
119 |
+
|
120 |
+
SE3_log("attachments where: ".$where);
|
121 |
+
return $where;
|
122 |
+
}
|
123 |
+
|
124 |
+
//search comments
|
125 |
+
function SE3_search_comments($where) {
|
126 |
+
global $wp_query;
|
127 |
+
if (!empty($wp_query->query_vars['s'])) {
|
128 |
+
$where .= " OR (comment_content LIKE '%" . $wp_query->query_vars['s'] . "%') ";
|
129 |
+
}
|
130 |
+
|
131 |
+
SE3_log("comments where: ".$where);
|
132 |
+
|
133 |
+
return $where;
|
134 |
+
}
|
135 |
+
|
136 |
+
//join for searching comments
|
137 |
+
function SE3_comments_join($join) {
|
138 |
+
global $wp_query, $wpdb;
|
139 |
+
|
140 |
+
if (!empty($wp_query->query_vars['s'])) {
|
141 |
+
|
142 |
+
if ('true' == get_option('SE3_approved_comments_only')) {
|
143 |
+
$comment_approved = " AND comment_approved = '1'";
|
144 |
+
} else {
|
145 |
+
$comment_approved = '';
|
146 |
+
}
|
147 |
+
|
148 |
+
$join .= "LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID " . $comment_approved . ") ";
|
149 |
+
}
|
150 |
+
SE3_log("comments join: ".$join);
|
151 |
+
return $join;
|
152 |
+
}
|
153 |
+
|
154 |
+
//search metadata
|
155 |
+
function SE3_search_metadata($where) {
|
156 |
+
global $wp_query;
|
157 |
+
if (!empty($wp_query->query_vars['s'])) {
|
158 |
+
$where .= " OR meta_value LIKE '%" . $wp_query->query_vars['s'] . "%' ";
|
159 |
+
}
|
160 |
+
|
161 |
+
SE3_log("metadata where: ".$where);
|
162 |
+
|
163 |
+
return $where;
|
164 |
+
}
|
165 |
+
|
166 |
+
//join for searching metadata
|
167 |
+
function SE3_search_metadata_join($join) {
|
168 |
+
global $wp_query, $wpdb;
|
169 |
+
|
170 |
+
if (!empty($wp_query->query_vars['s'])) {
|
171 |
+
|
172 |
+
$join .= "LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
|
173 |
+
}
|
174 |
+
SE3_log("metadata join: ".$join);
|
175 |
+
return $join;
|
176 |
+
}
|
177 |
+
|
178 |
+
|
179 |
+
//build admin interface
|
180 |
+
function SE3_option_page() {
|
181 |
+
|
182 |
+
global $wpdb, $table_prefix;
|
183 |
+
|
184 |
+
if ( isset($_POST['SE3_update_options']) ) {
|
185 |
+
|
186 |
+
$errs = array();
|
187 |
+
|
188 |
+
if ( !empty($_POST['search_pages']) ) {
|
189 |
+
update_option('SE3_use_page_search', "true");
|
190 |
+
} else {
|
191 |
+
update_option('SE3_use_page_search', "false");
|
192 |
+
}
|
193 |
+
|
194 |
+
if ( !empty($_POST['search_comments']) ) {
|
195 |
+
update_option('SE3_use_comment_search', "true");
|
196 |
+
} else {
|
197 |
+
update_option('SE3_use_comment_search', "false");
|
198 |
+
}
|
199 |
+
|
200 |
+
if ( !empty($_POST['appvd_comments']) ) {
|
201 |
+
update_option('SE3_approved_comments_only', "true");
|
202 |
+
} else {
|
203 |
+
update_option('SE3_approved_comments_only', "false");
|
204 |
+
}
|
205 |
+
|
206 |
+
if ( !empty($_POST['search_drafts']) ) {
|
207 |
+
update_option('SE3_use_draft_search', "true");
|
208 |
+
} else {
|
209 |
+
update_option('SE3_use_draft_search', "false");
|
210 |
+
}
|
211 |
+
|
212 |
+
if ( !empty($_POST['search_attachments']) ) {
|
213 |
+
update_option('SE3_use_attachment_search', "true");
|
214 |
+
} else {
|
215 |
+
update_option('SE3_use_attachment_search', "false");
|
216 |
+
}
|
217 |
+
|
218 |
+
if ( !empty($_POST['search_metadata']) ) {
|
219 |
+
update_option('SE3_use_metadata_search', "true");
|
220 |
+
} else {
|
221 |
+
update_option('SE3_use_metadata_search', "false");
|
222 |
+
}
|
223 |
+
|
224 |
+
if ( empty($errs) ) {
|
225 |
+
echo '<div id="message" class="updated fade"><p>Options updated!</p></div>';
|
226 |
+
} else {
|
227 |
+
echo '<div id="message" class="error fade"><ul>';
|
228 |
+
foreach ( $errs as $name => $msg ) {
|
229 |
+
echo '<li>'.wptexturize($msg).'</li>';
|
230 |
+
}
|
231 |
+
echo '</ul></div>';
|
232 |
+
}
|
233 |
+
} // End if update
|
234 |
+
|
235 |
+
//set up option checkbox values
|
236 |
+
if ('true' == get_option('SE3_use_page_search')) {
|
237 |
+
$page_search = 'checked="true"';
|
238 |
+
} else {
|
239 |
+
$page_search = '';
|
240 |
+
}
|
241 |
+
|
242 |
+
if ('true' == get_option('SE3_use_comment_search')) {
|
243 |
+
$comment_search = 'checked="true"';
|
244 |
+
} else {
|
245 |
+
$comment_search = '';
|
246 |
+
}
|
247 |
+
|
248 |
+
if ('true' == get_option('SE3_approved_comments_only')) {
|
249 |
+
$appvd_comment = 'checked="true"';
|
250 |
+
} else {
|
251 |
+
$appvd_comment = '';
|
252 |
+
}
|
253 |
+
|
254 |
+
if ('true' == get_option('SE3_use_draft_search')) {
|
255 |
+
$draft_search = 'checked="true"';
|
256 |
+
} else {
|
257 |
+
$draft_search = '';
|
258 |
+
}
|
259 |
+
|
260 |
+
if ('true' == get_option('SE3_use_attachment_search')) {
|
261 |
+
$attachment_search = 'checked="true"';
|
262 |
+
} else {
|
263 |
+
$attachment_search = '';
|
264 |
+
}
|
265 |
+
|
266 |
+
if ('true' == get_option('SE3_use_metadata_search')) {
|
267 |
+
$metadata_search = 'checked="true"';
|
268 |
+
} else {
|
269 |
+
$metadata_search = '';
|
270 |
+
}
|
271 |
+
|
272 |
+
?>
|
273 |
+
|
274 |
+
<div style="width:75%;" class="wrap" id="SE3_options_panel">
|
275 |
+
<h2>Search Everything</h2>
|
276 |
+
<p>The options selected below will be used in every search query on this site; in addition to the built-in post search.</p>
|
277 |
+
<div id="searchform">
|
278 |
+
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>">
|
279 |
+
<div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
|
280 |
+
<input type="submit" id="searchsubmit" value="Test Search" />
|
281 |
+
</div>
|
282 |
+
</form>
|
283 |
+
</div>
|
284 |
+
|
285 |
+
|
286 |
+
|
287 |
+
<form method="post">
|
288 |
+
|
289 |
+
<table id="search_options" cell-spacing="2" cell-padding="2">
|
290 |
+
<tr>
|
291 |
+
<td class="col1"><input type="checkbox" name="search_pages" value="<?php echo get_option('SE3_use_page_search'); ?>" <?php echo $page_search; ?> /></td>
|
292 |
+
<td class="col2">Search Every Page</td>
|
293 |
+
</tr>
|
294 |
+
<tr>
|
295 |
+
<td class="col1"><input type="checkbox" name="search_comments" value="<?php echo get_option('SE3_use_comment_search'); ?>" <?php echo $comment_search; ?> /></td>
|
296 |
+
<td class="col2">Search Every Comment</td>
|
297 |
+
</tr>
|
298 |
+
<tr class="child_option">
|
299 |
+
<td> </td>
|
300 |
+
<td>
|
301 |
+
<table>
|
302 |
+
<tr>
|
303 |
+
<td class="col1"><input type="checkbox" name="appvd_comments" value="<?php echo get_option('SE3_approved_comments_only'); ?>" <?php echo $appvd_comment; ?> /></td>
|
304 |
+
<td class="col2">Search only Approved comments only?</td>
|
305 |
+
</tr>
|
306 |
+
</table>
|
307 |
+
</td>
|
308 |
+
</tr>
|
309 |
+
<tr>
|
310 |
+
<td class="col1"><input type="checkbox" name="search_drafts" value="<?php echo get_option('SE3_use_draft_search'); ?>" <?php echo $draft_search; ?> /></td>
|
311 |
+
<td class="col2">Search Every Draft</td>
|
312 |
+
</tr>
|
313 |
+
<tr>
|
314 |
+
<td class="col1"><input type="checkbox" name="search_attachments" value="<?php echo get_option('SE3_use_attachment_search'); ?>" <?php echo $attachment_search; ?> /></td>
|
315 |
+
<td class="col2">Search Every Attachment</td>
|
316 |
+
</tr>
|
317 |
+
<tr>
|
318 |
+
<td class="col1"><input type="checkbox" name="search_metadata" value="<?php echo get_option('SE3_use_metadata_search'); ?>" <?php echo $metadata_search; ?> /></td>
|
319 |
+
<td class="col2">Search Custom Fields (Metadata)</td>
|
320 |
+
</tr>
|
321 |
+
</table>
|
322 |
+
|
323 |
+
<p class="submit">
|
324 |
+
<input type="submit" name="SE3_update_options" value="Save"/>
|
325 |
+
</p>You may have to update your options twice before it sticks.
|
326 |
+
</form>
|
327 |
+
|
328 |
+
</div>
|
329 |
+
|
330 |
+
<?php
|
331 |
+
} //end SE3_option_page
|
332 |
+
|
333 |
+
function SE3_add_options_panel() {
|
334 |
+
add_options_page('Search Everything', 'Search Everything', 1, 'SE3_options_page', 'SE3_option_page');
|
335 |
+
}
|
336 |
+
add_action('admin_menu', 'SE3_add_options_panel');
|
337 |
+
|
338 |
+
//styling options page
|
339 |
+
function SE3_options_style() {
|
340 |
+
?>
|
341 |
+
<style type="text/css">
|
342 |
+
|
343 |
+
table#search_options {
|
344 |
+
table-layout: auto;
|
345 |
+
}
|
346 |
+
|
347 |
+
|
348 |
+
#search_options td.col1, #search_options th.col1 {
|
349 |
+
width: 30px;
|
350 |
+
text-align: left;
|
351 |
+
}
|
352 |
+
|
353 |
+
#search_options td.col2, #search_options th.col2 {
|
354 |
+
width: 220px;
|
355 |
+
margin-left: -15px;
|
356 |
+
text-align: left;
|
357 |
+
}
|
358 |
+
|
359 |
+
#search_options tr.child_option {
|
360 |
+
margin-left: 15px;
|
361 |
+
margin-top: -3px;
|
362 |
+
}
|
363 |
+
|
364 |
+
#SE3_options_panel p.submit {
|
365 |
+
text-align: left;
|
366 |
+
}
|
367 |
+
|
368 |
+
div#searchform div {
|
369 |
+
margin-left: auto;
|
370 |
+
margin-right: auto;
|
371 |
+
margin-top: 5px;
|
372 |
+
margin-bottom: 5px;
|
373 |
+
}
|
374 |
+
|
375 |
+
</style>
|
376 |
+
|
377 |
+
<?php
|
378 |
+
}
|
379 |
+
|
380 |
+
|
381 |
+
add_action('admin_head', 'SE3_options_style');
|
382 |
+
|
383 |
+
?>
|