| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* It is best practice that plugins do nothing before `plugins_loaded` is fired. |
| 8 |
* So it is what Polylang intends to do. |
| 9 |
* But some plugins load their textdomain as soon as loaded, thus before `plugins_loaded` is fired. |
| 10 |
* This class defers textdomain loading until the language is defined either in a `plugins_loaded` action |
| 11 |
* or in a `wp` action (when the language is set from content on frontend). |
| 12 |
* |
| 13 |
* @since 1.2 |
| 14 |
*/ |
| 15 |
class PLL_OLT_Manager { |
| 16 |
/** |
| 17 |
* Singleton instance |
| 18 |
* |
| 19 |
* @var PLL_OLT_Manager|null |
| 20 |
*/ |
| 21 |
protected static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor: setups relevant filters. |
| 25 |
* |
| 26 |
* @since 1.2 |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
// Allows Polylang to be the first plugin loaded ;-) |
| 30 |
add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); |
| 31 |
add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); |
| 32 |
|
| 33 |
// Overriding load text domain only on front since WP 4.7. |
| 34 |
if ( ( is_admin() && ! Polylang::is_ajax_on_front() ) || Polylang::is_rest_request() ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// Filters for text domain management. |
| 39 |
add_filter( 'load_textdomain_mofile', '__return_empty_string' ); |
| 40 |
|
| 41 |
// Loads text domains. |
| 42 |
add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined. |
| 43 |
add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Access to the single instance of the class |
| 48 |
* |
| 49 |
* @since 1.7 |
| 50 |
* |
| 51 |
* @return PLL_OLT_Manager |
| 52 |
*/ |
| 53 |
public static function instance() { |
| 54 |
if ( empty( self::$instance ) ) { |
| 55 |
self::$instance = new self(); |
| 56 |
} |
| 57 |
|
| 58 |
return self::$instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Loads textdomains. |
| 63 |
* |
| 64 |
* @since 0.1 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function load_textdomains() { |
| 69 |
// Our load_textdomain_mofile filter has done its job. let's remove it to enable translation. |
| 70 |
remove_filter( 'load_textdomain_mofile', '__return_empty_string' ); |
| 71 |
|
| 72 |
$GLOBALS['l10n'] = array(); |
| 73 |
$new_locale = get_locale(); |
| 74 |
|
| 75 |
load_default_textdomain( $new_locale ); |
| 76 |
|
| 77 |
// Act only if the language has not been set early (before default textdomain loading and $wp_locale creation). |
| 78 |
if ( ! empty( $GLOBALS['wp_locale'] ) ) { |
| 79 |
// Reinitializes wp_locale for weekdays and months. |
| 80 |
unset( $GLOBALS['wp_locale'] ); |
| 81 |
$GLOBALS['wp_locale'] = new WP_Locale(); |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! empty( $GLOBALS['wp_locale_switcher'] ) ) { |
| 85 |
/** This action is documented in wp-includes/class-wp-locale-switcher.php */ |
| 86 |
do_action( 'change_locale', $new_locale ); |
| 87 |
} |
| 88 |
|
| 89 |
do_action_deprecated( 'pll_translate_labels', array(), '3.7', 'change_locale' ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Allows Polylang to be the first plugin loaded ;-). |
| 94 |
* |
| 95 |
* @since 1.2 |
| 96 |
* |
| 97 |
* @param string[] $plugins List of active plugins. |
| 98 |
* @return string[] List of active plugins. |
| 99 |
*/ |
| 100 |
public function make_polylang_first( $plugins ) { |
| 101 |
if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) { |
| 102 |
unset( $plugins[ $key ] ); |
| 103 |
array_unshift( $plugins, POLYLANG_BASENAME ); |
| 104 |
} |
| 105 |
return $plugins; |
| 106 |
} |
| 107 |
} |
| 108 |
|