public function register_string($context, $name, $value, $allow_empty_value = false) { global $wpdb; /* cpt slugs - do not register them when scanning themes and plugins * if name starting from 'URL slug: ' * and context is different from 'WordPress' */ if (substr($name, 0, 10) === 'URL slug: ' && 'WordPress' !== $context) { return false; } // if the default language is not set up return without doing anything if (!icl_get_setting('existing_content_language_verified')) { return false; } $translation = $this->string_from_registered($name, $context); if ($translation === $value) { return false; } list($name, $context) = $this->truncate_name_and_context($name, $context); $language = $this->language; $query = $wpdb->prepare("SELECT id, value, status FROM {$wpdb->prefix}icl_strings WHERE context=%s AND name=%s", $context, $name); $res = $wpdb->get_row($query); if ($res) { $string_id = $res->id; /* * If Sticky Links plugin is active and set to change links in Strings, * we need to process $value and change links into sticky before comparing * with saved in DB $res->value. * Otherwise after every String Translation screen refresh status of this string * will be changed into 'needs update' */ $alp_settings = get_option('alp_settings'); if (!empty($alp_settings['sticky_links_strings']) && $alp_settings['sticky_links_strings'] && defined('WPML_STICKY_LINKS_VERSION')) { // sticky links plugin is active? require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $absolute_links_object = new AbsoluteLinks(); $alp_broken_links = array(); $value = $absolute_links_object->_process_generic_text($value, $alp_broken_links); } $update_string = array(); if ($value != $res->value) { $update_string['value'] = $value; } if (!empty($update_string)) { $wpdb->update($wpdb->prefix . 'icl_strings', $update_string, array('id' => $string_id)); $wpdb->update($wpdb->prefix . 'icl_string_translations', array('status' => ICL_TM_NEEDS_UPDATE), array('string_id' => $string_id)); icl_update_string_status($string_id); } } else { $string_id = $this->save_string($value, $allow_empty_value, $language, $context, $name); } global $WPML_Sticky_Links; if (defined('WPML_TM_PATH') && !empty($WPML_Sticky_Links) && $WPML_Sticky_Links->settings['sticky_links_strings']) { require_once WPML_TM_PATH . '/inc/translation-proxy/wpml-pro-translation.class.php'; WPML_Pro_Translation::_content_make_links_sticky($string_id, 'string', false); } $this->name_cache[$name . $context] = $value; return $string_id; }
public function register_string($context, $name, $value, $allow_empty_value = false) { $name = trim($name) ? $name : md5($value); /* cpt slugs - do not register them when scanning themes and plugins * if name starting from 'URL slug: ' * and context is different from 'WordPress' */ if (substr($name, 0, 10) === 'URL slug: ' && 'WordPress' !== $context) { return false; } list($domain, $context) = $this->key_by_name_and_context($name, $context); list($name, $context) = $this->truncate_name_and_context($name, $context); $res = $this->get_registered_string($domain, $context, $name); if ($res) { $string_id = $res['id']; /* * If Sticky Links plugin is active and set to change links in Strings, * we need to process $value and change links into sticky before comparing * with saved in DB $res->value. * Otherwise after every String Translation screen refresh status of this string * will be changed into 'needs update' */ $alp_settings = get_option('alp_settings'); if (!empty($alp_settings['sticky_links_strings']) && $alp_settings['sticky_links_strings'] && defined('WPML_STICKY_LINKS_VERSION')) { // sticky links plugin is active? require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $absolute_links_object = new AbsoluteLinks(); $alp_broken_links = array(); $value = $absolute_links_object->_process_generic_text($value, $alp_broken_links); } $update_string = array(); if ($value != $res['value']) { $update_string['value'] = $value; } if (!empty($update_string)) { $this->wpdb->update($this->wpdb->prefix . 'icl_strings', $update_string, array('id' => $string_id)); $this->wpdb->update($this->wpdb->prefix . 'icl_string_translations', array('status' => ICL_TM_NEEDS_UPDATE), array('string_id' => $string_id)); icl_update_string_status($string_id); } } else { $string_id = $this->save_string($value, $allow_empty_value, 'en', $domain, $context, $name); } global $WPML_Sticky_Links; if (defined('WPML_TM_PATH') && !empty($WPML_Sticky_Links) && $WPML_Sticky_Links->settings['sticky_links_strings']) { require_once WPML_TM_PATH . '/inc/translation-proxy/wpml-pro-translation.class.php'; WPML_Pro_Translation::_content_make_links_sticky($string_id, 'string', false); } $this->name_cache[md5($domain . $name . $context)] = $value; return $string_id; }
function save_default_urls($post_id, $post) { if ($post->post_status == 'auto-draft' || isset($_POST['autosave'])) { return; } if (!in_array($post->post_type, get_post_types(array('show_ui' => true)))) { return; } if (!post_type_supports($post->post_type, 'editor')) { return; } if (in_array($post->post_type, array('revision', 'attachment', 'nav_menu_item'))) { return; } $this->absolute_links_object->process_post($post_id); }
public static function _content_make_links_sticky($element_id, $element_type = 'post', $string_translation = true) { if (strpos($element_type, 'post') === 0) { // only need to do it if sticky links is not enabled. // create the object require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $icl_abs_links = new AbsoluteLinks(); $icl_abs_links->process_post($element_id); } elseif ($element_type == 'string') { require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $icl_abs_links = new AbsoluteLinks(); // call just for strings $icl_abs_links->process_string($element_id, $string_translation); } }
/** * Used as filter for wordpress core function url_to_postid() * * @global AbsoluteLinks $absolute_links_object * @param string $url URL to filter * @return string URL changed into format ...?p={ID} or original */ function url_to_postid($url) { if (strpos($url, 'wp-login.php') !== false) { return $url; } else { if (strpos($url, '/wp-admin/') !== false) { return $url; } else { if (strpos($url, '/wp-content/') !== false) { return $url; } } } $is_language_in_domain = false; // if language negotiation type as lang. in domain $is_translated_domain = false; // if this url is in secondary language domain // for 'diffrent domain per language' we need to switch_lang according to domain of parsed $url if (2 == $this->settings['language_negotiation_type'] && isset($this->settings['language_domains'])) { $is_language_in_domain = true; // if url domain fits to one of secondary language domains // switch sitepress language to this // but save current language context in $current_language, we will have to switch to this back foreach ($this->settings['language_domains'] as $code => $domain) { if (strpos($url, $domain) === 0) { $is_translated_domain = true; $current_language = $this->get_current_language(); $this->switch_lang($code); $url = str_replace($domain, site_url(), $url); break; } } // if it is url in original domain // switch sitepress language to default language // but save current language context in $current_language, we will have to switch to this back if (!$is_translated_domain) { $current_language = $this->get_current_language(); $default_language = $this->get_default_language(); $this->switch_lang($default_language); } } // we will use AbsoluteLinks::_process_generic_text, so make sure that // we have this object here global $absolute_links_object; if (!isset($absolute_links_object) || !is_a($absolute_links_object, 'AbsoluteLinks') || $is_language_in_domain) { require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $absolute_links_object = new AbsoluteLinks(); } // in next steps we will have to compare processed url with original, // so we need to save original $original_url = $url; // we also need site_url for comparisions $site_url = site_url(); // _process_generic_text will change slug urls into ?p=1 or ?cpt-slug=cpt-title // but this function operates not on clean url but on html <a> element // we need to change temporary url into html, pass to this function and // extract url from returned html $html = '<a href="' . $url . '">removeit</a>'; $alp_broken_links = array(); remove_filter('url_to_postid', array($this, 'url_to_postid')); $html = $absolute_links_object->_process_generic_text($html, $alp_broken_links); add_filter('url_to_postid', array($this, 'url_to_postid')); $url = str_replace(array('<a href="', '">removeit</a>'), array('', ''), $html); // for 'diffrent domain per language', switch language back. now we can do this if ($is_language_in_domain) { $this->switch_lang($current_language); } // if this is not url to external site if (0 === strpos($original_url, $site_url)) { // if this is url like ...?cpt-rewrite-slug=cpt-title // change it into ...?p=11 $url2 = $this->cpt_url_to_id_url($url, $original_url); if ($url2 == $url && $original_url != $url) { // if it was not a case with ?cpt-slug=cpt-title // if this is translated post and it has the same slug as original, // _process_generic_text returns the same ID for both // lets check if it is this case and replace ID in returned url $url = $this->maybe_adjust_url($url, $original_url); } else { // yes! it was not a case with ?cpt-slug=cpt-title $url = $url2; } } return $url; }
public function register_string($context, $name, $value, $allow_empty_value = false, $source_lang = '') { global $WPML_Sticky_Links; $name = trim($name) ? $name : md5($value); /* cpt slugs - do not register them when scanning themes and plugins * if name starting from 'URL slug: ' * and context is different from 'WordPress' */ if (substr($name, 0, 10) === 'URL slug: ' && 'WordPress' !== $context) { return false; } list($domain, $context, $key) = $this->key_by_name_and_context($name, $context); list($name, $context) = $this->truncate_name_and_context($name, $context); if ($source_lang == '') { $lang_of_domain = new WPML_Language_Of_Domain($this->sitepress); $domain_lang = $lang_of_domain->get_language($domain); $source_lang = $domain_lang ? $domain_lang : (strpos($domain, 'admin_texts_') === 0 || $name === 'Tagline' || $name === 'Blog Title' ? $this->sitepress->get_user_admin_language(get_current_user_id()) : 'en'); $source_lang = $source_lang ? $source_lang : 'en'; } $res = $this->get_registered_string($domain, $context, $name); if ($res) { $string_id = $res['id']; /* * If Sticky Links plugin is active and set to change links in Strings, * we need to process $value and change links into sticky before comparing * with saved in DB $res->value. * Otherwise after every String Translation screen refresh status of this string * will be changed into 'needs update' */ $alp_settings = get_option('alp_settings'); if (!empty($alp_settings['sticky_links_strings']) && $alp_settings['sticky_links_strings'] && defined('WPML_STICKY_LINKS_VERSION')) { // sticky links plugin is active? require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $absolute_links_object = new AbsoluteLinks(); $alp_broken_links = array(); $value = $absolute_links_object->_process_generic_text($value, $alp_broken_links); } $update_string = array(); if ($value != $res['value']) { $update_string['value'] = $value; } $existing_lang = $this->string_factory->find_by_id($res['id'])->get_language(); if (!empty($update_string)) { if ($existing_lang == $source_lang) { $this->wpdb->update($this->wpdb->prefix . 'icl_strings', $update_string, array('id' => $string_id)); $this->wpdb->update($this->wpdb->prefix . 'icl_string_translations', array('status' => ICL_TM_NEEDS_UPDATE), array('string_id' => $string_id)); icl_update_string_status($string_id); } else { $orig_data = array('string_id' => $string_id, 'language' => $source_lang); $update_string['status'] = ICL_TM_COMPLETE; if ($this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(*)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$this->wpdb->prefix}icl_string_translations\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE string_id = %d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t \tAND language = %s", $string_id, $source_lang))) { $this->wpdb->update($this->wpdb->prefix . 'icl_string_translations', $update_string, $orig_data); } else { $this->wpdb->insert($this->wpdb->prefix . 'icl_string_translations', array_merge($update_string, $orig_data)); } icl_update_string_status($string_id); } } } else { $string_id = $this->save_string($value, $allow_empty_value, $source_lang, $domain, $context, $name); } if (defined('WPML_TM_PATH') && !empty($WPML_Sticky_Links) && $WPML_Sticky_Links->settings['sticky_links_strings']) { require_once WPML_TM_PATH . '/inc/translation-proxy/wpml-pro-translation.class.php'; WPML_Pro_Translation::_content_make_links_sticky($string_id, 'string', false); } if (!isset($this->name_cache[$key])) { $this->name_cache[$key] = $value; } return $string_id; }
function icl_register_string($context, $name, $value, $allow_empty_value = false) { global $wpdb, $sitepress_settings; /* cpt slugs - do not register them when scanning themes and plugins * if name starting from 'URL slug: ' * and context is different from 'WordPress' */ if (substr($name, 0, 10) === 'URL slug: ' && 'WordPress' !== $context) { return false; } // if the default language is not set up return without doing anything if (!isset($sitepress_settings['existing_content_language_verified']) || !$sitepress_settings['existing_content_language_verified']) { return false; } // Check if cached (so exists) $cached = icl_t_cache_lookup($context, $name); if ($cached && isset($cached['original']) && $cached['original'] == $value) { return false; } $language = isset($sitepress_settings['st']['strings_language']) ? $sitepress_settings['st']['strings_language'] : 'en'; $res = $wpdb->get_row("SELECT id, value, status, language FROM {$wpdb->prefix}icl_strings WHERE context='" . esc_sql($context) . "' AND name='" . esc_sql($name) . "'"); if ($res) { $string_id = $res->id; $update_string = array(); /* * If Sticky Links plugin is active and set to change links in Strings, * we need to process $value and change links into sticky before comparing * with saved in DB $res->value. * Otherwise after every String Translation screen refresh status of this string * will be changed into 'needs update' */ $alp_settings = get_option('alp_settings'); if (!empty($alp_settings['sticky_links_strings']) && $alp_settings['sticky_links_strings'] && defined('WPML_STICKY_LINKS_VERSION')) { // sticky links plugin is active? require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $absolute_links_object = new AbsoluteLinks(); $alp_broken_links = array(); $value = $absolute_links_object->_process_generic_text($value, $alp_broken_links); } if ($value != $res->value) { $update_string['value'] = $value; } if ($language != $res->language) { $update_string['language'] = $language; } if (!empty($update_string)) { $wpdb->update($wpdb->prefix . 'icl_strings', $update_string, array('id' => $string_id)); $wpdb->update($wpdb->prefix . 'icl_string_translations', array('status' => ICL_STRING_TRANSLATION_NEEDS_UPDATE), array('string_id' => $string_id)); icl_update_string_status($string_id); } } else { if (!empty($value) && is_scalar($value) && trim($value) || $allow_empty_value) { $string = array('language' => $language, 'context' => $context, 'name' => $name, 'value' => $value, 'status' => ICL_STRING_TRANSLATION_NOT_TRANSLATED); $wpdb->insert($wpdb->prefix . 'icl_strings', $string); $string_id = $wpdb->insert_id; } else { $string_id = 0; } } global $WPML_Sticky_Links; if (!empty($WPML_Sticky_Links) && $WPML_Sticky_Links->settings['sticky_links_strings']) { require_once ICL_PLUGIN_PATH . '/inc/translation-management/pro-translation.class.php'; ICL_Pro_Translation::_content_make_links_sticky($string_id, 'string', false); } return $string_id; }
public static function _content_make_links_sticky($element_id, $element_type = 'post', $string_translation = true) { require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $icl_abs_links = new AbsoluteLinks(); if (strpos($element_type, 'post') === 0) { $icl_abs_links->process_post($element_id); } elseif ($element_type == 'string') { $icl_abs_links->process_string($element_id, $string_translation); } }
/** * Converts links in a string to the corresponding ones in the current language * * @param $body string to check against * @return bool|mixed|string $body * * @since unknown */ function wpml_content_fix_links_to_translated_content($body){ global $WPV_settings, $wpdb, $sitepress, $sitepress_settings, $wp_taxonomies; if ( ! $WPV_settings->wpml_fix_urls ) { return $body; } if (isset($sitepress)) { static $content_cache = array(); $target_lang_code = $sitepress->get_current_language(); $cache_code = md5($body . $target_lang_code); if (isset($content_cache[$cache_code])) { $body = $content_cache[$cache_code]; } else { // On the latest fix, those two hooks were moved to after the _process_generic_text call // This needs wild testing on sites with a non-english first language add_filter('icl_current_language', 'wpv_wpml_icl_current_language'); remove_filter('option_rewrite_rules', array($sitepress, 'rewrite_rules_filter')); require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $icl_abs_links = new AbsoluteLinks; $old_body = $body; $alp_broken_links = array(); $body = $icl_abs_links->_process_generic_text($body, $alp_broken_links); // Restore the language as the above call can change the current language. $sitepress->switch_lang($target_lang_code); if ($body == '') { // Handle a problem with abs links occasionally return empty. $body = $old_body; } $new_body = $body; $base_url_parts = parse_url(get_option('home')); $links = wpml_content_get_link_paths($body); $all_links_fixed = 1; $pass_on_qvars = array(); $pass_on_fragments = array(); foreach($links as $link_idx => $link) { $path = $link[2]; $url_parts = parse_url($path); if(isset($url_parts['fragment'])){ $pass_on_fragments[$link_idx] = $url_parts['fragment']; } if((!isset($url_parts['host']) or $base_url_parts['host'] == $url_parts['host']) and (!isset($url_parts['scheme']) or $base_url_parts['scheme'] == $url_parts['scheme']) and isset($url_parts['query'])) { $query_parts = explode('&', $url_parts['query']); foreach($query_parts as $query){ // find p=id or cat=id or tag=id queries list($key, $value) = explode('=', $query); $translations = NULL; $is_tax = false; if($key == 'p'){ $kind = 'post_' . $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d LIMIT 1", $value ) ); } else if($key == "page_id"){ $kind = 'post_page'; } else if($key == 'cat' || $key == 'cat_ID'){ $is_tax = true; $kind = 'tax_category'; $taxonomy = 'category'; } else if($key == 'tag'){ $is_tax = true; $taxonomy = 'post_tag'; $kind = 'tax_' . $taxonomy; $value = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id WHERE x.taxonomy = %s AND t.slug = %s LIMIT 1", $taxonomy, $value ) ); } else { $found = false; foreach($wp_taxonomies as $ktax => $tax){ if($tax->query_var && $key == $tax->query_var){ $found = true; $is_tax = true; $kind = 'tax_' . $ktax; $value = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id WHERE x.taxonomy = %s AND t.slug = %s LIMIT 1", $ktax, $value ) ); $taxonomy = $ktax; } } if(!$found){ $pass_on_qvars[$link_idx][] = $query; continue; } } $link_id = (int)$value; if (!$link_id) { continue; } $trid = $sitepress->get_element_trid($link_id, $kind); if(!$trid){ continue; } if($trid !== NULL){ $translations = $sitepress->get_element_translations($trid, $kind); } if(isset($translations[$target_lang_code]) && $translations[$target_lang_code]->element_id != null){ // use the new translated id in the link path. $translated_id = $translations[$target_lang_code]->element_id; if($is_tax){ //if it's a tax, get the translated link based on the term slug (to avoid the need to convert from term_taxonomy_id to term_id) $translated_id = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id WHERE x.term_taxonomy_id = %d LIMIT 1", $translated_id ) ); } // if absolute links is not on turn into WP permalinks if(empty($GLOBALS['WPML_Sticky_Links'])){ //////// if(preg_match('#^post_#', $kind)){ $replace = get_permalink($translated_id); }elseif(preg_match('#^tax_#', $kind)){ remove_filter('icl_current_language', 'wpv_wpml_icl_current_language'); if(is_numeric($translated_id)) $translated_id = intval($translated_id); $replace = get_term_link($translated_id, $taxonomy); add_filter('icl_current_language', 'wpv_wpml_icl_current_language'); } $new_link = str_replace($link[2], $replace, $link[0]); $replace_link_arr[$link_idx] = array('from'=> $link[2], 'to'=>$replace); }else{ $replace = $key . '=' . $translated_id; $new_link = str_replace($query, $replace, $link[0]); $replace_link_arr[$link_idx] = array('from'=> $query, 'to'=>$replace); } // replace the link in the body. // $new_body = str_replace($link[0], $new_link, $new_body); $all_links_arr[$link_idx] = array('from'=> $link[0], 'to'=>$new_link); // done in the next loop } else { // translation not found for this. $all_links_fixed = 0; } } } } if(!empty($replace_link_arr)) foreach($replace_link_arr as $link_idx => $rep){ $rep_to = $rep['to']; $fragment = ''; // if sticky links is not ON, fix query parameters and fragments if(empty($GLOBALS['WPML_Sticky_Links'])){ if(!empty($pass_on_fragments[$link_idx])){ $fragment = '#' . $pass_on_fragments[$link_idx]; } if(!empty($pass_on_qvars[$link_idx])){ $url_glue = (strpos($rep['to'], '?') === false) ? '?' : '&'; $rep_to = $rep['to'] . $url_glue . join('&', $pass_on_qvars[$link_idx]); } } $all_links_arr[$link_idx]['to'] = str_replace($rep['to'], $rep_to . $fragment, $all_links_arr[$link_idx]['to']); } if(!empty($all_links_arr)) foreach($all_links_arr as $link){ $new_body = str_replace($link['from'], $link['to'], $new_body); } $body = $new_body; $content_cache[$cache_code] = $body; remove_filter('icl_current_language', 'wpv_wpml_icl_current_language'); add_filter('option_rewrite_rules', array($sitepress, 'rewrite_rules_filter')); } } return $body; }