| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* stats.class.php |
| 5 |
* |
| 6 |
* Various searches on worker |
| 7 |
* |
| 8 |
* |
| 9 |
* Copyright (c) 2011 Prelovac Media |
| 10 |
* www.prelovac.com |
| 11 |
**************************************************************/ |
| 12 |
|
| 13 |
mmb_add_action('search_posts_by_term', 'search_posts_by_term'); |
| 14 |
|
| 15 |
function search_posts_by_term($params = false) |
| 16 |
{ |
| 17 |
global $wpdb, $current_user; |
| 18 |
|
| 19 |
|
| 20 |
$num_posts = 10; |
| 21 |
$num_content_char = 30; |
| 22 |
|
| 23 |
$term_orig = trim($params['search_term']); |
| 24 |
$term_base = addslashes(trim($params['search_term'])); |
| 25 |
|
| 26 |
$query = "SELECT * |
| 27 |
FROM $wpdb->posts |
| 28 |
WHERE $wpdb->posts.post_status = 'publish' |
| 29 |
AND ($wpdb->posts.post_title LIKE '%$term_base%' |
| 30 |
OR $wpdb->posts.post_content LIKE '%$term_base%') |
| 31 |
ORDER BY $wpdb->posts.post_modified DESC |
| 32 |
LIMIT 0, $num_posts |
| 33 |
"; |
| 34 |
|
| 35 |
$posts_array = $wpdb->get_results($query); |
| 36 |
|
| 37 |
$ret_posts = array(); |
| 38 |
|
| 39 |
foreach ($posts_array as $post) { |
| 40 |
//highlight searched term |
| 41 |
|
| 42 |
if (substr_count(strtolower($post->post_title), strtolower($term_orig))) { |
| 43 |
$str_position_start = strpos(strtolower($post->post_title), strtolower($term_orig)); |
| 44 |
|
| 45 |
$post->post_title = substr($post->post_title, 0, $str_position_start) . '<b>' . substr($post->post_title, $str_position_start, strlen($term_orig)) . '</b>' . substr($post->post_title, $str_position_start + strlen($term_orig)); |
| 46 |
|
| 47 |
} |
| 48 |
$post->post_content = html_entity_decode($post->post_content); |
| 49 |
|
| 50 |
$post->post_content = strip_tags($post->post_content); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if (substr_count(strtolower($post->post_content), strtolower($term_orig))) { |
| 55 |
$str_position_start = strpos(strtolower($post->post_content), strtolower($term_orig)); |
| 56 |
|
| 57 |
$start = $str_position_start > $num_content_char ? $str_position_start - $num_content_char : 0; |
| 58 |
$first_len = $str_position_start > $num_content_char ? $num_content_char : $str_position_start; |
| 59 |
|
| 60 |
$start_substring = $start > 0 ? '...' : ''; |
| 61 |
$post->post_content = $start_substring . substr($post->post_content, $start, $first_len) . '<b>' . substr($post->post_content, $str_position_start, strlen($term_orig)) . '</b>' . substr($post->post_content, $str_position_start + strlen($term_orig), $num_content_char) . '...'; |
| 62 |
|
| 63 |
|
| 64 |
} else { |
| 65 |
$post->post_content = substr($post->post_content, 0, 50) . '...'; |
| 66 |
} |
| 67 |
|
| 68 |
$ret_posts[] = array( |
| 69 |
'ID' => $post->ID, |
| 70 |
'post_permalink' => get_permalink($post->ID), |
| 71 |
'post_date' => $post->post_date, |
| 72 |
'post_title' => $post->post_title, |
| 73 |
'post_content' => $post->post_content, |
| 74 |
'post_modified' => $post->post_modified, |
| 75 |
'comment_count' => $post->comment_count |
| 76 |
|
| 77 |
|
| 78 |
); |
| 79 |
} |
| 80 |
mmb_response($ret_posts, true); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
?> |