Making WordPress.org


Ignore:
Timestamp:
04/06/2016 03:48:34 PM (10 years ago)
Author:
ocean90
Message:

Theme Directory: Add hreflang link attributes.

Same code is already used for 2 months in the plugin directory.

See #990.

File:
1 edited

Legend:

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

    r2908 r2909  
    10271027
    10281028/**
     1029 * Adds hreflang link attributes to theme pages.
     1030 *
     1031 * @link https://support.google.com/webmasters/answer/189077?hl=en Use hreflang for language and regional URLs
     1032 * @link https://sites.google.com/site/webmasterhelpforum/en/faq-internationalisation FAQ: Internationalisation
     1033 */
     1034function wporg_themes_add_hreflang_link_attributes() {
     1035        $sites = wp_cache_get( 'local-sites', 'wporg-theme-directory' );
     1036
     1037        if ( false === $sites ) {
     1038                global $wpdb;
     1039                $sites = $wpdb->get_results( 'SELECT locale, subdomain FROM locales', OBJECT_K );
     1040                if ( ! $sites ) {
     1041                        return;
     1042                }
     1043
     1044                require_once GLOTPRESS_LOCALES_PATH;
     1045
     1046                foreach ( $sites as $site ) {
     1047                        $gp_locale = GP_Locales::by_field( 'wp_locale', $site->locale );
     1048                        if ( ! $gp_locale ) {
     1049                                unset( $sites[ $site->locale ] );
     1050                                continue;
     1051                        }
     1052
     1053                        // Note that Google only supports ISO 639-1 codes.
     1054                        if ( isset( $gp_locale->lang_code_iso_639_1 ) && isset( $gp_locale->country_code ) ) {
     1055                                $hreflang = $gp_locale->lang_code_iso_639_1 . '-' . $gp_locale->country_code;
     1056                        } elseif ( isset( $gp_locale->lang_code_iso_639_1 ) ) {
     1057                                $hreflang = $gp_locale->lang_code_iso_639_1;
     1058                        } elseif ( isset( $gp_locale->lang_code_iso_639_2 ) ) {
     1059                                $hreflang = $gp_locale->lang_code_iso_639_2;
     1060                        } elseif ( isset( $gp_locale->lang_code_iso_639_3 ) ) {
     1061                                $hreflang = $gp_locale->lang_code_iso_639_3;
     1062                        }
     1063
     1064                        if ( $hreflang ) {
     1065                                $sites[ $site->locale ]->hreflang = strtolower( $hreflang );
     1066                        } else {
     1067                                unset( $sites[ $site->locale ] );
     1068                        }
     1069                }
     1070
     1071                // Add en_US to the list of sites.
     1072                $sites['en_US'] = (object) array(
     1073                        'locale'    => 'en_US',
     1074                        'hreflang'  => 'en',
     1075                        'subdomain' => ''
     1076                );
     1077
     1078                uasort( $sites, function( $a, $b ) {
     1079                        return strcasecmp( $a->hreflang, $b->hreflang );
     1080                } );
     1081
     1082                wp_cache_set( 'local-sites', $sites, 'wporg-theme-directory', DAY_IN_SECONDS );
     1083        }
     1084
     1085        foreach ( $sites as $site ) {
     1086                $url = sprintf(
     1087                        'https://%swordpress.org%s',
     1088                        $site->subdomain ? "{$site->subdomain}." : '',
     1089                        $_SERVER[ 'REQUEST_URI' ]
     1090                );
     1091
     1092                printf(
     1093                        '<link rel="alternate" href="%s" hreflang="%s" />',
     1094                        esc_url( $url ),
     1095                        esc_attr( $site->hreflang )
     1096                );
     1097        }
     1098        echo "\n";
     1099}
     1100add_action( 'wp_head', 'wporg_themes_add_hreflang_link_attributes' );
     1101
     1102/**
    10291103 * Filter the URLs to use the current localized domain name, rather than WordPress.org.
    10301104 *
Note: See TracChangeset for help on using the changeset viewer.