$option name of the option * @param string|array $value new value of the option * @param string|array $old_value currently saved value for the option * * @return string|array the value actually to be written into the wp_options table */ function pre_update_option_settings( $option, $value, $old_value ) { $wp_api = $this->sitepress->get_wp_api(); if ( $wp_api->is_multisite() && $wp_api->ms_is_switched() && ! $this->sitepress->get_setting( 'setup_complete' ) ) { return $value; } $option = new WPML_ST_Admin_Blog_Option( $this->sitepress, $this, $option ); return $option->pre_update_filter( $old_value, $value ); } /** * Instantiates a new admin option translation object * * @param string $option_name * @param string $language_code * * @return WPML_ST_Admin_Option_Translation */ public function get_admin_option( $option_name, $language_code = '' ) { return new WPML_ST_Admin_Option_Translation( $this->sitepress, $this, $option_name, $language_code ); } /** * @return WPML_ST_String_Factory */ public function string_factory() { return $this->string_factory; } /** * @param string $lang_code */ public function clear_string_filter( $lang_code ) { unset( $this->string_filters[ $lang_code ] ); } /** * @param string $lang * * @return WPML_Displayed_String_Filter */ public function get_string_filter( $lang ) { if ( true === (bool) $this->active_languages && in_array( $lang, $this->active_languages, true ) ) { return $this->get_admin_string_filter( $lang ); } else { return null; } } /** * @param string $lang * * @return mixed|\WPML_Register_String_Filter|null * @throws \WPML\Auryn\InjectionException */ public function get_admin_string_filter( $lang ) { global $sitepress_settings, $wpdb, $sitepress; if ( isset( $sitepress_settings['st']['db_ok_for_gettext_context'] ) ) { if ( ! ( isset( $this->string_filters[ $lang ] ) && 'WPML_Register_String_Filter' == get_class( $this->string_filters[ $lang ] ) ) ) { $this->string_filters[ $lang ] = isset( $this->string_filters[ $lang ] ) ? $this->string_filters[ $lang ] : false; /** @var AutoRegisterSettings $auto_register_settings */ $auto_register_settings = WPML\Container\make( AutoRegisterSettings::class ); $this->string_filters[ $lang ] = new WPML_Register_String_Filter( $wpdb, $sitepress, $this->string_factory, make( Translator::class, [ ':language' => $lang ] ), $auto_register_settings->getExcludedDomains() ); } return $this->string_filters[ $lang ]; } else { return null; } } /** * @deprecated 3.3 - Each string has its own language now. */ public function get_strings_language( $language = '' ) { $string_settings = $this->get_strings_settings(); $string_language = $language ? $language : 'en'; if ( isset( $string_settings['strings_language'] ) ) { $string_language = $string_settings['strings_language']; } return $string_language; } public function delete_all_string_data( $string_id ) { global $wpdb; $icl_string_positions_query = "DELETE FROM {$wpdb->prefix}icl_string_positions WHERE string_id=%d"; $icl_string_status_query = "DELETE FROM {$wpdb->prefix}icl_string_status WHERE string_translation_id IN (SELECT id FROM {$wpdb->prefix}icl_string_translations WHERE string_id=%d)"; $icl_string_translations_query = "DELETE FROM {$wpdb->prefix}icl_string_translations WHERE string_id=%d"; $icl_strings_query = "DELETE FROM {$wpdb->prefix}icl_strings WHERE id=%d"; $icl_string_positions_prepare = $wpdb->prepare( $icl_string_positions_query, $string_id ); $icl_string_status_prepare = $wpdb->prepare( $icl_string_status_query, $string_id ); $icl_string_translations_prepare = $wpdb->prepare( $icl_string_translations_query, $string_id ); $icl_strings_prepare = $wpdb->prepare( $icl_strings_query, $string_id ); $wpdb->query( $icl_string_positions_prepare ); $wpdb->query( $icl_string_status_prepare ); $wpdb->query( $icl_string_translations_prepare ); $wpdb->query( $icl_strings_prepare ); // Action called after all string data is deleted do_action( 'wpml_st_string_unregistered' ); } public function get_strings_settings() { global $sitepress; if ( version_compare( ICL_SITEPRESS_VERSION, '3.2', '<' ) ) { global $sitepress_settings; $string_settings = isset( $sitepress_settings['st'] ) ? $sitepress_settings['st'] : array(); } else { $string_settings = $sitepress ? $sitepress->get_string_translation_settings() : array(); } $string_settings['strings_language'] = 'en'; if ( ! isset( $string_settings['icl_st_auto_reg'] ) ) { $string_settings['icl_st_auto_reg'] = 'disable'; } $string_settings['strings_per_page'] = ICL_STRING_TRANSLATION_AUTO_REGISTER_THRESHOLD; return $string_settings; } /** * @param null $empty Not used, but needed for the hooked filter * @param int $string_id * * @return null|string */ public function get_string_status_filter( $empty = null, $string_id = 0 ) { return $this->get_string_status( $string_id ); } /** * @param int|null $default Set the default value to return in case no string or more than one string is found * @param array $string_data { * * @type string $context * @type string $name Optional * } * @return int|null If there is more than one string_id, it will return the value set in $default. */ public function get_string_id_filter( $default = null, $string_data = array() ) { $result = $default; $string_id = $this->get_string_id( $string_data ); return $string_id ? $string_id : $result; } private function get_string_status( $string_id ) { global $wpdb; $status = $wpdb->get_var( $wpdb->prepare( " SELECT MIN(status) FROM {$wpdb->prefix}icl_string_translations WHERE string_id=%d ", $string_id ) ); return $status !== null ? (int) $status : null; } /** * @param array $string_data { * * @type string $context * @type string $name Optional * } * @return int|null */ private function get_string_id( $string_data ) { $context = isset( $string_data['context'] ) ? $string_data['context'] : null; $name = isset( $string_data['name'] ) ? $string_data['name'] : null; $result = null; if ( $name && $context ) { global $wpdb; $string_id_query = "SELECT id FROM {$wpdb->prefix}icl_strings WHERE context=%s"; $string_id_args = array( $context ); if ( $name ) { $string_id_query .= ' AND name=%s'; $string_id_args[] = $name; } $string_id_prepare = $wpdb->prepare( $string_id_query, $string_id_args ); $string_id = $wpdb->get_var( $string_id_prepare ); $result = (int) $string_id; } return $result; } /** * @param null $empty Not used, but needed for the hooked filter * @param string $domain * @param string $name * * @return null|string */ public function get_string_language_filter( $empty = null, $domain = '', $name = '' ) { global $wpdb; $key = md5( $domain . '_' . $name ); list( $string_lang, $found ) = $this->get_cache()->get_with_found( $key ); if ( ! $found ) { $string_query = "SELECT language FROM {$wpdb->prefix}icl_strings WHERE context=%s AND name=%s"; $string_prepare = $wpdb->prepare( $string_query, $domain, $name ); $string_lang = $wpdb->get_var( $string_prepare ); $this->get_cache()->set( $key, $string_lang, 600 ); } return $string_lang; } /** * @param WPML_WP_Cache $cache */ public function set_cache( WPML_WP_Cache $cache ) { $this->cache = $cache; } /** * @return WPML_WP_Cache */ public function get_cache() { if ( null === $this->cache ) { $this->cache = new WPML_WP_Cache( self::CACHE_GROUP ); } return $this->cache; } function check_db_for_gettext_context() { $string_settings = apply_filters( 'wpml_get_setting', [], 'st' ); if ( ! isset( $string_settings['db_ok_for_gettext_context'] ) ) { if ( function_exists( 'icl_table_column_exists' ) && icl_table_column_exists( 'icl_strings', 'domain_name_context_md5' ) ) { $string_settings['db_ok_for_gettext_context'] = true; do_action( 'wpml_set_setting', 'st', $string_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(); // create a list of active widgets $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'] ); } } } } /** * Returns the language the current string is to be translated into. * * @param string|bool|null $name * * @return string */ 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' ) ); WPML_Non_Persistent_Cache::set( $key, $should_use_admin_language, 'WPML_String_Translation' ); } return $should_use_admin_language; } /** * @return string */ 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; } /** * @return bool */ 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() { // clear the current language cache $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' ); } global $wpdb; $change_string_language_dialog = new WPML_Change_String_Language_Select( $wpdb, $this->sitepress ); $string_ids = array_map( 'intval', $_POST['strings'] ); $lang = filter_var( isset( $_POST['language'] ) ? $_POST['language'] : '', FILTER_SANITIZE_SPECIAL_CHARS ); $response = $change_string_language_dialog->change_language_of_strings( $string_ids, (string) $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' ); } global $wpdb, $sitepress; $change_string_language_domain_dialog = new WPML_Change_String_Domain_Language_Dialog( $wpdb, $sitepress, $this->string_factory ); $response = $change_string_language_domain_dialog->change_language_of_strings( $_POST['domain'], isset( $_POST['langs'] ) ? $_POST['langs'] : array(), $_POST['language'], $_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 ); } }