Making WordPress.org


Ignore:
Timestamp:
06/15/2017 12:57:31 PM (8 years ago)
Author:
Otto42
Message:

Plugin Directory: Move hreflang from the theme into the plugin-directory plugin, change it to only show translated languages instead of all languages, in order to make google behave better. Props @joostdevalk, @otto42. Fixes #2870

File:
1 edited

Legend:

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

    r5457 r5570  
    630630        ), home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/favorite' ) );
    631631    }
     632
     633    /**
     634     * Adds hreflang link attributes to WordPress.org pages.
     635     *
     636     * @link https://support.google.com/webmasters/answer/189077?hl=en Use hreflang for language and regional URLs.
     637     * @link https://sites.google.com/site/webmasterhelpforum/en/faq-internationalisation FAQ: Internationalisation.
     638     */
     639    public function hreflang_link_attributes() {
     640        global $wpdb;
     641
     642        wp_cache_add_global_groups( array( 'locale-associations' ) );
     643
     644        if ( false === ( $sites = wp_cache_get( 'local-sites', 'locale-associations' ) ) ) {
     645
     646            // get subdomain/locale associations
     647            $subdomains = $wpdb->get_results( 'SELECT locale, subdomain FROM locales', OBJECT_K );
     648
     649            $sites = Plugin_I18n::instance()->get_locales();
     650
     651            require_once GLOTPRESS_LOCALES_PATH;
     652
     653            foreach ( $sites as $key => $site ) {
     654                $gp_locale = \GP_Locales::by_field( 'wp_locale', $site->wp_locale );
     655                if ( empty( $gp_locale ) ) {
     656                    unset( $sites[ $key ] );
     657                    continue;
     658                }
     659
     660                $sites[ $key ]->subdomain = $subdomains[ $site->wp_locale ]->subdomain;
     661
     662                // Note that Google only supports ISO 639-1 codes.
     663                if ( isset( $gp_locale->lang_code_iso_639_1 ) && isset( $gp_locale->country_code ) ) {
     664                    $hreflang = $gp_locale->lang_code_iso_639_1 . '-' . $gp_locale->country_code;
     665                } elseif ( isset( $gp_locale->lang_code_iso_639_1 ) ) {
     666                    $hreflang = $gp_locale->lang_code_iso_639_1;
     667                } elseif ( isset( $gp_locale->lang_code_iso_639_2 ) ) {
     668                    $hreflang = $gp_locale->lang_code_iso_639_2;
     669                } elseif ( isset( $gp_locale->lang_code_iso_639_3 ) ) {
     670                    $hreflang = $gp_locale->lang_code_iso_639_3;
     671                }
     672
     673                if ( $hreflang ) {
     674                    $sites[ $key ]->hreflang = strtolower( $hreflang );
     675                } else {
     676                    unset( $sites[ $key ] );
     677                }
     678            }
     679
     680            // Add en_US to the list of sites.
     681            $sites['en_US'] = (object) array(
     682                'locale'    => 'en_US',
     683                'hreflang'  => 'en',
     684                'subdomain' => ''
     685            );
     686
     687            uasort( $sites, function( $a, $b ) {
     688                return strcasecmp( $a->hreflang, $b->hreflang );
     689            } );
     690
     691
     692            wp_cache_set( 'local-sites', $sites, 'locale-associations' );
     693        }
     694
     695        foreach ( $sites as $site ) {
     696            $url = sprintf(
     697                'https://%swordpress.org%s',
     698                $site->subdomain ? "{$site->subdomain}." : '',
     699                $_SERVER[ 'REQUEST_URI' ]
     700            );
     701
     702            printf(
     703                '<link rel="alternate" href="%s" hreflang="%s" />' . "\n",
     704                esc_url( $url ),
     705                esc_attr( $site->hreflang )
     706            );
     707        }
     708    }
    632709}
Note: See TracChangeset for help on using the changeset viewer.