[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/search/ -> template.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Search Template Tags
   5   *
   6   * @package bbPress
   7   * @subpackage TemplateTags
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** Search Loop Functions *****************************************************/
  14  
  15  /**
  16   * The main search loop. WordPress does the heavy lifting.
  17   *
  18   * @since 2.3.0 bbPress (r4579)
  19   *
  20   * @param array $args All the arguments supported by {@link WP_Query}
  21   * @return object Multidimensional array of search information
  22   */
  23  function bbp_has_search_results( $args = array() ) {
  24  
  25      /** Defaults **************************************************************/
  26  
  27      $default_search_terms = bbp_get_search_terms();
  28      $default_post_types   = bbp_get_post_types();
  29  
  30      // Default query args
  31      $default = array(
  32          'post_type'           => $default_post_types,        // Forums, topics, and replies
  33          'posts_per_page'      => bbp_get_replies_per_page(), // This many
  34          'paged'               => bbp_get_paged(),            // On this page
  35          'orderby'             => 'date',                     // Sorted by date
  36          'order'               => 'DESC',                     // Most recent first
  37          'ignore_sticky_posts' => true,                       // Stickies not supported,
  38  
  39          // Conditionally prime the cache for last active posts
  40          'update_post_family_cache' => true
  41      );
  42  
  43      // Only set 's' if search terms exist
  44      // https://bbpress.trac.wordpress.org/ticket/2607
  45      if ( false !== $default_search_terms ) {
  46          $default['s'] = $default_search_terms;
  47      }
  48  
  49      // What are the default allowed statuses (based on user caps)
  50      if ( bbp_get_view_all() ) {
  51  
  52          // Default view=all statuses
  53          $post_statuses = array_keys( bbp_get_topic_statuses() );
  54  
  55          // Add support for private status
  56          if ( current_user_can( 'read_private_topics' ) ) {
  57              $post_statuses[] = bbp_get_private_status_id();
  58          }
  59  
  60          // Join post statuses together
  61          $default['post_status'] = $post_statuses;
  62  
  63      // Lean on the 'perm' query var value of 'readable' to provide statuses
  64      } else {
  65          $default['perm'] = 'readable';
  66      }
  67  
  68      /** Setup *****************************************************************/
  69  
  70      // Parse arguments against default values
  71      $r = bbp_parse_args( $args, $default, 'has_search_results' );
  72  
  73      // Get bbPress
  74      $bbp = bbpress();
  75  
  76      // Only call the search query if 's' is not empty
  77      if ( ! empty( $r['s'] ) ) {
  78          $bbp->search_query = new WP_Query( $r );
  79      }
  80  
  81      // Maybe prime last active posts
  82      if ( ! empty( $r['update_post_family_cache'] ) ) {
  83          bbp_update_post_family_caches( $bbp->search_query->posts );
  84      }
  85  
  86      // Add pagination values to query object
  87      $bbp->search_query->posts_per_page = (int) $r['posts_per_page'];
  88      $bbp->search_query->paged          = (int) $r['paged'];
  89  
  90      // Never home, regardless of what parse_query says
  91      $bbp->search_query->is_home        = false;
  92  
  93      // Only add pagination is query returned results
  94      if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) {
  95  
  96          // Total for pagination boundaries
  97          $total_pages = ( $bbp->search_query->posts_per_page === $bbp->search_query->found_posts )
  98              ? 1
  99              : ceil( $bbp->search_query->found_posts / $bbp->search_query->posts_per_page );
 100  
 101          // Pagination settings with filter
 102          $bbp_search_pagination = apply_filters( 'bbp_search_results_pagination', array(
 103              'base'    => bbp_get_search_pagination_base(),
 104              'total'   => $total_pages,
 105              'current' => $bbp->search_query->paged
 106          ) );
 107  
 108          // Add pagination to query object
 109          $bbp->search_query->pagination_links = bbp_paginate_links( $bbp_search_pagination );
 110      }
 111  
 112      // Filter & return
 113      return apply_filters( 'bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query );
 114  }
 115  
 116  /**
 117   * Whether there are more search results available in the loop
 118   *
 119   * @since 2.3.0 bbPress (r4579)
 120   *
 121   * @return object Search information
 122   */
 123  function bbp_search_results() {
 124  
 125      // Put into variable to check against next
 126      $have_posts = bbpress()->search_query->have_posts();
 127  
 128      // Reset the post data when finished
 129      if ( empty( $have_posts ) ) {
 130          wp_reset_postdata();
 131      }
 132  
 133      return $have_posts;
 134  }
 135  
 136  /**
 137   * Loads up the current search result in the loop
 138   *
 139   * @since 2.3.0 bbPress (r4579)
 140   *
 141   * @return object Search information
 142   */
 143  function bbp_the_search_result() {
 144      $search_result = bbpress()->search_query->the_post();
 145  
 146      // Reset each current (forum|topic|reply) id
 147      bbpress()->current_forum_id = bbp_get_forum_id();
 148      bbpress()->current_topic_id = bbp_get_topic_id();
 149      bbpress()->current_reply_id = bbp_get_reply_id();
 150  
 151      return $search_result;
 152  }
 153  
 154  /**
 155   * Output the search page title
 156   *
 157   * @since 2.3.0 bbPress (r4579)
 158   */
 159  function bbp_search_title() {
 160      echo bbp_get_search_title();
 161  }
 162  
 163      /**
 164       * Get the search page title
 165       *
 166       * @since 2.3.0 bbPress (r4579)
 167       */
 168  	function bbp_get_search_title() {
 169  
 170          // Get search terms
 171          $search_terms = bbp_get_search_terms();
 172  
 173          // No search terms specified
 174          if ( empty( $search_terms ) ) {
 175              $title = esc_html__( 'Search', 'bbpress' );
 176  
 177          // Include search terms in title
 178          } else {
 179              $title = sprintf( esc_html__( "Search Results for '%s'", 'bbpress' ), esc_attr( $search_terms ) );
 180          }
 181  
 182          // Filter & return
 183          return apply_filters( 'bbp_get_search_title', $title, $search_terms );
 184      }
 185  
 186  /**
 187   * Output the search url
 188   *
 189   * @since 2.3.0 bbPress (r4579)
 190   */
 191  function bbp_search_url() {
 192      echo esc_url( bbp_get_search_url() );
 193  }
 194      /**
 195       * Return the search url
 196       *
 197       * @since 2.3.0 bbPress (r4579)
 198       *
 199       * @return string Search url
 200       */
 201  	function bbp_get_search_url() {
 202  
 203          // Pretty permalinks
 204          if ( bbp_use_pretty_urls() ) {
 205  
 206              // Run through home_url()
 207              $url = bbp_get_root_url() . bbp_get_search_slug();
 208              $url = user_trailingslashit( $url );
 209              $url = home_url( $url );
 210  
 211          // Unpretty permalinks
 212          } else {
 213              $url = add_query_arg( array(
 214                  bbp_get_search_rewrite_id() => ''
 215              ), home_url( '/' ) );
 216          }
 217  
 218          // Filter & return
 219          return apply_filters( 'bbp_get_search_url', $url );
 220      }
 221  
 222  /**
 223   * Output the search results url
 224   *
 225   * @since 2.4.0 bbPress (r4928)
 226   */
 227  function bbp_search_results_url() {
 228      echo esc_url( bbp_get_search_results_url() );
 229  }
 230      /**
 231       * Return the search url
 232       *
 233       * @since 2.4.0 bbPress (r4928)
 234       *
 235       * @return string Search url
 236       */
 237  	function bbp_get_search_results_url() {
 238  
 239          // Get the search terms
 240          $search_terms = bbp_get_search_terms();
 241  
 242          // Pretty permalinks
 243          if ( bbp_use_pretty_urls() ) {
 244  
 245              // Root search URL
 246              $url = bbp_get_root_url() . bbp_get_search_slug();
 247  
 248              // Append search terms
 249              if ( ! empty( $search_terms ) ) {
 250                  $url = trailingslashit( $url ) . urlencode( $search_terms );
 251              }
 252  
 253              // Run through home_url()
 254              $url = user_trailingslashit( $url );
 255              $url = home_url( $url );
 256  
 257          // Unpretty permalinks
 258          } else {
 259              $url = add_query_arg( array(
 260                  bbp_get_search_rewrite_id() => urlencode( $search_terms )
 261              ), home_url( '/' ) );
 262          }
 263  
 264          // Filter & return
 265          return apply_filters( 'bbp_get_search_results_url', $url );
 266      }
 267  
 268  /**
 269   * Output the search terms
 270   *
 271   * @since 2.3.0 bbPress (r4579)
 272   *
 273   * @param string $search_terms Optional. Search terms
 274   */
 275  function bbp_search_terms( $search_terms = '' ) {
 276      echo esc_attr( bbp_get_search_terms( $search_terms ) );
 277  }
 278  
 279      /**
 280       * Get the search terms
 281       *
 282       * @since 2.3.0 bbPress (r4579)
 283       *
 284       * If search terms are supplied, those are used. Otherwise check the
 285       * search rewrite id query var.
 286       *
 287       * @param string $passed_terms Optional. Search terms
 288       * @return bool|string Search terms on success, false on failure
 289       */
 290  	function bbp_get_search_terms( $passed_terms = '' ) {
 291  
 292          // Sanitize terms if they were passed in
 293          if ( ! empty( $passed_terms ) ) {
 294              $search_terms = sanitize_title( $passed_terms );
 295  
 296          // Use query variable if not
 297          } else {
 298  
 299              // Global
 300              if ( get_query_var( bbp_get_search_rewrite_id() ) ) {
 301                  $search_terms = get_query_var( bbp_get_search_rewrite_id() );
 302  
 303              // Other searches
 304              } else {
 305  
 306                  // Get known search type IDs
 307                  $types = bbp_get_search_type_ids();
 308  
 309                  // Filterable, so make sure types exist
 310                  if ( ! empty( $types ) ) {
 311  
 312                      // Loop through types
 313                      foreach ( $types as $type ) {
 314  
 315                          // Look for search terms
 316                          $terms = bbp_sanitize_search_request( $type );
 317  
 318                          // Skip if no terms
 319                          if ( empty( $terms ) ) {
 320                              continue;
 321                          }
 322  
 323                          // Set terms if not empty
 324                          $search_terms = $terms;
 325                      }
 326                  }
 327              }
 328          }
 329  
 330          // Trim whitespace & decode if non-empty string, or set to false
 331          $search_terms = ! empty( $search_terms ) && is_string( $search_terms )
 332              ? urldecode( trim( $search_terms ) )
 333              : false;
 334  
 335          // Filter & return
 336          return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms );
 337      }
 338  
 339  /** Pagination ****************************************************************/
 340  
 341  /**
 342   * Return the base URL used inside of pagination links
 343   *
 344   * @since 2.6.0 bbPress (r6679)
 345   *
 346   * @return string
 347   */
 348  function bbp_get_search_pagination_base() {
 349  
 350      // If pretty permalinks are enabled, make our pagination pretty
 351      if ( bbp_use_pretty_urls() ) {
 352  
 353          // Shortcode territory
 354          if ( is_page() || is_single() ) {
 355              $base = get_permalink();
 356  
 357          // Default search location
 358          } else {
 359              $base = bbp_get_search_results_url();
 360          }
 361  
 362          // Add pagination base
 363          $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
 364  
 365      // Unpretty permalinks
 366      } else {
 367          $base = add_query_arg( 'paged', '%#%' );
 368      }
 369  
 370      // Filter & return
 371      return apply_filters( 'bbp_get_search_pagination_base', $base );
 372  }
 373  
 374  /**
 375   * Output the search result pagination count
 376   *
 377   * @since 2.3.0 bbPress (r4579)
 378   */
 379  function bbp_search_pagination_count() {
 380      echo bbp_get_search_pagination_count();
 381  }
 382  
 383      /**
 384       * Return the search results pagination count
 385       *
 386       * @since 2.3.0 bbPress (r4579)
 387       *
 388       * @return string Search pagination count
 389       */
 390  	function bbp_get_search_pagination_count() {
 391          $bbp = bbpress();
 392  
 393          // Define local variable(s)
 394          $retstr = '';
 395  
 396          // Set pagination values
 397          $total_int = intval( $bbp->search_query->found_posts    );
 398          $ppp_int   = intval( $bbp->search_query->posts_per_page );
 399          $start_int = intval( ( $bbp->search_query->paged - 1 ) * $ppp_int ) + 1;
 400          $to_int    = intval( ( $start_int + ( $ppp_int - 1 ) > $total_int )
 401                  ? $total_int
 402                  : $start_int + ( $ppp_int - 1 ) );
 403  
 404          // Format numbers for display
 405          $total_num = bbp_number_format( $total_int );
 406          $from_num  = bbp_number_format( $start_int );
 407          $to_num    = bbp_number_format( $to_int    );
 408  
 409          // Single page of results
 410          if ( empty( $to_num ) ) {
 411              $retstr = sprintf( _n( 'Viewing %1$s result', 'Viewing %1$s results', $total_int, 'bbpress' ), $total_num );
 412  
 413          // Several pages of results
 414          } else {
 415              $retstr = sprintf( _n( 'Viewing %2$s results (of %4$s total)', 'Viewing %1$s results - %2$s through %3$s (of %4$s total)', $bbp->search_query->post_count, 'bbpress' ), $bbp->search_query->post_count, $from_num, $to_num, $total_num );
 416          }
 417  
 418          // Filter & return
 419          return apply_filters( 'bbp_get_search_pagination_count', esc_html( $retstr ) );
 420      }
 421  
 422  /**
 423   * Output search pagination links
 424   *
 425   * @since 2.3.0 bbPress (r4579)
 426   */
 427  function bbp_search_pagination_links() {
 428      echo bbp_get_search_pagination_links();
 429  }
 430  
 431      /**
 432       * Return search pagination links
 433       *
 434       * @since 2.3.0 bbPress (r4579)
 435       *
 436       * @return string Search pagination links
 437       */
 438  	function bbp_get_search_pagination_links() {
 439          $bbp = bbpress();
 440  
 441          if ( ! isset( $bbp->search_query->pagination_links ) || empty( $bbp->search_query->pagination_links ) ) {
 442              return false;
 443          }
 444  
 445          // Filter & return
 446          return apply_filters( 'bbp_get_search_pagination_links', $bbp->search_query->pagination_links );
 447      }


Generated: Sat Sep 21 01:01:35 2019 Cross-referenced by PHPXref 0.7.1