| @@ -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,37 +9,14 @@ | ||
| 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 | */ |
| @@ -50,10 +24,11 @@ | ||
| 50 | 24 | // Allows Polylang to be the first plugin loaded ;-) |
| 51 | 25 | add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); |
| 52 | 26 | add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); |
| 53 | 27 | |
| 54 | - // Overriding load text domain only on front since WP 4.7. | |
| 55 | - if ( is_admin() && ! Polylang::is_ajax_on_front() ) { | |
| 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() && function_exists( 'get_user_locale' ) ) { | |
| 56 | 31 | return; |
| 57 | 32 | } |
| 58 | 33 | |
| 59 | 34 | // Saves the default locale before we start any language manipulation |
| @@ -66,8 +41,9 @@ | ||
| 66 | 41 | |
| 67 | 42 | // Loads text domains |
| 68 | 43 | add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined |
| 69 | 44 | add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) ); |
| 45 | + | |
| 70 | 46 | } |
| 71 | 47 | |
| 72 | 48 | /** |
| 73 | 49 | * Access to the single instance of the class |
| @@ -75,9 +51,9 @@ | ||
| 75 | 51 | * @since 1.7 |
| 76 | 52 | * |
| 77 | 53 | * @return object |
| 78 | 54 | */ |
| 79 | - public static function instance() { | |
| 55 | + static public function instance() { | |
| 80 | 56 | if ( empty( self::$instance ) ) { |
| 81 | 57 | self::$instance = new self(); |
| 82 | 58 | } |
| 83 | 59 | |
| @@ -87,10 +63,8 @@ | ||
| 87 | 63 | /** |
| 88 | 64 | * Loads text domains |
| 89 | 65 | * |
| 90 | 66 | * @since 0.1 |
| 91 | - * | |
| 92 | - * @return void | |
| 93 | 67 | */ |
| 94 | 68 | public function load_textdomains() { |
| 95 | 69 | // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain |
| 96 | 70 | remove_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); |
| @@ -97,18 +71,18 @@ | ||
| 97 | 71 | remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 ); |
| 98 | 72 | remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 ); |
| 99 | 73 | $new_locale = get_locale(); |
| 100 | 74 | |
| 75 | + | |
| 101 | 76 | // 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 | |
| 77 | + // Now we can load all overriden text domains with the right language | |
| 103 | 78 | 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 ); | |
| 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' ) ) { | |
| 84 | + _get_path_to_translation( null, true ); | |
| 111 | 85 | } |
| 112 | 86 | |
| 113 | 87 | foreach ( $this->list_textdomains as $textdomain ) { |
| 114 | 88 | // Since WP 4.6, plugins translations are first loaded from wp-content/languages |
| @@ -122,16 +96,16 @@ | ||
| 122 | 96 | } |
| 123 | 97 | } |
| 124 | 98 | |
| 125 | 99 | // 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 ) ); | |
| 100 | + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' ); | |
| 101 | + $post_types = array( 'polylang_mo' ); | |
| 128 | 102 | |
| 129 | 103 | // We don't need to translate core taxonomies and post types labels when setting the language from the url |
| 130 | 104 | // As they will be translated when registered the second time |
| 131 | 105 | 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 ); | |
| 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 ); | |
| 134 | 108 | } |
| 135 | 109 | |
| 136 | 110 | // Translate labels of post types and taxonomies |
| 137 | 111 | foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) { |
| @@ -162,25 +136,35 @@ | ||
| 162 | 136 | unset( $this->default_locale, $this->list_textdomains, $this->labels ); |
| 163 | 137 | } |
| 164 | 138 | |
| 165 | 139 | /** |
| 166 | - * Saves all text domains in a table for later usage. | |
| 167 | - * It replaces the 'override_load_textdomain' filter previously used. | |
| 140 | + * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4 | |
| 141 | + * To remove in Polylang 2.1 | |
| 168 | 142 | * |
| 143 | + * @since 0.1 | |
| 144 | + * | |
| 145 | + * @param bool $bool not used | |
| 146 | + * @param string $domain text domain name | |
| 147 | + * @param string $mofile translation file name | |
| 148 | + * @return bool | |
| 149 | + */ | |
| 150 | + public function mofile( $bool, $domain, $mofile ) { | |
| 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 | + * | |
| 169 | 158 | * @since 2.0.4 |
| 170 | 159 | * |
| 171 | - * @param string $mofile The translation file name. | |
| 172 | - * @param string $domain The text domain name. | |
| 173 | - * @return string | |
| 160 | + * @param string $mofile translation file name | |
| 161 | + * @param string $domain text domain name | |
| 162 | + * @return bool | |
| 174 | 163 | */ |
| 175 | 164 | 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. | |
| 165 | + $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); | |
| 166 | + return ''; // Hack to prevent WP loading text domains as we will load them all later | |
| 183 | 167 | } |
| 184 | 168 | |
| 185 | 169 | /** |
| 186 | 170 | * Saves post types and taxonomies labels for a later usage |
| @@ -215,14 +199,13 @@ | ||
| 215 | 199 | return $translation; |
| 216 | 200 | } |
| 217 | 201 | |
| 218 | 202 | /** |
| 219 | - * Translates post types and taxonomies labels once the language is known. | |
| 203 | + * Translates post types and taxonomies labels once the language is known | |
| 220 | 204 | * |
| 221 | 205 | * @since 0.9 |
| 222 | 206 | * |
| 223 | - * @param WP_Post_Type|WP_Taxonomy $type Either a post type or a taxonomy. | |
| 224 | - * @return void | |
| 207 | + * @param object $type either a post type or a taxonomy | |
| 225 | 208 | */ |
| 226 | 209 | public function translate_labels( $type ) { |
| 227 | 210 | // Use static array to avoid translating several times the same ( default ) labels |
| 228 | 211 | static $translated = array(); |
| @@ -229,13 +212,11 @@ | ||
| 229 | 212 | |
| 230 | 213 | foreach ( $type->labels as $key => $label ) { |
| 231 | 214 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 232 | 215 | if ( empty( $translated[ $label ] ) ) { |
| 233 | - // PHPCS:disable WordPress.WP.I18n | |
| 234 | 216 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| 235 | 217 | _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) : |
| 236 | 218 | __( $label, $this->labels[ $label ]['domain'] ); |
| 237 | - // PHPCS:enable | |
| 238 | 219 | } |
| 239 | 220 | else { |
| 240 | 221 | $type->labels->$key = $translated[ $label ]; |
| 241 | 222 | } |
| @@ -243,14 +224,14 @@ | ||
| 243 | 224 | } |
| 244 | 225 | } |
| 245 | 226 | |
| 246 | 227 | /** |
| 247 | - * Allows Polylang to be the first plugin loaded ;-). | |
| 228 | + * Allows Polylang to be the first plugin loaded ;- ) | |
| 248 | 229 | * |
| 249 | 230 | * @since 1.2 |
| 250 | 231 | * |
| 251 | - * @param string[] $plugins List of active plugins. | |
| 252 | - * @return string[] List of active plugins. | |
| 232 | + * @param array $plugins list of active plugins | |
| 233 | + * @return array list of active plugins | |
| 253 | 234 | */ |
| 254 | 235 | public function make_polylang_first( $plugins ) { |
| 255 | 236 | if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) { |
| 256 | 237 | unset( $plugins[ $key ] ); |