Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if( !function_exists( 'ms_global_search_page' ) ) {
- function ms_global_search_page( $atts ) {
- global $wp_query, $wpdb;
- extract( shortcode_atts( array( 'excerpt' => 'no' ), $atts ) );
- $term = strip_tags( apply_filters( 'get_search_query', get_query_var( 'mssearch' ) ) );
- if( !empty( $term ) ) {
- // Literal keyword
- if( preg_match( '/^\"(.*?)\"$/', stripslashes($term) , $termmatch) ) {
- if( !empty( $termmatch[1] ) ) {
- $termsearch = "( post_title LIKE '%%".$termmatch[1]."%%' OR post_content LIKE '%%".$termmatch[1]."%%' OR ".$wpdb->users.".display_name LIKE '%%".$termmatch[1]."%%' ) ";
- } else { ?>
- <h3 class='globalpage_title center'><?php _e( "Not found", 'ms-global-search' ) ?></h3>
- <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
- <?php
- }
- } else {
- // Multiple keywords
- $multipleterms = explode ( " ", $term );
- if( count($multipleterms) != 1 ) {
- $termsearch = "( ";
- $totalterms = count($multipleterms);
- $numterms = 1;
- foreach( $multipleterms as $aterm ) {
- if( $numterms < $totalterms ) {
- $termsearch .= " ( post_title LIKE '%%".$aterm."%%' OR post_content LIKE '%%".$aterm."%%' OR ".$wpdb->users.".display_name LIKE '%%".$aterm."%%' ) AND ";
- } else {
- $termsearch .= "( post_title LIKE '%%".$aterm."%%' OR post_content LIKE '%%".$aterm."%%' OR ".$wpdb->users.".display_name LIKE '%%".$aterm."%%' ) )";
- }
- $numterms++;
- }
- } else {
- $termsearch = "( post_title LIKE '%%".$term."%%' OR post_content LIKE '%%".$term."%%' OR ".$wpdb->users.".display_name LIKE '%%".$term."%%' ) ";
- }
- }
- $wheresearch = '';
- // Search only on user blogs.
- $userid = get_current_user_id();
- if( strcmp( apply_filters ( 'get_search_query', get_query_var( 'mswhere' ) ), 'my' ) == 0 && $userid != 0 ) {
- $userblogs = get_blogs_of_user( $userid );
- $i=0;
- foreach( $userblogs as $ub ) {
- if( $i != 0 ) $wheresearch .= " OR ";
- else $wheresearch .= "( ";
- $i++;
- $wheresearch .= $wpdb->base_prefix."v_posts.blog_id = ".$ub->userblog_id;
- if( count( $userblogs ) == $i ) $wheresearch .= " ) AND ";
- }
- }
- // Search on pages.
- if(get_query_var( 'msp' )) {
- $post_type = "( post_type = 'post' OR post_type = 'page' )";
- } else {
- $post_type = "post_type = 'post'";
- }
- // Show private posts
- if ($userid != 0) {
- $request = $wpdb->prepare( "SELECT ".$wpdb->base_prefix."v_posts.* from ".$wpdb->base_prefix."v_posts left join ".$wpdb->users." on ".$wpdb->users.".ID=".$wpdb->base_prefix."v_posts.post_author ".
- "where ".$wheresearch." ".$termsearch.
- "AND ( post_status = 'publish' OR post_status = 'private' ) AND ".$post_type." ORDER BY ".$wpdb->base_prefix."v_posts.blog_id ASC, ".$wpdb->base_prefix."v_posts.post_date DESC, ".$wpdb->base_prefix."v_posts.comment_count DESC" );
- } else {
- $request = $wpdb->prepare( "SELECT ".$wpdb->base_prefix."v_posts.* from ".$wpdb->base_prefix."v_posts left join ".$wpdb->users." on ".$wpdb->users.".ID=".$wpdb->base_prefix."v_posts.post_author ".
- "where ".$wheresearch." ".$termsearch.
- "AND post_status = 'publish' AND ".$post_type." ORDER BY ".$wpdb->base_prefix."v_posts.blog_id ASC, ".$wpdb->base_prefix."v_posts.post_date DESC, ".$wpdb->base_prefix."v_posts.comment_count DESC" );
- }
- $search = $wpdb->get_results( $request );
- // Show search results.
- if( empty( $search ) ) { ?>
- <h3 class='globalpage_title center'><?php _e( "Not found", 'ms-global-search' ) ?> <span class='ms-global-search_term'><?php echo stripslashes($term); ?></span><?php if( !empty( $wheresearch ) ) echo " ".__( 'in your blogs', 'ms-global-search' ); ?>.</h3>
- <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
- <?php
- } else {
- $countResult = count( $search );
- if($countResult < 2){
- echo '<p>'.$countResult." ".__( 'match with', 'ms-global-search' )." ";
- }else{
- echo '<p>'.$countResult." ".__( 'matches with', 'ms-global-search' )." ";
- } ?>
- <span class='ms-global-search_term'><?php echo stripslashes($term); ?></span><?php if( !empty( $wheresearch ) ) echo " ".__( 'in your blogs', 'ms-global-search' ); ?>.</p>
- <?php
- $rows_per_page = 20;
- $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
- $rows = $search;
- global $wp_rewrite;
- $pagination_args = array(
- 'base' => @add_query_arg('paged','%#%'),
- 'format' => '',
- 'total' => ceil(sizeof($rows)/$rows_per_page),
- 'current' => $current,
- 'show_all' => false,
- 'type' => 'plain',
- );
- if( $wp_rewrite->using_permalinks() )
- $pagination_args['base'] = $url = $_SERVER['REQUEST_URI'] . '&paged=%#%';
- if( !empty($wp_query->query_vars['s']) )
- $pagination_args['add_args'] = array('s'=>get_query_var('s'));
- $start = ($current - 1) * $rows_per_page;
- $end = $start + $rows_per_page;
- $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
- for ($i=$start;$i < $end ;++$i ) {
- $s = $rows[$i];
- $author = get_userdata( $s->post_author );
- if( $blogid != $s->blog_id ) {
- $blogid = $s->blog_id; ?>
- <h2 class='globalblog_title'><?php echo get_blog_option( $blogid, 'blogname' ) ?></h2>
- <?php
- } ?>
- <div <?php post_class( 'globalsearch_post' ) ?>>
- <small class="date"><?php echo date( __( 'm/d/Y', 'ms-global-search' ) ,strtotime( $s->post_date ) ); ?></small>
- <h2 id="post-<?php echo $s->ID.$s->blog_id; ?>" class="globalsearch_title"><a href="<?php echo get_blog_permalink( $s->blog_id, $s->ID ); ?>" rel="bookmark" title="<?php echo __( 'Permanent Link to', 'ms-global-search' ).' '.$s->post_title; ?>"><?php echo $s->post_title ?></a></h2>
- <div class="entry">
- <?php
- $sc = strip_tags($s->post_content);
- $words = explode(" ",$sc);
- $words = implode(" ",array_splice($words,0,50));
- echo $words;
- ?>
- <div class="clear"></div>
- <a class="smallyellow" href="<?php echo get_blog_permalink( $s->blog_id, $s->ID ); ?>" rel="bookmark" title="<?php echo __( 'Permanent Link to', 'ms-global-search' ).' '.$s->post_title; ?>"><span>Read More</span></a>
- <div class="clear"></div>
- </div>
- </div>
- <?php
- }
- echo '<div class="wp-pagenavi">'.paginate_links($pagination_args).'</div>';
- }
- } else { ?>
- <h3 class='globalpage_title center'><?php _e( "Not found", 'ms-global-search' ) ?></h3>
- <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
- <?php
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement