Making WordPress.org

Changeset 3386


Ignore:
Timestamp:
06/16/2016 11:26:21 AM (9 years ago)
Author:
obenland
Message:

Plugin Directory: Add support for hreflang link attributes.

See #1691.

File:
1 edited

Legend:

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

    r3359 r3386  
    7272    wp_enqueue_script( 'wporg-plugins-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true );
    7373    wp_enqueue_script( 'wporg-plugins-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );
    74    
     74
    7575    if ( is_singular( 'plugin' ) ) {
    7676        wp_enqueue_script( 'wporg-plugins-accordion', get_template_directory_uri() . '/js/section-accordion.js', array(), '20160525', true );
     
    106106add_action( 'customize_preview_init',  __NAMESPACE__ . '\customize_preview_js' );
    107107
     108
     109/**
     110 * Adds hreflang link attributes to plugin pages.
     111 *
     112 * @link https://support.google.com/webmasters/answer/189077?hl=en Use hreflang for language and regional URLs.
     113 * @link https://sites.google.com/site/webmasterhelpforum/en/faq-internationalisation FAQ: Internationalisation.
     114 */
     115function hreflang_link_attributes() {
     116
     117    if ( false === ( $sites = wp_cache_get( 'local-sites', 'locale-associations' ) ) ) {
     118        global $wpdb;
     119
     120        $sites = $wpdb->get_results( 'SELECT locale, subdomain FROM locales', OBJECT_K );
     121        if ( ! $sites ) {
     122            return;
     123        }
     124
     125        require_once GLOTPRESS_LOCALES_PATH;
     126
     127        foreach ( $sites as $site ) {
     128            $gp_locale = \GP_Locales::by_field( 'wp_locale', $site->locale );
     129            if ( ! $gp_locale ) {
     130                unset( $sites[ $site->locale ] );
     131                continue;
     132            }
     133
     134            // Note that Google only supports ISO 639-1 codes.
     135            if ( isset( $gp_locale->lang_code_iso_639_1 ) && isset( $gp_locale->country_code ) ) {
     136                $hreflang = $gp_locale->lang_code_iso_639_1 . '-' . $gp_locale->country_code;
     137            } elseif ( isset( $gp_locale->lang_code_iso_639_1 ) ) {
     138                $hreflang = $gp_locale->lang_code_iso_639_1;
     139            } elseif ( isset( $gp_locale->lang_code_iso_639_2 ) ) {
     140                $hreflang = $gp_locale->lang_code_iso_639_2;
     141            } elseif ( isset( $gp_locale->lang_code_iso_639_3 ) ) {
     142                $hreflang = $gp_locale->lang_code_iso_639_3;
     143            }
     144
     145            if ( $hreflang ) {
     146                $sites[ $site->locale ]->hreflang = strtolower( $hreflang );
     147            } else {
     148                unset( $sites[ $site->locale ] );
     149            }
     150        }
     151
     152        // Add en_US to the list of sites.
     153        $sites['en_US'] = (object) array(
     154            'locale'    => 'en_US',
     155            'hreflang'  => 'en',
     156            'subdomain' => ''
     157        );
     158
     159        uasort( $sites, function( $a, $b ) {
     160            return strcasecmp( $a->hreflang, $b->hreflang );
     161        } );
     162
     163        wp_cache_set( 'local-sites', $sites, 'locale-associations', DAY_IN_SECONDS );
     164    }
     165
     166    foreach ( $sites as $site ) {
     167        $url = sprintf(
     168            'https://%swordpress.org%s',
     169            $site->subdomain ? "{$site->subdomain}." : '',
     170            $_SERVER[ 'REQUEST_URI' ]
     171        );
     172
     173        printf(
     174            '<link rel="alternate" href="%s" hreflang="%s" />' . "\n",
     175            esc_url( $url ),
     176            esc_attr( $site->hreflang )
     177        );
     178    }
     179}
     180add_action( 'wp_head', __NAMESPACE__ . '\hreflang_link_attributes' );
     181
    108182/**
    109183 * Custom template tags for this theme.
Note: See TracChangeset for help on using the changeset viewer.