| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * It is best practice that plugins do nothing before plugins_loaded is fired |
| 8 | 5 | * so it is what Polylang intends to do |
| @@ -12,9 +9,9 @@ | ||
| 12 | 9 | * |
| 13 | 10 | * @since 1.2 |
| 14 | 11 | */ |
| 15 | 12 | class PLL_OLT_Manager { |
| 16 | - protected static $instance; // For singleton | |
| 13 | + static protected $instance; // For singleton | |
| 17 | 14 | protected $default_locale; |
| 18 | 15 | protected $list_textdomains = array(); // All text domains |
| 19 | 16 | public $labels = array(); // Post types and taxonomies labels to translate |
| 20 | 17 | |
| @@ -29,9 +26,9 @@ | ||
| 29 | 26 | add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); |
| 30 | 27 | |
| 31 | 28 | // Overriding load text domain only on front since WP 4.7 |
| 32 | 29 | // FIXME test get_user_locale for backward compatibility with WP < 4.7 |
| 33 | - if ( is_admin() && ! Polylang::is_ajax_on_front() && function_exists( 'get_user_locale' ) ) { | |
| 30 | + if ( is_admin() && function_exists( 'get_user_locale' ) ) { | |
| 34 | 31 | return; |
| 35 | 32 | } |
| 36 | 33 | |
| 37 | 34 | // Saves the default locale before we start any language manipulation |
| @@ -44,8 +41,9 @@ | ||
| 44 | 41 | |
| 45 | 42 | // Loads text domains |
| 46 | 43 | add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined |
| 47 | 44 | add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) ); |
| 45 | + | |
| 48 | 46 | } |
| 49 | 47 | |
| 50 | 48 | /** |
| 51 | 49 | * Access to the single instance of the class |
| @@ -53,9 +51,9 @@ | ||
| 53 | 51 | * @since 1.7 |
| 54 | 52 | * |
| 55 | 53 | * @return object |
| 56 | 54 | */ |
| 57 | - public static function instance() { | |
| 55 | + static public function instance() { | |
| 58 | 56 | if ( empty( self::$instance ) ) { |
| 59 | 57 | self::$instance = new self(); |
| 60 | 58 | } |
| 61 | 59 | |
| @@ -73,17 +71,17 @@ | ||
| 73 | 71 | remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 74 | 72 | remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 75 | 73 | $new_locale = get_locale(); |
| 76 | 74 | |
| 75 | + | |
| 77 | 76 | // Don't try to save time for en_US as some users have theme written in another language |
| 78 | - // Now we can load all overridden text domains with the right language | |
| 77 | + // Now we can load all overriden text domains with the right language | |
| 79 | 78 | if ( ! empty( $this->list_textdomains ) ) { |
| 80 | - /* | |
| 81 | - * FIXME: Backward compatibility with WP < 5.6 | |
| 82 | - * From WP 4.7 to 5.5, we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US. | |
| 83 | - * See WP_Locale_Switcher::change_locale() | |
| 84 | - */ | |
| 85 | - if ( ! class_exists( 'WP_Textdomain_Registry' ) && function_exists( '_get_path_to_translation' ) ) { | |
| 79 | + | |
| 80 | + // Since WP 4.7 we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US | |
| 81 | + // See WP_Locale_Switcher::changle_locale() | |
| 82 | + // FIXME test _get_path_to_translation for backward compatibility with WP < 4.7 | |
| 83 | + if ( function_exists( '_get_path_to_translation' ) ) { | |
| 86 | 84 | _get_path_to_translation( null, true ); |
| 87 | 85 | } |
| 88 | 86 | |
| 89 | 87 | foreach ( $this->list_textdomains as $textdomain ) { |
| @@ -98,16 +96,16 @@ | ||
| 98 | 96 | } |
| 99 | 97 | } |
| 100 | 98 | |
| 101 | 99 | // First remove taxonomies and post_types labels that we don't need to translate |
| 102 | - $taxonomies = get_taxonomies( array( '_pll' => true ) ); | |
| 103 | - $post_types = get_post_types( array( '_pll' => true ) ); | |
| 100 | + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' ); | |
| 101 | + $post_types = array( 'polylang_mo' ); | |
| 104 | 102 | |
| 105 | 103 | // We don't need to translate core taxonomies and post types labels when setting the language from the url |
| 106 | 104 | // As they will be translated when registered the second time |
| 107 | 105 | if ( ! did_action( 'setup_theme' ) ) { |
| 108 | - $taxonomies = array_merge( get_taxonomies( array( '_builtin' => true ) ), $taxonomies ); | |
| 109 | - $post_types = array_merge( get_post_types( array( '_builtin' => true ) ), $post_types ); | |
| 106 | + $taxonomies = array_merge( array( 'category', 'post_tag', 'nav_menu', 'link_category', 'post_format' ), $taxonomies ); | |
| 107 | + $post_types = array_merge( array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' ), $post_types ); | |
| 110 | 108 | } |
| 111 | 109 | |
| 112 | 110 | // Translate labels of post types and taxonomies |
| 113 | 111 | foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) { |
| @@ -139,16 +137,18 @@ | ||
| 139 | 137 | } |
| 140 | 138 | |
| 141 | 139 | /** |
| 142 | 140 | * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4 |
| 143 | - * Was formerly hooked to the filter 'override_load_textdomain' | |
| 141 | + * To remove in Polylang 2.1 | |
| 144 | 142 | * |
| 145 | 143 | * @since 0.1 |
| 146 | 144 | * |
| 147 | - * @param bool $bool Whether to override the .mo file loading. | |
| 145 | + * @param bool $bool not used | |
| 146 | + * @param string $domain text domain name | |
| 147 | + * @param string $mofile translation file name | |
| 148 | 148 | * @return bool |
| 149 | 149 | */ |
| 150 | - public function mofile( $bool ) { | |
| 150 | + public function mofile( $bool, $domain, $mofile ) { | |
| 151 | 151 | return $bool; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| @@ -161,14 +161,9 @@ | ||
| 161 | 161 | * @param string $domain text domain name |
| 162 | 162 | * @return bool |
| 163 | 163 | */ |
| 164 | 164 | public function load_textdomain_mofile( $mofile, $domain ) { |
| 165 | - // On multisite, 2 files are sharing the same domain so we need to distinguish them | |
| 166 | - if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) { | |
| 167 | - $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 168 | - } else { | |
| 169 | - $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 170 | - } | |
| 165 | + $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 171 | 166 | return ''; // Hack to prevent WP loading text domains as we will load them all later |
| 172 | 167 | } |
| 173 | 168 | |
| 174 | 169 | /** |
| @@ -217,13 +212,11 @@ | ||
| 217 | 212 | |
| 218 | 213 | foreach ( $type->labels as $key => $label ) { |
| 219 | 214 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 220 | 215 | if ( empty( $translated[ $label ] ) ) { |
| 221 | - // PHPCS:disable WordPress.WP.I18n | |
| 222 | 216 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| 223 | 217 | _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) : |
| 224 | 218 | __( $label, $this->labels[ $label ]['domain'] ); |
| 225 | - // PHPCS:enable | |
| 226 | 219 | } |
| 227 | 220 | else { |
| 228 | 221 | $type->labels->$key = $translated[ $label ]; |
| 229 | 222 | } |