selected_translator = new WPML_Translator(); $this->selected_translator->ID = 0; $this->current_translator = new WPML_Translator(); $this->current_translator->ID = 0; $this->cache_factory = $wpml_cache_factory; add_action( 'init', array( $this, 'init' ), self::INIT_PRIORITY ); add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'delete_post', array( $this, 'delete_post_actions' ), 1, 1 ); // Calling *before* the SitePress actions. add_action( 'icl_ajx_custom_call', array( $this, 'ajax_calls' ), 10, 2 ); add_action( 'wpml_tm_basket_add_message', array( $this, 'add_basket_message' ), 10, 3 ); // 1. on Translation Management dashboard and jobs tabs // 2. on Translation Management dashboard tab (called without sm parameter as default page) // 3. Translations queue if ( ( isset( $_GET['sm'] ) && ( $_GET['sm'] == 'dashboard' || $_GET['sm'] == 'jobs' ) ) || ( isset( $_GET['page'] ) && preg_match( '@/menu/main\.php$@', $_GET['page'] ) && ! isset( $_GET['sm'] ) ) || ( 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_action( 'wp_ajax_icl_tm_abort_translation', array( $this, 'abort_translation' ) ); add_action( 'display_basket_notification', array( $this, 'display_basket_notification' ), 10, 1 ); Fns::each( function( $type ) { add_action( "wpml_tm_send_{$type}_jobs", [ $this, 'send_jobs' ], 10, 2 ); }, [ 'post', 'package', 'st-batch' ] ); $this->init_comments_synchronization(); add_action( 'wpml_loaded', array( $this, 'wpml_loaded_action' ) ); /** * @api * @uses \TranslationManagement::get_translation_job_id */ add_filter( 'wpml_translation_job_id', array( $this, 'get_translation_job_id_filter' ), 10, 2 ); $this->filters_and_actions = new WPML_Translation_Management_Filters_And_Actions( $this, $sitepress ); $this->wpml_cookie = $wpml_cookie ?: new WPML_Cookie(); } public function wpml_loaded_action() { $this->load_settings_if_required(); if ( is_admin() ) { add_action( 'wpml_config', array( $this, 'wpml_config_action' ), 10, 1 ); } } public function load_settings_if_required() { if ( ! $this->settings ) { $this->settings = apply_filters( 'wpml_setting', null, 'translation-management' ); } } /** * @param array $args { * * @type string $section * @type string $key * @type mixed $value (when used as translation action: 0: do not translate, 1: copy, 2: translate) * @type bool $read_only Options. Default to true. * } */ public function wpml_config_action( $args ) { if ( current_user_can( 'manage_options' ) ) { $this->update_section_translation_setting( $args ); } } /** * @return WPML_Custom_Field_Setting_Factory */ public function settings_factory() { $this->settings_factory = $this->settings_factory ? $this->settings_factory : new WPML_Custom_Field_Setting_Factory( $this ); return $this->settings_factory; } /** * @param WP_User $current_user * @param WPML_Translator $current_translator * * @return WPML_Translator */ private function init_translator_language_pairs( WP_User $current_user, WPML_Translator $current_translator ) { global $wpdb; $current_translator_language_pairs = get_user_meta( $current_user->ID, $wpdb->prefix . 'language_pairs', true ); $current_translator->language_pairs = $this->sanitize_language_pairs( $current_translator_language_pairs ); if ( ! count( $current_translator->language_pairs ) ) { $current_translator->language_pairs = array(); } return $current_translator; } /** * @param string $code * * @return bool */ private function is_valid_language_code_format( $code ) { return $code && is_string( $code ) && strlen( $code ) >= 2; } /** * @param array $language_pairs * * @return array */ private function sanitize_language_pairs( $language_pairs ) { if ( ! $language_pairs || ! is_array( $language_pairs ) ) { $language_pairs = array(); } else { $language_codes_from = array_keys( $language_pairs ); foreach ( $language_codes_from as $code_from ) { $language_codes_to = array_keys( $language_pairs[ $code_from ] ); foreach ( $language_codes_to as $code_to ) { if ( ! $this->is_valid_language_code_format( $code_to ) ) { unset( $language_pairs[ $code_from ][ $code_to ] ); } } if ( ! $this->is_valid_language_code_format( $code_from ) || ! count( $language_pairs[ $code_from ] ) ) { unset( $language_pairs[ $code_from ] ); } } } return $language_pairs; } /** * @param array $args @see \TranslationManagement::wpml_config_action */ private function update_section_translation_setting( $args ) { $section = $args['section']; $key = $args['key']; $value = $args['value']; $read_only = isset( $args['read_only'] ) ? $args['read_only'] : true; $section = preg_replace( '/-/', '_', $section ); $config_section = $this->get_translation_setting_name( $section ); $custom_config_readonly_section = $this->get_custom_readonly_translation_setting_name( $section ); if ( isset( $this->settings[ $config_section ] ) ) { $this->settings[ $config_section ][ esc_sql( $key ) ] = esc_sql( $value ); if ( ! isset( $this->settings[ $custom_config_readonly_section ] ) ) { $this->settings[ $custom_config_readonly_section ] = array(); } if ( $read_only === true && ! in_array( $key, $this->settings[ $custom_config_readonly_section ] ) ) { $this->settings[ $custom_config_readonly_section ][] = esc_sql( $key ); } $this->save_settings(); } } public function init() { $this->init_comments_synchronization(); $this->init_default_settings(); WPML_Config::load_config(); if ( is_admin() ) { 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' ) ); } // Add duplicate identifier actions. $this->wpml_add_duplicate_check_actions(); // default settings if ( empty( $this->settings['doc_translation_method'] ) || ! defined( 'WPML_TM_VERSION' ) ) { $this->settings['doc_translation_method'] = ICL_TM_TMETHOD_MANUAL; } } } public function get_settings() { $this->load_settings_if_required(); return $this->settings; } public function wpml_add_duplicate_check_actions() { global $pagenow; if ( 'post.php' === $pagenow || ( isset( $_POST['action'] ) && 'check_duplicate' === $_POST['action'] && DOING_AJAX ) ) { return new WPML_Translate_Independently(); } } public function wp_loaded() { if ( isset( $_POST['icl_tm_action'] ) ) { $this->process_request( $_POST ); } elseif ( isset( $_GET['icl_tm_action'] ) ) { $this->process_request( $_GET ); } } public function admin_enqueue_scripts( $hook ) { if ( ! defined( 'WPML_TM_FOLDER' ) ) { return; } $menus_default_tab = array( 'wpml_page_' . WPML_TM_FOLDER . '/menu/main' => 'dashboard', 'wpml_page_' . WPML_TM_FOLDER . '/menu/settings' => 'mcsetup', ); $submenu = filter_input( INPUT_GET, 'sm' ); if ( ! array_key_exists( $hook, $menus_default_tab ) && ! $submenu ) { return; } if ( ! $submenu ) { $submenu = $menus_default_tab[ $hook ]; } switch ( $submenu ) { case 'jobs': wp_register_script( 'translation-remote-jobs', WPML_TM_URL . '/dist/js/jobs/app.js', array(), false, true ); wp_enqueue_script( 'translation-remote-jobs' ); wp_register_style( 'translation-remote-jobs', WPML_TM_URL . '/res/css/translation-jobs.css', array(), WPML_TM_VERSION ); wp_enqueue_style( 'translation-remote-jobs' ); wp_enqueue_script( OTGS_Assets_Handles::POPOVER_TOOLTIP ); wp_enqueue_style( OTGS_Assets_Handles::POPOVER_TOOLTIP ); break; case 'basket': wp_register_style( 'translation-basket', WPML_TM_URL . '/res/css/translation-basket.css', array(), WPML_TM_VERSION ); wp_enqueue_style( 'translation-basket' ); break; case 'translators': wp_register_style( 'translation-translators', WPML_TM_URL . '/res/css/translation-translators.css', array( 'otgs-ico' ), WPML_TM_VERSION ); wp_enqueue_style( 'translation-translators' ); break; case 'mcsetup': wp_register_style( 'sitepress-translation-options', ICL_PLUGIN_URL . '/res/css/translation-options.css', array(), WPML_TM_VERSION ); wp_enqueue_style( 'sitepress-translation-options' ); break; default: wp_register_style( 'translation-dashboard', WPML_TM_URL . '/res/css/translation-dashboard.css', array(), WPML_TM_VERSION ); wp_enqueue_style( 'translation-dashboard' ); wp_register_style( 'translation-translators', WPML_TM_URL . '/res/css/translation-translators.css', array( 'otgs-ico' ), WPML_TM_VERSION ); wp_enqueue_style( 'translation-translators' ); } } public static function get_batch_name( $batch_id ) { $batch_data = self::get_batch_data( $batch_id ); if ( ! $batch_data || ! isset( $batch_data->batch_name ) ) { $batch_name = __( 'No Batch', 'sitepress' ); } else { $batch_name = $batch_data->batch_name; } return $batch_name; } public static function get_batch_url( $batch_id ) { $batch_data = self::get_batch_data( $batch_id ); $batch_url = ''; if ( $batch_data && isset( $batch_data->tp_id ) && $batch_data->tp_id != 0 ) { $batch_url = OTG_TRANSLATION_PROXY_URL . "/projects/{$batch_data->tp_id}/external"; } return $batch_url; } public static function get_batch_last_update( $batch_id ) { $batch_data = self::get_batch_data( $batch_id ); return $batch_data ? $batch_data->last_update : false; } public static function get_batch_tp_id( $batch_id ) { $batch_data = self::get_batch_data( $batch_id ); return $batch_data ? $batch_data->tp_id : false; } public static function get_batch_data( $batch_id ) { $cache_key = $batch_id; $cache_group = 'get_batch_data'; $cache_found = false; $batch_data = wp_cache_get( $cache_key, $cache_group, false, $cache_found ); if ( $cache_found ) { return $batch_data; } global $wpdb; $batch_data_sql = "SELECT * FROM {$wpdb->prefix}icl_translation_batches WHERE id=%d"; $batch_data_prepared = $wpdb->prepare( $batch_data_sql, array( $batch_id ) ); $batch_data = $wpdb->get_row( $batch_data_prepared ); wp_cache_set( $cache_key, $batch_data, $cache_group ); return $batch_data; } function save_settings() { global $sitepress; $icl_settings['translation-management'] = $this->settings; $cpt_sync_option = $sitepress->get_setting( 'custom_posts_sync_option', array() ); $cpt_sync_option = (bool) $cpt_sync_option === false ? $sitepress->get_setting( 'custom-types_sync_option', array() ) : $cpt_sync_option; $cpt_unlock_options = $sitepress->get_setting( 'custom_posts_unlocked_option', array() ); if ( ! isset( $icl_settings['custom_posts_sync_option'] ) ) { $icl_settings['custom_posts_sync_option'] = array(); } foreach ( $cpt_sync_option as $k => $v ) { $icl_settings['custom_posts_sync_option'][ $k ] = $v; } $icl_settings['translation-management']['custom-types_readonly_config'] = isset( $icl_settings['translation-management']['custom-types_readonly_config'] ) ? $icl_settings['translation-management']['custom-types_readonly_config'] : array(); foreach ( $icl_settings['translation-management']['custom-types_readonly_config'] as $k => $v ) { if ( ! $this->is_unlocked_type( $k, $cpt_unlock_options ) ) { $icl_settings['custom_posts_sync_option'][ $k ] = $v; } } $sitepress->set_setting( 'translation-management', $icl_settings['translation-management'], true ); $sitepress->set_setting( 'custom_posts_sync_option', $icl_settings['custom_posts_sync_option'], true ); $this->settings = $sitepress->get_setting( 'translation-management' ); } /** * @return string[] */ public function initial_custom_field_translate_states() { global $wpdb; $this->initial_term_custom_field_translate_states(); return $this->initial_translation_states( $wpdb->postmeta ); } /** * @return string[] */ public function initial_term_custom_field_translate_states() { global $wpdb; return ! empty( $wpdb->termmeta ) ? $this->initial_translation_states( $wpdb->termmeta ) : array(); } function process_request( $data ) { $action = $data['icl_tm_action']; $data = stripslashes_deep( $data ); switch ( $action ) { case 'edit': $this->selected_translator->ID = intval( $data['user_id'] ); break; case 'dashboard_filter': $cookie_data = filter_var( http_build_query( $data['filter'] ), FILTER_SANITIZE_URL ); $this->set_cookie( 'wp-translation_dashboard_filter', $cookie_data, time() + HOUR_IN_SECONDS ); wp_safe_redirect( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=dashboard', 302, 'WPML' ); break; case 'reset_dashboard_filters': unset( $_COOKIE['wp-translation_dashboard_filter'] ); $this->set_cookie( 'wp-translation_dashboard_filter', '', time() - HOUR_IN_SECONDS ); break; case 'sort': $cookie_data = $this->get_cookie( 'wp-translation_dashboard_filter' ); if ( isset( $data['sort_by'] ) ) { $cookie_data['sort_by'] = $data['sort_by']; } if ( isset( $data['sort_order'] ) ) { $cookie_data['sort_order'] = $data['sort_order']; } $cookie_data = filter_var( http_build_query( $cookie_data ), FILTER_SANITIZE_URL ); $this->set_cookie( 'wp-translation_dashboard_filter', $cookie_data, time() + HOUR_IN_SECONDS ); wp_safe_redirect( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=dashboard', 302, 'WPML' ); break; case 'add_jobs': if ( isset( $data['iclnonce'] ) && wp_verify_nonce( $data['iclnonce'], 'pro-translation-icl' ) ) { TranslationProxy_Basket::add_posts_to_basket( $data ); do_action( 'wpml_tm_add_to_basket', $data ); } break; case 'ujobs_filter': $cookie_data = filter_var( http_build_query( $data['filter'] ), FILTER_SANITIZE_URL ); $_COOKIE['wp-translation_ujobs_filter'] = $cookie_data; $this->set_cookie( 'wp-translation_ujobs_filter', $cookie_data, time() + HOUR_IN_SECONDS ); wp_safe_redirect( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php', 302, 'WPML' ); break; case 'save_translation': if ( ! empty( $data['resign'] ) ) { $this->resign_translator( $data['job_id'] ); if ( wp_safe_redirect( admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&resigned=' . $data['job_id'] ), 302, 'WPML' ) ) { exit; } } else { do_action( 'wpml_save_translation_data', $data ); } break; case 'save_notification_settings': $this->icl_tm_save_notification_settings( $data ); break; case 'cancel_jobs': $this->icl_tm_cancel_jobs( $data ); break; } } /** * @param string $name * @param string $value * @param int $expiration */ private function set_cookie( $name, $value, $expiration ) { $this->wpml_cookie->set_cookie( $name, $value, $expiration, COOKIEPATH, COOKIE_DOMAIN ); } /** * @param string $name * * @return array */ private function get_cookie( $name ) { $result = []; $cookie = new WPML_Cookie(); $value = $cookie->get_cookie( $name ); parse_str( $value, $result ); return $result; } function ajax_calls( $call, $data ) { global $wpdb, $sitepress; switch ( $call ) { case 'assign_translator': $translator_data = TranslationProxy_Service::get_translator_data_from_wpml( $data['translator_id'] ); $service_id = $translator_data['translation_service']; $translator_id = $translator_data['translator_id']; $assign_translation_job = $this->assign_translation_job( $data['job_id'], $translator_id, $service_id, $data['job_type'] ); if ( $assign_translation_job ) { $translator_edit_link = ''; if ( $translator_id ) { if ( $service_id == TranslationProxy::get_current_service_id() ) { $job = $this->get_translation_job( $data['job_id'] ); /** @var WPML_Pro_Translation $ICL_Pro_Translation */ global $ICL_Pro_Translation; $ICL_Pro_Translation->send_post( $job->original_doc_id, array( $job->language_code ), $translator_id, $data['job_id'] ); $project = TranslationProxy::get_current_project(); $translator_edit_link = TranslationProxy_Popup::get_link( $project->translator_contact_iframe_url( $translator_id ), array( 'title' => __( 'Contact the translator', 'sitepress' ), 'unload_cb' => 'icl_thickbox_refresh', ) ) . esc_html( TranslationProxy_Translator::get_translator_name( $translator_id ) ) . " ($project->service->name)"; } else { $translator_edit_link = '' . esc_html( $wpdb->get_var( $wpdb->prepare( "SELECT display_name FROM {$wpdb->users} WHERE ID=%d", $data['translator_id'] ) ) ) . ''; } } echo wp_json_encode( array( 'error' => 0, 'message' => $translator_edit_link, 'status' => self::status2text( ICL_TM_WAITING_FOR_TRANSLATOR ), 'service' => $service_id, ) ); } else { echo wp_json_encode( array( 'error' => 1 ) ); } break; case 'icl_cf_translation': case 'icl_tcf_translation': foreach ( array( 'cf' => $call === 'icl_tcf_translation' ? WPML_TERM_META_SETTING_INDEX_PLURAL : WPML_POST_META_SETTING_INDEX_PLURAL, 'cf_unlocked' => $call === 'icl_tcf_translation' ? WPML_TERM_META_UNLOCKED_SETTING_INDEX : WPML_POST_META_UNLOCKED_SETTING_INDEX, ) as $field => $setting ) { if ( ! empty( $data[ $field ] ) ) { $cft = array(); foreach ( $data[ $field ] as $k => $v ) { $cft[ base64_decode( $k ) ] = $v; } if ( ! empty( $cft ) ) { if ( ! isset( $this->settings[ $setting ] ) ) { $this->settings[ $setting ] = array(); } $this->settings[ $setting ] = array_merge( $this->settings[ $setting ], $cft ); $this->save_settings(); /** * Fires after update of custom fields synchronisation preferences in WPML > Settings */ do_action( 'wpml_custom_fields_sync_option_updated', $cft ); } } } echo '1|'; break; case 'icl_doc_translation_method': $this->settings['doc_translation_method'] = Obj::prop( 't_method', $data ); $sitepress->set_setting( 'doc_translation_method', $this->settings['doc_translation_method'] ); $sitepress->save_settings( array( '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', '' ); } if ( isset( $data['translation_memory'] ) ) { $sitepress->set_setting( 'translation_memory', $data['translation_memory'] ); } $this->save_settings(); echo '1|'; break; case 'reset_duplication': $this->reset_duplicate_flag( $_POST['post_id'] ); break; case 'set_duplication': $new_id = $this->set_duplicate( $_POST['wpml_original_post_id'], $_POST['post_lang'] ); wp_send_json_success( array( 'id' => $new_id ) ); break; } } /** * @param string $element_type_full * * @return mixed */ public function get_element_prefix( $element_type_full ) { $element_type_parts = explode( '_', $element_type_full ); $element_type = $element_type_parts[0]; return $element_type; } /** * @param int $job_id * * @return mixed */ public function get_element_type_prefix_from_job_id( $job_id ) { $job = $this->get_translation_job( $job_id ); if ( isset( $job->element_type_prefix ) ) { return $job->element_type_prefix; } return $job ? $this->get_element_type_prefix_from_job( $job ) : false; } /** * @param \stdClass $job * * @return mixed */ public function get_element_type_prefix_from_job( $job ) { if ( is_object( $job ) ) { $element_type = $this->get_element_type( $job->trid ); $element_type_prefix = $this->get_element_prefix( $element_type ); } else { $element_type_prefix = false; } return $element_type_prefix; } /** * Display admin notices. */ public function show_messages() { foreach ( $this->message_ids as $message_suffix ) { $message_id = 'icl_tm_message_' . $message_suffix; $message = ICL_AdminNotifier::get_message( $message_id ); if ( isset( $message['type'], $message['text'] ) ) { echo '
'; ICL_AdminNotifier::remove_message( $message_id ); } } } /* TRANSLATORS */ /** * @deprecated use `WPML_TM_Blog_Translators::get_blog_translators` instead * * @return bool */ public function has_translators() { if ( function_exists( 'wpml_tm_load_blog_translators' ) ) { return wpml_tm_load_blog_translators()->has_translators(); } return false; } /** * @deprecated use `WPML_TM_Blog_Translators::get_blog_translators` instead * * @param array $args * * @return array */ public static function get_blog_translators( $args = array() ) { $translators = array(); if ( function_exists( 'wpml_tm_load_blog_translators' ) ) { $translators = wpml_tm_load_blog_translators()->get_blog_translators( $args ); } return $translators; } /** * @return WPML_Translator */ function get_selected_translator() { global $wpdb; if ( $this->selected_translator && $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 $this->selected_translator; } /** * @return WPML_Translator */ function get_current_translator() { $current_translator = $this->current_translator; $current_translator_is_set = $current_translator && $current_translator->ID > 0 && $current_translator->language_pairs; if ( ! $current_translator_is_set ) { $this->init_current_translator(); } return $this->current_translator; } public static 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; } /* HOOKS */ 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 ) { global $sitepress; return $sitepress->make_duplicate( $master_post_id, $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_details_original->language_code ) { $data['duplicate_to'][ $lang ] = 1; } } $this->make_duplicates( $data ); } function reset_duplicate_flag( $post_id ) { global $sitepress; $post = get_post( $post_id ); $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type ); $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type ); foreach ( $translations as $tr ) { if ( $tr->element_id == $post_id ) { $this->update_translation_status( array( 'translation_id' => $tr->translation_id, 'status' => ICL_TM_COMPLETE, ) ); } } delete_post_meta( $post_id, '_icl_lang_duplicate_of' ); } function set_duplicate( $master_post_id, $post_lang ) { $new_id = 0; if ( $master_post_id && $post_lang ) { $new_id = $this->make_duplicate( $master_post_id, $post_lang ); } return $new_id; } function duplication_delete_comment( $comment_id ) { global $wpdb; $original_comment = (bool) get_comment_meta( $comment_id, '_icl_duplicate_of', true ) === false; if ( $original_comment ) { $duplicates = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key='_icl_duplicate_of' AND meta_value=%d", $comment_id ) ); foreach ( $duplicates as $dup ) { wp_delete_comment( $dup, true ); } } } function duplication_edit_comment( $comment_id ) { global $wpdb; $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID=%d", $comment_id ), ARRAY_A ); unset( $comment['comment_ID'], $comment['comment_post_ID'] ); $comment_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->commentmeta} WHERE comment_id=%d AND meta_key <> '_icl_duplicate_of'", $comment_id ) ); $original_comment = get_comment_meta( $comment_id, '_icl_duplicate_of', true ); if ( $original_comment ) { $duplicates = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key='_icl_duplicate_of' AND meta_value=%d", $original_comment ) ); $duplicates = array( $original_comment ) + array_diff( $duplicates, array( $comment_id ) ); } else { $duplicates = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key='_icl_duplicate_of' AND meta_value=%d", $comment_id ) ); } if ( ! empty( $duplicates ) ) { foreach ( $duplicates as $dup ) { $wpdb->update( $wpdb->comments, $comment, array( 'comment_ID' => $dup ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id=%d AND meta_key <> '_icl_duplicate_of'", $dup ) ); if ( $comment_meta ) { foreach ( $comment_meta as $key => $value ) { wp_cache_delete( $dup, 'comment_meta' ); update_comment_meta( $dup, $value->meta_key, $value->meta_value ); } } } } } function duplication_status_comment( $comment_id, $comment_status ) { global $wpdb; static $_avoid_8_loop; if ( isset( $_avoid_8_loop ) ) { return; } $_avoid_8_loop = true; $original_comment = get_comment_meta( $comment_id, '_icl_duplicate_of', true ); if ( $original_comment ) { $duplicates = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key='_icl_duplicate_of' AND meta_value=%d", $original_comment ) ); $duplicates = array( $original_comment ) + array_diff( $duplicates, array( $comment_id ) ); } else { $duplicates = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key='_icl_duplicate_of' AND meta_value=%d", $comment_id ) ); } if ( ! empty( $duplicates ) ) { foreach ( $duplicates as $duplicate ) { wp_set_comment_status( $duplicate, $comment_status ); } } unset( $_avoid_8_loop ); } function duplication_insert_comment( $comment_id ) { global $wpdb, $sitepress; $duplicator = $this->get_comment_duplicator(); $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID=%d", $comment_id ), ARRAY_A ); // loop duplicate posts, add new comment $post_id = $comment['comment_post_ID']; // if this is a duplicate post $duplicate_of = get_post_meta( $post_id, '_icl_lang_duplicate_of', true ); if ( $duplicate_of ) { $post_duplicates = $sitepress->get_duplicates( $duplicate_of ); $duplicator->move_to_original( $duplicate_of, $post_duplicates, $comment ); $this->duplication_insert_comment( $comment_id ); return; } else { $post_duplicates = $sitepress->get_duplicates( $post_id ); } unset( $comment['comment_ID'], $comment['comment_post_ID'] ); foreach ( $post_duplicates as $lang => $dup_id ) { $comment['comment_post_ID'] = $dup_id; if ( $comment['comment_parent'] ) { $translated_parent = $duplicator->get_correct_parent( $comment, $dup_id ); if ( ! $translated_parent ) { $this->duplication_insert_comment( $comment['comment_parent'] ); $translated_parent = $duplicator->get_correct_parent( $comment, $dup_id ); } $comment['comment_parent'] = $translated_parent; } $duplicator->insert_duplicated_comment( $comment, $dup_id, $comment_id ); } } private function get_comment_duplicator() { if ( ! $this->comment_duplicator ) { require WPML_PLUGIN_PATH . '/inc/post-translation/wpml-comment-duplication.class.php'; $this->comment_duplicator = new WPML_Comment_Duplication(); } return $this->comment_duplicator; } /** * @param int $post_id Post ID. */ public function delete_post_actions( $post_id ) { global $wpdb; $post_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $post_id ) ); if ( ! empty( $post_type ) ) { $trid_subquery = $wpdb->prepare( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", $post_id, 'post_' . $post_type ); $translation_ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE trid = (" . $trid_subquery . ')' ); if ( $translation_ids ) { $rids = $wpdb->get_col( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "SELECT rid FROM {$wpdb->prefix}icl_translation_status WHERE translation_id IN (" . wpml_prepare_in( $translation_ids, '%d' ) . ')' ); $wpdb->query( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "DELETE FROM {$wpdb->prefix}icl_translation_status WHERE translation_id IN (" . wpml_prepare_in( $translation_ids, '%d' ) . ')' ); if ( $rids ) { $job_ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "SELECT job_id FROM {$wpdb->prefix}icl_translate_job WHERE rid IN (" . wpml_prepare_in( $rids, '%d' ) . ')' ); $wpdb->query( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "DELETE FROM {$wpdb->prefix}icl_translate_job WHERE rid IN (" . wpml_prepare_in( $rids, '%d' ) . ')' ); if ( $job_ids ) { $wpdb->query( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "DELETE FROM {$wpdb->prefix}icl_translate WHERE job_id IN (" . wpml_prepare_in( $job_ids, '%d' ) . ')' ); } } } } } /* TRANSLATIONS */ /** * calculate post md5 * * @param object|int $post * * @return string */ function post_md5( $post ) { return apply_filters( 'wpml_tm_element_md5', $post ); } function get_element_translation( $element_id, $language, $element_type = 'post_post' ) { global $wpdb, $sitepress; $trid = $sitepress->get_element_trid( $element_id, $element_type ); $translation = array(); if ( $trid ) { $translation = $wpdb->get_row( $wpdb->prepare( " SELECT * FROM {$wpdb->prefix}icl_translations tr JOIN {$wpdb->prefix}icl_translation_status ts ON tr.translation_id = ts.translation_id WHERE tr.trid=%d AND tr.language_code= %s ", $trid, $language ) ); } return $translation; } function get_element_translations( $element_id, $element_type = 'post_post', $service = false ) { global $wpdb, $sitepress; $trid = $sitepress->get_element_trid( $element_id, $element_type ); $translations = array(); if ( $trid ) { $service = $service ? $wpdb->prepare( ' AND translation_service = %s ', $service ) : ''; $translations = $wpdb->get_results( $wpdb->prepare( " SELECT * FROM {$wpdb->prefix}icl_translations tr JOIN {$wpdb->prefix}icl_translation_status ts ON tr.translation_id = ts.translation_id WHERE tr.trid=%d {$service} ", $trid ) ); foreach ( $translations as $k => $v ) { $translations[ $v->language_code ] = $v; unset( $translations[ $k ] ); } } return $translations; } /** * returns icon class according to status code * * @param int $status * @param int $needs_update * * @return string */ public function status2icon_class( $status, $needs_update = 0 ) { if ( $needs_update ) { $icon_class = 'otgs-ico-needs-update'; } else { switch ( $status ) { case ICL_TM_NOT_TRANSLATED: $icon_class = 'otgs-ico-not-translated'; break; case ICL_TM_WAITING_FOR_TRANSLATOR: $icon_class = 'otgs-ico-waiting'; break; case ICL_TM_IN_PROGRESS: case ICL_TM_TRANSLATION_READY_TO_DOWNLOAD: $icon_class = 'otgs-ico-in-progress'; break; case ICL_TM_IN_BASKET: $icon_class = 'otgs-ico-basket'; break; case ICL_TM_NEEDS_UPDATE: $icon_class = 'otgs-ico-needs-update'; break; case ICL_TM_DUPLICATE: $icon_class = 'otgs-ico-duplicate'; break; case ICL_TM_COMPLETE: $icon_class = 'otgs-ico-translated'; break; default: $icon_class = 'otgs-ico-not-translated'; } } return $icon_class; } public static function status2text( $status ) { switch ( $status ) { case ICL_TM_NOT_TRANSLATED: $text = __( 'Not translated', 'sitepress' ); break; case ICL_TM_WAITING_FOR_TRANSLATOR: $text = __( 'Waiting for translator', 'sitepress' ); break; case ICL_TM_IN_PROGRESS: $text = __( 'In progress', 'sitepress' ); break; case ICL_TM_NEEDS_UPDATE: $text = __( 'Needs update', 'sitepress' ); break; case ICL_TM_DUPLICATE: $text = __( 'Duplicate', 'sitepress' ); break; case ICL_TM_COMPLETE: $text = __( 'Complete', 'sitepress' ); break; case ICL_TM_TRANSLATION_READY_TO_DOWNLOAD: $text = __( 'Translation ready to download', 'sitepress' ); break; default: $text = ''; } return $text; } public function decode_field_data( $data, $format ) { if ( $format == 'base64' ) { $data = base64_decode( $data ); } elseif ( $format == 'csv_base64' ) { $exp = explode( ',', $data ); foreach ( $exp as $k => $e ) { $exp[ $k ] = base64_decode( trim( $e, '"' ) ); } $data = $exp; } return $data; } /** * create translation package * * @param object|int $post * * @return array|false */ function create_translation_package( $post ) { return Maybe::fromNullable( make( 'WPML_Element_Translation_Package' ) ) ->map( invoke( 'create_translation_package' )->with( $post ) ) ->getOrElse( false ); } function messages_by_type( $type ) { $messages = $this->messages; $result = false; foreach ( $messages as $message ) { if ( $type === false || ( ! empty( $message['type'] ) && $message['type'] == $type ) ) { $result[] = $message; } } return $result; } public function add_basket_message( $type, $message, $id = null ) { $message = array( 'type' => $type, 'text' => $message, ); if ( $id ) { $message['id'] = $id; } $this->add_message( $message ); } function add_message( $message ) { $this->messages[] = $message; $this->messages = array_unique( $this->messages, SORT_REGULAR ); } /** * add/update icl_translation_status record * * @param array $data * @param int $rid * * @return array */ function update_translation_status( $data, $rid = null ) { global $wpdb; if ( ! isset( $data['translation_id'] ) ) { return array( false, false ); } if ( ! $rid ) { $rid = $this->get_rid_from_translation_id( $data['translation_id'] ); } $update = (bool) $rid; if ( true === $update ) { $data_where = array( 'rid' => $rid ); $wpdb->update( $wpdb->prefix . 'icl_translation_status', $data, $data_where ); } else { $wpdb->insert( $wpdb->prefix . 'icl_translation_status', $data ); $rid = $wpdb->insert_id; } $data['rid'] = $rid; do_action( 'wpml_updated_translation_status', $data ); return array( $rid, $update ); } /** * @param int $translation_id * * @return int */ private function get_rid_from_translation_id( $translation_id ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT rid FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation_id ) ); } /* TRANSLATION JOBS */ /** * @param \WPML_TM_Translation_Batch $batch * @param string $type * * @return array */ function send_jobs( $batch, $type = 'post' ) { /** * `\TranslationManagement::send_jobs` is in Core, and requires an instance of \WPML_TM_Translation_Batch * which is defined in TM. * * We should move this code to TM instead. * * Until then, to prevent tests from failing: * * - We remove the type-hint from the method's signature * - We compensate by using the following check */ if ( ! is_a( $batch, '\WPML_TM_Translation_Batch' ) ) { throw new InvalidArgumentException( '$batch must be an instance of \WPML_TM_Translation_Batch' ); } global $sitepress; $job_ids = array(); $added_jobs = array(); $batch_id = TranslationProxy_Batch::update_translation_batch( $batch->get_basket_name() ); /** * Allows to filter the translation batch * * @since 4.3.0 * * @param \WPML_TM_Translation_Batch $batch */ $batch = apply_filters( 'wpml_send_jobs_batch', $batch ); foreach ( $batch->get_elements_by_type( $type ) as $element ) { $post = $this->get_post( $element->get_element_id(), $type ); if ( ! $post ) { continue; } if ( $post instanceof \WP_Post ) { /** * Registers strings coming from page builder shortcodes * * @param \WP_Post * * @since 4.3.16 */ do_action( 'wpml_pb_register_all_strings_for_translation', $post ); } $element_type = $type . '_' . $post->post_type; $post_trid = $sitepress->get_element_trid( $element->get_element_id(), $element_type ); $post_translations = $sitepress->get_element_translations( $post_trid, $element_type ); $md5 = $this->post_md5( $post ); $translation_package = $this->create_translation_package( $post ); foreach ( $element->get_target_langs() as $lang => $action ) { if ( $action == self::DUPLICATE_ELEMENT_ACTION ) { // don't send documents that are in progress $current_translation_status = $this->get_element_translation( $element->get_element_id(), $lang, $element_type ); if ( $current_translation_status && $current_translation_status->status == ICL_TM_IN_PROGRESS ) { continue; } $job_ids[] = $this->make_duplicate( $element->get_element_id(), $lang ); } elseif ( $action == self::TRANSLATE_ELEMENT_ACTION ) { // INSERT DATA TO icl_translations if ( empty( $post_translations[ $lang ] ) ) { $translation_id = $sitepress->set_element_language_details( null, $element_type, $post_trid, $lang, $element->get_source_lang() ); } else { $translation_id = $post_translations[ $lang ]->translation_id; $sitepress->set_element_language_details( $post_translations[ $lang ]->element_id, $element_type, $post_trid, $lang, $element->get_source_lang() ); } $current_translation_status = $this->get_element_translation( $element->get_element_id(), $lang, $element_type ); if ( $current_translation_status ) { if ( $current_translation_status->status == ICL_TM_IN_PROGRESS ) { $this->cancel_previous_job_if_in_progress( $translation_id ); } else { $this->cancel_previous_job_if_still_waiting( $translation_id, $current_translation_status->status ); } } $_status = ICL_TM_WAITING_FOR_TRANSLATOR; $translator = $batch->get_translator( $lang ); $translation_data = TranslationProxy_Service::get_translator_data_from_wpml( $translator ); $translator_id = $translation_data['translator_id']; $translation_service = $translation_data['translation_service']; // add translation_status record $data = array( 'translation_id' => $translation_id, 'status' => $_status, 'translator_id' => $translator_id, 'needs_update' => 0, 'md5' => $md5, 'translation_service' => $translation_service, 'translation_package' => serialize( $translation_package ), 'batch_id' => $batch_id, 'uuid' => $this->get_uuid( $current_translation_status, $post ), 'ts_status' => null, 'timestamp' => date( 'Y-m-d H:i:s', time() ), ); $backup_translation_status = $this->get_translation_status_data( $translation_id ); $prevstate = $this->get_translation_prev_state( $backup_translation_status ); if ( $prevstate ) { $data['_prevstate'] = serialize( $prevstate ); } $rid = isset( $backup_translation_status['rid'] ) ? $backup_translation_status['rid'] : null; list( $rid ) = $this->update_translation_status( $data, $rid ); $job_id = $this->add_translation_job( $rid, $translator_id, $translation_package, $batch->get_batch_options() ); wpml_tm_load_job_factory()->update_job_data( $job_id, array( 'editor' => WPML_TM_Editors::NONE ) ); $job_ids[] = $job_id; if ( $translation_service !== 'local' ) { /** @global WPML_Pro_Translation $ICL_Pro_Translation */ global $ICL_Pro_Translation; $tp_job_id = $ICL_Pro_Translation->send_post( $post, array( $lang ), $translator_id, $job_id ); if ( ! $tp_job_id ) { $this->revert_job_when_tp_job_could_not_be_created( $job_ids, $rid, $data['translation_id'], $backup_translation_status ); } // save associated TP JOB ID $this->update_translation_status( array( 'translation_id' => $translation_id, 'tp_id' => $tp_job_id, ), $rid ); } $added_jobs[ $translation_service ][] = $job_id; } /** * @param WPML_TM_Translation_Batch_Element $element * @param mixed $post * @since 4.4.0 */ do_action( 'wpml_tm_added_translation_element', $element, $post ); } } do_action( 'wpml_added_translation_jobs', $added_jobs ); icl_cache_clear(); do_action( 'wpml_tm_empty_mail_queue' ); return $job_ids; } private function revert_job_when_tp_job_could_not_be_created( $job_ids, $rid, $translator_id, $backup_translation_status ) { /** @global WPML_Pro_Translation $ICL_Pro_Translation */ global $wpdb, $ICL_Pro_Translation; $job_id = array_pop( $job_ids ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $job_id ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}icl_translate_job SET revision = NULL WHERE rid=%d ORDER BY job_id DESC LIMIT 1", $rid ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}icl_translate WHERE job_id=%d", $job_id ) ); if ( $backup_translation_status ) { $wpdb->update( "{$wpdb->prefix}icl_translation_status", $backup_translation_status, [ 'translation_id' => $translator_id ] ); } else { $wpdb->delete( "{$wpdb->prefix}icl_translation_status", [ 'translation_id' => $translator_id ] ); } foreach ( $ICL_Pro_Translation->errors as $error ) { if ( $error instanceof Exception ) { /** @var Exception $error */ $message = [ 'type' => 'error', 'text' => $error->getMessage(), ]; $this->add_message( $message ); } } } /** * @param stdClass|null $current_translation_status * @param WP_Post|WPML_Package $post * * @return string */ private function get_uuid( $current_translation_status, $post ) { if ( ! empty( $current_translation_status->uuid ) ) { return $current_translation_status->uuid; } else { return wpml_uuid( $post->ID, $post->post_type ); } } private function get_translation_status_data( $translation_id ) { global $wpdb; $data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation_id ), ARRAY_A ); return isset( $data[0] ) ? $data[0] : array(); } /** * @param string $translation_id * @param string $status */ private function cancel_previous_job_if_still_waiting( $translation_id, $status ) { if ( ICL_TM_WAITING_FOR_TRANSLATOR === (int) $status ) { $this->cancel_translation_request( $translation_id, false ); } } private function cancel_previous_job_if_in_progress( $translation_id ) { global $wpdb; $sql = " SELECT j.job_id FROM {$wpdb->prefix}icl_translate_job j INNER JOIN {$wpdb->prefix}icl_translation_status ts ON ts.rid = j.rid WHERE ts.translation_id = %d AND ts.status = %s ORDER BY job_id DESC "; $job_id = (int) $wpdb->get_var( $wpdb->prepare( $sql, $translation_id, ICL_TM_IN_PROGRESS ) ); if ( ! $job_id ) { return; } $wpdb->update( $wpdb->prefix . 'icl_translate_job', array( 'translated' => 1 ), array( 'job_id' => $job_id ) ); } /** * Adds a translation job record in icl_translate_job * * @param mixed $rid * @param mixed $translator_id * @param array