/** * Returns the HTML for the display of all terms with a language suffix in the troubleshooting menu. * @return string */ public static function display_terms_with_suffix() { $terms_to_display = WPML_Terms_Translations::get_all_terms_with_language_suffix(); $output = ''; if (!empty($terms_to_display)) { $output = '<div class="icl_cyan_box">'; $output .= '<table class="widefat" id="icl-updated-term-names-table">'; $output .= '<a name="termsuffixupdate"></a>'; $output .= '<tr><h3>' . __("Remove language suffixes from taxonomy names.", 'sitepress') . '</h3></tr>'; $output .= '<tr id="icl-updated-term-names-headings"><th></th><th>' . __("Old Name", "sitepress") . '</th><th>' . __("Updated Name", "sitepress") . '</th><th>' . __("Affected Taxonomies", "sitepress") . '</th></tr>'; foreach ($terms_to_display as $term_id => $term) { $updated_term_name = self::strip_language_suffix($term['name']); $output .= '<tr class="icl-term-with-suffix-row"><td>'; $output .= '<input type="checkbox" checked="checked" name="' . $updated_term_name . '" value="' . $term_id . '"/>'; $output .= '</td>'; $output .= '<td>' . $term['name'] . '</td>'; $output .= '<td id="term_' . $term_id . '">' . $updated_term_name . '</td>'; $output .= '<td>' . join(', ', $term['taxonomies']) . '</td>'; $output .= '</tr>'; } $output .= '</table>'; $output .= '</br></br>'; $output .= '<button id="icl-update-term-names" class="button-primary">' . __('Update term names', 'sitepress') . '</button>'; $output .= '<button id="icl-update-term-names-done" class="button-primary" disabled="disabled" style="display:none;">' . __('All term names updated', 'sitepress') . '</button>'; $output .= '</div>'; } return $output; }
/** * Uses the API provided in \WPML_Terms_Translations to create missing term translations. * These arise when a term, previously having been untranslated, is set to be translated * and assigned to posts in more than one language. * * @param $trid int The trid value for which term translations are missing. * @param $source_lang string The source language of this trid. * @param $langs array The languages' codes for which term translations are missing. */ private function prepare_missing_translations($trid, $source_lang, $langs) { $existing_translations = $this->sitepress->term_translations()->get_element_translations(false, $trid); foreach ($langs as $lang) { if (!isset($existing_translations[$lang])) { $this->term_utils->create_automatic_translation(array('lang_code' => $lang, 'source_language' => $source_lang, 'trid' => $trid, 'taxonomy' => $this->taxonomy)); } } }
/** * @param object[] $terms * @param string $lang * @param string $taxonomy * @param bool $duplicate sets whether missing terms should be created by duplicating the original term * * @return array */ private function get_translated_term_ids($terms, $lang, $taxonomy, $duplicate) { /** @var WPML_Term_Translation $wpml_term_translations */ global $wpml_term_translations; $term_utils = new WPML_Terms_Translations(); $wpml_term_translations->reload(); $translated_terms = array(); foreach ($terms as $orig_term) { $translated_id = (int) $wpml_term_translations->term_id_in($orig_term->term_id, $lang); if (!$translated_id && $duplicate) { $translation = $term_utils->create_automatic_translation(array('lang_code' => $lang, 'taxonomy' => $taxonomy, 'trid' => $wpml_term_translations->get_element_trid($orig_term->term_taxonomy_id), 'source_language' => $wpml_term_translations->get_element_lang_code($orig_term->term_taxonomy_id))); $translated_id = isset($translation['term_id']) ? $translation['term_id'] : false; } if ($translated_id) { $translated_terms[] = $translated_id; } } return $translated_terms; }
/** * @param String $slug * @param String $taxonomy * Filters slug input, so to ensure uniqueness of term slugs. * * @return String Either the original slug or a new slug that has been generated from the original one in order to * ensure slug uniqueness. */ public function pre_term_slug_filter($slug, $taxonomy) { if ((isset($_REQUEST['tag-name']) || isset($_REQUEST['name'])) && (isset($_REQUEST['action']) && $_REQUEST['action'] === 'add-tag')) { $lang = $this->lang_term_slug_save($taxonomy); if ($slug === '') { if (isset($_REQUEST['tag-name'])) { $slug = sanitize_title($_REQUEST['tag-name']); } elseif (isset($_REQUEST['name'])) { $slug = sanitize_title($_REQUEST['name']); } } $slug = $slug !== '' ? WPML_Terms_Translations::term_unique_slug($slug, $taxonomy, $lang) : $slug; } return $slug; }
/** * Manages the creation of new taxonomy terms from metadata values * * @since 2.20 * * @param mixed string or array value returned by the rule * @param string field name or taxonomy name * @param integer post ID to be evaluated * @param string category/scope to evaluate against: iptc_exif_standard_mapping, iptc_exif_taxonomy_mapping or iptc_exif_custom_mapping * @param array attachment_metadata, default NULL * * @return array updated rule EXIF/Template value */ public static function mla_mapping_new_text($new_text, $setting_key, $post_id, $category, $attachment_metadata) { global $sitepress; static $replicate = NULL, $current_language, $taxonomies, $other_languages; if ('iptc_exif_taxonomy_mapping' !== $category) { return $new_text; } if (is_null($replicate)) { $replicate = 'checked' == MLACore::mla_get_option('term_mapping_replication', false, false, MLA_WPML::$mla_language_option_definitions); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $replicate = ' . var_export( $replicate, true ), 0 ); //$term_utils = new WPML_Terms_Translations(); $current_language = $sitepress->get_current_language(); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $current_language = ' . var_export( $current_language, true ), 0 ); $taxonomies = $sitepress->get_translatable_taxonomies(true, 'attachment'); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $taxonomies = ' . var_export( $taxonomies, true ), 0 ); $other_languages = $sitepress->get_active_languages(); unset($other_languages[$current_language]); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $other_languages = ' . var_export( $other_languages, true ), 0 ); } if (!empty($new_text) && in_array($setting_key, $taxonomies)) { //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text mapping rule = ' . var_export( self::$current_mapping_rule, true ), 0 ); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $new_text = ' . var_export( $new_text, true ), 0 ); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $setting_key = ' . var_export( $setting_key, true ), 0 ); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $post_id = ' . var_export( $post_id, true ), 0 ); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $category = ' . var_export( $category, true ), 0 ); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 ); $language_details = $sitepress->get_element_language_details($post_id, 'post_attachment'); $item_language = $language_details->language_code; //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $language_details = ' . var_export( $language_details, true ), 0 ); /* * Find the parent term and its translations */ if (isset(self::$current_mapping_rule['parent'])) { if ($parent_term = absint(self::$current_mapping_rule['parent'])) { $parent_term = self::_get_relevant_term('id', $parent_term, $setting_key); } } else { $parent_term = 0; } //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $parent_term = ' . var_export( $parent_term, true ), 0 ); $new_terms = array(); foreach ($new_text as $new_name) { $relevant_term = self::_get_relevant_term('name', $new_name, $setting_key); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $relevant_term = ' . var_export( $relevant_term, true ), 0 ); if ($relevant_term) { if (isset($relevant_term['translations'][$item_language])) { $new_terms[] = absint($relevant_term['translations'][$item_language]->term_id); } } else { /* * Always create the new term in the current language */ if ($parent_term && isset($parent_term['translations'][$current_language])) { $parent = $parent_term['translations'][$current_language]->term_id; } else { $parent = 0; } //error_log( __LINE__ . ' MLA_Polylang::mla_mapping_new_text $parent = ' . var_export( $parent, true ), 0 ); $args = array('taxonomy' => $setting_key, 'lang_code' => $current_language, 'term' => $new_name, 'parent' => $parent); $res = WPML_Terms_Translations::create_new_term($args); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $res = ' . var_export( $res, true ), 0 ); /* * Add translations in the other languages? */ if ($replicate) { $trid = $sitepress->get_element_trid($res['term_taxonomy_id'], 'tax_' . $setting_key); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $trid = ' . var_export( $trid, true ), 0 ); $original_term = get_term($res['term_id'], $setting_key, OBJECT, 'no'); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $original_term = ' . var_export( $original_term, true ), 0 ); $args = array('trid' => $trid, 'source_language' => $current_language, 'term' => $new_name, 'original_id' => $res['term_id'], 'original_tax_id' => $res['term_taxonomy_id'], 'taxonomy' => $setting_key, 'update_translations' => true); foreach ($other_languages as $language => $language_details) { if ($parent_term && isset($parent_term['translations'][$language])) { $parent = $parent_term['translations'][$language]->term_id; } else { $parent = 0; } //error_log( __LINE__ . ' MLA_Polylang::mla_mapping_new_text $parent = ' . var_export( $parent, true ), 0 ); $translated_slug = apply_filters('icl_duplicate_generic_string', $original_term->slug, $language, array('context' => 'taxonomy_slug', 'attribute' => $setting_key, 'key' => $original_term->term_id)); $translated_slug = WPML_Terms_Translations::term_unique_slug($translated_slug, $setting_key, $language); //error_log( __LINE__ . ' MLA_Polylang::mla_mapping_new_text $translated_slug = ' . var_export( $translated_slug, true ), 0 ); $args['slug'] = $translated_slug; $args['parent'] = $parent; $args['lang_code'] = $language; //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $args = ' . var_export( $args, true ), 0 ); $res = WPML_Terms_Translations::create_new_term($args); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $res = ' . var_export( $res, true ), 0 ); } } // replicate /* * Reload the term with all of its new translations */ $relevant_term = self::_get_relevant_term('name', $new_name, $setting_key, NULL, false, true); //error_log( __LINE__ . ' MLA_WPML::mla_mapping_new_text $relevant_term = ' . var_export( $relevant_term, true ), 0 ); if (isset($relevant_term['translations'][$item_language])) { $new_terms[] = absint($relevant_term['translations'][$item_language]->term_id); } } // new term } // foreach new_name MLACore::mla_debug_add(__LINE__ . " MLA_WPML::mla_mapping_new_text( {$setting_key}, {$post_id} ) \$new_terms = " . var_export($new_terms, true), MLACore::MLA_DEBUG_CATEGORY_AJAX); return $new_terms; } // translated taxonomy return $new_text; }
/** * Uses the API provided in \WPML_Terms_Translations to create missing term translations. * These arise when a term, previously having been untranslated, is set to be translated * and assigned to posts in more than one language. * * @param $trid int The trid value for which term translations are missing. * @param $source_lang string The source language of this trid. * @param $langs array The languages' codes for which term translations are missing. */ private function prepare_missing_translations($trid, $source_lang, $langs) { foreach ($langs as $lang) { WPML_Terms_Translations::create_automatic_translation(array('lang_code' => $lang, 'source_language' => $source_lang, 'trid' => $trid, 'taxonomy' => $this->taxonomy)); } }
public function __construct($args) { /** * Actual name of the term. Same as the name input argument to \wp_update_term or \wp_insert_term * @var string|bool */ $term = false; $slug = ''; $taxonomy = ''; /** @var string $lang_code */ $lang_code = ''; $trid = null; /** @var int|bool $original_tax_id */ $original_tax_id = false; /** * Taxonomy_term_id of the parent element * @var int */ $parent = 0; $description = false; $term_group = false; $source_language = null; extract($args, EXTR_OVERWRITE); // We cannot create a term unless we at least know its name if ($term && $taxonomy) { $this->wp_new_term_args['name'] = $term; $this->taxonomy = $taxonomy; } else { $this->is_valid = false; return; } if ($parent) { $this->wp_new_term_args['parent'] = $parent; } if ($description) { $this->wp_new_term_args['description'] = $description; } if ($term_group) { $this->wp_new_term_args['term_group'] = $term_group; } $this->wp_new_term_args['term_group'] = $term_group; $this->is_valid = $this->set_language_information($trid, $original_tax_id, $lang_code, $source_language); $this->set_action_type(); if (!$this->is_update || $this->is_update && $slug != '' && $slug != $this->old_slug) { if (trim($slug) == '') { $slug = sanitize_title($term); } $slug = WPML_Terms_Translations::term_unique_slug($slug, $taxonomy, $lang_code); $this->wp_new_term_args['slug'] = $slug; } }
/** * Ajax handler for switching the language of a post. */ public static function wpml_switch_post_language() { global $sitepress, $wpdb; $to = false; $post_id = false; if (isset($_POST['wpml_to'])) { $to = $_POST['wpml_to']; } if (isset($_POST['wpml_post_id'])) { $post_id = $_POST['wpml_post_id']; } $result = false; set_transient(md5($sitepress->get_current_user()->ID . 'current_user_post_edit_lang'), $to); if ($post_id && $to) { $post_type = get_post_type($post_id); $wpml_post_type = 'post_' . $post_type; $trid = $sitepress->get_element_trid($post_id, $wpml_post_type); /* Check if a translation in that language already exists with a different post id. * If so, then don't perform this action. */ $query_for_existing_translation = $wpdb->prepare("\tSELECT translation_id, element_id\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE element_type = %s\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND trid = %d\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND language_code = %s", $wpml_post_type, $trid, $to); $existing_translation = $wpdb->get_row($query_for_existing_translation); if ($existing_translation && $existing_translation->element_id != $post_id) { $result = false; } else { $sitepress->set_element_language_details($post_id, $wpml_post_type, $trid, $to); // Synchronize the posts terms languages. Do not create automatic translations though. WPML_Terms_Translations::sync_post_terms_language($post_id); require_once ICL_PLUGIN_PATH . '/inc/cache.php'; icl_cache_clear($post_type . 's_per_language', true); $result = $to; } } wp_send_json_success($result); }
function save_translation($data) { global $wpdb, $sitepress, $sitepress_settings, $ICL_Pro_Translation; $new_post_id = false; $is_incomplete = false; foreach ($data['fields'] as $field) { $this->_save_translation_field($field['tid'], $field); if (!isset($field['finished']) || !$field['finished']) { $is_incomplete = true; } } //check if translation job still exists $job_count = $wpdb->get_var($wpdb->prepare("SELECT count(1) FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id'])); if ($job_count == 0) { wp_redirect(admin_url(sprintf('admin.php?page=%s', WPML_TM_FOLDER . '/menu/translations-queue.php', 'job-cancelled'))); exit; } if (!empty($data['complete']) && !$is_incomplete) { $wpdb->update($wpdb->prefix . 'icl_translate_job', array('translated' => 1), array('job_id' => $data['job_id'])); $rid = $wpdb->get_var($wpdb->prepare("SELECT rid FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id'])); $translation_id = $wpdb->get_var($wpdb->prepare("SELECT translation_id FROM {$wpdb->prefix}icl_translation_status WHERE rid=%d", $rid)); $wpdb->update($wpdb->prefix . 'icl_translation_status', array('status' => ICL_TM_COMPLETE, 'needs_update' => 0), array('rid' => $rid)); list($element_id, $trid) = $wpdb->get_row($wpdb->prepare("SELECT element_id, trid FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id), ARRAY_N); $job = $this->get_translation_job($data['job_id'], true); $parts = explode('_', $job->original_doc_id); if ($parts[0] == 'external') { // Translations are saved in the string table for 'external' types $id = array_pop($parts); unset($parts[0]); $type = implode('_', $parts); $type = apply_filters('WPML_get_package_type', $type, $job->original_doc_id); foreach ($job->elements as $field) { if ($field->field_translate) { if (function_exists('icl_st_is_registered_string')) { $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type); if (!$string_id) { icl_register_string($type, $id . '_' . $field->field_type, self::decode_field_data($field->field_data, $field->field_format)); $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type); } if ($string_id) { icl_add_string_translation($string_id, $job->language_code, self::decode_field_data($field->field_data_translated, $field->field_format), ICL_STRING_TRANSLATION_COMPLETE); } } } } } else { if (!is_null($element_id)) { $postarr['ID'] = $_POST['post_ID'] = $element_id; } foreach ($job->elements as $field) { switch ($field->field_type) { case 'title': $postarr['post_title'] = self::decode_field_data($field->field_data_translated, $field->field_format); break; case 'body': $postarr['post_content'] = self::decode_field_data($field->field_data_translated, $field->field_format); break; case 'excerpt': $postarr['post_excerpt'] = self::decode_field_data($field->field_data_translated, $field->field_format); break; case 'URL': $postarr['post_name'] = self::decode_field_data($field->field_data_translated, $field->field_format); break; default: break; } } $original_post = get_post($job->original_doc_id); $postarr['post_author'] = $original_post->post_author; $postarr['post_type'] = $original_post->post_type; if ($sitepress_settings['sync_comment_status']) { $postarr['comment_status'] = $original_post->comment_status; } if ($sitepress_settings['sync_ping_status']) { $postarr['ping_status'] = $original_post->ping_status; } if ($sitepress_settings['sync_page_ordering']) { $postarr['menu_order'] = $original_post->menu_order; } if ($sitepress_settings['sync_private_flag'] && $original_post->post_status == 'private') { $postarr['post_status'] = 'private'; } if ($sitepress_settings['sync_post_date']) { $postarr['post_date'] = $original_post->post_date; } //set as draft or the same status as original post $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post->post_status; if ($original_post->post_parent) { $post_parent_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type='post_{$original_post->post_type}' AND element_id='{$original_post->post_parent}'"); if ($post_parent_trid) { $parent_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\tWHERE element_type='post_{$original_post->post_type}' AND trid='{$post_parent_trid}' AND language_code='{$job->language_code}'"); } } if (isset($parent_id) && $sitepress_settings['sync_page_parent']) { $_POST['post_parent'] = $postarr['post_parent'] = $parent_id; $_POST['parent_id'] = $postarr['parent_id'] = $parent_id; } $_POST['trid'] = $trid; $_POST['lang'] = $job->language_code; $_POST['skip_sitepress_actions'] = true; $postarr = apply_filters('icl_pre_save_pro_translation', $postarr); if (isset($element_id)) { // it's an update so dont change the url $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $element_id)); } if (isset($element_id)) { // it's an update so dont change post date $existing_post = get_post($element_id); $postarr['post_date'] = $existing_post->post_date; $postarr['post_date_gmt'] = $existing_post->post_date_gmt; } $new_post_id = $this->icl_insert_post($postarr, $job->language_code); icl_cache_clear($postarr['post_type'] . 's_per_language'); // clear post counter per language in cache do_action('icl_pro_translation_saved', $new_post_id, $data['fields']); // Allow identical slugs $post_name = sanitize_title($postarr['post_title']); // for Translated documents options:Page URL = Translate if (isset($data['fields']['URL']['data']) && $data['fields']['URL']['data']) { $post_name = $data['fields']['URL']['data']; } $post_name_rewritten = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $new_post_id)); $post_name_base = $post_name; if ($post_name != $post_name_rewritten || $postarr['post_type'] == 'post' || $postarr['post_type'] == 'page') { $incr = 1; do { $exists = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\t\tSELECT p.ID FROM {$wpdb->posts} p\n\t\t\t\t\t\t\t\tJOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID\n\t\t\t\t\t\t\tWHERE p.ID <> %d AND t.language_code = %s AND p.post_name=%s\n\t\t\t\t\t\t", $new_post_id, $job->language_code, $post_name)); if ($exists) { $incr++; } else { break; } $post_name = $post_name_base . '-' . $incr; } while ($exists); $wpdb->update($wpdb->posts, array('post_name' => $post_name), array('ID' => $new_post_id)); } $ICL_Pro_Translation->_content_fix_links_to_translated_content($new_post_id, $job->language_code); // update body translation with the links fixed $new_post_content = $wpdb->get_var($wpdb->prepare("SELECT post_content FROM {$wpdb->posts} WHERE ID=%d", $new_post_id)); foreach ($job->elements as $jel) { if ($jel->field_type == 'body') { $fields_data_translated = $this->encode_field_data($new_post_content, $jel->field_format); break; } } $wpdb->update($wpdb->prefix . 'icl_translate', array('field_data_translated' => $fields_data_translated), array('job_id' => $data['job_id'], 'field_type' => 'body')); // set stickiness //is the original post a sticky post? remove_filter('option_sticky_posts', array($sitepress, 'option_sticky_posts')); // remove filter used to get language relevant stickies. get them all $sticky_posts = get_option('sticky_posts'); $is_original_sticky = $original_post->post_type == 'post' && in_array($original_post->ID, $sticky_posts); if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) { stick_post($new_post_id); } else { if ($original_post->post_type == 'post' && !is_null($element_id)) { unstick_post($new_post_id); //just in case - if this is an update and the original post stckiness has changed since the post was sent to translation } } //sync plugins texts foreach ((array) $this->settings['custom_fields_translation'] as $cf => $op) { if ($op == 1) { update_post_meta($new_post_id, $cf, get_post_meta($original_post->ID, $cf, true)); } } // set specific custom fields $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome'); foreach ($copied_custom_fields as $ccf) { $val = get_post_meta($original_post->ID, $ccf, true); update_post_meta($new_post_id, $ccf, $val); } // sync _wp_page_template if ($sitepress_settings['sync_page_template']) { $_wp_page_template = get_post_meta($original_post->ID, '_wp_page_template', true); if (!empty($_wp_page_template)) { update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template); } } // sync post format if ($sitepress_settings['sync_post_format']) { $_wp_post_format = get_post_format($original_post->ID); set_post_format($new_post_id, $_wp_post_format); } // set the translated custom fields if we have any. foreach ((array) $this->settings['custom_fields_translation'] as $field_name => $val) { if ($val == 2) { // should be translated // find it in the translation foreach ($job->elements as $name => $eldata) { if ($eldata->field_data == $field_name) { if (preg_match("/field-(.*?)-name/", $eldata->field_type, $match)) { $field_id = $match[1]; foreach ($job->elements as $k => $v) { if ($v->field_type == 'field-' . $field_id) { $field_translation = self::decode_field_data($v->field_data_translated, $v->field_format); } if ($v->field_type == 'field-' . $field_id . '-type') { $field_type = $v->field_data; } } if (isset($field_type) && $field_type == 'custom_field') { $field_translation = str_replace('�A;', "\n", $field_translation); // always decode html entities eg decode & to & $field_translation = html_entity_decode($field_translation); update_post_meta($new_post_id, $field_name, $field_translation); } } } } } } $link = get_edit_post_link($new_post_id); if ($link == '') { // the current user can't edit so just include permalink $link = get_permalink($new_post_id); } if (is_null($element_id)) { $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id)); $user_message = __('Translation added: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.'; } else { $user_message = __('Translation updated: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.'; } // synchronize the page parent for translations if ($trid && $sitepress_settings['sync_page_parent']) { $translations = $sitepress->get_element_translations($trid, 'post_' . $postarr['post_type']); foreach ($translations as $target_lang => $target_details) { if ($target_lang != $job->language_code) { if ($target_details->element_id) { $sitepress->fix_translated_parent($new_post_id, $target_details->element_id, $target_lang); } } } } } if (isset($user_message)) { $this->messages[] = array('type' => 'updated', 'text' => $user_message); } if ($this->settings['notification']['completed'] != ICL_TM_NOTIFICATION_NONE) { require_once ICL_PLUGIN_PATH . '/inc/translation-management/tm-notification.class.php'; if ($data['job_id']) { $tn_notification = new TM_Notification(); $tn_notification->work_complete($data['job_id'], !is_null($element_id)); } } self::set_page_url($new_post_id); // redirect to jobs list wp_redirect(admin_url(sprintf('admin.php?page=%s&%s=%d', WPML_TM_FOLDER . '/menu/translations-queue.php', is_null($element_id) ? 'added' : 'updated', is_null($element_id) ? $new_post_id : $element_id))); } else { $this->messages[] = array('type' => 'updated', 'text' => __('Translation (incomplete) saved.', 'sitepress')); } /* * After all previous functionality the terms form the job are assigned to the new post just created or updated. * $overwrite is true by default for now. */ $overwrite = true; WPML_Terms_Translations::save_all_terms_from_job($data['job_id'], $new_post_id, $overwrite); do_action('icl_pro_translation_completed', $new_post_id); }
/** * Ajax wrapper for retrieving an array containing all taxonomies that are translated by WPML and a flag indicating whether they are hierarchical. */ public static function wpml_get_translated_taxonomies() { global $sitepress; $post_id = false; $lang = false; if (isset($_POST['wpml_post_id'])) { $post_id = $_POST['wpml_post_id']; } if (isset($_POST['wpml_to'])) { $lang = $_POST['wpml_to']; } $translated_taxonomies = array(); $taxonomy_search_args = array(); if ($post_id) { $post_object = get_post($post_id); $taxonomy_search_args[] = array($post_object->post_type); } $taxonomies = get_taxonomies(array(), 'objects'); foreach ($taxonomies as $key => $taxobject) { $tax = $taxobject->name; $hierarchical = false; if ($sitepress->is_translated_taxonomy($tax)) { if (is_taxonomy_hierarchical($tax)) { $hierarchical = true; } $args = array('post_id' => $post_id, 'lang' => $lang); $terms_in_tax = WPML_Terms_Translations::get_taxonomy_terms_by($tax, $args); if (!$terms_in_tax) { $terms_in_tax = array(); } $translated_taxonomies[] = array('label' => $taxobject->label, 'name' => $tax, 'hierarchical' => $hierarchical, 'terms' => $terms_in_tax); } } wp_send_json_success($translated_taxonomies); }
/** * @param WP_Query $query * * @return String[] */ private function get_tax_query_posttype($query) { global $sitepress; $tax = $query->get('taxonomy'); $post_type = WPML_Terms_Translations::get_taxonomy_post_types($tax); foreach ($post_type as $k => $v) { if (!$sitepress->is_translated_post_type($v)) { unset($post_type[$k]); } } return $post_type; }
function trbl_duplicate_terms() { $nonce = filter_input(INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if (!$nonce || !wp_verify_nonce($nonce, 'trbl_duplicate_terms')) { die('Invalid nonce'); } global $sitepress; $attr = isset($_POST['attr']) ? $_POST['attr'] : false; $terms = get_terms($attr, 'hide_empty=0'); $i = 0; $languages = $sitepress->get_active_languages(); foreach ($terms as $term) { foreach ($languages as $language) { $tr_id = apply_filters('translate_object_id', $term->term_id, $attr, false, $language['code']); if (is_null($tr_id)) { $term_args = array(); // hierarchy - parents if (is_taxonomy_hierarchical($attr)) { // fix hierarchy if ($term->parent) { $original_parent_translated = apply_filters('translate_object_id', $term->parent, $attr, false, $language['code']); if ($original_parent_translated) { $term_args['parent'] = $original_parent_translated; } } } if (version_compare(ICL_SITEPRESS_VERSION, '3.1.8.2', '<=')) { $term_name = $term->name . ' @' . $language['code']; } else { $term_name = $term->name; $slug = $term->name . '-' . $language['code']; $slug = WPML_Terms_Translations::term_unique_slug($slug, $attr, $language['code']); $term_args['slug'] = $slug; } $new_term = wp_insert_term($term_name, $attr, $term_args); if ($new_term && !is_wp_error($new_term)) { $tt_id = $sitepress->get_element_trid($term->term_taxonomy_id, 'tax_' . $attr); $sitepress->set_element_language_details($new_term['term_taxonomy_id'], 'tax_' . $attr, $tt_id, $language['code']); } } } } echo 1; die; }
public static function save_term_translation() { global $sitepress, $wpdb; $original_element = $_POST['translation_of']; $taxonomy = $_POST['taxonomy']; $language = $_POST['language']; $trid = $sitepress->get_element_trid($original_element, 'tax_' . $taxonomy); $translations = $sitepress->get_element_translations($trid, 'tax_' . $taxonomy); $_POST['icl_tax_' . $taxonomy . '_language'] = $language; $_POST['icl_trid'] = $trid; $_POST['icl_translation_of'] = $original_element; $errors = ''; $term_args = array('name' => $_POST['name'], 'slug' => WPML_Terms_Translations::pre_term_slug_filter($_POST['slug'], $taxonomy), 'description' => $_POST['description']); $original_tax_sql = "SELECT * FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s AND term_taxonomy_id = %d"; $original_tax_prepared = $wpdb->prepare($original_tax_sql, array($taxonomy, $original_element)); $original_tax = $wpdb->get_row($original_tax_prepared); // hierarchy - parents if (is_taxonomy_hierarchical($taxonomy)) { // fix hierarchy if ($original_tax->parent) { $original_parent_translated = icl_object_id($original_tax->parent, $taxonomy, false, $_POST['language']); if ($original_parent_translated) { $term_args['parent'] = $original_parent_translated; } } } if (isset($translations[$language])) { $result = wp_update_term($translations[$language]->term_id, $taxonomy, $term_args); } else { $result = wp_insert_term($_POST['name'], $taxonomy, $term_args); $original_element_lang_details = $sitepress->get_element_language_details($original_element, 'tax_' . $taxonomy); if (isset($original_element_lang_details->language_code)) { $sitepress->set_element_language_details($result['term_taxonomy_id'], 'tax_' . $taxonomy, $trid, $language, $original_element_lang_details->language_code); } } if (is_wp_error($result)) { foreach ($result->errors as $ers) { $errors .= join('<br />', $ers); } $errors .= '<br />'; } else { // hierarchy - children if (is_taxonomy_hierarchical($taxonomy)) { // get children of original $children_sql = "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s AND parent=%d"; $children_prepared = $wpdb->prepare($children_sql, array($taxonomy, $original_tax->term_id)); $children = $wpdb->get_col($children_prepared); if ($children) { foreach ($children as $child) { $child_translated = icl_object_id($child, $taxonomy, false, $_POST['language']); if ($child_translated) { $wpdb->update($wpdb->term_taxonomy, array('parent' => $result['term_id']), array('taxonomy' => $taxonomy, 'term_id' => $child_translated)); } } } $sitepress->update_terms_relationship_cache($children, $taxonomy); //delete_option($_POST['taxonomy'] . '_children'); } $term = get_term($result['term_id'], $taxonomy); do_action('icl_save_term_translation', $original_tax, $result); } $html = ''; echo json_encode(array('html' => $html, 'slug' => isset($term) ? urldecode($term->slug) : '', 'errors' => $errors)); exit; }