Skip to:
Content

bbPress.org


Ignore:
Timestamp:
04/18/2019 07:54:43 PM (6 years ago)
Author:
johnjamesjacoby
Message:

Search: add support for fs query-arg to search forums list:

  • Introduces bbp_sanitize_search_request() to encapsulate duplicate code across forums/topics/replies
  • Introduces bbp_get_search_type_ids() to stub out future enhancements (tags, users, etc...)
  • Use these new functions where intended
  • Update bbp_get_search_terms() to loop through known search-type IDs

This commit also fixes debug notices that would happen when these query arguments were not explicitly strings.

Fixes #3245.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/search/functions.php

    r6573 r6903  
    8282    bbp_redirect( $redirect_to );
    8383}
     84
     85/**
     86 * Return an array of search types
     87 *
     88 * @since 2.6.0 bbPress (r6903)
     89 *
     90 * @return array
     91 */
     92function bbp_get_search_type_ids() {
     93    return apply_filters( 'bbp_get_search_types', array( 's', 'fs', 'ts', 'rs' ) );
     94}
     95
     96/**
     97 * Sanitize a query argument used to pass some search terms.
     98 *
     99 * Accepts a single parameter to be used for forums, topics, or replies.
     100 *
     101 * @since 2.6.0 bbPress (r6903)
     102 *
     103 * @param string $query_arg s|fs|ts|rs
     104 *
     105 * @return mixed
     106 */
     107function bbp_sanitize_search_request( $query_arg = 's' ) {
     108
     109    // Define allowed keys
     110    $allowed = bbp_get_search_type_ids();
     111
     112    // Bail if not an allowed query string key
     113    if ( ! in_array( $query_arg, $allowed, true ) ) {
     114        return false;
     115    }
     116
     117    // Get search terms if requested
     118    $terms = ! empty( $_REQUEST[ $query_arg ] )
     119        ? $_REQUEST[ $query_arg ]
     120        : false;
     121
     122    // Bail if query argument does not exist
     123    if ( empty( $terms ) ) {
     124        return false;
     125    }
     126
     127    // Maybe implode if an array
     128    if ( is_array( $terms ) ) {
     129        $terms = implode( ' ', $terms );
     130    }
     131
     132    // Sanitize
     133    $retval = sanitize_title( trim( $terms ) );
     134
     135    // Filter & return
     136    return apply_filters( 'bbp_sanitize_search_request', $retval, $query_arg );
     137}
Note: See TracChangeset for help on using the changeset viewer.