Advertisement
Guest User

WordPress Plugin: MultiSite Global Search Pagination

a guest
Oct 30th, 2012
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.84 KB | None | 0 0
  1. <?php
  2. if( !function_exists( 'ms_global_search_page' ) ) {
  3.     function ms_global_search_page( $atts ) {
  4.    
  5.         global $wp_query, $wpdb;
  6.        
  7.         extract( shortcode_atts( array( 'excerpt' => 'no' ), $atts ) );
  8.        
  9.         $term = strip_tags( apply_filters( 'get_search_query', get_query_var( 'mssearch' ) ) );
  10.    
  11.         if( !empty( $term ) ) {
  12.             // Literal keyword
  13.             if( preg_match( '/^\"(.*?)\"$/', stripslashes($term) , $termmatch) ) {
  14.                 if( !empty( $termmatch[1] ) ) {
  15.                     $termsearch = "( post_title LIKE '%%".$termmatch[1]."%%' OR post_content LIKE '%%".$termmatch[1]."%%' OR ".$wpdb->users.".display_name LIKE '%%".$termmatch[1]."%%' ) ";
  16.                 } else { ?>
  17.                     <h3 class='globalpage_title center'><?php _e( "Not found", 'ms-global-search' ) ?></h3>
  18.                     <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
  19.                 <?php
  20.                 }
  21.             } else {
  22.                 // Multiple keywords
  23.                 $multipleterms = explode ( " ", $term );
  24.                 if( count($multipleterms) != 1 ) {
  25.                     $termsearch = "( ";
  26.                     $totalterms = count($multipleterms);
  27.                     $numterms = 1;
  28.                     foreach( $multipleterms as $aterm ) {
  29.                         if( $numterms < $totalterms ) {
  30.                             $termsearch .= " ( post_title LIKE '%%".$aterm."%%' OR post_content LIKE '%%".$aterm."%%' OR ".$wpdb->users.".display_name LIKE '%%".$aterm."%%' ) AND ";
  31.                         } else {
  32.                             $termsearch .= "( post_title LIKE '%%".$aterm."%%' OR post_content LIKE '%%".$aterm."%%' OR ".$wpdb->users.".display_name LIKE '%%".$aterm."%%' ) )";
  33.                         }
  34.                         $numterms++;
  35.                     }
  36.                 } else {
  37.                     $termsearch = "( post_title LIKE '%%".$term."%%' OR post_content LIKE '%%".$term."%%' OR ".$wpdb->users.".display_name LIKE '%%".$term."%%' ) ";
  38.                 }
  39.             }
  40.            
  41.             $wheresearch = '';
  42.             // Search only on user blogs.
  43.             $userid = get_current_user_id();
  44.             if( strcmp( apply_filters ( 'get_search_query', get_query_var( 'mswhere' ) ), 'my' ) == 0 && $userid != 0 ) {
  45.                 $userblogs = get_blogs_of_user( $userid );
  46.                
  47.                 $i=0;
  48.                 foreach( $userblogs as $ub ) {
  49.                     if( $i != 0 ) $wheresearch .= " OR ";
  50.                     else $wheresearch .= "( ";
  51.                     $i++;
  52.                     $wheresearch .= $wpdb->base_prefix."v_posts.blog_id = ".$ub->userblog_id;
  53.                     if( count( $userblogs ) == $i ) $wheresearch .= " ) AND ";
  54.                 }
  55.             }
  56.            
  57.             // Search on pages.
  58.             if(get_query_var( 'msp' )) {
  59.                 $post_type = "( post_type = 'post' OR post_type = 'page' )";
  60.             } else {
  61.                 $post_type = "post_type = 'post'";
  62.             }
  63.            
  64.             // Show private posts
  65.             if ($userid != 0) {
  66.                 $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 ".
  67.                 "where ".$wheresearch." ".$termsearch.
  68.                 "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" );
  69.    
  70.             } else {
  71.                 $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 ".
  72.                 "where ".$wheresearch." ".$termsearch.
  73.                 "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" );
  74.             }
  75.            
  76.             $search = $wpdb->get_results( $request );
  77.            
  78.             // Show search results.
  79.             if( empty( $search ) ) { ?>
  80.                 <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>
  81.                 <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
  82.             <?php
  83.             } else {
  84.                 $countResult = count( $search );
  85.                 if($countResult < 2){
  86.                     echo '<p>'.$countResult." ".__( 'match with', 'ms-global-search' )." ";
  87.                 }else{
  88.                     echo '<p>'.$countResult." ".__( 'matches with', 'ms-global-search' )." ";
  89.                 } ?>
  90.                 <span class='ms-global-search_term'><?php echo stripslashes($term); ?></span><?php if( !empty( $wheresearch ) ) echo " ".__( 'in your blogs', 'ms-global-search' ); ?>.</p>
  91.                
  92.              <?php
  93.    
  94.             $rows_per_page = 20;
  95.             $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
  96.              
  97.             $rows = $search;
  98.              
  99.             global $wp_rewrite;
  100.              
  101.             $pagination_args = array(
  102.              'base' => @add_query_arg('paged','%#%'),
  103.              'format' => '',
  104.              'total' => ceil(sizeof($rows)/$rows_per_page),
  105.              'current' => $current,
  106.              'show_all' => false,
  107.              'type' => 'plain',
  108.             );
  109.              
  110.             if( $wp_rewrite->using_permalinks() )
  111.              $pagination_args['base'] = $url = $_SERVER['REQUEST_URI'] . '&paged=%#%';
  112.              
  113.             if( !empty($wp_query->query_vars['s']) )
  114.              $pagination_args['add_args'] = array('s'=>get_query_var('s'));
  115.              
  116.            
  117.              
  118.             $start = ($current - 1) * $rows_per_page;
  119.             $end = $start + $rows_per_page;
  120.             $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
  121.            
  122.             for ($i=$start;$i < $end ;++$i ) {
  123.              $s = $rows[$i];
  124.             $author = get_userdata( $s->post_author );
  125.                     if( $blogid != $s->blog_id ) {
  126.                         $blogid = $s->blog_id; ?>
  127.                        
  128.                         <h2 class='globalblog_title'><?php echo get_blog_option( $blogid, 'blogname' ) ?></h2>
  129.                     <?php
  130.                     } ?>
  131.    
  132.                     <div <?php post_class( 'globalsearch_post' ) ?>>
  133.                        
  134.                             <small class="date"><?php echo date( __( 'm/d/Y', 'ms-global-search' ) ,strtotime( $s->post_date ) ); ?></small>
  135.                             <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>
  136.                            
  137.                             <div class="entry">
  138.                                 <?php
  139.                                
  140.                                     $sc = strip_tags($s->post_content);
  141.                                    
  142.                                      $words = explode(" ",$sc);
  143.                                      
  144.                                     $words = implode(" ",array_splice($words,0,50));
  145.                                    
  146.                                     echo $words;
  147.                                
  148.                                 ?>
  149.                                 <div class="clear"></div>
  150.                                 <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>
  151.                                 <div class="clear"></div>
  152.                             </div>
  153.                    
  154.                     </div>
  155.                 <?php
  156.                 }
  157.            
  158.        echo '<div class="wp-pagenavi">'.paginate_links($pagination_args).'</div>';
  159.  
  160.                
  161.             }
  162.         } else { ?>
  163.             <h3 class='globalpage_title center'><?php _e( "Not found", 'ms-global-search' ) ?></h3>
  164.             <p class='globalpage_message center'><?php _e( "Sorry, but you are looking for something that isn't here.", 'ms-global-search' ) ?></p>
  165.         <?php
  166.         }
  167.     }
  168. }
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement