/wp-content/plugins/sitepress-multilingual-cms/inc/translation-management/translation-management.class.php
PHP | 4216 lines | 3263 code | 643 blank | 310 comment | 770 complexity | d801fb1245a37a064a7a36afd3c95f6a MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- define ( 'ICL_TM_NOT_TRANSLATED', 0);
- define ( 'ICL_TM_WAITING_FOR_TRANSLATOR', 1);
- define ( 'ICL_TM_IN_PROGRESS', 2);
- define ( 'ICL_TM_NEEDS_UPDATE', 3); //virt. status code (based on needs_update)
- define ( 'ICL_TM_DUPLICATE', 9);
- define ( 'ICL_TM_COMPLETE', 10);
- define('ICL_TM_NOTIFICATION_NONE', 0);
- define('ICL_TM_NOTIFICATION_IMMEDIATELY', 1);
- define('ICL_TM_NOTIFICATION_DAILY', 2);
- define('ICL_TM_TMETHOD_MANUAL', 0);
- define('ICL_TM_TMETHOD_EDITOR', 1);
- define('ICL_TM_TMETHOD_PRO', 2);
- define('ICL_TM_DOCS_PER_PAGE', 20);
- $asian_languages = array('ja', 'ko', 'zh-hans', 'zh-hant', 'mn', 'ne', 'hi', 'pa', 'ta', 'th');
- if(!class_exists('WPML_Config')) {
- require ICL_PLUGIN_PATH . '/inc/wpml-config/wpml-config.class.php';
- }
- class TranslationManagement{
- private $selected_translator = array('ID'=>0);
- private $current_translator = array('ID'=>0);
- public $messages = array();
- public $dashboard_select = array();
- public $settings;
- public $admin_texts_to_translate = array();
- function __construct(){
- add_action('init', array($this, 'init'), 1500);
- if(isset($_GET['icl_tm_message'])){
- $this->messages[] = array(
- 'type' => isset($_GET['icl_tm_message_type']) ? $_GET['icl_tm_message_type'] : 'updated',
- 'text' => $_GET['icl_tm_message']
- );
- }
- add_action('save_post', array($this, 'save_post_actions'), 110, 2); // calling *after* the Sitepress actions
- add_action('delete_post', array($this, 'delete_post_actions'), 1, 1); // calling *before* the Sitepress actions
- //add_action('edit_term', array($this, 'edit_term'),11, 2); // calling *after* the Sitepress actions
- add_action('icl_ajx_custom_call', array($this, 'ajax_calls'), 10, 2);
- add_action('wp_ajax_show_post_content', array($this, '_show_post_content'));
- if(isset($_GET['sm']) && ($_GET['sm'] == 'dashboard' || $_GET['sm'] == 'jobs')){@session_start();}
- elseif(isset($_GET['page']) && preg_match('@/menu/translations-queue\.php$@', $_GET['page'])){@session_start();}
- add_filter('icl_additional_translators', array($this, 'icl_additional_translators'), 99, 3);
- add_filter('icl_translators_list', array(__CLASS__, 'icanlocalize_translators_list'));
- add_action('user_register', array($this, 'clear_cache'));
- add_action('profile_update', array($this, 'clear_cache'));
- add_action('delete_user', array($this, 'clear_cache'));
- add_action('added_existing_user', array($this, 'clear_cache'));
- add_action('remove_user_from_blog', array($this, 'clear_cache'));
- add_action('admin_print_scripts', array($this, '_inline_js_scripts'));
- add_action('wp_ajax_icl_tm_user_search', array($this, '_user_search'));
- add_action('wp_ajax_icl_tm_abort_translation', array($this, 'abort_translation'));
- }
- function save_settings()
- {
- global $sitepress, $sitepress_settings;
- $iclsettings[ 'translation-management' ] = $this->settings;
- $custom_posts_sync_option = isset( $sitepress_settings[ 'custom_posts_sync_option' ] ) ? $sitepress_settings[ 'custom_posts_sync_option' ] : false;
- if ( is_array( $custom_posts_sync_option ) ) {
- foreach ( $custom_posts_sync_option as $k => $v ) {
- $iclsettings[ 'custom_posts_sync_option' ][ $k ] = $v;
- }
- }
- $sitepress->save_settings( $iclsettings );
- }
- function init(){
- global $wpdb, $current_user, $sitepress_settings, $sitepress;
- $this->settings =& $sitepress_settings['translation-management'];
- //logic for syncing comments
- if($sitepress->get_option('sync_comments_on_duplicates')){
- add_action('delete_comment', array($this, 'duplication_delete_comment'));
- add_action('edit_comment', array($this, 'duplication_edit_comment'));
- add_action('wp_set_comment_status', array($this, 'duplication_status_comment'), 10, 2);
- add_action('wp_insert_comment', array($this, 'duplication_insert_comment'), 100);
- }
- $this->initial_custom_field_translate_states();
- // defaults
- if(!isset($this->settings['notification']['new-job'])) $this->settings['notification']['new-job'] = ICL_TM_NOTIFICATION_IMMEDIATELY;
- if(!isset($this->settings['notification']['completed'])) $this->settings['notification']['completed'] = ICL_TM_NOTIFICATION_IMMEDIATELY;
- if(!isset($this->settings['notification']['resigned'])) $this->settings['notification']['resigned'] = ICL_TM_NOTIFICATION_IMMEDIATELY;
- if(!isset($this->settings['notification']['dashboard'])) $this->settings['notification']['dashboard'] = true;
- if(!isset($this->settings['notification']['purge-old'])) $this->settings['notification']['purge-old'] = 7;
- if(!isset($this->settings['custom_fields_translation'])) $this->settings['custom_fields_translation'] = array();
- if(!isset($this->settings['doc_translation_method'])) $this->settings['doc_translation_method'] = ICL_TM_TMETHOD_MANUAL;
- get_currentuserinfo();
- $user = false;
- if(isset($current_user->ID)){
- $user = new WP_User($current_user->ID);
- }
- if(!$user || empty($user->data)) return;
- $ct['translator_id'] = $current_user->ID;
- $ct['display_name'] = isset($user->data->display_name) ? $user->data->display_name : $user->data->user_login;
- $ct['user_login'] = $user->data->user_login;
- $ct['language_pairs'] = get_user_meta($current_user->ID, $wpdb->prefix.'language_pairs', true);
- if(empty($ct['language_pairs'])) $ct['language_pairs'] = array();
- $this->current_translator = (object)$ct;
- WPML_Config::load_config();
- if(isset($_POST['icl_tm_action'])){
- $this->process_request($_POST['icl_tm_action'], $_POST);
- }elseif(isset($_GET['icl_tm_action'])){
- $this->process_request($_GET['icl_tm_action'], $_GET);
- }
- if($GLOBALS['pagenow']=='edit.php'){ // use standard WP admin notices
- add_action('admin_notices', array($this, 'show_messages'));
- }else{ // use custom WP admin notices
- add_action('icl_tm_messages', array($this, 'show_messages'));
- }
- if(isset($_GET['page']) && basename($_GET['page']) == 'translations-queue.php' && isset($_GET['job_id'])){
- add_filter('admin_head',array($this, '_show_tinyMCE'));
- }
- //if(!isset($this->settings['doc_translation_method'])){
- if(isset($this->settings['doc_translation_method']) && $this->settings['doc_translation_method'] < 0 ){
- if(isset($_GET['sm']) && $_GET['sm']=='mcsetup' && isset($_GET['src']) && $_GET['src']=='notice'){
- $this->settings['doc_translation_method'] = ICL_TM_TMETHOD_MANUAL;
- $this->save_settings();
- }else{
- add_action('admin_notices', array($this, '_translation_method_notice'));
- }
- }
- if(defined('WPML_TM_VERSION') && isset($_GET['page']) && $_GET['page'] == WPML_TM_FOLDER. '/menu/main.php' && isset($_GET['sm']) && $_GET['sm'] == 'translators'){
- $iclsettings =& $sitepress_settings;
- $sitepress->get_icl_translator_status($iclsettings);
- $sitepress->save_settings($iclsettings);
- }
- // default settings
- if(empty($this->settings['doc_translation_method']) || !defined('WPML_TM_VERSION')){
- $this->settings['doc_translation_method'] = ICL_TM_TMETHOD_MANUAL;
- }
- }
- function initial_custom_field_translate_states() {
- global $wpdb;
- $cf_keys_limit = 1000; // jic
- $custom_keys = $wpdb->get_col( "
- SELECT meta_key
- FROM $wpdb->postmeta
- GROUP BY meta_key
- ORDER BY meta_key
- LIMIT $cf_keys_limit" );
- $changed = false;
- foreach($custom_keys as $cfield) {
- if(empty($this->settings['custom_fields_translation'][$cfield]) || $this->settings['custom_fields_translation'][$cfield] == 0) {
- // see if a plugin handles this field
- $override = apply_filters('icl_cf_translate_state', 'nothing', $cfield);
- switch($override) {
- case 'nothing':
- break;
- case 'ignore':
- $changed = true;
- $this->settings['custom_fields_translation'][$cfield] = 3;
- break;
- case 'translate':
- $changed = true;
- $this->settings['custom_fields_translation'][$cfield] = 2;
- break;
- case 'copy':
- $changed = true;
- $this->settings['custom_fields_translation'][$cfield] = 1;
- break;
- }
- }
- }
- if ($changed) {
- $this->save_settings();
- }
- }
- function _translation_method_notice(){
- echo '<div class="error fade"><p id="icl_side_by_site">'.sprintf(__('New - side-by-site translation editor: <a href="%s">try it</a> | <a href="#cancel">no thanks</a>.', 'sitepress'),
- admin_url('admin.php?page='.WPML_TM_FOLDER.'/menu/main.php&sm=mcsetup&src=notice')) . '</p></div>';
- }
- function _show_tinyMCE() {
- wp_print_scripts('editor');
- //add_filter('the_editor', array($this, 'editor_directionality'), 9999);
- add_filter('tiny_mce_before_init', array($this, '_mce_set_direction'), 9999);
- add_filter('mce_buttons', array($this, '_mce_remove_fullscreen'), 9999);
- if (version_compare($GLOBALS['wp_version'], '3.1.4', '<=') && function_exists('wp_tiny_mce'))
- try{
- /** @noinspection PhpDeprecationInspection */
- @wp_tiny_mce();
- } catch(Exception $e) { /*don't do anything with this */ }
- }
- function _mce_remove_fullscreen($options){
- foreach($options as $k=>$v) if($v == 'fullscreen') unset($options[$k]);
- return $options;
- }
- function _inline_js_scripts(){
- // remove fullscreen mode
- if(defined('WPML_TM_FOLDER') && isset($_GET['page']) && $_GET['page'] == WPML_TM_FOLDER . '/menu/translations-queue.php' && isset($_GET['job_id'])){
- ?>
- <script type="text/javascript">addLoadEvent(function(){jQuery('#ed_fullscreen').remove();});</script>
- <?php
- }
- }
- function _mce_set_direction($settings) {
- $job = $this->get_translation_job((int)$_GET['job_id'], false, true);
- if (!empty($job)) {
- $rtl_translation = in_array($job->language_code, array('ar','he','fa'));
- if ($rtl_translation) {
- $settings['directionality'] = 'rtl';
- } else {
- $settings['directionality'] = 'ltr';
- }
- }
- return $settings;
- }
- /*
- function editor_directionality($tag) {
- $job = $this->get_translation_job((int)$_GET['job_id'], false, true);
- $rtl_translation = in_array($job->language_code, array('ar','he','fa'));
- if ($rtl_translation) {
- $dir = 'dir="rtl"';
- } else {
- $dir = 'dir="ltr"';
- }
- return str_replace('<textarea', '<textarea ' . $dir, $tag);
- }
- */
- function process_request($action, $data){
- $data = stripslashes_deep($data);
- switch($action){
- case 'add_translator':
- if(wp_create_nonce('add_translator') == $data['add_translator_nonce']){
- // Initial adding
- if (isset($data['from_lang']) && isset($data['to_lang'])) {
- $data['lang_pairs'] = array();
- $data['lang_pairs'][$data['from_lang']] = array($data['to_lang'] => 1);
- }
- $this->add_translator($data['user_id'], $data['lang_pairs']);
- $_user = new WP_User($data['user_id']);
- wp_redirect('admin.php?page='.WPML_TM_FOLDER.'/menu/main.php&sm=translators&icl_tm_message='.urlencode(sprintf(__('%s has been added as a translator for this site.','sitepress'),$_user->data->display_name)).'&icl_tm_message_type=updated');
- }
- break;
- case 'edit_translator':
- if(wp_create_nonce('edit_translator') == $data['edit_translator_nonce']){
- $this->edit_translator($data['user_id'], isset($data['lang_pairs']) ? $data['lang_pairs'] : array());
- $_user = new WP_User($data['user_id']);
- wp_redirect('admin.php?page='.WPML_TM_FOLDER.'/menu/main.php&sm=translators&icl_tm_message='.urlencode(sprintf(__('Language pairs for %s have been edited.','sitepress'),$_user->data->display_name)).'&icl_tm_message_type=updated');
- }
- break;
- case 'remove_translator':
- if(wp_create_nonce('remove_translator') == $data['remove_translator_nonce']){
- $this->remove_translator($data['user_id']);
- $_user = new WP_User($data['user_id']);
- wp_redirect('admin.php?page='.WPML_TM_FOLDER.'/menu/main.php&sm=translators&icl_tm_message='.urlencode(sprintf(__('%s has been removed as a translator for this site.','sitepress'),$_user->data->display_name)).'&icl_tm_message_type=updated');
- }
- break;
- case 'edit':
- $this->selected_translator['ID'] = intval($data['user_id']);
- break;
- case 'dashboard_filter':
- $_SESSION['translation_dashboard_filter'] = $data['filter'];
- wp_redirect('admin.php?page='.WPML_TM_FOLDER . '/menu/main.php&sm=dashboard');
- break;
- case 'sort':
- if(isset($data['sort_by'])) $_SESSION['translation_dashboard_filter']['sort_by'] = $data['sort_by'];
- if(isset($data['sort_order'])) $_SESSION['translation_dashboard_filter']['sort_order'] = $data['sort_order'];
- break;
- case 'reset_filters':
- unset($_SESSION['translation_dashboard_filter']);
- break;
- case 'send_jobs':
- if(isset($data['iclnonce']) && wp_verify_nonce($data['iclnonce'], 'pro-translation-icl')){
- $this->send_jobs($data);
- }
- break;
- case 'jobs_filter':
- $_SESSION['translation_jobs_filter'] = $data['filter'];
- wp_redirect('admin.php?page='.WPML_TM_FOLDER . '/menu/main.php&sm=jobs');
- break;
- case 'ujobs_filter':
- $_SESSION['translation_ujobs_filter'] = $data['filter'];
- wp_redirect('admin.php?page='.WPML_TM_FOLDER . '/menu/translations-queue.php');
- break;
- case 'save_translation':
- if(!empty($data['resign'])){
- $this->resign_translator($data['job_id']);
- wp_redirect(admin_url('admin.php?page='.WPML_TM_FOLDER.'/menu/translations-queue.php&resigned='.$data['job_id']));
- exit;
- }else{
- $this->save_translation($data);
- }
- break;
- case 'save_notification_settings':
- $this->settings['notification'] = $data['notification'];
- $this->save_settings();
- $this->messages[] = array(
- 'type'=>'updated',
- 'text' => __('Preferences saved.', 'sitepress')
- );
- break;
- case 'create_job':
- global $current_user;
- if(!isset($this->current_translator->ID) && isset($current_user->ID)){
- $this->current_translator->ID = $current_user->ID;
- }
- $data['translator'] = $this->current_translator->ID;
- $job_ids = $this->send_jobs($data);
- wp_redirect('admin.php?page='.WPML_TM_FOLDER . '/menu/translations-queue.php&job_id=' . array_pop($job_ids));
- break;
- case 'cancel_jobs':
- $ret = $this->cancel_translation_request($data['icl_translation_id']);
- $this->messages[] = array(
- 'type'=>'updated',
- 'text' => __('Translation requests cancelled.', 'sitepress')
- );
- break;
- }
- }
- function ajax_calls( $call, $data ) {
- global $wpdb, $sitepress, $sitepress_settings;
- switch ( $call ) {
- /*
- case 'save_dashboard_setting':
- $iclsettings['dashboard'] = $sitepress_settings['dashboard'];
- if(isset($data['setting']) && isset($data['value'])){
- $iclsettings['dashboard'][$data['setting']] = $data['value'];
- $sitepress->save_settings($iclsettings);
- }
- break;
- */
- case 'assign_translator':
- $_exp = explode( '-', $data[ 'translator_id' ] );
- $service = isset( $_exp[ 1 ] ) ? $_exp[ 1 ] : 'local';
- $translator_id = $_exp[ 0 ];
- if ( $this->assign_translation_job( $data[ 'job_id' ], $translator_id, $service ) ) {
- if ( $service == 'icanlocalize' ) {
- $job = $this->get_translation_job( $data[ 'job_id' ] );
- global $ICL_Pro_Translation;
- $ICL_Pro_Translation->send_post( $job->original_doc_id, array( $job->language_code ), $translator_id );
- $lang_tr_id = false;
- $contract_id = false;
- foreach ( $sitepress_settings[ 'icl_lang_status' ] as $lp ) {
- if ( $lp[ 'from' ] == $job->source_language_code && $lp[ 'to' ] == $job->language_code ) {
- $contract_id = $lp[ 'contract_id' ];
- $lang_tr_id = $lp[ 'id' ];
- break;
- }
- }
- $popup_link = ICL_API_ENDPOINT . '/websites/' . $sitepress_settings[ 'site_id' ] . '/website_translation_offers/' . $lang_tr_id . '/website_translation_contracts/' . $contract_id;
- $popup_args = array(
- 'title' => __( 'Chat with translator', 'sitepress' ),
- 'unload_cb' => 'icl_thickbox_refresh'
- );
- $translator_edit_link = $sitepress->create_icl_popup_link( $popup_link, $popup_args );
- $translator_edit_link .= esc_html( ICL_Pro_Translation::get_translator_name( $translator_id ) );
- $translator_edit_link .= '</a> (ICanLocalize)';
- } else {
- $translator_edit_link =
- '<a href="' . $this->get_translator_edit_url( $data[ 'translator_id' ] ) . '">' . esc_html( $wpdb->get_var( $wpdb->prepare( "SELECT display_name FROM {$wpdb->users} WHERE ID=%d", $data[ 'translator_id' ] ) ) ) . '</a>';
- }
- echo json_encode( array( 'error' => 0, 'message' => $translator_edit_link, 'status' => $this->status2text( ICL_TM_WAITING_FOR_TRANSLATOR ), 'service' => $service ) );
- } else {
- echo json_encode( array( 'error' => 1 ) );
- }
- break;
- case 'icl_cf_translation':
- if ( !empty( $data[ 'cf' ] ) ) {
- foreach ( $data[ 'cf' ] as $k => $v ) {
- $cft[ base64_decode( $k ) ] = $v;
- }
- if ( isset( $cft ) ) {
- $this->settings['custom_fields_translation'] = $cft;
- $this->save_settings();
- }
- }
- echo '1|';
- break;
- case 'icl_doc_translation_method':
- $this->settings['doc_translation_method'] = intval($data['t_method']);
- $sitepress->set_setting( 'hide_how_to_translate', empty( $data[ 'how_to_translate' ] ) );
- if (isset($data[ 'tm_block_retranslating_terms' ])) {
- $sitepress->set_setting( 'tm_block_retranslating_terms', $data[ 'tm_block_retranslating_terms' ] );
- } else {
- $sitepress->set_setting( 'tm_block_retranslating_terms', '' );
- }
- $this->save_settings();
- echo '1|';
- break;
- case 'reset_duplication':
- $this->reset_duplicate_flag( $_POST[ 'post_id' ] );
- break;
- case 'set_duplication':
- $this->set_duplicate( $_POST[ 'post_id' ] );
- break;
- case 'make_duplicates':
- $mdata[ 'iclpost' ] = array( $data[ 'post_id' ] );
- $langs = explode( ',', $data[ 'langs' ] );
- foreach ( $langs as $lang ) {
- $mdata[ 'duplicate_to' ][ $lang ] = 1;
- }
- $this->make_duplicates( $mdata );
- break;
- }
- }
- function show_messages(){
- if(!empty($this->messages)){
- foreach($this->messages as $m){
- echo '<div class="'.$m['type'].' below-h2"><p>' . $m['text'] . '</p></div>';
- }
- }
- }
- /* TRANSLATORS */
- /* ******************************************************************************************** */
- function add_translator($user_id, $language_pairs){
- global $wpdb;
- $user = new WP_User($user_id);
- $user->add_cap('translate');
- $um = get_user_meta($user_id, $wpdb->prefix . 'language_pairs', true);
- if(!empty($um)){
- foreach($um as $fr=>$to){
- if(isset($language_pairs[$fr])){
- $language_pairs[$fr] = array_merge($language_pairs[$fr], $to);
- }
- }
- }
- update_user_meta($user_id, $wpdb->prefix . 'language_pairs', $language_pairs);
- $this->clear_cache();
- }
- function edit_translator($user_id, $language_pairs){
- global $wpdb;
- $_user = new WP_User($user_id);
- if(empty($language_pairs)){
- $this->remove_translator($user_id);
- wp_redirect('admin.php?page='.WPML_TM_FOLDER.'/menu/main.php&sm=translators&icl_tm_message='.
- urlencode(sprintf(__('%s has been removed as a translator for this site.','sitepress'),$_user->data->display_name)).'&icl_tm_message_type=updated'); exit;
- }
- else{
- if(!$_user->has_cap('translate')) $_user->add_cap('translate');
- update_user_meta($user_id, $wpdb->prefix . 'language_pairs', $language_pairs);
- }
- }
- function remove_translator($user_id){
- global $wpdb;
- $user = new WP_User($user_id);
- $user->remove_cap('translate');
- delete_user_meta($user_id, $wpdb->prefix . 'language_pairs');
- $this->clear_cache();
- }
- function is_translator( $user_id, $args = array() )
- {
- extract( $args, EXTR_OVERWRITE );
- global $wpdb;
- $user = new WP_User( $user_id );
- $is_translator = $user->has_cap( 'translate' );
- // check if user is administrator and return true if he is
- $user_caps = $user->caps;
- if ( isset( $user_caps[ 'activate_plugins' ] ) && $user_caps[ 'activate_plugins' ] == true ) {
- $is_translator = true;
- } else {
- if ( isset( $lang_from ) && isset( $lang_to ) ) {
- $um = get_user_meta( $user_id, $wpdb->prefix . 'language_pairs', true );
- $is_translator = $is_translator && isset( $um[ $lang_from ] ) && isset( $um[ $lang_from ][ $lang_to ] ) && $um[ $lang_from ][ $lang_to ];
- }
- if ( isset( $job_id ) ) {
- $translator_id = $wpdb->get_var( $wpdb->prepare( "
- SELECT j.translator_id
- FROM {$wpdb->prefix}icl_translate_job j
- JOIN {$wpdb->prefix}icl_translation_status s ON j.rid = s.rid
- WHERE job_id = %d AND s.translation_service='local'
- ", $job_id ) );
- $is_translator = $is_translator && ( ( $translator_id == $user_id ) || empty( $translator_id ) );
- }
- }
- return $is_translator;
- }
- function translator_exists($id, $from, $to, $type = 'local'){
- global $sitepress_settings;
- $exists = false;
- if($type == 'icanlocalize' && !empty($sitepress_settings['icl_lang_status'])){
- foreach($sitepress_settings['icl_lang_status'] as $lpair){
- if($lpair['from'] == $from && $lpair['to'] == $to){
- if(!empty($lpair['translators'])){
- foreach($lpair['translators'] as $t){
- if($t['id'] == $id){
- $exists = true;
- break(2);
- }
- }
- }
- }
- }
- }elseif($type == 'local'){
- $exists = $this->is_translator($id, array('lang_from'=>$from, 'lang_to'=>$to));
- }
- return $exists;
- }
- function set_default_translator($id, $from, $to, $type = 'local'){
- global $sitepress, $sitepress_settings;
- $iclsettings['default_translators'] = isset($sitepress_settings['default_translators']) ? $sitepress_settings['default_translators'] : array();
- $iclsettings['default_translators'][$from][$to] = array('id'=>$id, 'type'=>$type);
- $sitepress->save_settings($iclsettings);
- }
- function get_default_translator($from, $to){
- global $sitepress_settings;
- if(isset($sitepress_settings['default_translators'][$from][$to])){
- $dt = $sitepress_settings['default_translators'][$from][$to];
- }else{
- $dt = array();
- }
- return $dt;
- }
- public static function get_blog_not_translators(){
- global $wpdb;
- $cached_translators = get_option($wpdb->prefix . 'icl_non_translators_cached', array());
- if (!empty($cached_translators)) {
- return $cached_translators;
- }
- $sql = "SELECT u.ID, u.user_login, u.display_name, m.meta_value AS caps
- FROM {$wpdb->users} u JOIN {$wpdb->usermeta} m ON u.id=m.user_id AND m.meta_key = '{$wpdb->prefix}capabilities' ORDER BY u.display_name";
- $res = $wpdb->get_results($sql);
- $users = array();
- foreach($res as $row){
- $caps = @unserialize($row->caps);
- if(!isset($caps['translate'])){
- $users[] = $row;
- }
- }
- update_option($wpdb->prefix . 'icl_non_translators_cached', $users);
- return $users;
- }
- /**
- * Implementation of 'icl_translators_list' hook
- *
- * @global object $sitepress
- * @param array $array
- * @return array
- */
- public static function icanlocalize_translators_list() {
- global $sitepress_settings, $sitepress;
- $lang_status = isset($sitepress_settings['icl_lang_status']) ? $sitepress_settings['icl_lang_status'] : array();
- if (0 != key($lang_status)){
- $buf[] = $lang_status;
- $lang_status = $buf;
- }
- $translators = array();
- foreach($lang_status as $lpair){
- foreach((array)$lpair['translators'] as $translator){
- $translators[$translator['id']]['name'] = $translator['nickname'];
- $translators[$translator['id']]['langs'][$lpair['from']][] = $lpair['to'];
- $translators[$translator['id']]['type'] = 'ICanLocalize';
- $translators[$translator['id']]['action'] = $sitepress->create_icl_popup_link(ICL_API_ENDPOINT . '/websites/' . $sitepress_settings['site_id']
- . '/website_translation_offers/' . $lpair['id'] . '/website_translation_contracts/'
- . $translator['contract_id'], array('title' => __('Chat with translator', 'sitepress'), 'unload_cb' => 'icl_thickbox_refresh', 'ar'=>1)) . __('Chat with translator', 'sitepress') . '</a>';
- }
- }
- return $translators;
- }
- public static function get_blog_translators($args = array()){
- global $wpdb;
- $args_default = array('from'=>false, 'to'=>false);
- extract($args_default);
- extract($args, EXTR_OVERWRITE);
- // $sql = "SELECT u.ID, u.user_login, u.display_name, u.user_email, m.meta_value AS caps
- // FROM {$wpdb->users} u JOIN {$wpdb->usermeta} m ON u.id=m.user_id AND m.meta_key LIKE '{$wpdb->prefix}capabilities' ORDER BY u.display_name";
- // $res = $wpdb->get_results($sql);
- $cached_translators = get_option($wpdb->prefix . 'icl_translators_cached', array());
- if (empty($cached_translators)) {
- $sql = "SELECT u.ID FROM {$wpdb->users} u JOIN {$wpdb->usermeta} m ON u.id=m.user_id AND m.meta_key = '{$wpdb->prefix}language_pairs' ORDER BY u.display_name";
- $res = $wpdb->get_results($sql);
- update_option($wpdb->prefix . 'icl_translators_cached', $res);
- } else {
- $res = $cached_translators;
- }
- $users = array();
- foreach($res as $row){
- $user = new WP_User($row->ID);
- // $caps = @unserialize($row->caps);
- // $row->language_pairs = (array)get_user_meta($row->ID, $wpdb->prefix.'language_pairs', true);
- $user->language_pairs = (array)get_user_meta($row->ID, $wpdb->prefix.'language_pairs', true);
- // if(!empty($from) && !empty($to) && (!isset($row->language_pairs[$from][$to]) || !$row->language_pairs[$from][$to])){
- // continue;
- // }
- if(!empty($from) && !empty($to) && (!isset($user->language_pairs[$from][$to]) || !$user->language_pairs[$from][$to])){
- continue;
- }
- // if(isset($caps['translate'])){
- // $users[] = $user;
- // }
- if($user->has_cap('translate')){
- $users[] = $user;
- }
- }
- return $users;
- }
- function get_selected_translator(){
- global $wpdb;
- if($this->selected_translator['ID']){
- $user = new WP_User($this->selected_translator['ID']);
- $this->selected_translator['display_name'] = $user->data->display_name;
- $this->selected_translator['user_login'] = $user->data->user_login;
- $this->selected_translator['language_pairs'] = get_user_meta($this->selected_translator['ID'], $wpdb->prefix.'language_pairs', true);
- }else{
- $this->selected_translator['ID'] = 0;
- }
- return (object)$this->selected_translator;
- }
- function get_current_translator(){
- return $this->current_translator;
- }
- public function get_translator_edit_url($translator_id){
- $url = '';
- if(!empty($translator_id)){
- $url = 'admin.php?page='. WPML_TM_FOLDER .'/menu/main.php&sm=translators&icl_tm_action=edit&user_id='. $translator_id;
- }
- return $url;
- }
- public function translators_dropdown( $args = array() ) {
- global $sitepress_settings;
- $args_default = array(
- 'from' => false,
- 'to' => false,
- 'name' => 'translator_id',
- 'selected' => 0,
- 'echo' => true,
- 'services' => array( 'local' ),
- 'show_service' => true,
- 'disabled' => false
- );
- extract( $args_default );
- extract( $args, EXTR_OVERWRITE );
- $translators = array();
- /** @var $from string|false */
- /** @var $to string|false */
- /** @var $name string|false */
- /** @var $selected bool */
- /** @var $echo bool */
- /** @var $services array */
- /** @var $show_service bool */
- /** @var $disabled bool */
- if ( in_array( 'icanlocalize', $services ) ) {
- if ( empty( $sitepress_settings[ 'icl_lang_status' ] ) ) {
- $sitepress_settings[ 'icl_lang_status' ] = array();
- }
- foreach ( (array) $sitepress_settings[ 'icl_lang_status' ] as $language_pair ) {
- if ( $from && $from != $language_pair[ 'from' ] ) {
- continue;
- }
- if ( $to && $to != $language_pair[ 'to' ] ) {
- continue;
- }
- if ( !empty( $language_pair[ 'translators' ] ) ) {
- if ( 1 < count( $language_pair[ 'translators' ] ) ) {
- $translators[ ] = (object) array(
- 'ID' => '0-icanlocalize',
- 'display_name' => __( 'First available', 'sitepress' ),
- 'service' => 'ICanLocalize'
- );
- }
- foreach ( $language_pair[ 'translators' ] as $tr ) {
- if ( !isset( $_icl_translators[ $tr[ 'id' ] ] ) ) {
- $translators[ ] = $_icl_translators[ $tr[ 'id' ] ] = (object) array(
- 'ID' => $tr[ 'id' ] . '-icanlocalize',
- 'display_name' => $tr[ 'nickname' ],
- 'service' => 'ICanLocalize'
- );
- }
- }
- }
- }
- }
- if ( in_array( 'local', $services ) ) {
- $translators[ ] = (object) array(
- 'ID' => 0,
- 'display_name' => __( 'First available', 'sitepress' ),
- );
- $translators = array_merge( $translators, self::get_blog_translators( array( 'from' => $from, 'to' => $to ) ) );
- }
- $translators = apply_filters( 'wpml_tm_translators_list', $translators );
- ?>
- <select name="<?php echo $name ?>" <?php if ($disabled): ?>disabled="disabled"<?php endif; ?>>
- <?php foreach ( $translators as $t ): ?>
- <option value="<?php echo $t->ID ?>" <?php if ($selected == $t->ID): ?>selected="selected"<?php endif; ?>><?php echo esc_html( $t->display_name );
- ?> <?php if ( $show_service ) {
- echo '(';
- echo isset( $t->service ) ? $t->service : _e( 'Local', 'sitepress' );
- echo ')';
- } ?></option>
- <?php endforeach; ?>
- </select>
- <?php
- }
- public function get_number_of_docs_sent($service = 'icanlocalize'){
- global $wpdb;
- $n = $wpdb->get_var($wpdb->prepare("
- SELECT COUNT(rid) FROM {$wpdb->prefix}icl_translation_status WHERE translation_service=%s
- ", $service));
- return $n;
- }
- public function get_number_of_docs_pending($service = 'icanlocalize'){
- global $wpdb;
- $n = $wpdb->get_var($wpdb->prepare("
- SELECT COUNT(rid) FROM {$wpdb->prefix}icl_translation_status WHERE translation_service=%s AND status < " . ICL_TM_COMPLETE . "
- ", $service));
- return $n;
- }
- /* HOOKS */
- /* ******************************************************************************************** */
- function save_post_actions( $post_id, $post, $force_set_status = false )
- {
- global $wpdb, $sitepress, $current_user;
- // skip revisions
- if ( $post->post_type == 'revision' ) {
- return;
- }
- // skip auto-drafts
- if ( $post->post_status == 'auto-draft' ) {
- return;
- }
- // skip autosave
- if ( isset( $_POST[ 'autosave' ] ) ) {
- return;
- }
- if ( isset( $_POST[ 'icl_trid' ] ) && is_numeric($_POST['icl_trid']) ) {
- $trid = $_POST['icl_trid'];
- } else {
- $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type );
- }
- // set trid and lang code if front-end translation creating
- $trid = apply_filters( 'wpml_tm_save_post_trid_value', isset( $trid ) ? $trid : '', $post_id );
- $lang = apply_filters( 'wpml_tm_save_post_lang_value', isset( $lang ) ? $lang : '', $post_id );
- // is this the original document?
- $is_original = false;
- if ( !empty( $trid ) ) {
- $is_original = $wpdb->get_var( $wpdb->prepare( "SELECT source_language_code IS NULL FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND trid=%d", $post_id, $trid ) );
- }
- // when a manual translation is added/edited make sure to update translation tables
- if ( !empty( $trid ) && !$is_original ) {
- if ( ( !isset( $lang ) || !$lang ) && isset( $_POST[ 'icl_post_language' ] ) && !empty( $_POST[ 'icl_post_language' ] ) ) {
- $lang = $_POST[ 'icl_post_language' ];
- }
- $res = $wpdb->get_row( $wpdb->prepare( "
- SELECT element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL
- ", $trid ) );
- if ( $res ) {
- $original_post_id = $res->element_id;
- $from_lang = $res->language_code;
- $original_post = get_post( $original_post_id );
- $md5 = $this->post_md5( $original_post );
- $translation_id_prepared = $wpdb->prepare( "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s", $trid, $lang );
- $translation_id = $wpdb->get_var( $translation_id_prepared );
- get_currentuserinfo();
- $user_id = $current_user->ID;
- if ( !$this->is_translator( $user_id, array( 'lang_from' => $from_lang, 'lang_to' => $lang ) ) ) {
- $this->add_translator( $user_id, array( $from_lang => array( $lang => 1 ) ) );
- }
- if ( $translation_id ) {
- $translation_package = $this->create_translation_package( $original_post_id );
- list( $rid, $update ) = $this->update_translation_status( array(
- 'translation_id' => $translation_id,
- 'status' => isset( $force_set_status ) && $force_set_status > 0 ? $force_set_status : ICL_TM_COMPLETE,
- 'translator_id' => $user_id,
- 'needs_update' => 0,
- 'md5' => $md5,
- 'translation_service' => 'local',
- 'translation_package' => serialize( $translation_package )
- ) );
- if ( !$update ) {
- $job_id = $this->add_translation_job( $rid, $user_id, $translation_package );
- } else {
- $job_id = $wpdb->get_var( $wpdb->prepare( "SELECT MAX(job_id) FROM {$wpdb->prefix}icl_translate_job WHERE rid=%d GROUP BY rid", $rid ) );
- }
- // saving the translation
- $this->save_job_fields_from_post( $job_id, $post );
- }
- }
- }
- // if this is an original post - compute md5 hash and mark for update if neded
- if ( !empty( $trid ) && empty( $_POST[ 'icl_minor_edit' ] ) ) {
- $is_original = false;
- $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );
- foreach ( $translations as $lang => $translation ) {
- if ( $translation->original == 1 && $translation->element_id == $post_id ) {
- $is_original = true;
- break;
- }
- }
- if ( $is_original ) {
- $md5 = $this->post_md5( $post_id );
- foreach ( $translations as $lang => $translation ) {
- if ( !$translation->original ) {
- $emd5_prepared = $wpdb->prepare( "SELECT md5 FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation->translation_id );
- $emd5 = $wpdb->get_var( $emd5_prepared );
- if ( $md5 != $emd5 ) {
- $translation_package = $this->create_translation_package( $post_id );
- list( $rid, $update ) = $this->update_translation_status( array(
- 'translation_id' => $translation->translation_id,
- 'needs_update' => 1,
- 'md5' => $md5,
- 'translation_package' => serialize( $translation_package )
- ) );
- // update
- $translator_id_prepared = $wpdb->prepare( "SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation->translation_id );
- $translator_id = $wpdb->get_var( $translator_id_prepared );
- $job_id = $this->add_translation_job( $rid, $translator_id, $translation_package );
- // updating a post that's being translated - update fields in icl_translate
- if ( false === $job_id ) {
- $job_id_prepared = $wpdb->prepare( "SELECT MAX(job_id) FROM {$wpdb->prefix}icl_translate_job WHERE rid = %d", $rid );
- $job_id = $wpdb->get_var( $job_id_prepared );
- if ( $job_id ) {
- $job = $this->get_translation_job( $job_id );
- if ( $job ) {
- foreach ( $job->elements as $element ) {
- unset( $field_data );
- $_taxs_ids = false;
- switch ( $element->field_type ) {
- case 'title':
- $field_data = $this->encode_field_data( $post->post_title, $element->field_format );
- break;
- case 'body':
- $field_data = $this->encode_field_data( $post->post_content, $element->field_format );
- break;
- case 'excerpt':
- $field_data = $this->encode_field_data( $post->post_excerpt, $element->field_format );
- break;
- case 'tags':
- $terms = (array)get_the_terms( $post->ID, 'post_tag' );
- $_taxs = array();
- foreach ( $terms as $term ) {
- $_taxs[ ] = $term->name;
- $_taxs_ids[ ] = $term->term_taxonomy_id;
- }
- $field_data = $this->encode_field_data( $_taxs, $element->field_format );
- break;
- case 'categories':
- $terms = get_the_terms( $post->ID, 'category' );
- $_taxs = array();
- foreach ( $terms as $term ) {
- $_taxs[ ] = $term->name;
- $_taxs_ids[ ] = $term->term_taxonomy_id;
- }
- $field_data = $this->encode_field_data( $_taxs, $element->field_format );
- break;
- default:
- if ( false !== strpos( $element->field_type, 'field-' ) && !empty( $this->settings[ 'custom_fields_translation' ] ) ) {
- $cf_name = preg_replace( '#^field-#', '', $element->field_type );
- if ( isset( $this->settings[ 'custom_fields_translation' ][ $cf_name ] ) ) {
- $field_data = get_post_meta( $post->ID, $cf_name, 1 );
- $field_data = $this->encode_field_data( $field_data, $element->field_format );
- }
- } else {
- // taxonomies
- if ( taxonomy_exists( $element->field_type ) ) {
- $terms = get_the_terms( $post->ID, $element->field_type );
- $_taxs = array();
- foreach ( $terms as $term ) {
- $_taxs[ ] = $term->name;
- $_taxs_ids[ ] = $term->term_taxonomy_id;
- }
- $field_data = $this->encode_field_data( $_taxs, $element->field_format );
- }
- }
- }
- if ( isset( $field_data ) && $field_data != $element->field_data ) {
- $wpdb->update( $wpdb->prefix . 'icl_translate', array( 'field_data' => $field_data ), array( 'tid' => $element->tid ) );
- if ( $_taxs_ids && $element->field_type == 'categories' ) {
- $wpdb->update( $wpdb->prefix . 'icl_translate', array( 'field_data' => join( ',', $_taxs_ids ) ), array( 'job_id' => $job_id, 'field_type' => 'category_ids' ) );
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- // sync copies/duplicates
- $duplicates = $this->get_duplicates( $post_id );
- static $duplicated_post_ids;
- if ( !isset( $duplicated_post_ids ) ) {
- $duplicated_post_ids = array();
- }
- foreach ( $duplicates as $lang => $_pid ) {
- // Avoid infinite recursions
- if ( !in_array( $post_id . '|' . $lang, $duplicated_post_ids ) ) {
- $duplicated_post_ids[ ] = $post_id . '|' . $lang;
- $this->make_duplicate( $post_id, $lang );
- }
- }
- }
- function make_duplicates( $data )
- {
- foreach ( $data[ 'iclpost' ] as $master_post_id ) {
- foreach ( $data[ 'duplicate_to' ] as $lang => $one ) {
- $this->make_duplicate( $master_post_id, $lang );
- }
- }
- }
- function make_duplicate( $master_post_id, $lang )
- {
- static $duplicated_post_ids;
- if(!isset($duplicated_post_ids)) {
- $duplicated_post_ids = array();
- }
- //It is already done? (avoid infinite recursions)
- if(in_array($master_post_id . '|' . $lang, $duplicated_post_ids)) {
- return true;
- }
- $duplicated_post_ids[] = $master_post_id . '|' . $lang;
- global $sitepress, $sitepress_settings, $wpdb;
- do_action( 'icl_before_make_duplicate', $master_post_id, $lang );
- $master_post = get_post( $master_post_id );
- $is_duplicated = false;
- $trid = $sitepress->get_element_trid( $master_post_id, 'post_' . $master_post->post_type );
- if ( $trid ) {
- $translations = $sitepress->get_element_translations( $trid, 'post_' . $master_post->post_type );
- if ( isset( $translations[ $lang ] ) ) {
- $post_array[ 'ID' ] = $translations[ $lang ]->element_id;
- $is_duplicated = get_post_meta( $translations[ $lang ]->element_id, '_icl_lang_duplicate_of', true );
- }
- }
- // covers the case when deleting in bulk from all languages
- // setting post_status to trash before wp_trash_post runs issues an WP error
- $posts_to_delete_or_restore_in_bulk = array();
- if ( isset( $_GET[ 'action' ] ) && ( $_GET[ 'action' ] == 'trash' || $_GET[ 'action' ] == 'untrash' ) && isset( $_GET[ 'lang' ] ) && $_GET[ 'lang' ] == 'all' ) {
- static $posts_to_delete_or_restore_in_bulk;
- if ( is_null( $posts_to_delete_or_restore_in_bulk ) ) {
- $posts_to_delete_or_restore_in_bulk = isset( $_GET[ 'post' ] ) && is_array( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : array();
- }
- }
- $post_array[ 'post_author' ] = $master_post->post_author;
- $post_array[ 'post_date' ] = $master_post->post_date;
- $post_array[ 'post_date_gmt' ] = $master_post->post_date_gmt;
- $post_array[ 'post_content' ] = addslashes_gpc(apply_filters( 'icl_duplicate_generic_string', $master_post->post_content, $lang, array( 'context' => 'post', 'attribute' => 'content', 'key' => $master_post->ID ) ));
- $post_array[ 'post_title' ] = addslashes_gpc(apply_filters( 'icl_duplicate_generic_string', $master_post->post_title, $lang, array( 'context' => 'post', 'attribute' => 'title', 'key' => $master_post->ID ) ));
- $post_array[ 'post_excerpt' ] = addslashes_gpc(apply_filters( 'icl_duplicate_generic_string', $master_post->post_excerpt, $lang, array( 'context' => 'post', 'attribute' => 'excerpt', 'key' => $master_post->ID ) ));
- if ( isset( $sitepress_settings[ 'sync_post_status' ] ) && $sitepress_settings[ 'sync_post_status' ] ) {
- $sync_post_status = true;
- } else {
- $sync_post_status = ( !isset( $post_array[ 'ID' ] ) || ( $sitepress_settings[ 'sync_delete' ] && $master_post->post_status == 'trash' ) || $is_duplicated );
- $sync_post_status &= ( !$posts_to_delete_or_restore_in_bulk || ( !isset( $post_array[ 'ID' ] ) || !in_array( $post_array[ 'ID' ], $posts_to_delete_or_restore_in_bulk ) ) );
- }
- if ( $sync_post_status ) {
- $post_array[ 'post_status' ] = $master_post->post_status;
- }
- $post_array[ 'comment_status' ] = $master_post->comment_status;
- $post_array[ 'ping_status' ] = $master_post->ping_status;
- $post_array[ 'post_name' ] = $master_post->post_name;
- if ( $master_post->post_parent ) {
- $parent = icl_object_id( $master_post->post_parent, $master_post->post_type, false, $lang );
- $post_array[ 'post_parent' ] = $parent;
- }
- $post_array[ 'menu_order' ] = $master_post->menu_order;
- $post_array[ 'post_type' ] = $master_post->post_type;
- $post_array[ 'post_mime_type' ] = $master_post->post_mime_type;
- $trid = $sitepress->get_element_trid( $master_post->ID, 'post_' . $master_post->post_type );
- $_POST[ 'icl_trid' ] = $trid;
- $_POST[ 'icl_post_language' ] = $lang;
- $_POST[ 'skip_sitepress_actions' ] = true;
- $_POST[ 'post_type' ] = $master_post->post_type;
- if ( isset( $post_array[ 'ID' ] ) ) {
- $id = wp_update_post( $post_array );
- } else {
- $id = $this->icl_insert_post( $post_array, $lang );
- }
- require_once ICL_PLUGIN_PATH . '/inc/cache.php';
- icl_cache_clear( $post_array[ 'post_type' ] . 's_per_language' );
- global $ICL_Pro_Translation;
- $ICL_Pro_Translation->_content_fix_links_to_translated_content( $id, $lang );
- if ( !is_wp_error( $id ) ) {
- $sitepress->set_element_language_details( $id, 'post_' . $master_post->post_type, $trid, $lang );
- $this->save_post_actions( $id, get_post( $id ), ICL_TM_DUPLICATE );
- $this->duplicate_fix_children( $master_post_id, $lang );
- // dup comments
- if ( $sitepress->get_option( 'sync_comments_on_duplicates' ) ) {
- $this->duplicate_comments( $id, $master_post_id );
- }
- // make sure post name is copied
- $wpdb->update( $wpdb->posts, array( 'post_name' => $master_post->post_name ), array( 'ID' => $id ) );
- update_post_meta( $id, '_icl_lang_duplicate_of', $master_post->ID );
- if ( $sitepress->get_option( 'sync_post_taxonomies' ) ) {
- $this->duplicate_taxonomies( $master_post_id, $lang );
- }
- $this->duplicate_custom_fields( $master_post_id, $lang );
- $ret = $id;
- do_action( 'icl_make_duplicate', $master_post_id, $lang, $post_array, $id );
- } else {
- $ret = false;
- }
- return $ret;
- }
- function duplicate_taxonomies( $master_post_id, $lang )
- {
- global $wpdb, $sitepress;
- $post_type = get_post_field( 'post_type', $master_post_id );
- $taxonomies = get_object_taxonomies( $post_type );
- $trid = $sitepress->get_element_trid( $master_post_id, 'post_' . $post_type );
- $duplicate_post_id = false;
- if ( $trid ) {
- $translations = $sitepress->get_element_translations( $trid, 'post_' . $post_type, false, false, true );
- if ( isset( $translations[ $lang ] ) ) {
- $duplicate_post_id = $translations[ $lang ]->element_id;
- /* If we have an existing post, we first of all remove all terms currently attached to it.
- * The main reason behind is the removal of the potentially present default category on the post.
- */
- wp_delete_object_term_relationships( $duplicate_post_id, $taxonomies );
- } else {
- return false; // translation not found!
- }
- }
- foreach ( $taxonomies as $taxonomy ) {
- if ( $sitepress->is_translated_taxonomy ( $taxonomy ) ) {
- WPML_Terms_Translations::sync_post_and_taxonomy_terms_language( $master_post_id, $taxonomy, true );
- }
- }
- return true;
- }
- function duplicate_custom_fields( $master_post_id, $lang )
- {
- global $wpdb, $sitepress;
- $duplicate_post_id = false;
- $post_type = get_post_field( 'post_type', $master_post_id );
- $trid = $sitepress->get_element_trid( $master_post_id, 'post_' . $post_type );
- if ( $trid ) {
- $translations = $sitepress->get_element_translations( $trid, 'post_' . $post_type );
- if ( isset( $translations[ $lang ] ) ) {
- $duplicate_post_id = $translations[ $lang ]->element_id;
- } else {
- return false; // translation not found!
- }
- }
- $default_exceptions = array(
- '_wp_old_slug',
- '_edit_last',
- '_edit_lock',
- '_icl_translator_note',
- '_icl_lang_duplicate_of',
- '_wpml_media_duplicate',
- '_wpml_media_featured'
- );
- $exceptions = $default_exceptions;
- //Todo: make sure the following filter won't remove the default exceptions
- $exceptions = apply_filters('wpml_duplicate_custom_fields_exceptions', $exceptions);
- // low level copy
- $custom_fields_master = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->postmeta} WHERE post_id=%d group by meta_key", $master_post_id ) );
- $custom_fields_duplicate = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->postmeta} WHERE post_id=%d group by meta_key", $duplicate_post_id ) );
- $custom_fields_master = array_diff( $custom_fields_master, $exceptions );
- $custom_fields_duplicate = array_diff( $custom_fields_duplicate, $exceptions );
- $remove = array_diff( $custom_fields_duplicate, $custom_fields_master );
- foreach ( $remove as $key ) {
- delete_post_meta( $duplicate_post_id, $key );
- }
- foreach ( $custom_fields_master as $key ) {
- $master_custom_field_values_array = get_post_meta( $master_post_id, $key );
- $master_custom_field_values_single = get_post_meta( $master_post_id, $key, true );
- $is_repeated = false;
- if($master_custom_field_values_array != $master_custom_field_values_single) {
- //Repeated fields
- $master_custom_field_values = $master_custom_field_values_array;
- $is_repeated = true;
- } else {
- //Field stored as serialized array
- $master_custom_field_values[] = $master_custom_field_values_single;
- }
- if($is_repeated) {
- $duplicate_custom_field_values = get_post_meta( $duplicate_post_id, $key );
- } else {
- $duplicate_custom_field_values[] = get_post_meta( $duplicate_post_id, $key, true );
- }
- if ( !$duplicate_custom_field_values || $master_custom_field_values != $duplicate_custom_field_values ) {
- if($is_repeated) {
- //Delete the old one
- delete_post_meta($duplicate_post_id, $key);
- //And add new ones from the original
- foreach($master_custom_field_values as $master_custom_field_value) {
- add_post_meta( $duplicate_post_id, $key, addslashes_gpc(apply_filters( 'icl_duplicate_generic_string', $master_custom_field_value, $lang, array( 'context' => 'custom_field', 'attribute' => 'value', 'key' => $key ) ) ) );
- }
- } else {
- update_post_meta( $duplicate_post_id, $key, addslashes_gpc(apply_filters( 'icl_duplicate_generic_string', $master_custom_field_value, $lang, array( 'context' => 'custom_field', 'attribute' => 'value', 'key' => $key ) ) ) );
- }
- }
- }
- return true;
- }
- function duplicate_fix_children( $master_post_id, $lang )
- {
- global $wpdb;
- $post_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $master_post_id ) );
- $master_children = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_type != 'revision'", $master_post_id ) );
- $dup_parent = icl_object_id( $master_post_id, $post_type, false, $lang );
- if ( $master_children ) {
- foreach ( $master_children as $master_child ) {
- $dup_child = icl_object_id( $master_child, $post_type, false, $lang );
- if ( $dup_child ) {
- $wpdb->update( $wpdb->posts, array( 'post_parent' => $dup_parent ), array( 'ID' => $dup_child ) );
- }
- $this->duplicate_fix_children( $master_child, $lang );
- }
- }
- }
- function make_duplicates_all( $master_post_id )
- {
- global $sitepress;
- $master_post = get_post( $master_post_id );
- if($master_post->post_status == 'auto-draft' || $master_post->post_type == 'revision') {
- return;
- }
- $language_details_original = $sitepress->get_element_language_details( $master_post_id, 'post_' . $master_post->post_type );
- if(!$language_details_original) return;
- $data[ 'iclpost' ] = array( $master_post_id );
- foreach ( $sitepress->get_active_languages() as $lang => $details ) {
- if ( $lang != $language_de…
Large files files are truncated, but you can click here to view the full file