Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.03% |
2 / 66 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_wporg_to_wpcom_locale_mo_file | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 | |||
| wpcomsh_allow_en_locale_override | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| wpcomsh_use_wpcomsh_fallback_for_jetpack_mu_wpcom_text_domain | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WPCOMSH internationalization file. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Provides a fallback mofile that uses wpcom locale slugs instead of wporg locale slugs |
| 10 | * This is needed for WP.COM themes that have their translations bundled with the theme. |
| 11 | * |
| 12 | * @see p8yzl4-4c-p2 |
| 13 | * |
| 14 | * @param string $mofile .mo language file being loaded by load_textdomain(). |
| 15 | * @return string $mofile same or alternate mo file. |
| 16 | */ |
| 17 | function wpcomsh_wporg_to_wpcom_locale_mo_file( $mofile ) { |
| 18 | if ( file_exists( $mofile ) ) { |
| 19 | return $mofile; |
| 20 | } |
| 21 | |
| 22 | if ( ! class_exists( 'GP_Locales' ) ) { |
| 23 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
| 24 | return $mofile; |
| 25 | } |
| 26 | |
| 27 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
| 28 | } |
| 29 | |
| 30 | $locale_slug = basename( $mofile, '.mo' ); |
| 31 | $actual_locale_slug = $locale_slug; |
| 32 | |
| 33 | // These locales are not in our GP_Locales file, so rewrite them. |
| 34 | $locale_mappings = array( |
| 35 | 'de_DE_formal' => 'de_DE', // formal German |
| 36 | ); |
| 37 | |
| 38 | if ( isset( $locale_mappings[ $locale_slug ] ) ) { |
| 39 | $locale_slug = $locale_mappings[ $locale_slug ]; |
| 40 | } |
| 41 | |
| 42 | $locale_object = GP_Locales::by_field( 'wp_locale', $locale_slug ); |
| 43 | if ( ! $locale_object ) { |
| 44 | return $mofile; |
| 45 | } |
| 46 | |
| 47 | $locale_slug = $locale_object->slug; |
| 48 | |
| 49 | // For these languages we have a different slug than WordPress.org. |
| 50 | $locale_mappings = array( |
| 51 | 'nb' => 'no', // Norwegian Bokmål. |
| 52 | ); |
| 53 | |
| 54 | if ( isset( $locale_mappings[ $locale_slug ] ) ) { |
| 55 | $locale_slug = $locale_mappings[ $locale_slug ]; |
| 56 | } |
| 57 | |
| 58 | // phpcs:ignore WordPress.PHP.PregQuoteDelimiter.Missing |
| 59 | $mofile = preg_replace( '/' . preg_quote( $actual_locale_slug ) . '\.mo$/', $locale_slug . '.mo', $mofile ); |
| 60 | return $mofile; |
| 61 | } |
| 62 | add_filter( 'load_textdomain_mofile', 'wpcomsh_wporg_to_wpcom_locale_mo_file', 9999 ); |
| 63 | |
| 64 | // Load translations for wpcomsh itself via MO file. |
| 65 | add_action( |
| 66 | 'after_setup_theme', |
| 67 | function () { |
| 68 | load_theme_textdomain( 'wpcomsh', WP_LANG_DIR . '/mu-plugins' ); |
| 69 | load_theme_textdomain( 'jetpack-mu-wpcom', WP_LANG_DIR . '/mu-plugins' ); |
| 70 | } |
| 71 | ); |
| 72 | |
| 73 | /* |
| 74 | * Early deploy of this fix in Jetpack: https://github.com/Automattic/jetpack/pull/14797 |
| 75 | * To be removed after the release of 8.5 (but things won't break with the Jetpack fix shipped). |
| 76 | */ |
| 77 | add_filter( |
| 78 | 'load_script_textdomain_relative_path', |
| 79 | function ( $relative, $src ) { |
| 80 | if ( class_exists( 'Jetpack_Photon_Static_Assets_CDN' ) ) { |
| 81 | // Get the local path from a URL which was CDN'ed by cdnize_plugin_assets(). |
| 82 | if ( preg_match( '#^' . preg_quote( Jetpack_Photon_Static_Assets_CDN::CDN, '#' ) . 'p/[^/]+/[^/]+/(.*)$#', $src, $m ) ) { |
| 83 | return $m[1]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return $relative; |
| 88 | }, |
| 89 | 10, |
| 90 | 2 |
| 91 | ); |
| 92 | |
| 93 | // Ensure use of the correct local path when loading the JavaScript translation file for a CDN'ed asset. |
| 94 | add_filter( |
| 95 | 'load_script_translation_file', |
| 96 | function ( $file, $handle, $domain ) { |
| 97 | global $wp_scripts; |
| 98 | |
| 99 | if ( in_array( $domain, array( 'jetpack-mu-wpcom', 'wpcomsh' ), true ) ) { |
| 100 | return WP_LANG_DIR . '/mu-plugins/' . basename( $file ); |
| 101 | } |
| 102 | |
| 103 | if ( class_exists( 'Jetpack_Photon_Static_Assets_CDN' ) ) { |
| 104 | // This is a rewritten plugin URL, so load the language file from the plugins path. |
| 105 | if ( isset( $wp_scripts->registered[ $handle ] ) && wp_startswith( $wp_scripts->registered[ $handle ]->src, Jetpack_Photon_Static_Assets_CDN::CDN . 'p' ) ) { |
| 106 | return WP_LANG_DIR . '/plugins/' . basename( $file ); |
| 107 | } |
| 108 | } |
| 109 | return $file; |
| 110 | }, |
| 111 | 10, |
| 112 | 3 |
| 113 | ); |
| 114 | |
| 115 | // end of https://github.com/Automattic/jetpack/pull/14797 |
| 116 | |
| 117 | /** |
| 118 | * Always allow override of _locale to English by setting ?_locale=en_US in the URL. |
| 119 | * All sites will have English translations available. |
| 120 | * |
| 121 | * This is used by class.wpcom-jetpack-mapper-get-admin-menu.php on WPCOM, which lets A8C users in support sessions view |
| 122 | * Atomic sites in languages that might not be installed on that Atomic site. WPCOM requests menu items in English, then |
| 123 | * retrieves them from the Atomic side, then translates them before display. |
| 124 | * |
| 125 | * @see D59986-code |
| 126 | * |
| 127 | * @param string $locale_in Default locale. |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | function wpcomsh_allow_en_locale_override( $locale_in ) { |
| 132 | if ( ! empty( $_GET['_locale'] ) && 'en_US' === $_GET['_locale'] ) { // phpcs:ignore WordPress.Security |
| 133 | return 'en_US'; |
| 134 | } |
| 135 | return $locale_in; |
| 136 | } |
| 137 | add_filter( 'pre_determine_locale', 'wpcomsh_allow_en_locale_override' ); |
| 138 | |
| 139 | /** |
| 140 | * Filter to hook into the `gettext` filter for requests against the `jetpack-mu-wpcom` |
| 141 | * text domain, as those translations are loaded into the `wpcomsh` text domain. |
| 142 | * |
| 143 | * @see https://github.com/Automattic/wpcomsh/issues/1727 |
| 144 | * |
| 145 | * @param string $translation The translated text. |
| 146 | * @param string $singular The text to translate. |
| 147 | * @param string $domain The text domain. |
| 148 | * @return string |
| 149 | */ |
| 150 | function wpcomsh_use_wpcomsh_fallback_for_jetpack_mu_wpcom_text_domain( $translation, $singular, $domain = 'default' ) { |
| 151 | if ( $domain !== 'jetpack-mu-wpcom' ) { |
| 152 | return $translation; |
| 153 | } |
| 154 | |
| 155 | if ( $translation !== $singular ) { |
| 156 | return $translation; |
| 157 | } |
| 158 | |
| 159 | // This is a low-level filter, and we trust that $singular is a string, so we can ignore these important warnings. |
| 160 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction |
| 161 | return translate( $singular, 'wpcomsh' ); |
| 162 | } |
| 163 | add_filter( 'gettext', 'wpcomsh_use_wpcomsh_fallback_for_jetpack_mu_wpcom_text_domain', 10, 3 ); |