Making WordPress.org

Changeset 11606


Ignore:
Timestamp:
02/25/2022 04:23:33 AM (3 years ago)
Author:
dd32
Message:

WordPress.TV: Remove the search functions.

This sync's meta with WordPress.TV trunk.

See #3387.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php

    r10466 r11606  
    3030        add_action( 'init', array( $this, 'register_taxonomies' ), 0 );
    3131        add_action( 'pre_get_posts', array( $this, 'posts_per_page' ) );
    32         add_action( 'init', array( $this, 'improve_search' ) );
    3332        add_action( 'publish_post', array( $this, 'publish_post' ), 10, 1 );
    3433        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
     
    223222            */
    224223
    225             $data[] = array_filter( $video_data, function( $item ) { 
     224            $data[] = array_filter( $video_data, function( $item ) {
    226225                return '' !== $item && [] !== $item;
    227226            } );
     
    414413    }
    415414
    416     /**
    417      * Activates the improved search, but not in admin.
    418      */
    419     function improve_search() {
    420         if ( is_admin() ) {
    421             return;
    422         }
    423 
    424         add_filter( 'posts_search', array( $this, 'search_posts_search' ), 10, 2 );
    425         add_action( 'pre_get_posts', array( $this, 'search_pre_get_posts' ) );
    426     }
    427 
    428     /**
    429      * Sort the search results by the number of views each post has
    430      *
    431      * @param WP_Query $query
    432      */
    433     function search_pre_get_posts( $query ) {
    434         /*
    435          * @todo Optimize this before re-enabling
    436          *
    437          * This method was disabled because it caused 504 errors on large result sets
    438          * (e.g., https://wordpress.tv/?s=keynote). Sorting by a meta value is not performant.
    439          *
    440          * Maybe look at ways to do the sorting in PHP, or just use Elasticsearch instead.
    441          */
    442         return;
    443 
    444         if ( ! $query->is_main_query() || ! $query->is_search ) {
    445             return;
    446         }
    447 
    448         // Set custom sorting
    449         $query->set( 'meta_key', 'wptv_post_views' );
    450         $query->set( 'orderby', 'meta_value_num' );
    451         $query->set( 'order', 'DESC' );
    452     }
    453 
    454     /**
    455      * Improved Search: posts_search filter
    456      *
    457      * Recreates the search SQL by including a taxonomy search.
    458      * Relies on various other filters used once.
    459      * @todo optimize the get_tax_query part.
    460      *
    461      * @global wpdb $wpdb WordPress database abstraction object.
    462      *
    463      * @param string   $search
    464      * @param WP_Query $query
    465      * @return string
    466      */
    467     function search_posts_search( $search, $query ) {
    468         global $wpdb;
    469         if ( ! $query->is_main_query() || ! $query->is_search || is_admin() ) {
    470             return $search;
    471         }
    472 
    473         // Get the tax query and replace the leading AND with an OR
    474         $tax_query = get_tax_sql( $this->get_tax_query( get_query_var( 's' ) ), $wpdb->posts, 'ID' );
    475         if ( 'and' == substr( trim( strtolower( $tax_query['where'] ) ), 0, 3 ) ) {
    476             $tax_query['where'] = ' OR ' . substr( trim( $tax_query['where'] ), 3 );
    477         }
    478 
    479         // Mostly taken from query.php
    480         if ( isset( $query->query_vars['search_terms'] ) ) {
    481             $search = $searchand = '';
    482             $n      = empty( $query->query_vars['exact'] ) ? '%' : '';
    483 
    484             foreach ( (array) $query->query_vars['search_terms'] as $term ) {
    485                 $term = esc_sql( $wpdb->esc_like( $term ) );
    486                 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    487                 $searchand = ' AND ';
    488             }
    489 
    490             // Combine the search and tax queries.
    491             if ( ! empty( $search ) ) {
    492                 // Add the tax search to the query
    493                 if ( ! empty( $tax_query['where'] ) ) {
    494                     $search .= $tax_query['where'];
    495                 }
    496 
    497                 $search = " AND ({$search}) ";
    498                 if ( ! is_user_logged_in() ) {
    499                     $search .= " AND ($wpdb->posts.post_password = '') ";
    500                 }
    501             }
    502         }
    503 
    504         // These are single-use filters, they delete themselves right after they're used.
    505         add_filter( 'posts_join', array( $this, 'search_posts_join' ), 10, 2 );
    506         add_filter( 'posts_groupby', array( $this, 'search_posts_groupby' ), 10, 2 );
    507 
    508         return $search;
    509     }
    510 
    511     /**
    512      * Improved Search: posts_join filter
    513      *
    514      * This adds the JOIN clause resulting from the taxonomy
    515      * search. Make sure this filter runs only once per WP_Query request.
    516      *
    517      * @global wpdb $wpdb WordPress database abstraction object.
    518      *
    519      * @param string   $join
    520      * @param WP_Query $query
    521      * @return string
    522      */
    523     function search_posts_join( $join, &$query ) {
    524         // Make sure this filter doesn't run again.
    525         remove_filter( 'posts_join', array( $this, 'search_posts_join' ), 10, 2 );
    526 
    527         if ( $query->is_main_query() ) {
    528             global $wpdb;
    529             $tax_query = get_tax_sql( $this->get_tax_query( get_query_var( 's' ) ), $wpdb->posts, 'ID' );
    530             $join .= $tax_query['join'];
    531         }
    532 
    533         return $join;
    534     }
    535 
    536     /**
    537      * Improved Search: posts_groupby filter
    538      *
    539      * Searching with taxonomies may include duplicates when
    540      * search query matches content and one or more taxonomies.
    541      * This filter glues all duplicates. Use only once per WP_Query.
    542      *
    543      * @global wpdb $wpdb WordPress database abstraction object.
    544      *
    545      * @param string   $group_by
    546      * @param WP_Query $query
    547      * @return string
    548      */
    549     function search_posts_groupby( $group_by, &$query ) {
    550         // Never run this again
    551         remove_filter( 'posts_groupby', array( $this, 'search_posts_groupby' ), 10, 2 );
    552 
    553         global $wpdb;
    554         $group_by = "$wpdb->posts.ID";
    555 
    556         return $group_by;
    557     }
    558 
    559     /**
    560      * Returns a $tax_query array for an improved search.
    561      *
    562      * @param string $search
    563      * @return array
    564      */
    565     function get_tax_query( $search ) {
    566         $taxonomies = array(
    567             'producer',
    568             'speakers', /*'flavor', 'language',*/
    569             'event'
    570         );
    571 
    572         $terms    = get_terms( $taxonomies, array(
    573             'search' => $search,
    574         ) );
    575         $term_ids = wp_list_pluck( $terms, 'term_id' );
    576 
    577         $tax_query = array();
    578         foreach ( $taxonomies as $taxonomy ) {
    579             $tax_query[] = array(
    580                 'taxonomy' => $taxonomy,
    581                 'terms'    => $term_ids,
    582             );
    583         }
    584         $tax_query['relation'] = 'OR';
    585 
    586         return $tax_query;
    587     }
    588415
    589416    /**
     
    875702    /**
    876703     * Convert a WPTV Locale to a WP Locale code.
    877      * 
     704     *
    878705     * This isn't all correct, and many of these need to be combined.
    879      * 
     706     *
    880707     * See https://meta.trac.wordpress.org/ticket/1156
    881708     */
Note: See TracChangeset for help on using the changeset viewer.