| @@ -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 | |
| @@ -23,23 +20,13 @@ | ||
| 23 | 20 | * |
| 24 | 21 | * @since 1.2 |
| 25 | 22 | */ |
| 26 | 23 | public function __construct() { |
| 27 | - // Allows Polylang to be the first plugin loaded ;-) | |
| 28 | - add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); | |
| 29 | - add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); | |
| 30 | - | |
| 31 | - // Overriding load text domain only on front since WP 4.7 | |
| 32 | - // 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' ) ) { | |
| 34 | - return; | |
| 35 | - } | |
| 36 | - | |
| 37 | 24 | // Saves the default locale before we start any language manipulation |
| 38 | 25 | $this->default_locale = get_locale(); |
| 39 | 26 | |
| 40 | 27 | // Filters for text domain management |
| 41 | - add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); | |
| 28 | + add_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 ); | |
| 42 | 29 | add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 43 | 30 | add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 44 | 31 | |
| 45 | 32 | // Loads text domains |
| @@ -44,8 +31,12 @@ | ||
| 44 | 31 | |
| 45 | 32 | // Loads text domains |
| 46 | 33 | add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined |
| 47 | 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' ) ); | |
| 48 | 39 | } |
| 49 | 40 | |
| 50 | 41 | /** |
| 51 | 42 | * Access to the single instance of the class |
| @@ -53,9 +44,9 @@ | ||
| 53 | 44 | * @since 1.7 |
| 54 | 45 | * |
| 55 | 46 | * @return object |
| 56 | 47 | */ |
| 57 | - public static function instance() { | |
| 48 | + static public function instance() { | |
| 58 | 49 | if ( empty( self::$instance ) ) { |
| 59 | 50 | self::$instance = new self(); |
| 60 | 51 | } |
| 61 | 52 | |
| @@ -67,28 +58,19 @@ | ||
| 67 | 58 | * |
| 68 | 59 | * @since 0.1 |
| 69 | 60 | */ |
| 70 | 61 | public function load_textdomains() { |
| 71 | - // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain | |
| 72 | - 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 ); | |
| 73 | 64 | remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 74 | 65 | remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 75 | 66 | $new_locale = get_locale(); |
| 76 | 67 | |
| 77 | 68 | // 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 | |
| 69 | + // Now we can load all overriden text domains with the right language | |
| 79 | 70 | 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' ) ) { | |
| 86 | - _get_path_to_translation( null, true ); | |
| 87 | - } | |
| 88 | - | |
| 89 | 71 | foreach ( $this->list_textdomains as $textdomain ) { |
| 90 | - // 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 | |
| 91 | 73 | if ( ! load_textdomain( $textdomain['domain'], str_replace( "{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo'] ) ) ) { |
| 92 | 74 | // Since WP 3.5 themes may store languages files in /wp-content/languages/themes |
| 93 | 75 | if ( ! load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo" ) ) { |
| 94 | 76 | // Since WP 3.7 plugins may store languages files in /wp-content/languages/plugins |
| @@ -98,16 +80,16 @@ | ||
| 98 | 80 | } |
| 99 | 81 | } |
| 100 | 82 | |
| 101 | 83 | // 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 ) ); | |
| 84 | + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' ); | |
| 85 | + $post_types = array( 'polylang_mo' ); | |
| 104 | 86 | |
| 105 | 87 | // We don't need to translate core taxonomies and post types labels when setting the language from the url |
| 106 | 88 | // As they will be translated when registered the second time |
| 107 | 89 | 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 ); | |
| 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 ); | |
| 110 | 92 | } |
| 111 | 93 | |
| 112 | 94 | // Translate labels of post types and taxonomies |
| 113 | 95 | foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) { |
| @@ -138,38 +120,23 @@ | ||
| 138 | 120 | unset( $this->default_locale, $this->list_textdomains, $this->labels ); |
| 139 | 121 | } |
| 140 | 122 | |
| 141 | 123 | /** |
| 142 | - * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4 | |
| 143 | - * Was formerly hooked to the filter 'override_load_textdomain' | |
| 124 | + * Saves all text domains in a table for later usage | |
| 144 | 125 | * |
| 145 | 126 | * @since 0.1 |
| 146 | 127 | * |
| 147 | - * @param bool $bool Whether to override the .mo file loading. | |
| 148 | - * @return bool | |
| 149 | - */ | |
| 150 | - public function mofile( $bool ) { | |
| 151 | - return $bool; | |
| 152 | - } | |
| 153 | - | |
| 154 | - /** | |
| 155 | - * Saves all text domains in a table for later usage | |
| 156 | - * It replaces the 'override_load_textdomain' filter used since 0.1 | |
| 157 | - * | |
| 158 | - * @since 2.0.4 | |
| 159 | - * | |
| 128 | + * @param bool $bool not used | |
| 129 | + * @param string $domain text domain name | |
| 160 | 130 | * @param string $mofile translation file name |
| 161 | - * @param string $domain text domain name | |
| 162 | 131 | * @return bool |
| 163 | 132 | */ |
| 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 | - } | |
| 171 | - return ''; // Hack to prevent WP loading text domains as we will load them all later | |
| 133 | + public function mofile( $bool, $domain, $mofile ) { | |
| 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', '<' ); | |
| 172 | 139 | } |
| 173 | 140 | |
| 174 | 141 | /** |
| 175 | 142 | * Saves post types and taxonomies labels for a later usage |
| @@ -217,13 +184,11 @@ | ||
| 217 | 184 | |
| 218 | 185 | foreach ( $type->labels as $key => $label ) { |
| 219 | 186 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 220 | 187 | if ( empty( $translated[ $label ] ) ) { |
| 221 | - // PHPCS:disable WordPress.WP.I18n | |
| 222 | 188 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| 223 | 189 | _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) : |
| 224 | 190 | __( $label, $this->labels[ $label ]['domain'] ); |
| 225 | - // PHPCS:enable | |
| 226 | 191 | } |
| 227 | 192 | else { |
| 228 | 193 | $type->labels->$key = $translated[ $label ]; |
| 229 | 194 | } |