Making WordPress.org


Ignore:
Timestamp:
06/11/2021 05:39:41 AM (5 years ago)
Author:
dd32
Message:

Support: Attempt to use bbPress/WordPress search for support forums.

This is kind of ugly and not perfect, this is just to get it out there.

Searches from Plugin/Theme subforums should be limited to that plugin/theme forum.
Searches for support threads (without plugin/themes) should be limited to not-plugin/theme/review forums.
Searches from homepage or where it says 'Documentation' should be limited to HelpHub articles/Pages.

This is temporary, and as a result, the urls are not super pretty, but should suffice for the immediate need.

SEe #5771.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php

    r10638 r11025  
    3232
    3333                // Redirect search results.
    34                 add_action( 'bbp_template_redirect', array( $this, 'redirect_search_results_to_google_search' ) );
     34                add_action( 'bbp_template_redirect', array( $this, 'redirect_search_results' ) );
     35                add_filter( 'pre_get_posts', array( $this, 'search_alterations' ) );
     36                add_filter( 'pre_get_posts', array( $this, 'search_forum_alterations' ) );
     37                add_filter( 'bbp_after_has_search_results_parse_args', array( $this, 'search_forum_alteration_args' ) );
    3538
    3639                // Redirect (.+)/page/[01]/ and (.+)?paged=[01] to $1
     
    7780
    7881        /**
    79          * Redirects search results to Google Custom Search.
    80          */
    81         public function redirect_search_results_to_google_search() {
    82                 $is_wp_search  = is_search();
    83                 $is_bbp_search = bbp_is_search_results();
    84 
    85                 if ( ! $is_wp_search && ! $is_bbp_search ) {
     82         * Redirects search results to appropriate place.
     83         */
     84        public function redirect_search_results() {
     85                if ( ! is_search() ) {
    8686                        return;
    8787                }
     
    9191                }
    9292
    93                 $search_terms = $search_url = '';
    94 
    95                 if ( $is_bbp_search ) {
    96                         $search_terms = bbp_get_search_terms();
    97                 } elseif ( $is_wp_search ) {
    98                         $search_terms = get_search_query( false );
    99                 }
    100 
    101                 if ( isset( $_GET['intext'] ) ) {
    102                         $search_terms .= ' intext:"' . esc_attr( $_GET['intext'] ) . '"';
    103                 }
    104 
    105                 if ( $search_terms ) {
    106                         $tab = ! empty( $_GET['tab'] ) && 'docs' === $_GET['tab'] ? 'docs' : 'forums';
    107                         $search_url = sprintf( "https://wordpress.org/search/%s/?{$tab}=1", urlencode( $search_terms ) );
    108                         $search_url = esc_url_raw( $search_url );
    109                 }
    110 
    111                 if ( ! $search_url ) {
    112                         wp_safe_redirect( home_url( '/' ) );
    113                         exit;
    114                 }
    115 
    116                 wp_safe_redirect( $search_url );
    117                 exit;
     93                $search_terms = get_search_query( false );
     94
     95                $tab = $_GET['tab'] ?? 'support';
     96                if ( ! in_array( $tab, [ 'support', 'docs', 'plugin', 'theme' ] ) ) {
     97                        $tab = 'support';
     98                }
     99
     100                switch ( $tab ) {
     101                        case 'theme':
     102                        case 'plugin':
     103                                wp_safe_redirect( add_query_arg( $tab, $_GET[ $tab ], home_url( "/search/{$search_terms}/" ) ) );
     104                                exit;
     105                        case 'support':
     106                                wp_safe_redirect( home_url( "/search/{$search_terms}/" ) );
     107                                exit;
     108                        case 'docs':
     109                                // Do nothing.
     110                }
     111        }
     112
     113        /**
     114         * Search alterations for forums.
     115         */
     116        public function search_alterations( $query ) {
     117                if ( ! is_search() || ! $query->is_search() ) {
     118                        return;
     119                }
     120
     121                $query->set( 'post_type', [ 'page', 'helphub_article' ] );
     122
     123        }
     124
     125        public function search_forum_alterations( $query ) {
     126                if ( empty( $query->query['_bbp_search_query'] ) ) {
     127                        return;
     128                }
     129
     130                // Limit searches to published topics.
     131                $query->set( 'post_type', 'topic' );
     132                $query->set( 'post_status', 'publish' );
     133
     134                // Limit to either the plugin/theme in question, or to general support threads.
     135                if ( !empty( $_GET['plugin'] ) ) {
     136                        $query->set( 'tax_query', [
     137                                [
     138                                        'taxonomy' => 'topic-plugin',
     139                                        'field' => 'slug',
     140                                        'terms' => [ $_GET['plugin'] ]
     141                                ]
     142                        ] );
     143
     144                        add_filter( 'posts_where', array( $this, 'posts_in_last_year' ) );
     145                } elseif ( ! empty( $_GET['theme'] ) ) {
     146                        $query->set( 'tax_query', [
     147                                [
     148                                        'taxonomy' => 'topic-theme',
     149                                        'field' => 'slug',
     150                                        'terms' => [ $_GET['theme'] ]
     151                                ]
     152                        ] );
     153
     154                        add_filter( 'posts_where', array( $this, 'posts_in_last_year' ) );
     155                } else {
     156                        $query->set( 'post_parent__not_in', [21261, 21262, 21272] ); // Magic numbers: Plugins, Themes, Reviews
     157
     158                        add_filter( 'posts_where', array( $this, 'posts_in_last_six_months' ) );
     159                }
     160        }
     161
     162        public function search_forum_alteration_args( $args ) {
     163                $args['_bbp_search_query'] = true;
     164
     165                return $args;
    118166        }
    119167
Note: See TracChangeset for help on using the changeset viewer.