Making WordPress.org


Ignore:
Timestamp:
04/26/2021 11:09:28 PM (4 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

https://github.com/WordPress/learn/commit/5133da6d34c524f7b4685400c8359842153fbf17

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2020/functions.php

    r10710 r10926  
    290290}
    291291add_action( 'pre_get_posts', 'wporg_archive_modify_query' );
     292
     293/**
     294 * Modify the workshop post type archive query to prioritize workshops in the user's locale.
     295 *
     296 * In order to show all workshops, but with the ones that are presented/captioned in the user's locale shown first, we
     297 * need to modify the posts query in ways that can't be done through the WP_Query or WP_Meta_Query APIs. Instead, here,
     298 * we're filtering the individual clauses of the query to add the pieces we need.
     299 *
     300 * Examples, slightly truncated for simplicity:
     301 *
     302 * Before:
     303 * SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
     304 * FROM wp_posts
     305 * WHERE 1=1
     306 * AND wp_posts.post_type = 'wporg_workshop'
     307 * ORDER BY wp_posts.post_date DESC
     308 *
     309 * After:
     310 * SELECT SQL_CALC_FOUND_ROWS wp_posts.*,
     311 *   MAX( IF( pmeta.meta_key = 'video_language' AND pmeta.meta_value LIKE 'art_%', 1, 0 ) ) AS has_language,
     312 *   MAX( IF( pmeta.meta_key = 'video_caption_language' AND pmeta.meta_value LIKE 'art_%', 1, 0 ) ) AS has_caption
     313 * FROM wp_posts
     314 * INNER JOIN wp_postmeta pmeta ON ( wp_posts.ID = pmeta.post_id )
     315 * WHERE 1=1
     316 * AND wp_posts.post_type = 'wporg_workshop'
     317 * GROUP BY wp_posts.ID
     318 * ORDER BY has_language DESC, has_caption DESC, wp_posts.post_date DESC
     319 *
     320 * @param array    $clauses
     321 * @param WP_Query $query
     322 *
     323 * @return array
     324 */
     325function wporg_archive_query_prioritize_locale( $clauses, $query ) {
     326    if ( ! $query->is_post_type_archive( 'wporg_workshop' ) || is_admin() ) {
     327        return $clauses;
     328    }
     329
     330    global $wpdb;
     331
     332    $locale      = get_locale();
     333    $locale_root = preg_replace( '#^([a-z]{2,3}_?)[a-zA-Z_-]*#', '$1', $locale, -1, $count );
     334
     335    if ( $count ) {
     336        /**
     337         * $clauses['fields'] contains the SELECT part of the query.
     338         *
     339         * The extra fields clauses are calculated fields that will contain a `1` if the workshop post row has a postmeta
     340         * value that matches the locale root. The MAX() and the groupby clause below ensure that all the rows for a
     341         * given workshop are consolidated into one with the highest value in the calculated column. Without the
     342         * grouping, there would be a separate row for each postmeta value for each workshop post.
     343         */
     344        $clauses['fields']  .= ",
     345            MAX( IF( pmeta.meta_key = 'video_language' AND pmeta.meta_value LIKE '{$locale_root}%', 1, 0 ) ) AS has_language
     346        ";
     347        $clauses['fields']  .= ",
     348            MAX( IF( pmeta.meta_key = 'video_caption_language' AND pmeta.meta_value LIKE '{$locale_root}%', 1, 0 ) ) AS has_caption
     349        ";
     350        $clauses['join']    .= " INNER JOIN {$wpdb->postmeta} pmeta ON ( {$wpdb->posts}.ID = pmeta.post_id )";
     351        // This orderby clause ensures that the workshops are sorted by the values in the calculated columns first.
     352        $clauses['orderby'] = 'has_language DESC, has_caption DESC, ' . $clauses['orderby'];
     353
     354        if ( false === strpos( $clauses['groupby'], "{$wpdb->posts}.ID" ) ) {
     355            $clauses['groupby'] = "{$wpdb->posts}.ID";
     356        }
     357    }
     358
     359    return $clauses;
     360}
     361add_filter( 'posts_clauses', 'wporg_archive_query_prioritize_locale', 10, 2 );
    292362
    293363/**
     
    344414    if ( is_array( $filters ) ) {
    345415        $filters = array_filter( $filters );
     416
     417        // If both language and captions filters are set, we assume an "OR" relationship.
     418        if ( isset( $filters['captions'], $filters['language'] ) ) {
     419            $meta_query[] = array(
     420                'relation' => 'OR',
     421                array(
     422                    'key'   => $entity_map['captions'],
     423                    'value' => $filters['captions'],
     424                ),
     425                array(
     426                    'key'   => $entity_map['language'],
     427                    'value' => $filters['language'],
     428                ),
     429            );
     430
     431            unset( $filters['captions'], $filters['language'] );
     432        }
    346433
    347434        foreach ( $filters as $filter_name => $filter_value ) {
Note: See TracChangeset for help on using the changeset viewer.