Making WordPress.org

Ticket #6600: 6600.2.diff

File 6600.2.diff, 6.6 KB (added by dd32, 23 months ago)
  • lang-guess-ajax.php

     
    11<?php
    22define( 'DB_USER', '' );
    33define( 'DB_PASSWORD', '' );
    44define( 'DB_NAME', '' );
    55define( 'DB_HOST', '' );
    66
    77// Relevant: Environment, versions, errors.
    88// Not relevant: options, users, super admins, keys, secrets, sso.
    99require dirname( __DIR__ ) . '/define.php';
     10require WPORGPATH . 'wp-includes/plugin.php';
     11require WPORGPATH . 'wp-includes/formatting.php';
    1012require WPORGPATH . 'hyperdb/bb-10-hyper-db.php';
    1113require WPORGPATH . 'object-cache.php';
    1214wp_cache_init();
    1315require __DIR__ . '/class-guess-lang.php';
    1416require GLOTPRESS_LOCALES_PATH;
    1517
    1618// Search engines don't need to index this content. See #meta4154
    1719header( 'X-Robots-Tag: noindex' );
    1820
    1921// Cache the result for a day in browsers.
    2022header( 'Vary: Origin, Accept-Language' );
    2123header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 86400 /* 24 * 60 * 60 */ ) . ' GMT' );
    2224
    2325// Set a CORS header to allow WordPress.org subdomains to request it.
    2426if ( isset( $_SERVER['HTTP_ORIGIN'] ) && preg_match( '!^https?://([^.]+\.)?wordpress.org/?$!i', $_SERVER['HTTP_ORIGIN'], $m ) ) {
    function lang_guess_request() { 
    6668        /*
    6769         * Add a debugging endpoint, so that we can get feedback from end-users easier.
    6870         */
    6971        if ( ! empty( $_GET['debug'] ) ) {
    7072                header( 'Content-type: text/plain; charset=utf-8' );
    7173
    7274                $language_header = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'Not Set';
    7375                $anonymized_ip   = preg_replace( '!\.\d+$!', '.xxx', $_SERVER['REMOTE_ADDR'] );
    7476                $ip_country      = $guesser->get_country_by_ip();
    7577
    7678                echo json_encode( compact( 'language_header', 'anonymized_ip', 'ip_country', 'guesses' ), JSON_PRETTY_PRINT );
    7779                die();
    7880        }
    7981
    8082        if ( is_array( $guesses ) && ! empty( $guesses ) ) {
    81                 $num_guesses = count( $guesses );
    82 
    8383                if ( $is_forums ) {
    84                         if ( 1 === $num_guesses ) {
    85                                 $string = translate_gp( 'WordPress support forums are also available in %s.', $guesses[0]['locale'] );
    86                         } else {
    87                                 $string = translate_gp( 'WordPress support forums are also available in %s (also %s).', $guesses[0]['locale'] );
    88                         }
     84                        $string = translate_gp( 'WordPress support forums are also available in %s.', $guesses[0]['locale'] );
    8985                } else {
    90                         if ( 1 === $num_guesses ) {
    91                                 $string = translate_gp( 'WordPress is also available in %s.', $guesses[0]['locale'] );
    92                         } else {
    93                                 $string = translate_gp( 'WordPress is also available in %s (also %s).', $guesses['0']['locale'] );
    94                         }
     86                        $string = translate_gp( 'WordPress is also available in %s.', $guesses[0]['locale'] );
    9587                }
    9688
    9789                $translated = array();
    9890                foreach ( $guesses as $guess ) {
    9991                        if ( isset( $guess['name'], $guess['subdomain'] ) ) {
    10092                                $path = '/';
    10193                                if ( $is_forums ) {
    10294                                        $path = '/support/';
    10395                                } else if ( $is_download ) {
    10496                                        $path = '/downloads/';
    10597                                }
    10698
    107                                 $translated[] = "<a href='https://{$guess['subdomain']}.wordpress.org{$path}'>{$guess['name']}</a>";
     99                                $name = \GP_Locales::by_field( 'wp_locale', $guess['locale'] )->native_name;
     100                                $translated[] = "<a href='https://{$guess['subdomain']}.wordpress.org{$path}'>{$name}</a>";
    108101                        }
    109102                }
    110103
    111104                // All locales found didn't pass the Name/Subdomain checks above.
    112105                if ( ! $translated ) {
    113106                        return;
    114107                }
    115108
    116                 echo '<div id="lang-guess">';
     109                // Load translations for wp_sprintf_l().
     110                add_filter( 'wp_sprintf_l', function( $translations ) use( $guesses ) {
     111                        $translations['between']          = sprintf( translate_gp( '%1$s, %2$s', $guesses[0]['locale'] ), '', '' );
     112                        $translations['between_last_two'] = sprintf( translate_gp( '%1$s, and %2$s', $guesses[0]['locale'] ), '', '' );
     113                        $translations['between_only_two'] = sprintf( translate_gp( '%1$s and %2$s', $guesses[0]['locale'] ), '', '' );
    117114
    118                 if ( 1 === $num_guesses ) {
    119                         printf( $string, $translated[0] );
    120                 } else {
    121                         $main_language = array_shift( $translated );
    122                         printf( $string, $main_language, implode( ', ', $translated ) );
    123                 }
     115                        return $translations;
     116                } );
    124117
     118                echo '<div id="lang-guess">';
     119                printf( $string, wp_sprintf_l( '%l', $translated ) );
    125120                echo '</div>';
    126121        }
    127122}
    128123
    129124function translate_gp( $string, $wp_locale ) {
    130125        global $wpdb;
     126
     127        // Short-circuit for english variants.
     128        if ( 0 === stripos( $wp_locale, 'en_' ) ) {
     129                return $string;
     130        }
     131
    131132        $strings = array(
     133                // These are in wp/dev
     134                7233895 => '%1$s, %2$s',
     135                7234333 => '%1$s, and %2$s',
     136                7960    => '%1$s and %2$s',
     137
     138                // These are in meta/forums-old
    132139                204369 => 'WordPress support forums are also available in %s.',
    133                 204370 => 'WordPress support forums are also available in %s (also %s).',
    134140                204371 => 'WordPress is also available in %s.',
    135                 204372 => 'WordPress is also available in %s (also %s).',
    136141        );
    137142
    138143        $original_id = array_search( $string, $strings, true );
    139144        $gp_locale = GP_Locales::by_field( 'wp_locale', $wp_locale )->slug;
    140145
    141146        $cache = wp_cache_get( 'original-' . $original_id, 'lang-guess-translations' );
    142147        if ( false !== $cache ) {
    143148                return isset( $cache[ $gp_locale ] ) ? $cache[ $gp_locale ] : $string;
    144149        }
    145150
    146         // Magic number: 81 is rosetta/forums.
     151        // Magic number: 81 is rosetta/forums-old, 2 is wp/dev
    147152        $translations = $wpdb->get_results( $wpdb->prepare(
    148153                "SELECT locale as gp_locale, translation_0 as translation
    149154                FROM translate_translation_sets ts
    150155                INNER JOIN translate_translations t
    151156                ON ts.id = t.translation_set_id
    152                 WHERE project_id = 81 AND slug = 'default' AND t.status = 'current'
     157                WHERE project_id IN( 2, 81 ) AND slug = 'default' AND t.status = 'current'
    153158                AND original_id = %d", $original_id ), OBJECT_K );
    154159
    155160        foreach ( $translations as &$translation ) {
    156161                $translation = $translation->translation;
    157162        }
    158163        unset( $translation );
    159164
    160165        wp_cache_add( 'original-' . $original_id, $translations, 'lang-guess-translations', 900 );
    161166
    162167        return isset( $translations[ $gp_locale ] ) ? $translations[ $gp_locale ] : $string;
    163168}
    164169
    165170function lang_guess_jsonp_request() {
    166171        if ( ! isset( $_GET['callback'] ) ) {
    167172                return;
    function lang_guess_jsonp_request() { 
    187192        $jsonp_callback = preg_replace( '/[^a-z0-9]/', '', $_GET['callback'] );
    188193        echo '/**/' . $jsonp_callback . '(' . $result . ')';
    189194}
    190195
    191196
    192197$return = isset( $_GET['return'] ) ? preg_replace( '/[^a-z]/', '', $_GET['return'] ) : 'html';
    193198if ( 'jsonp' === $return ) {
    194199        lang_guess_jsonp_request();
    195200} else {
    196201        lang_guess_request();
    197202}
    198203
    199204exit;
    200205
    201206__( 'WordPress support forums are also available in %s.' );
    202 __( 'WordPress support forums are also available in %s (also %s).' );
    203207__( 'WordPress is also available in %s.' );
    204 __( 'WordPress is also available in %s (also %s).' );
    205