Making WordPress.org


Ignore:
Timestamp:
02/09/2024 02:56:58 AM (7 months ago)
Author:
dd32
Message:

SEO: Canonical: Support basic multi-taxonomy queries.

This isn't perfect, and will definately have edge-cases.

See #7430.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/wporg-seo/canonical.php

    r10281 r13194  
    6565        $url = get_term_link( $queried_object );
    6666
    67         // Detect multi-term queries.
    68         // Create a copy and remove the 'relation' param if present.
    69         $tax_queries = $wp_query->tax_query->queries;
    70         unset( $tax_queries['relation'] );
     67        // TODO: This isn't strictly correct for nested taxonomies.
     68        $glue = ( 'AND' === $wp_query->tax_query->relation ) ? '+' : ',';
     69        foreach ( $wp_query->tax_query->queried_terms as $taxonomy => $term_data ) {
     70            if ( $taxonomy === $queried_object->taxonomy ) {
     71                // TODO: If multiple tax_queries for the same taxonomy are included, this will miss those.
     72                if ( count( $term_data['terms'] ) > 1 ) {
     73                    $url = str_replace(
     74                        '/' . $queried_object->slug . '/',
     75                        '/' . implode( $glue, $term_data['terms'] ) . '/',
     76                        $url
     77                    );
     78                }
    7179
    72         $term_queries    = count( $tax_queries );
    73         $term_query_zero = count( $tax_queries[0]['terms'] ?? [] );
    74         if ( $term_queries > 1 || $term_query_zero > 1 ) {
    75             // Multiple terms are being queried for.
    76             $terms = wp_list_pluck( $tax_queries, 'terms' );
    77             $terms = call_user_func_array( 'array_merge', $terms );
    78 
    79             // Determine how many taxonomies are involved in this query.
    80             $taxonomies = array_unique( wp_list_pluck( $tax_queries, 'taxonomy' ) );
    81 
    82             if ( count( $taxonomies ) > 1 ) {
    83                 // Multiple-taxonomy query. No canonical produced.
    84                 // TODO: Edgecase: on a site where a taxonomy query is added via pre_get_posts this will result in no canonical produced.
    85                 $url = false;
    86             } elseif ( $term_queries > 1 && 1 === $term_query_zero ) {
    87                 // AND +
    88                 $glue = '+';
    89             } elseif ( $term_query_zero > 1 && 1 === $term_queries && 'AND' === $wp_query->tax_query->relation ) {
    90                 if ( 'AND' === $tax_queries[0]['operator'] ) {
    91                     // AND +
    92                     $glue = '+';
    93                 } else {
    94                     // Union ,
    95                     $glue = ',';
    96                 }
    97             } else {
    98                 $url = false;
     80                continue;
    9981            }
    10082
    101             if ( $url ) {
    102                 $url = str_replace(
    103                     '/' . $queried_object->slug . '/',
    104                     '/' . implode( $glue, $terms ) . '/',
     83            // If we have other taxonomies, append as query vars.
     84            $tax_obj = get_taxonomy( $taxonomy );
     85            if ( ! empty( $tax_obj->query_var ) ) {
     86                $url = add_query_arg(
     87                    $tax_obj->query_var,
     88                    urlencode( implode( $glue, $term_data['terms'] ) ),
    10589                    $url
    10690                );
Note: See TracChangeset for help on using the changeset viewer.