(.*?)<\/a>/i",*/
"/]*href\s*=\s*([\"\']??)([^\"^>]+)[\"\']??([^>]*)>/i",
);
$links = array();
foreach($regexp_links as $regexp) {
if (preg_match_all($regexp, $body, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$links[] = $match;
}
}
}
return $links;
}
public static function _content_make_links_sticky( $element_id, $element_type = 'post' ) {
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 );
}
}
public function fix_links_to_translated_content( $element_id, $target_lang_code, $element_type = 'post' ){
global $wpdb, $sitepress, $wp_taxonomies;
self::_content_make_links_sticky( $element_id, $element_type );
$current_language = $sitepress->get_current_language();
$sitepress->switch_lang( $target_lang_code );
$wpml_element_type = $element_type;
$body = false;
if(strpos($element_type, 'post') === 0){
$post_prepared = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID=%d", array($element_id));
$post = $wpdb->get_row($post_prepared);
$body = $post->post_content;
$wpml_element_type = 'post_' . $post->post_type;
}elseif($element_type=='string'){
$string_prepared = $wpdb->prepare( "SELECT string_id, value FROM {$wpdb->prefix}icl_string_translations WHERE id=%d", array( $element_id ) );
$data = $wpdb->get_row( $string_prepared );
$body = $data->value;
$original_string_id = $data->string_id;
$string_type = $wpdb->get_var( $wpdb->prepare( "SELECT type FROM {$wpdb->prefix}icl_strings WHERE id=%d", $original_string_id ) );
if ( 'LINK' === $string_type ) {
$body = 'removeit';
}
}
$new_body = $body;
$base_url_parts = parse_url(site_url());
$links = $this->_content_get_link_paths($body);
$all_links_fixed = 1;
$pass_on_query_vars = array();
$pass_on_fragments = array();
$all_links_arr = 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;
$kind = false;
$taxonomy = false;
if($key == 'p'){
$kind = 'post_' . $wpdb->get_var( $wpdb->prepare("SELECT post_type
FROM {$wpdb->posts}
WHERE ID = %d ",
$value));
} else if($key == "page_id"){
$kind = 'post_page';
} else if($key == 'cat' || $key == 'cat_ID'){
$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", $taxonomy, $value ) );
} else {
$found = false;
foreach($wp_taxonomies as $taxonomy_name => $taxonomy_object){
if($taxonomy_object->query_var && $key == $taxonomy_object->query_var){
$found = true;
$is_tax = true;
$kind = 'tax_' . $taxonomy_name;
$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",
$taxonomy_name, $value ));
$taxonomy = $taxonomy_name;
}
}
if(!$found){
$pass_on_query_vars[$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, false, false, true );
}
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){
$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",
$translated_id));
}
// if absolute links is not on turn into WP permalinks
if ( $this->should_links_be_converted_back_to_permalinks( $element_type ) ) {
////////
$replace = false;
if(preg_match('#^post_#', $kind)){
$replace = get_permalink($translated_id);
}elseif(preg_match('#^tax_#', $kind)){
if ( is_numeric( $translated_id ) ) {
$translated_id = (int) $translated_id;
}
$replace = get_term_link($translated_id, $taxonomy);
}
$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 = $link[0];
if($replace) {
$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 ( $this->should_links_be_converted_back_to_permalinks( $element_type ) ) {
if ( ! empty( $pass_on_fragments[ $link_idx ] ) ) {
$fragment = '#' . $pass_on_fragments[ $link_idx ];
}
if ( ! empty( $pass_on_query_vars[ $link_idx ] ) ) {
foreach ( $pass_on_query_vars[ $link_idx ] as $query_fragment ) {
$query_pair = array();
wp_parse_str( $query_fragment, $query_pair );
$rep_to = add_query_arg( $query_pair, $rep_to );
}
}
}
$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);
}
if ( $new_body != $body ){
if ( strpos( $element_type, 'post' ) === 0 ) {
$wpdb->update( $wpdb->posts, array( 'post_content' => $new_body ), array( 'ID' => $element_id) );
} elseif ($element_type == 'string') {
if ( 'LINK' === $string_type ) {
$new_body = str_replace( array( 'removeit' ), array( '', '' ), $new_body );
$wpdb->update( $wpdb->prefix . 'icl_string_translations', array( 'value' => $new_body, 'status' => ICL_TM_COMPLETE ), array( 'id' => $element_id ) );
do_action( 'icl_st_add_string_translation', $element_id );
} else {
$wpdb->update( $wpdb->prefix . 'icl_string_translations', array( 'value' => $new_body ), array( 'id' => $element_id ) );
}
}
}
$links_fixed_status_factory = new WPML_Links_Fixed_Status_Factory( $wpdb, new WPML_WP_API() );
$links_fixed_status = $links_fixed_status_factory->create( $element_id, $wpml_element_type );
$links_fixed_status->set( $all_links_fixed );
$sitepress->switch_lang( $current_language );
return sizeof( $all_links_arr );
}
function translation_error_handler($error_number, $error_string, $error_file, $error_line){
switch($error_number){
case E_ERROR:
case E_USER_ERROR:
throw new Exception ($error_string . ' [code:e' . $error_number . '] in '. $error_file . ':' . $error_line);
case E_WARNING:
case E_USER_WARNING:
return true;
default:
return true;
}
}
function should_links_be_converted_back_to_permalinks( $element_type ) {
return 'string' === $element_type || empty( $GLOBALS['WPML_Sticky_Links'] );
}
function post_submitbox_start(){
global $post, $iclTranslationManagement;
if(empty($post)|| !$post->ID){
return;
}
$translations = $iclTranslationManagement->get_element_translations($post->ID, 'post_' . $post->post_type);
$show_box = 'display:none';
foreach($translations as $t){
if($t->element_id == $post->ID){
return;
}
if($t->status == ICL_TM_COMPLETE && !$t->needs_update){
$show_box = '';
break;
}
}
echo '';
echo '';
echo '
';
echo '
';
}
private function process_translated_string( $translation_proxy_job_id, $language ) {
$project = TranslationProxy::get_current_project();
$translation = $project->fetch_translation( $translation_proxy_job_id );
$translation = apply_filters( 'icl_data_from_pro_translation', $translation );
$ret = false;
$translation = $this->xliff_reader_factory->string_xliff_reader()->get_data( $translation );
if ( $translation ) {
$ret = icl_translation_add_string_translation( $translation_proxy_job_id, $translation, $language );
if ( $ret ) {
$project->update_job( $translation_proxy_job_id );
}
}
return $ret;
}
private function add_error( $project_error ) {
$this->errors[] = $project_error;
}
/**
* @param $project TranslationProxy_Project
*/
function enqueue_project_errors( $project ) {
if ( isset( $project ) && isset( $project->errors ) && $project->errors ) {
foreach ( $project->errors as $project_error ) {
$this->add_error( $project_error );
}
}
}
/**
* @param TranslationManagement $iclTranslationManagement
*/
private function maybe_init_translation_management( $iclTranslationManagement ) {
if ( empty( $this->tmg->settings ) ) {
$iclTranslationManagement->init();
}
}
}