: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use WPML\Collect\Support\Collection;
/** @var Collection $all_locales */
* WPML_Locale constructor.
* @param SitePress $sitepress
public function __construct( wpdb &$wpdb, SitePress &$sitepress, &$locale ) {
$this->sitepress =& $sitepress;
$this->locale =& $locale;
$this->locale_cache = null;
if ( $this->language_needs_title_sanitization() ) {
add_filter( 'sanitize_title', array( $this, 'filter_sanitize_title' ), 10, 2 );
* @see \Test_Admin_Settings::test_locale
* Due to the way these tests work (global state issues) I had to create this method
* to ensure we have full coverage of the code.
* This method shouldn't be used anywhere else and should be removed once tests are migrated
* to the new tests framework.
public function reset_cached_data() {
$this->locale_cache = null;
$this->all_locales = null;
* Hooked to 'sanitize_title' in case the user is using a language that has either German or Danish locale, to
* ensure that WP Core sanitization functions handle special chars accordingly.
* @param string $raw_title
public function filter_sanitize_title( $title, $raw_title ) {
if ( $title !== $raw_title ) {
remove_filter( 'sanitize_title', array( $this, 'filter_sanitize_title' ), 10 );
$chars[ chr( 195 ) . chr( 132 ) ] = 'Ae';
$chars[ chr( 195 ) . chr( 133 ) ] = 'Aa';
$chars[ chr( 195 ) . chr( 134 ) ] = 'Ae';
$chars[ chr( 195 ) . chr( 150 ) ] = 'Oe';
$chars[ chr( 195 ) . chr( 152 ) ] = 'Oe';
$chars[ chr( 195 ) . chr( 156 ) ] = 'Ue';
$chars[ chr( 195 ) . chr( 159 ) ] = 'ss';
$chars[ chr( 195 ) . chr( 164 ) ] = 'ae';
$chars[ chr( 195 ) . chr( 165 ) ] = 'aa';
$chars[ chr( 195 ) . chr( 166 ) ] = 'ae';
$chars[ chr( 195 ) . chr( 182 ) ] = 'oe';
$chars[ chr( 195 ) . chr( 184 ) ] = 'oe';
$chars[ chr( 195 ) . chr( 188 ) ] = 'ue';
$title = sanitize_title( strtr( $raw_title, $chars ) );
add_filter( 'sanitize_title', array( $this, 'filter_sanitize_title' ), 10, 2 );
public function locale() {
if ( ! $this->locale_cache ) {
add_filter( 'language_attributes', array( $this, '_language_attributes' ) );
$wp_api = $this->sitepress->get_wp_api();
$is_ajax = $wp_api->is_ajax();
if ( $is_ajax && isset( $_REQUEST['action'], $_REQUEST['lang'] ) ) {
$locale_lang_code = $_REQUEST['lang'];
} elseif ( $wp_api->is_admin()
|| $this->sitepress->check_if_admin_action_from_referer() )
$locale_lang_code = $this->sitepress->user_lang_by_authcookie();
$locale_lang_code = $this->sitepress->get_current_language();
$locale = $this->get_locale( $locale_lang_code );
if ( did_action( 'plugins_loaded' ) ) {
$this->locale_cache = $locale;
return $this->locale_cache;
public function get_locale( $code ) {
return $this->get_all_locales()->get( $code, $code );
public function get_all_locales() {
if ( ! $this->all_locales ) {
FROM {$this->wpdb->prefix}icl_languages AS l
LEFT JOIN {$this->wpdb->prefix}icl_locale_map AS m ON m.code = l.code
$this->all_locales = wpml_collect( $this->wpdb->get_results( $sql ) )
} elseif ( $row->default_locale ) {
$locale = $row->default_locale;
return [ $row->code => $locale ];
return $this->all_locales;
public function switch_locale( $lang_code = false ) {
if ( ! empty( $lang_code ) ) {
$original_l10n = isset( $l10n['sitepress'] ) ? $l10n['sitepress'] : null;
if ( $original_l10n !== null ) {
unset( $l10n['sitepress'] );
load_textdomain( 'sitepress',
WPML_PLUGIN_PATH . '/locale/sitepress-' . $this->get_locale( $lang_code ) . '.mo' );
$l10n['sitepress'] = $original_l10n;
public function get_locale_file_names() {
$res = $this->wpdb->get_results( "
FROM {$this->wpdb->prefix}icl_locale_map lm JOIN {$this->wpdb->prefix}icl_languages l ON lm.code = l.code AND l.active=1" );
foreach ( $res as $row ) {
$locales[ $row->code ] = $row->locale;
private function language_needs_title_sanitization() {
$lang_needs_filter = array( 'de_DE', 'da_DK' );
$current_lang = $this->sitepress->get_language_details( $this->sitepress->get_current_language() );
if ( ! isset( $current_lang['default_locale'] ) ) {
if ( in_array( $current_lang['default_locale'], $lang_needs_filter, true ) ) {
function _language_attributes( $latr ) {
'lang="' . str_replace( '_', '-', $this->locale ) . '"',
public static function get_instance_from_sitepress() {
/** SitePress $sitepress */
return $sitepress->get_wpml_locale();