ing_settings, true ); } } } public function initialize_wp_and_widget_strings() { $this->check_db_for_gettext_context(); icl_register_string( WPML_ST_Blog_Name_And_Description_Hooks::STRING_DOMAIN, WPML_ST_Blog_Name_And_Description_Hooks::STRING_NAME_BLOGNAME, get_option( 'blogname' ) ); icl_register_string( WPML_ST_Blog_Name_And_Description_Hooks::STRING_DOMAIN, WPML_ST_Blog_Name_And_Description_Hooks::STRING_NAME_BLOGDESCRIPTION, get_option( 'blogdescription' ) ); wpml_st_init_register_widget_titles(); $active_text_widgets = array(); $widgets = (array) get_option( 'sidebars_widgets' ); foreach ( $widgets as $k => $w ) { if ( 'wp_inactive_widgets' != $k && $k != 'array_version' ) { if ( is_array( $widgets[ $k ] ) ) { foreach ( $widgets[ $k ] as $v ) { if ( preg_match( '#text-([0-9]+)#i', $v, $matches ) ) { $active_text_widgets[] = $matches[1]; } } } } } $widget_text = get_option( 'widget_text' ); if ( is_array( $widget_text ) ) { foreach ( $widget_text as $k => $w ) { if ( ! empty( $w ) && isset( $w['title'], $w['text'] ) && in_array( $k, $active_text_widgets ) && $w['text'] ) { icl_register_string( WPML_ST_WIDGET_STRING_DOMAIN, 'widget body - ' . md5( $w['text'] ), $w['text'] ); } } } } public function get_current_string_language( $name ) { if ( isset( $this->current_string_language_cache[ $name ] ) ) { return $this->current_string_language_cache[ $name ]; } $key = 'current_language'; $found = false; $current_language = WPML_Non_Persistent_Cache::get( $key, 'WPML_String_Translation', $found ); if ( ! $found ) { $wp_api = $this->sitepress->get_wp_api(); $current_language = $wp_api->constant( 'DOING_AJAX' ) && $this->is_admin_action_from_referer() ? $this->sitepress->user_lang_by_authcookie() : $this->sitepress->get_current_language(); WPML_Non_Persistent_Cache::set( $key, $current_language, 'WPML_String_Translation' ); } if ( $this->should_use_admin_language() && ! WPML_ST_Blog_Name_And_Description_Hooks::is_string( (string) $name ) ) { $admin_display_lang = $this->get_admin_language(); $current_language = $admin_display_lang ? $admin_display_lang : $current_language; } $ret = apply_filters( 'icl_current_string_language', $current_language, $name ); $this->current_string_language_cache[ $name ] = $ret === 'all' ? $this->sitepress->get_default_language() : $ret; return $this->current_string_language_cache[ $name ]; } public function should_use_admin_language() { $key = 'should_use_admin_language'; $found = false; $should_use_admin_language = WPML_Non_Persistent_Cache::get( $key, 'WPML_String_Translation', $found ); if ( ! $found ) { $wp_api = $this->sitepress->get_wp_api(); $should_use_admin_language = ( $wp_api->constant( 'WP_ADMIN' ) && ( $this->is_admin_action_from_referer() || ! $wp_api->constant( 'DOING_AJAX' ) ) ) || $this->sitepress->is_admin_originated_rest_request(); WPML_Non_Persistent_Cache::set( $key, $should_use_admin_language, 'WPML_String_Translation' ); } return $should_use_admin_language; } public function get_admin_language() { if ( $this->sitepress->is_wpml_switch_language_triggered() ) { return $this->sitepress->get_admin_language(); } if ( ! $this->admin_language ) { $this->admin_language = $this->sitepress->get_admin_language(); } return $this->admin_language; } private function is_admin_action_from_referer() { if ( $this->is_admin_action_from_referer === null ) { $this->is_admin_action_from_referer = $this->sitepress->check_if_admin_action_from_referer(); } return $this->is_admin_action_from_referer; } public function wpml_language_has_switched() { $this->current_string_language_cache = array(); } public function change_string_lang_ajax_callback() { if ( ! $this->verify_ajax_call( 'wpml_change_string_language_nonce' ) ) { die( 'verification failed' ); } check_ajax_referer( 'wpml_change_string_language_nonce', 'wpnonce' ); if ( ! self::user_can_change_string_language() ) { wp_send_json_error( 'not allowed', 403 ); return; } $strings = \WPML\Request\Payload::listField( $_POST, 'strings', /* translators: Shown on the String Translation screen when a bulk change of the target language is chosen with no strings selected. */ __( 'Select the strings you want to change first.', 'wpml-string-translation' ) ); if ( \WPML\Request\Payload::isRefusal( $strings ) ) { \WPML\Request\Payload::refuse( $strings ); return; } global $wpdb; $change_string_language_dialog = new WPML_Change_String_Language_Select( $wpdb, $this->sitepress ); $string_ids = array_map( 'intval', $strings ); $lang = \WPML\Language\RequestedLanguage::validate( isset( $_POST['language'] ) ? sanitize_text_field( wp_unslash( $_POST['language'] ) ) : '', \WPML\Language\RequestedLanguage::SCOPE_CONFIGURED ); if ( null === $lang ) { wp_send_json_error( 'invalid language', 400 ); return; } $response = $change_string_language_dialog->change_language_of_strings( $string_ids, $lang ); wp_send_json( $response ); } public function change_string_lang_of_domain_ajax_callback() { if ( ! $this->verify_ajax_call( 'wpml_change_string_domain_language_nonce' ) ) { die( 'verification failed' ); } check_ajax_referer( 'wpml_change_string_domain_language_nonce', 'wpnonce' ); if ( ! self::user_can_change_string_language() ) { wp_send_json_error( 'not allowed', 403 ); return; } global $wpdb, $sitepress; $to_lang = \WPML\Language\RequestedLanguage::validate( isset( $_POST['language'] ) ? sanitize_text_field( wp_unslash( $_POST['language'] ) ) : '', \WPML\Language\RequestedLanguage::SCOPE_CONFIGURED ); if ( null === $to_lang ) { wp_send_json_error( 'invalid language', 400 ); return; } $from_langs = array_values( array_filter( array_map( function ( $code ) { return \WPML\Language\RequestedLanguage::validate( $code, \WPML\Language\RequestedLanguage::SCOPE_CONFIGURED ); }, isset( $_POST['langs'] ) ? array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['langs'] ) ) : array() ) ) ); if ( ! empty( $_POST['langs'] ) && empty( $from_langs ) ) { wp_send_json_error( 'invalid language', 400 ); return; } $change_string_language_domain_dialog = make( \WPML_Change_String_Domain_Language_Dialog::class ); $response = $change_string_language_domain_dialog->change_language_of_strings( $_POST['domain'], $from_langs, $to_lang, $_POST['use_default'] == 'true' ); wp_send_json( $response ); } private function verify_ajax_call( $ajax_action ) { return isset( $_POST['wpnonce'] ) && wp_verify_nonce( $_POST['wpnonce'], $ajax_action ); } }