[ 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              $search_terms = get_query_var( bbp_get_search_rewrite_id(), null );
 301  
 302              // Searching globally
 303              if ( ! is_null( $search_terms )  ) {
 304                  $search_terms = wp_unslash( $search_terms );
 305  
 306              // Other searches
 307              } else {
 308  
 309                  // Get known search type IDs
 310                  $types = bbp_get_search_type_ids();
 311  
 312                  // Filterable, so make sure types exist
 313                  if ( ! empty( $types ) ) {
 314  
 315                      // Loop through types
 316                      foreach ( $types as $type ) {
 317  
 318                          // Look for search terms
 319                          $terms = bbp_sanitize_search_request( $type );
 320  
 321                          // Skip if no terms
 322                          if ( empty( $terms ) ) {
 323                              continue;
 324                          }
 325  
 326                          // Set terms if not empty
 327                          $search_terms = $terms;
 328                      }
 329                  }
 330              }
 331          }
 332  
 333          // Trim whitespace & decode if non-empty string, or set to false
 334          $search_terms = ! empty( $search_terms ) && is_string( $search_terms )
 335              ? urldecode( trim( $search_terms ) )
 336              : false;
 337  
 338          // Filter & return
 339          return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms );
 340      }
 341  
 342  /** Pagination ****************************************************************/
 343  
 344  /**
 345   * Return the base URL used inside of pagination links
 346   *
 347   * @since 2.6.0 bbPress (r6679)
 348   *
 349   * @return string
 350   */
 351  function bbp_get_search_pagination_base() {
 352  
 353      // If pretty permalinks are enabled, make our pagination pretty
 354      if ( bbp_use_pretty_urls() ) {
 355  
 356          // Any single post (for shortcodes)
 357          if ( is_singular() ) {
 358              $base = get_permalink();
 359  
 360          // Default search location
 361          } else {
 362              $base = bbp_get_search_results_url();
 363          }
 364  
 365          // Add pagination base
 366          $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
 367  
 368      // Unpretty permalinks
 369      } else {
 370          $base = add_query_arg( 'paged', '%#%' );
 371      }
 372  
 373      // Filter & return
 374      return apply_filters( 'bbp_get_search_pagination_base', $base );
 375  }
 376  
 377  /**
 378   * Output the search result pagination count
 379   *
 380   * @since 2.3.0 bbPress (r4579)
 381   */
 382  function bbp_search_pagination_count() {
 383      echo bbp_get_search_pagination_count();
 384  }
 385  
 386      /**
 387       * Return the search results pagination count
 388       *
 389       * @since 2.3.0 bbPress (r4579)
 390       *
 391       * @return string Search pagination count
 392       */
 393  	function bbp_get_search_pagination_count() {
 394          $bbp = bbpress();
 395  
 396          // Define local variable(s)
 397          $retstr = '';
 398  
 399          // Set pagination values
 400          $total_int = intval( $bbp->search_query->found_posts    );
 401          $ppp_int   = intval( $bbp->search_query->posts_per_page );
 402          $start_int = intval( ( $bbp->search_query->paged - 1 ) * $ppp_int ) + 1;
 403          $to_int    = intval( ( $start_int + ( $ppp_int - 1 ) > $total_int )
 404                  ? $total_int
 405                  : $start_int + ( $ppp_int - 1 ) );
 406  
 407          // Format numbers for display
 408          $total_num = bbp_number_format( $total_int );
 409          $from_num  = bbp_number_format( $start_int );
 410          $to_num    = bbp_number_format( $to_int    );
 411  
 412          // Single page of results
 413          if ( empty( $to_num ) ) {
 414              $retstr = sprintf( _n( 'Viewing %1$s result', 'Viewing %1$s results', $total_int, 'bbpress' ), $total_num );
 415  
 416          // Several pages of results
 417          } else {
 418              $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 );  //phpcs:ignore
 419          }
 420  
 421          // Filter & return
 422          return apply_filters( 'bbp_get_search_pagination_count', esc_html( $retstr ) );
 423      }
 424  
 425  /**
 426   * Output search pagination links
 427   *
 428   * @since 2.3.0 bbPress (r4579)
 429   */
 430  function bbp_search_pagination_links() {
 431      echo bbp_get_search_pagination_links();
 432  }
 433  
 434      /**
 435       * Return search pagination links
 436       *
 437       * @since 2.3.0 bbPress (r4579)
 438       *
 439       * @return string Search pagination links
 440       */
 441  	function bbp_get_search_pagination_links() {
 442          $bbp = bbpress();
 443  
 444          if ( ! isset( $bbp->search_query->pagination_links ) || empty( $bbp->search_query->pagination_links ) ) {
 445              return false;
 446          }
 447  
 448          // Filter & return
 449          return apply_filters( 'bbp_get_search_pagination_links', $bbp->search_query->pagination_links );
 450      }


Generated: Fri Dec 6 01:00:52 2024 Cross-referenced by PHPXref 0.7.1