Making WordPress.org

Changeset 8189


Ignore:
Timestamp:
02/04/2019 05:14:33 AM (8 years ago)
Author:
dd32
Message:

Theme Directory: Add <link rel="canonical"> to Theme Directory archives and queries.

See #4067.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php

    r7898 r8189  
    10941094        }
    10951095
     1096        $path = wporg_themes_get_current_url( $path_only = true );
     1097        if ( ! $path ) {
     1098                return;
     1099        }
     1100
    10961101        wp_cache_add_global_groups( array( 'locale-associations' ) );
    10971102
     
    11541159        }
    11551160
    1156         if ( is_singular() ) {
    1157                 $path = parse_url( get_permalink(), PHP_URL_PATH );
    1158         } else {
    1159                 // WordPress doesn't have a good way to get the canonical version of non-singular urls.
    1160                 $path = $_SERVER['REQUEST_URI']; // phpcs:ignore
    1161         }
    1162 
    11631161        foreach ( $sites as $site ) {
    11641162                $url = sprintf(
     
    11771175}
    11781176add_action( 'wp_head', 'wporg_themes_add_hreflang_link_attributes' );
     1177
     1178/**
     1179 * Outputs a <link rel="canonical"> on archive pages.
     1180 */
     1181function wporg_themes_archive_rel_canonical_link() {
     1182        if ( $url = wporg_themes_get_current_url() ) {
     1183                printf(
     1184                        '<link rel="canonical" href="%s">' . "\n",
     1185                        esc_url( $url )
     1186                );
     1187        }
     1188}
     1189add_action( 'wp_head', 'wporg_themes_archive_rel_canonical_link' );
     1190remove_action( 'wp_head', 'rel_canonical' );
     1191
     1192/**
     1193 * Get the current front-end requested URL.
     1194 */
     1195function wporg_themes_get_current_url( $path_only = false ) {
     1196        $queried_object = get_queried_object();
     1197        $link = false;
     1198
     1199        if ( get_query_var( 'browse' ) ) {
     1200                // The browse/% urls on the Theme directory are front-page-query alterations.
     1201                $link = home_url( 'browse/' . get_query_var( 'browse' ) . '/' );
     1202        } elseif ( is_tax() || is_tag() || is_category() ) {
     1203                $link = get_term_link( $queried_object );
     1204        } elseif ( is_author() ) {
     1205                $link = get_author_link( $queried_object );
     1206        } elseif ( is_singular() ) {
     1207                $link = get_permalink( $queried_object );
     1208        } elseif ( is_search() ) {
     1209                $link = home_url( 'search/' . urlencode( get_query_var( 's' ) ) . '/' );
     1210        } elseif ( is_front_page() ) {
     1211                $link = home_url( '/' );
     1212        }
     1213
     1214
     1215        if ( $link && is_paged() ) {
     1216                if ( false !== stripos( $link, '?' ) ) {
     1217                        $link = add_query_arg( 'paged', (int) get_query_var( 'paged' ), $link );
     1218                } else {
     1219                        $link = rtrim( $link, '/' ) . '/page/' . (int) get_query_var( 'paged' ) . '/';
     1220                }
     1221        }
     1222
     1223        if ( $path_only && $link ) {
     1224                $path = parse_url( $link, PHP_URL_PATH );
     1225                if ( $query = parse_url( $link, PHP_URL_QUERY ) ) {
     1226                        $path .= '?' . $query;
     1227                }
     1228
     1229                return $path;
     1230        }
     1231
     1232        return $link;
     1233}
Note: See TracChangeset for help on using the changeset viewer.