PageRenderTime 62ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/site_v1.0/wordpress/wp-content/plugins/search-everything/search-everything.php

http://pumpmyvote.googlecode.com/
PHP | 804 lines | 713 code | 48 blank | 43 comment | 108 complexity | e3301906c1c4e29612514f4176eccb96 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, AGPL-1.0, GPL-2.0
  1. <?php
  2. /*
  3. Plugin Name: Search Everything
  4. Plugin URI: https://core.sproutventure.com/projects/show/search-everything
  5. Description: Adds search functionality without modifying any template pages: Activate, Configure and Search. Options Include: search highlight, search pages, excerpts, attachments, drafts, comments, tags and custom fields (metadata). Also offers the ability to exclude specific pages and posts. Does not search password-protected content.
  6. Version: 6.6
  7. Author: Dan Cameron of Sprout Venture
  8. Author URI: http://sproutventure.com/
  9. */
  10. /*
  11. 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.
  12. 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.
  13. */
  14. if ( !defined('WP_CONTENT_DIR') )
  15. define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  16. if (!defined('DIRECTORY_SEPARATOR'))
  17. {
  18. if (strpos(php_uname('s'), 'Win') !== false )
  19. define('DIRECTORY_SEPARATOR', '\\');
  20. else
  21. define('DIRECTORY_SEPARATOR', '/');
  22. }
  23. define('SE_ABSPATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  24. $SE = new SearchEverything();
  25. //add filters based upon option settings
  26. Class SearchEverything {
  27. var $logging = false;
  28. var $options;
  29. var $wp_ver23;
  30. var $wp_ver25;
  31. var $wp_ver28;
  32. function SearchEverything(){
  33. global $wp_version;
  34. $this->wp_ver23 = ($wp_version >= '2.3');
  35. $this->wp_ver25 = ($wp_version >= '2.5');
  36. $this->wp_ver28 = ($wp_version >= '2.8');
  37. $this->options = get_option('se_options');
  38. if (is_admin()) {
  39. include ( SE_ABSPATH . 'views/options.php' );
  40. $SEAdmin = new se_admin();
  41. }
  42. //add filters based upon option settings
  43. if ("Yes" == $this->options['se_use_tag_search'] || "Yes" == $this->options['se_use_category_search'] || "Yes" == $this->options['se_use_tax_search'])
  44. {
  45. add_filter('posts_join', array(&$this, 'se_terms_join'));
  46. if ("Yes" == $this->options['se_use_tag_search'])
  47. {
  48. $this->se_log("searching tags");
  49. }
  50. if ("Yes" == $this->options['se_use_category_search'])
  51. {
  52. $this->se_log("searching categories");
  53. }
  54. if ("Yes" == $this->options['se_use_tax_search'])
  55. {
  56. $this->se_log("searching custom taxonomies");
  57. }
  58. }
  59. if ("Yes" == $this->options['se_use_page_search'])
  60. {
  61. add_filter('posts_where', array(&$this, 'se_search_pages'));
  62. $this->se_log("searching pages");
  63. }
  64. if ("Yes" == $this->options['se_use_excerpt_search'])
  65. {
  66. $this->se_log("searching excerpts");
  67. }
  68. if ("Yes" == $this->options['se_use_comment_search'])
  69. {
  70. add_filter('posts_join', array(&$this, 'se_comments_join'));
  71. $this->se_log("searching comments");
  72. // Highlight content
  73. if("Yes" == $this->options['se_use_highlight'])
  74. {
  75. add_filter('comment_text', array(&$this,'se_postfilter'));
  76. }
  77. }
  78. if ("Yes" == $this->options['se_use_draft_search'])
  79. {
  80. add_filter('posts_where', array(&$this, 'se_search_draft_posts'));
  81. $this->se_log("searching drafts");
  82. }
  83. if ("Yes" == $this->options['se_use_attachment_search'])
  84. {
  85. add_filter('posts_where', array(&$this, 'se_search_attachments'));
  86. $this->se_log("searching attachments");
  87. }
  88. if ("Yes" == $this->options['se_use_metadata_search'])
  89. {
  90. add_filter('posts_join', array(&$this, 'se_search_metadata_join'));
  91. $this->se_log("searching metadata");
  92. }
  93. if ($this->options['se_exclude_posts_list'] != '')
  94. {
  95. $this->se_log("searching excluding posts");
  96. }
  97. if ($this->options['se_exclude_categories_list'] != '')
  98. {
  99. add_filter('posts_join', array(&$this, 'se_exclude_categories_join'));
  100. $this->se_log("searching excluding categories");
  101. }
  102. if ("Yes" == $this->options['se_use_authors'] && !$this->wp_ver28)// Need some help getting this back into the mix
  103. {
  104. add_filter('posts_where', array(&$this, 'se_search_authors'));
  105. add_filter('posts_join', array(&$this, 'se_search_authors_join'));
  106. $this->se_log("searching authors");
  107. }
  108. add_filter('posts_search', array(&$this, 'se_search_where'));
  109. add_filter('posts_where', array(&$this, 'se_no_revisions'));
  110. add_filter('posts_request', array(&$this, 'se_distinct'));
  111. add_filter('posts_where', array(&$this, 'se_no_future'));
  112. // Highlight content
  113. if("Yes" == $this->options['se_use_highlight'])
  114. {
  115. add_filter('the_content', array(&$this,'se_postfilter'), 11);
  116. add_filter('the_title', array(&$this,'se_postfilter'), 11);
  117. add_filter('the_excerpt', array(&$this,'se_postfilter'), 11);
  118. }
  119. }
  120. // creates the list of search keywords from the 's' parameters.
  121. function se_get_search_terms()
  122. {
  123. global $wp_query, $wpdb;
  124. $s = $wp_query->query_vars['s'];
  125. $sentence = $wp_query->query_vars['sentence'];
  126. $search_terms = array();
  127. if ( !empty($s) )
  128. {
  129. // added slashes screw with quote grouping when done early, so done later
  130. $s = stripslashes($s);
  131. if ($sentence)
  132. {
  133. $search_terms = array($s);
  134. } else {
  135. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
  136. $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  137. }
  138. }
  139. return $search_terms;
  140. }
  141. // add where clause to the search query
  142. function se_search_where($where)
  143. {
  144. global $wp_query, $wpdb;
  145. $searchQuery = '';
  146. //add filters based upon option settings
  147. if ("Yes" == $this->options['se_use_tag_search'])
  148. {
  149. $searchQuery .= $this->se_build_search_tag();
  150. }
  151. if ("Yes" == $this->options['se_use_category_search'])
  152. {
  153. $searchQuery .= $this->se_build_search_categories();
  154. }
  155. if ("Yes" == $this->options['se_use_metadata_search'])
  156. {
  157. $searchQuery .= $this->se_build_search_metadata();
  158. }
  159. if ("Yes" == $this->options['se_use_excerpt_search'])
  160. {
  161. $searchQuery .= $this->se_build_search_excerpt();
  162. }
  163. if ("Yes" == $this->options['se_use_comment_search'])
  164. {
  165. $searchQuery .= $this->se_build_search_comments();
  166. }
  167. if ($searchQuery != '')
  168. {
  169. $where = str_replace( ")))", ")$searchQuery))", $where );
  170. }
  171. if ($this->options['se_exclude_posts_list'] != '')
  172. {
  173. $where .= $this->se_build_exclude_posts();
  174. }
  175. if ($this->options['se_exclude_categories_list'] != '')
  176. {
  177. $where .= $this->se_build_exclude_categories();
  178. }
  179. $this->se_log("global where: ".$where);
  180. return $where;
  181. }
  182. // Exclude post revisions
  183. function se_no_revisions($where)
  184. {
  185. global $wp_query, $wpdb;
  186. if (!empty($wp_query->query_vars['s']))
  187. {
  188. if(!$this->wp_ver28)
  189. {
  190. $where = 'AND (' . substr($where, strpos($where, 'AND')+3) . ") AND $wpdb->posts.post_type != 'revision'";
  191. }
  192. $where = 'AND (' . substr($where, strpos($where, 'AND')+3) . ') AND post_type != \'revision\'';
  193. }
  194. return $where;
  195. }
  196. // Exclude future posts fix provided by Mx
  197. function se_no_future($where)
  198. {
  199. global $wp_query, $wpdb;
  200. if (!empty($wp_query->query_vars['s']))
  201. {
  202. if(!$this->wp_ver28)
  203. {
  204. $where = 'AND (' . substr($where, strpos($where, 'AND')+3) . ") AND $wpdb->posts.post_status != 'future'";
  205. }
  206. $where = 'AND (' . substr($where, strpos($where, 'AND')+3) . ') AND post_status != \'future\'';
  207. }
  208. return $where;
  209. }
  210. // Logs search into a file
  211. function se_log($msg)
  212. {
  213. if ($this->logging)
  214. {
  215. $fp = fopen( SE_ABSPATH . "logfile.log","a+");
  216. if ( !$fp )
  217. {
  218. echo 'unable to write to log file!';
  219. }
  220. $date = date("Y-m-d H:i:s ");
  221. $source = "search_everything plugin: ";
  222. fwrite($fp, "\n\n".$date."\n".$source."\n".$msg);
  223. fclose($fp);
  224. }
  225. return true;
  226. }
  227. //Duplicate fix provided by Tiago.Pocinho
  228. function se_distinct($query)
  229. {
  230. global $wp_query, $wpdb;
  231. if (!empty($wp_query->query_vars['s']))
  232. {
  233. if (strstr($where, 'DISTINCT'))
  234. {}
  235. else
  236. {
  237. $query = str_replace('SELECT', 'SELECT DISTINCT', $query);
  238. }
  239. }
  240. return $query;
  241. }
  242. //search pages (except password protected pages provided by loops)
  243. function se_search_pages($where)
  244. {
  245. global $wp_query, $wpdb;
  246. if (!empty($wp_query->query_vars['s']))
  247. {
  248. $where = str_replace('"', '\'', $where);
  249. if ('Yes' == $this->options['se_approved_pages_only'])
  250. {
  251. $where = str_replace("post_type = 'post'", " AND 'post_password = '' AND ", $where);
  252. } else { // < v 2.1
  253. $where = str_replace('post_type = \'post\' AND ', '', $where);
  254. }
  255. }
  256. $this->se_log("pages where: ".$where);
  257. return $where;
  258. }
  259. // create the search excerpts query
  260. function se_build_search_excerpt()
  261. {
  262. global $wp_query, $wpdb;
  263. $s = $wp_query->query_vars['s'];
  264. $search_terms = $this->se_get_search_terms();
  265. $exact = $wp_query->query_vars['exact'];
  266. $search = '';
  267. if ( !empty($search_terms) ) {
  268. // Building search query
  269. $n = ($exact) ? '' : '%';
  270. $searchand = '';
  271. foreach($search_terms as $term) {
  272. $term = addslashes_gpc($term);
  273. $search .= "{$searchand}($wpdb->posts.post_excerpt LIKE '{$n}{$term}{$n}')";
  274. $searchand = ' AND ';
  275. }
  276. $sentence_term = $wpdb->escape($s);
  277. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  278. {
  279. $search = "($search) OR ($wpdb->posts.post_excerpt LIKE '{$n}{$sentence_term}{$n}')";
  280. }
  281. if ( !empty($search) )
  282. $search = " OR ({$search}) ";
  283. }
  284. $this->se_log("excerpt where: ".$where);
  285. return $search;
  286. }
  287. //search drafts
  288. function se_search_draft_posts($where)
  289. {
  290. global $wp_query, $wpdb;
  291. if (!empty($wp_query->query_vars['s']))
  292. {
  293. $where = str_replace('"', '\'', $where);
  294. if(!$this->wp_ver28)
  295. {
  296. $where = str_replace(" AND (post_status = 'publish'", " AND ((post_status = 'publish' OR post_status = 'draft')", $where);
  297. }
  298. else
  299. {
  300. $where = str_replace(" AND ($wpdb->posts.post_status = 'publish'", " AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'draft'", $where);
  301. }
  302. $where = str_replace(" AND (post_status = 'publish'", " AND (post_status = 'publish' OR post_status = 'draft'", $where);
  303. }
  304. $this->se_log("drafts where: ".$where);
  305. return $where;
  306. }
  307. //search attachments
  308. function se_search_attachments($where)
  309. {
  310. global $wp_query, $wpdb;
  311. if (!empty($wp_query->query_vars['s']))
  312. {
  313. $where = str_replace('"', '\'', $where);
  314. if(!$this->wp_ver28)
  315. {
  316. $where = str_replace(" AND (post_status = 'publish'", " AND (post_status = 'publish' OR post_status = 'attachment'", $where);
  317. $where = str_replace("AND post_status != 'attachment'","",$where);
  318. }
  319. else
  320. {
  321. $where = str_replace(" AND ($wpdb->posts.post_status = 'publish'", " AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'attachment'", $where);
  322. $where = str_replace("AND $wpdb->posts.post_status != 'attachment'","",$where);
  323. }
  324. }
  325. $this->se_log("attachments where: ".$where);
  326. return $where;
  327. }
  328. // create the comments data query
  329. function se_build_search_comments()
  330. {
  331. global $wp_query, $wpdb;
  332. $s = $wp_query->query_vars['s'];
  333. $search_terms = $this->se_get_search_terms();
  334. $exact = $wp_query->query_vars['exact'];
  335. if ( !empty($search_terms) ) {
  336. // Building search query on comments content
  337. $n = ($exact) ? '' : '%';
  338. $searchand = '';
  339. $searchContent = '';
  340. foreach($search_terms as $term) {
  341. $term = addslashes_gpc($term);
  342. if ($this->wp_ver23)
  343. {
  344. $searchContent .= "{$searchand}(cmt.comment_content LIKE '{$n}{$term}{$n}')";
  345. }
  346. $searchand = ' AND ';
  347. }
  348. $sentense_term = $wpdb->escape($s);
  349. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentense_term )
  350. {
  351. if ($this->wp_ver23)
  352. {
  353. $searchContent = "($searchContent) OR (cmt.comment_content LIKE '{$n}{$sentense_term}{$n}')";
  354. }
  355. }
  356. $search = $searchContent;
  357. // Building search query on comments author
  358. if($this->options['se_use_cmt_authors'] == 'Yes')
  359. {
  360. $searchand = '';
  361. $comment_author = '';
  362. foreach($search_terms as $term) {
  363. $term = addslashes_gpc($term);
  364. if ($this->wp_ver23)
  365. {
  366. $comment_author .= "{$searchand}(cmt.comment_author LIKE '{$n}{$term}{$n}')";
  367. }
  368. $searchand = ' AND ';
  369. }
  370. $sentence_term = $wpdb->escape($s);
  371. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  372. {
  373. if ($this->wp_ver23)
  374. {
  375. $comment_author = "($comment_author) OR (cmt.comment_author LIKE '{$n}{$sentence_term}{$n}')";
  376. }
  377. }
  378. $search = "($search) OR ($comment_author)";
  379. }
  380. if ('Yes' == $this->options['se_approved_comments_only'])
  381. {
  382. $comment_approved = "AND cmt.comment_approved = '1'";
  383. $search = "($search) $comment_approved";
  384. }
  385. if ( !empty($search) )
  386. $search = " OR ({$search}) ";
  387. }
  388. $this->se_log("comments where: ".$where);
  389. $this->se_log("comments sql: ".$search);
  390. return $search;
  391. }
  392. // Build the author search
  393. function se_search_authors($where)
  394. {
  395. global $wp_query, $wpdb;
  396. $s = $wp_query->query_vars['s'];
  397. $search_terms = $this->se_get_search_terms();
  398. $exact = $wp_query->query_vars['exact'];
  399. $search = '';
  400. if ( !empty($search_terms) ) {
  401. // Building search query
  402. $n = ($exact) ? '' : '%';
  403. $searchand = '';
  404. foreach($search_terms as $term) {
  405. $term = addslashes_gpc($term);
  406. if ($this->wp_ver23)
  407. {
  408. $search .= "{$searchand}(u.display_name LIKE '{$n}{$term}{$n}')";
  409. } else {
  410. $search .= "{$searchand}(u.display_name LIKE '{$n}{$term}{$n}')";
  411. }
  412. $searchand = ' AND ';
  413. }
  414. $sentence_term = $wpdb->escape($s);
  415. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  416. {
  417. if ($this->wp_ver23)
  418. {
  419. $search = "($search) OR (u.display_name LIKE '{$n}{$sentence_term}{$n}')";
  420. } else {
  421. $search = "($search) OR (u.display_name LIKE '{$n}{$sentence_term}{$n}')";
  422. }
  423. }
  424. if ( !empty($search) )
  425. $search = " OR ({$search}) ";
  426. }
  427. $this->se_log("user where: ".$search);
  428. return $search;
  429. }
  430. // create the search meta data query
  431. function se_build_search_metadata()
  432. {
  433. global $wp_query, $wpdb;
  434. $s = $wp_query->query_vars['s'];
  435. $search_terms = $this->se_get_search_terms();
  436. $exact = $wp_query->query_vars['exact'];
  437. $search = '';
  438. if ( !empty($search_terms) ) {
  439. // Building search query
  440. $n = ($exact) ? '' : '%';
  441. $searchand = '';
  442. foreach($search_terms as $term) {
  443. $term = addslashes_gpc($term);
  444. if ($this->wp_ver23)
  445. {
  446. $search .= "{$searchand}(m.meta_value LIKE '{$n}{$term}{$n}')";
  447. } else {
  448. $search .= "{$searchand}(meta_value LIKE '{$n}{$term}{$n}')";
  449. }
  450. $searchand = ' AND ';
  451. }
  452. $sentence_term = $wpdb->escape($s);
  453. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  454. {
  455. if ($this->wp_ver23)
  456. {
  457. $search = "($search) OR (m.meta_value LIKE '{$n}{$sentence_term}{$n}')";
  458. } else {
  459. $search = "($search) OR (meta_value LIKE '{$n}{$sentence_term}{$n}')";
  460. }
  461. }
  462. if ( !empty($search) )
  463. $search = " OR ({$search}) ";
  464. }
  465. $this->se_log("meta where: ".$search);
  466. return $search;
  467. }
  468. // create the search tag query
  469. function se_build_search_tag()
  470. {
  471. global $wp_query, $wpdb;
  472. $s = $wp_query->query_vars['s'];
  473. $search_terms = $this->se_get_search_terms();
  474. $exact = $wp_query->query_vars['exact'];
  475. $search = '';
  476. if ( !empty($search_terms) )
  477. {
  478. // Building search query
  479. $n = ($exact) ? '' : '%';
  480. $searchand = '';
  481. foreach($search_terms as $term)
  482. {
  483. $term = addslashes_gpc($term);
  484. if ($this->wp_ver23)
  485. {
  486. $search .= "{$searchand}(tter.name LIKE '{$n}{$term}{$n}')";
  487. }
  488. $searchand = ' AND ';
  489. }
  490. $sentence_term = $wpdb->escape($s);
  491. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  492. {
  493. if ($this->wp_ver23)
  494. {
  495. $search = "($search) OR (tter.name LIKE '{$n}{$sentence_term}{$n}')";
  496. }
  497. }
  498. if ( !empty($search) )
  499. $search = " OR ({$search}) ";
  500. }
  501. $this->se_log("tag where: ".$search);
  502. return $search;
  503. }
  504. // create the search categories query
  505. function se_build_search_categories()
  506. {
  507. global $wp_query, $wpdb;
  508. $s = $wp_query->query_vars['s'];
  509. $search_terms = $this->se_get_search_terms();
  510. $exact = $wp_query->query_vars['exact'];
  511. $search = '';
  512. if ( !empty($search_terms) )
  513. {
  514. // Building search query for categories slug.
  515. $n = ($exact) ? '' : '%';
  516. $searchand = '';
  517. $searchSlug = '';
  518. foreach($search_terms as $term)
  519. {
  520. $term = addslashes_gpc($term);
  521. $searchSlug .= "{$searchand}(tter.slug LIKE '{$n}".sanitize_title_with_dashes($term)."{$n}')";
  522. $searchand = ' AND ';
  523. }
  524. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $s )
  525. {
  526. $searchSlug = "($searchSlug) OR (tter.slug LIKE '{$n}".sanitize_title_with_dashes($s)."{$n}')";
  527. }
  528. if ( !empty($searchSlug) )
  529. $search = " OR ({$searchSlug}) ";
  530. // Building search query for categories description.
  531. $searchand = '';
  532. $searchDesc = '';
  533. foreach($search_terms as $term)
  534. {
  535. $term = addslashes_gpc($term);
  536. $searchDesc .= "{$searchand}(ttax.description LIKE '{$n}{$term}{$n}')";
  537. $searchand = ' AND ';
  538. }
  539. $sentence_term = $wpdb->escape($s);
  540. if (!$sentence && count($search_terms) > 1 && $search_terms[0] != $sentence_term )
  541. {
  542. $searchDesc = "($searchDesc) OR (ttax.description LIKE '{$n}{$sentence_term}{$n}')";
  543. }
  544. if ( !empty($searchDesc) )
  545. $search = $search." OR ({$searchDesc}) ";
  546. }
  547. $this->se_log("categories where: ".$search);
  548. return $search;
  549. }
  550. // create the Posts exclusion query
  551. function se_build_exclude_posts()
  552. {
  553. global $wp_query, $wpdb;
  554. $excludeQuery = '';
  555. if (!empty($wp_query->query_vars['s']))
  556. {
  557. $excludedPostList = trim($this->options['se_exclude_posts_list']);
  558. if ($excludedPostList != '')
  559. {
  560. $excl_list = implode(',', explode(',',$excludedPostList));
  561. $excludeQuery = ' AND ('.$wpdb->posts.'.ID NOT IN ( '.$excl_list.' ))';
  562. }
  563. $this->se_log("ex posts where: ".$excludeQuery);
  564. }
  565. return $excludeQuery;
  566. }
  567. // create the Categories exclusion query
  568. function se_build_exclude_categories()
  569. {
  570. global $wp_query, $wpdb;
  571. $excludeQuery = '';
  572. if (!empty($wp_query->query_vars['s']))
  573. {
  574. $excludedCatList = trim($this->options['se_exclude_categories_list']);
  575. if ($excludedCatList != '')
  576. {
  577. $excl_list = implode(',', explode(',',$excludedCatList));
  578. if ($this->wp_ver23)
  579. {
  580. $excludeQuery = " AND ( ctax.term_id NOT IN ( ".$excl_list." ))";
  581. }
  582. else
  583. {
  584. $excludeQuery = ' AND (c.category_id NOT IN ( '.$excl_list.' ))';
  585. }
  586. }
  587. $this->se_log("ex category where: ".$excludeQuery);
  588. }
  589. return $excludeQuery;
  590. }
  591. //join for excluding categories - Deprecated in 2.3
  592. function se_exclude_categories_join($join)
  593. {
  594. global $wp_query, $wpdb;
  595. if (!empty($wp_query->query_vars['s']))
  596. {
  597. if ($this->wp_ver23)
  598. {
  599. $join .= " LEFT JOIN $wpdb->term_relationships AS crel ON ($wpdb->posts.ID = crel.object_id) LEFT JOIN $wpdb->term_taxonomy AS ctax ON (ctax.taxonomy = 'category' AND crel.term_taxonomy_id = ctax.term_taxonomy_id) LEFT JOIN $wpdb->terms AS cter ON (ctax.term_id = cter.term_id) ";
  600. } else {
  601. $join .= "LEFT JOIN $wpdb->post2cat AS c ON $wpdb->posts.ID = c.post_id";
  602. }
  603. }
  604. $this->se_log("category join: ".$join);
  605. return $join;
  606. }
  607. //join for searching comments
  608. function se_comments_join($join)
  609. {
  610. global $wp_query, $wpdb;
  611. if (!empty($wp_query->query_vars['s']))
  612. {
  613. if ($this->wp_ver23)
  614. {
  615. $join .= " LEFT JOIN $wpdb->comments AS cmt ON ( cmt.comment_post_ID = $wpdb->posts.ID ) ";
  616. } else {
  617. if ('Yes' == $this->options['se_approved_comments_only'])
  618. {
  619. $comment_approved = " AND comment_approved = '1'";
  620. } else {
  621. $comment_approved = '';
  622. }
  623. $join .= "LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID " . $comment_approved . ") ";
  624. }
  625. }
  626. $this->se_log("comments join: ".$join);
  627. return $join;
  628. }
  629. //join for searching authors
  630. function se_search_authors_join($join)
  631. {
  632. global $wp_query, $wpdb;
  633. if (!empty($wp_query->query_vars['s']))
  634. {
  635. $join .= " LEFT JOIN $wpdb->users AS u ON ($wpdb->posts.ID = u.ID) ";
  636. }
  637. $this->se_log("authors join: ".$join);
  638. return $join;
  639. }
  640. //join for searching metadata
  641. function se_search_metadata_join($join)
  642. {
  643. global $wp_query, $wpdb;
  644. if (!empty($wp_query->query_vars['s']))
  645. {
  646. if ($this->wp_ver23)
  647. $join .= " LEFT JOIN $wpdb->postmeta AS m ON ($wpdb->posts.ID = m.post_id) ";
  648. else
  649. $join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
  650. }
  651. $this->se_log("metadata join: ".$join);
  652. return $join;
  653. }
  654. //join for searching tags
  655. function se_terms_join($join)
  656. {
  657. global $wp_query, $wpdb;
  658. if (!empty($wp_query->query_vars['s']))
  659. {
  660. // if we're searching for categories
  661. if ( $this->options['se_use_category_search'] )
  662. {
  663. $on[] = "ttax.taxonomy = 'category'";
  664. }
  665. // if we're searching for tags
  666. if ( $this->options['se_use_tag_search'] )
  667. {
  668. $on[] = "ttax.taxonomy = 'post_tag'";
  669. }
  670. // if we're searching custom taxonomies
  671. if ( $this->options['se_use_tax_search'] )
  672. {
  673. $all_taxonomies = get_object_taxonomies('post');
  674. foreach ($all_taxonomies as $taxonomy)
  675. {
  676. if ($taxonomy == 'post_tag' || $taxonomy == 'category')
  677. continue;
  678. $on[] = "ttax.taxonomy = '".addslashes($taxonomy)."'";
  679. }
  680. }
  681. // build our final string
  682. $on = ' ( ' . implode( ' OR ', $on ) . ' ) ';
  683. $join .= " LEFT JOIN $wpdb->term_relationships AS trel ON ($wpdb->posts.ID = trel.object_id) LEFT JOIN $wpdb->term_taxonomy AS ttax ON ( " . $on . " AND trel.term_taxonomy_id = ttax.term_taxonomy_id) LEFT JOIN $wpdb->terms AS tter ON (ttax.term_id = tter.term_id) ";
  684. }
  685. $this->se_log("tags join: ".$join);
  686. return $join;
  687. }
  688. // Highlight the searched terms into Title, excerpt and content
  689. // in the search result page.
  690. function se_postfilter($postcontent)
  691. {
  692. global $wp_query, $wpdb;
  693. $s = $wp_query->query_vars['s'];
  694. // highlighting
  695. if (is_search() && $s != '')
  696. {
  697. $highlight_color = $this->options['se_highlight_color'];
  698. $highlight_style = $this->options['se_highlight_style'];
  699. $search_terms = $this->se_get_search_terms();
  700. foreach ( $search_terms as $term )
  701. {
  702. if (preg_match('/\>/', $term))
  703. continue; //don't try to highlight this one
  704. $term = preg_quote($term);
  705. if ($highlight_color != '')
  706. $postcontent = preg_replace(
  707. '"(?<!\<)(?<!\w)(\pL*'.$term.'\pL*)(?!\w|[^<>]*>)"i'
  708. , '<span class="search-everything-highlight-color" style="background-color:'.$highlight_color.'">$1</span>'
  709. , $postcontent
  710. );
  711. else
  712. $postcontent = preg_replace(
  713. '"(?<!\<)(?<!\w)(\pL*'.$term.'\pL*)(?!\w|[^<>]*>)"i'
  714. , '<span class="search-everything-highlight" style="'.$highlight_style.'">$1</span>'
  715. , $postcontent
  716. );
  717. }
  718. }
  719. return $postcontent;
  720. }
  721. } // END
  722. ?>