Ticket #6600: 6600.2.diff
File 6600.2.diff, 6.6 KB (added by , 23 months ago) |
---|
-
lang-guess-ajax.php
1 1 <?php 2 2 define( 'DB_USER', '' ); 3 3 define( 'DB_PASSWORD', '' ); 4 4 define( 'DB_NAME', '' ); 5 5 define( 'DB_HOST', '' ); 6 6 7 7 // Relevant: Environment, versions, errors. 8 8 // Not relevant: options, users, super admins, keys, secrets, sso. 9 9 require dirname( __DIR__ ) . '/define.php'; 10 require WPORGPATH . 'wp-includes/plugin.php'; 11 require WPORGPATH . 'wp-includes/formatting.php'; 10 12 require WPORGPATH . 'hyperdb/bb-10-hyper-db.php'; 11 13 require WPORGPATH . 'object-cache.php'; 12 14 wp_cache_init(); 13 15 require __DIR__ . '/class-guess-lang.php'; 14 16 require GLOTPRESS_LOCALES_PATH; 15 17 16 18 // Search engines don't need to index this content. See #meta4154 17 19 header( 'X-Robots-Tag: noindex' ); 18 20 19 21 // Cache the result for a day in browsers. 20 22 header( 'Vary: Origin, Accept-Language' ); 21 23 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 86400 /* 24 * 60 * 60 */ ) . ' GMT' ); 22 24 23 25 // Set a CORS header to allow WordPress.org subdomains to request it. 24 26 if ( isset( $_SERVER['HTTP_ORIGIN'] ) && preg_match( '!^https?://([^.]+\.)?wordpress.org/?$!i', $_SERVER['HTTP_ORIGIN'], $m ) ) { … … function lang_guess_request() { 66 68 /* 67 69 * Add a debugging endpoint, so that we can get feedback from end-users easier. 68 70 */ 69 71 if ( ! empty( $_GET['debug'] ) ) { 70 72 header( 'Content-type: text/plain; charset=utf-8' ); 71 73 72 74 $language_header = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'Not Set'; 73 75 $anonymized_ip = preg_replace( '!\.\d+$!', '.xxx', $_SERVER['REMOTE_ADDR'] ); 74 76 $ip_country = $guesser->get_country_by_ip(); 75 77 76 78 echo json_encode( compact( 'language_header', 'anonymized_ip', 'ip_country', 'guesses' ), JSON_PRETTY_PRINT ); 77 79 die(); 78 80 } 79 81 80 82 if ( is_array( $guesses ) && ! empty( $guesses ) ) { 81 $num_guesses = count( $guesses );82 83 83 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'] ); 89 85 } 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'] ); 95 87 } 96 88 97 89 $translated = array(); 98 90 foreach ( $guesses as $guess ) { 99 91 if ( isset( $guess['name'], $guess['subdomain'] ) ) { 100 92 $path = '/'; 101 93 if ( $is_forums ) { 102 94 $path = '/support/'; 103 95 } else if ( $is_download ) { 104 96 $path = '/downloads/'; 105 97 } 106 98 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>"; 108 101 } 109 102 } 110 103 111 104 // All locales found didn't pass the Name/Subdomain checks above. 112 105 if ( ! $translated ) { 113 106 return; 114 107 } 115 108 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'] ), '', '' ); 117 114 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 } ); 124 117 118 echo '<div id="lang-guess">'; 119 printf( $string, wp_sprintf_l( '%l', $translated ) ); 125 120 echo '</div>'; 126 121 } 127 122 } 128 123 129 124 function translate_gp( $string, $wp_locale ) { 130 125 global $wpdb; 126 127 // Short-circuit for english variants. 128 if ( 0 === stripos( $wp_locale, 'en_' ) ) { 129 return $string; 130 } 131 131 132 $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 132 139 204369 => 'WordPress support forums are also available in %s.', 133 204370 => 'WordPress support forums are also available in %s (also %s).',134 140 204371 => 'WordPress is also available in %s.', 135 204372 => 'WordPress is also available in %s (also %s).',136 141 ); 137 142 138 143 $original_id = array_search( $string, $strings, true ); 139 144 $gp_locale = GP_Locales::by_field( 'wp_locale', $wp_locale )->slug; 140 145 141 146 $cache = wp_cache_get( 'original-' . $original_id, 'lang-guess-translations' ); 142 147 if ( false !== $cache ) { 143 148 return isset( $cache[ $gp_locale ] ) ? $cache[ $gp_locale ] : $string; 144 149 } 145 150 146 // Magic number: 81 is rosetta/forums .151 // Magic number: 81 is rosetta/forums-old, 2 is wp/dev 147 152 $translations = $wpdb->get_results( $wpdb->prepare( 148 153 "SELECT locale as gp_locale, translation_0 as translation 149 154 FROM translate_translation_sets ts 150 155 INNER JOIN translate_translations t 151 156 ON ts.id = t.translation_set_id 152 WHERE project_id = 81AND slug = 'default' AND t.status = 'current'157 WHERE project_id IN( 2, 81 ) AND slug = 'default' AND t.status = 'current' 153 158 AND original_id = %d", $original_id ), OBJECT_K ); 154 159 155 160 foreach ( $translations as &$translation ) { 156 161 $translation = $translation->translation; 157 162 } 158 163 unset( $translation ); 159 164 160 165 wp_cache_add( 'original-' . $original_id, $translations, 'lang-guess-translations', 900 ); 161 166 162 167 return isset( $translations[ $gp_locale ] ) ? $translations[ $gp_locale ] : $string; 163 168 } 164 169 165 170 function lang_guess_jsonp_request() { 166 171 if ( ! isset( $_GET['callback'] ) ) { 167 172 return; … … function lang_guess_jsonp_request() { 187 192 $jsonp_callback = preg_replace( '/[^a-z0-9]/', '', $_GET['callback'] ); 188 193 echo '/**/' . $jsonp_callback . '(' . $result . ')'; 189 194 } 190 195 191 196 192 197 $return = isset( $_GET['return'] ) ? preg_replace( '/[^a-z]/', '', $_GET['return'] ) : 'html'; 193 198 if ( 'jsonp' === $return ) { 194 199 lang_guess_jsonp_request(); 195 200 } else { 196 201 lang_guess_request(); 197 202 } 198 203 199 204 exit; 200 205 201 206 __( 'WordPress support forums are also available in %s.' ); 202 __( 'WordPress support forums are also available in %s (also %s).' );203 207 __( 'WordPress is also available in %s.' ); 204 __( 'WordPress is also available in %s (also %s).' );205