| @@ -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,56 +9,24 @@ | ||
| 12 | 9 | * |
| 13 | 10 | * @since 1.2 |
| 14 | 11 | */ |
| 15 | 12 | class PLL_OLT_Manager { |
| 16 | - /** | |
| 17 | - * Singleton instance | |
| 18 | - * | |
| 19 | - * @var PLL_OLT_Manager | |
| 20 | - */ | |
| 21 | - protected static $instance; | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Stores the default site locale before it is modified. | |
| 25 | - * | |
| 26 | - * @var string | |
| 27 | - */ | |
| 13 | + static protected $instance; // For singleton | |
| 28 | 14 | protected $default_locale; |
| 15 | + protected $list_textdomains = array(); // All text domains | |
| 16 | + public $labels = array(); // Post types and taxonomies labels to translate | |
| 29 | 17 | |
| 30 | 18 | /** |
| 31 | - * Stores all loaded text domains and mo files. | |
| 32 | - * | |
| 33 | - * @var string[][] | |
| 34 | - */ | |
| 35 | - protected $list_textdomains = array(); | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Stores post types an taxonomies labels to translate. | |
| 39 | - * | |
| 40 | - * @var string[][] | |
| 41 | - */ | |
| 42 | - public $labels = array(); | |
| 43 | - | |
| 44 | - /** | |
| 45 | 19 | * Constructor: setups relevant filters |
| 46 | 20 | * |
| 47 | 21 | * @since 1.2 |
| 48 | 22 | */ |
| 49 | 23 | public function __construct() { |
| 50 | - // Allows Polylang to be the first plugin loaded ;-) | |
| 51 | - add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); | |
| 52 | - add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); | |
| 53 | - | |
| 54 | - // Overriding load text domain only on front since WP 4.7. | |
| 55 | - if ( is_admin() && ! Polylang::is_ajax_on_front() ) { | |
| 56 | - return; | |
| 57 | - } | |
| 58 | - | |
| 59 | 24 | // Saves the default locale before we start any language manipulation |
| 60 | 25 | $this->default_locale = get_locale(); |
| 61 | 26 | |
| 62 | 27 | // Filters for text domain management |
| 63 | - add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); | |
| 28 | + add_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 ); | |
| 64 | 29 | add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 65 | 30 | add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 66 | 31 | |
| 67 | 32 | // Loads text domains |
| @@ -66,8 +31,12 @@ | ||
| 66 | 31 | |
| 67 | 32 | // Loads text domains |
| 68 | 33 | add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined |
| 69 | 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' ) ); | |
| 70 | 39 | } |
| 71 | 40 | |
| 72 | 41 | /** |
| 73 | 42 | * Access to the single instance of the class |
| @@ -75,9 +44,9 @@ | ||
| 75 | 44 | * @since 1.7 |
| 76 | 45 | * |
| 77 | 46 | * @return object |
| 78 | 47 | */ |
| 79 | - public static function instance() { | |
| 48 | + static public function instance() { | |
| 80 | 49 | if ( empty( self::$instance ) ) { |
| 81 | 50 | self::$instance = new self(); |
| 82 | 51 | } |
| 83 | 52 | |
| @@ -87,32 +56,21 @@ | ||
| 87 | 56 | /** |
| 88 | 57 | * Loads text domains |
| 89 | 58 | * |
| 90 | 59 | * @since 0.1 |
| 91 | - * | |
| 92 | - * @return void | |
| 93 | 60 | */ |
| 94 | 61 | public function load_textdomains() { |
| 95 | - // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain | |
| 96 | - 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 ); | |
| 97 | 64 | remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 98 | 65 | remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 99 | 66 | $new_locale = get_locale(); |
| 100 | 67 | |
| 101 | 68 | // Don't try to save time for en_US as some users have theme written in another language |
| 102 | - // 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 | |
| 103 | 70 | if ( ! empty( $this->list_textdomains ) ) { |
| 104 | - /* | |
| 105 | - * FIXME: Backward compatibility with WP < 5.6 | |
| 106 | - * 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. | |
| 107 | - * See WP_Locale_Switcher::change_locale() | |
| 108 | - */ | |
| 109 | - if ( ! class_exists( 'WP_Textdomain_Registry' ) && function_exists( '_get_path_to_translation' ) ) { | |
| 110 | - _get_path_to_translation( '', true ); | |
| 111 | - } | |
| 112 | - | |
| 113 | 71 | foreach ( $this->list_textdomains as $textdomain ) { |
| 114 | - // 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 | |
| 115 | 73 | if ( ! load_textdomain( $textdomain['domain'], str_replace( "{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo'] ) ) ) { |
| 116 | 74 | // Since WP 3.5 themes may store languages files in /wp-content/languages/themes |
| 117 | 75 | if ( ! load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo" ) ) { |
| 118 | 76 | // Since WP 3.7 plugins may store languages files in /wp-content/languages/plugins |
| @@ -122,16 +80,16 @@ | ||
| 122 | 80 | } |
| 123 | 81 | } |
| 124 | 82 | |
| 125 | 83 | // First remove taxonomies and post_types labels that we don't need to translate |
| 126 | - $taxonomies = get_taxonomies( array( '_pll' => true ) ); | |
| 127 | - $post_types = get_post_types( array( '_pll' => true ) ); | |
| 84 | + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' ); | |
| 85 | + $post_types = array( 'polylang_mo' ); | |
| 128 | 86 | |
| 129 | 87 | // We don't need to translate core taxonomies and post types labels when setting the language from the url |
| 130 | 88 | // As they will be translated when registered the second time |
| 131 | 89 | if ( ! did_action( 'setup_theme' ) ) { |
| 132 | - $taxonomies = array_merge( get_taxonomies( array( '_builtin' => true ) ), $taxonomies ); | |
| 133 | - $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 ); | |
| 134 | 92 | } |
| 135 | 93 | |
| 136 | 94 | // Translate labels of post types and taxonomies |
| 137 | 95 | foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) { |
| @@ -162,25 +120,23 @@ | ||
| 162 | 120 | unset( $this->default_locale, $this->list_textdomains, $this->labels ); |
| 163 | 121 | } |
| 164 | 122 | |
| 165 | 123 | /** |
| 166 | - * Saves all text domains in a table for later usage. | |
| 167 | - * It replaces the 'override_load_textdomain' filter previously used. | |
| 124 | + * Saves all text domains in a table for later usage | |
| 168 | 125 | * |
| 169 | - * @since 2.0.4 | |
| 126 | + * @since 0.1 | |
| 170 | 127 | * |
| 171 | - * @param string $mofile The translation file name. | |
| 172 | - * @param string $domain The text domain name. | |
| 173 | - * @return string | |
| 128 | + * @param bool $bool not used | |
| 129 | + * @param string $domain text domain name | |
| 130 | + * @param string $mofile translation file name | |
| 131 | + * @return bool | |
| 174 | 132 | */ |
| 175 | - public function load_textdomain_mofile( $mofile, $domain ) { | |
| 176 | - // On multisite, 2 files are sharing the same domain so we need to distinguish them. | |
| 177 | - if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) { | |
| 178 | - $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 179 | - } else { | |
| 180 | - $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 181 | - } | |
| 182 | - 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', '<' ); | |
| 183 | 139 | } |
| 184 | 140 | |
| 185 | 141 | /** |
| 186 | 142 | * Saves post types and taxonomies labels for a later usage |
| @@ -215,14 +171,13 @@ | ||
| 215 | 171 | return $translation; |
| 216 | 172 | } |
| 217 | 173 | |
| 218 | 174 | /** |
| 219 | - * Translates post types and taxonomies labels once the language is known. | |
| 175 | + * Translates post types and taxonomies labels once the language is known | |
| 220 | 176 | * |
| 221 | 177 | * @since 0.9 |
| 222 | 178 | * |
| 223 | - * @param WP_Post_Type|WP_Taxonomy $type Either a post type or a taxonomy. | |
| 224 | - * @return void | |
| 179 | + * @param object $type either a post type or a taxonomy | |
| 225 | 180 | */ |
| 226 | 181 | public function translate_labels( $type ) { |
| 227 | 182 | // Use static array to avoid translating several times the same ( default ) labels |
| 228 | 183 | static $translated = array(); |
| @@ -229,13 +184,11 @@ | ||
| 229 | 184 | |
| 230 | 185 | foreach ( $type->labels as $key => $label ) { |
| 231 | 186 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 232 | 187 | if ( empty( $translated[ $label ] ) ) { |
| 233 | - // PHPCS:disable WordPress.WP.I18n | |
| 234 | 188 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| 235 | 189 | _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) : |
| 236 | 190 | __( $label, $this->labels[ $label ]['domain'] ); |
| 237 | - // PHPCS:enable | |
| 238 | 191 | } |
| 239 | 192 | else { |
| 240 | 193 | $type->labels->$key = $translated[ $label ]; |
| 241 | 194 | } |
| @@ -243,14 +196,14 @@ | ||
| 243 | 196 | } |
| 244 | 197 | } |
| 245 | 198 | |
| 246 | 199 | /** |
| 247 | - * Allows Polylang to be the first plugin loaded ;-). | |
| 200 | + * Allows Polylang to be the first plugin loaded ;- ) | |
| 248 | 201 | * |
| 249 | 202 | * @since 1.2 |
| 250 | 203 | * |
| 251 | - * @param string[] $plugins List of active plugins. | |
| 252 | - * @return string[] List of active plugins. | |
| 204 | + * @param array $plugins list of active plugins | |
| 205 | + * @return array list of active plugins | |
| 253 | 206 | */ |
| 254 | 207 | public function make_polylang_first( $plugins ) { |
| 255 | 208 | if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) { |
| 256 | 209 | unset( $plugins[ $key ] ); |