| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @since 1.2 |
| 11 | 11 | */ |
| 12 | 12 | class PLL_OLT_Manager { |
| 13 | - protected static $instance; // For singleton | |
| 13 | + static protected $instance; // For singleton | |
| 14 | 14 | protected $default_locale; |
| 15 | 15 | protected $list_textdomains = array(); // All text domains |
| 16 | 16 | public $labels = array(); // Post types and taxonomies labels to translate |
| 17 | 17 | |
| @@ -20,23 +20,13 @@ | ||
| 20 | 20 | * |
| 21 | 21 | * @since 1.2 |
| 22 | 22 | */ |
| 23 | 23 | public function __construct() { |
| 24 | - // Allows Polylang to be the first plugin loaded ;-) | |
| 25 | - add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); | |
| 26 | - add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); | |
| 27 | - | |
| 28 | - // Overriding load text domain only on front since WP 4.7 | |
| 29 | - // FIXME test get_user_locale for backward compatibility with WP < 4.7 | |
| 30 | - if ( is_admin() && ! Polylang::is_ajax_on_front() && function_exists( 'get_user_locale' ) ) { | |
| 31 | - return; | |
| 32 | - } | |
| 33 | - | |
| 34 | 24 | // Saves the default locale before we start any language manipulation |
| 35 | 25 | $this->default_locale = get_locale(); |
| 36 | 26 | |
| 37 | 27 | // Filters for text domain management |
| 38 | - add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); | |
| 28 | + add_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 ); | |
| 39 | 29 | add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 40 | 30 | add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 41 | 31 | |
| 42 | 32 | // Loads text domains |
| @@ -41,8 +31,12 @@ | ||
| 41 | 31 | |
| 42 | 32 | // Loads text domains |
| 43 | 33 | add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined |
| 44 | 34 | add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) ); |
| 35 | + | |
| 36 | + // Allows Polylang to be the first plugin loaded ;-) | |
| 37 | + add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); | |
| 38 | + add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); | |
| 45 | 39 | } |
| 46 | 40 | |
| 47 | 41 | /** |
| 48 | 42 | * Access to the single instance of the class |
| @@ -50,9 +44,9 @@ | ||
| 50 | 44 | * @since 1.7 |
| 51 | 45 | * |
| 52 | 46 | * @return object |
| 53 | 47 | */ |
| 54 | - public static function instance() { | |
| 48 | + static public function instance() { | |
| 55 | 49 | if ( empty( self::$instance ) ) { |
| 56 | 50 | self::$instance = new self(); |
| 57 | 51 | } |
| 58 | 52 | |
| @@ -64,27 +58,19 @@ | ||
| 64 | 58 | * |
| 65 | 59 | * @since 0.1 |
| 66 | 60 | */ |
| 67 | 61 | public function load_textdomains() { |
| 68 | - // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain | |
| 69 | - remove_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); | |
| 62 | + // Our override_load_textdomain filter has done its job. let's remove it before calling load_textdomain | |
| 63 | + remove_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 ); | |
| 70 | 64 | remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 71 | 65 | remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 72 | 66 | $new_locale = get_locale(); |
| 73 | 67 | |
| 74 | 68 | // Don't try to save time for en_US as some users have theme written in another language |
| 75 | - // Now we can load all overridden text domains with the right language | |
| 69 | + // Now we can load all overriden text domains with the right language | |
| 76 | 70 | if ( ! empty( $this->list_textdomains ) ) { |
| 77 | - | |
| 78 | - // Since WP 4.7 we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US | |
| 79 | - // See WP_Locale_Switcher::change_locale() | |
| 80 | - // FIXME test _get_path_to_translation for backward compatibility with WP < 4.7 | |
| 81 | - if ( function_exists( '_get_path_to_translation' ) ) { | |
| 82 | - _get_path_to_translation( null, true ); | |
| 83 | - } | |
| 84 | - | |
| 85 | 71 | foreach ( $this->list_textdomains as $textdomain ) { |
| 86 | - // Since WP 4.6, plugins translations are first loaded from wp-content/languages | |
| 72 | + // FIXME: Since WP 4.6, plugins translations are first loaded from wp-content/languages | |
| 87 | 73 | if ( ! load_textdomain( $textdomain['domain'], str_replace( "{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo'] ) ) ) { |
| 88 | 74 | // Since WP 3.5 themes may store languages files in /wp-content/languages/themes |
| 89 | 75 | if ( ! load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo" ) ) { |
| 90 | 76 | // Since WP 3.7 plugins may store languages files in /wp-content/languages/plugins |
| @@ -94,16 +80,16 @@ | ||
| 94 | 80 | } |
| 95 | 81 | } |
| 96 | 82 | |
| 97 | 83 | // First remove taxonomies and post_types labels that we don't need to translate |
| 98 | - $taxonomies = get_taxonomies( array( '_pll' => true ) ); | |
| 99 | - $post_types = get_post_types( array( '_pll' => true ) ); | |
| 84 | + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' ); | |
| 85 | + $post_types = array( 'polylang_mo' ); | |
| 100 | 86 | |
| 101 | 87 | // We don't need to translate core taxonomies and post types labels when setting the language from the url |
| 102 | 88 | // As they will be translated when registered the second time |
| 103 | 89 | if ( ! did_action( 'setup_theme' ) ) { |
| 104 | - $taxonomies = array_merge( get_taxonomies( array( '_builtin' => true ) ), $taxonomies ); | |
| 105 | - $post_types = array_merge( get_post_types( array( '_builtin' => true ) ), $post_types ); | |
| 90 | + $taxonomies = array_merge( array( 'category', 'post_tag', 'nav_menu', 'link_category', 'post_format' ), $taxonomies ); | |
| 91 | + $post_types = array_merge( array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' ), $post_types ); | |
| 106 | 92 | } |
| 107 | 93 | |
| 108 | 94 | // Translate labels of post types and taxonomies |
| 109 | 95 | foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) { |
| @@ -134,10 +120,9 @@ | ||
| 134 | 120 | unset( $this->default_locale, $this->list_textdomains, $this->labels ); |
| 135 | 121 | } |
| 136 | 122 | |
| 137 | 123 | /** |
| 138 | - * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4 | |
| 139 | - * To remove in Polylang 2.1 | |
| 124 | + * Saves all text domains in a table for later usage | |
| 140 | 125 | * |
| 141 | 126 | * @since 0.1 |
| 142 | 127 | * |
| 143 | 128 | * @param bool $bool not used |
| @@ -145,32 +130,16 @@ | ||
| 145 | 130 | * @param string $mofile translation file name |
| 146 | 131 | * @return bool |
| 147 | 132 | */ |
| 148 | 133 | public function mofile( $bool, $domain, $mofile ) { |
| 149 | - return $bool; | |
| 134 | + $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 135 | + // Prevents WP loading text domains as we will load them all later | |
| 136 | + // FIXME backward compatibility with WP < 4.6. See #34213 | |
| 137 | + // true for WP < 4.6, false for WP 4.6+ as we need to keep memory of the location of the language file inside the plugin directory | |
| 138 | + return version_compare( $GLOBALS['wp_version'], '4.6-alpha', '<' ); | |
| 150 | 139 | } |
| 151 | 140 | |
| 152 | 141 | /** |
| 153 | - * Saves all text domains in a table for later usage | |
| 154 | - * It replaces the 'override_load_textdomain' filter used since 0.1 | |
| 155 | - * | |
| 156 | - * @since 2.0.4 | |
| 157 | - * | |
| 158 | - * @param string $mofile translation file name | |
| 159 | - * @param string $domain text domain name | |
| 160 | - * @return bool | |
| 161 | - */ | |
| 162 | - public function load_textdomain_mofile( $mofile, $domain ) { | |
| 163 | - // On multisite, 2 files are sharing the same domain so we need to distinguish them | |
| 164 | - if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) { | |
| 165 | - $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 166 | - } else { | |
| 167 | - $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 168 | - } | |
| 169 | - return ''; // Hack to prevent WP loading text domains as we will load them all later | |
| 170 | - } | |
| 171 | - | |
| 172 | - /** | |
| 173 | 142 | * Saves post types and taxonomies labels for a later usage |
| 174 | 143 | * |
| 175 | 144 | * @since 0.9 |
| 176 | 145 | * |
| @@ -215,13 +184,11 @@ | ||
| 215 | 184 | |
| 216 | 185 | foreach ( $type->labels as $key => $label ) { |
| 217 | 186 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 218 | 187 | if ( empty( $translated[ $label ] ) ) { |
| 219 | - // PHPCS:disable WordPress.WP.I18n | |
| 220 | 188 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| 221 | 189 | _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) : |
| 222 | 190 | __( $label, $this->labels[ $label ]['domain'] ); |
| 223 | - // PHPCS:enable | |
| 224 | 191 | } |
| 225 | 192 | else { |
| 226 | 193 | $type->labels->$key = $translated[ $label ]; |
| 227 | 194 | } |