first commit
This commit is contained in:
@@ -0,0 +1,451 @@
|
||||
<?php
|
||||
|
||||
class WPML_Save_Translation_Data_Action extends WPML_Translation_Job_Helper_With_API {
|
||||
|
||||
/** @var WPML_TM_Records $tm_records */
|
||||
private $tm_records;
|
||||
|
||||
/** @var array $data */
|
||||
private $data;
|
||||
|
||||
private $redirect_target = false;
|
||||
private $translate_link_targets_in_posts;
|
||||
private $translate_link_targets_in_strings;
|
||||
|
||||
public function __construct( $data, $tm_records ) {
|
||||
global $wpdb, $ICL_Pro_Translation, $sitepress;
|
||||
parent::__construct();
|
||||
$this->data = $data;
|
||||
$this->tm_records = $tm_records;
|
||||
$translate_link_targets_global_state = new WPML_Translate_Link_Target_Global_State( $sitepress );
|
||||
$this->translate_link_targets_in_posts = new WPML_Translate_Link_Targets_In_Posts( $translate_link_targets_global_state, $wpdb, $ICL_Pro_Translation );
|
||||
$this->translate_link_targets_in_strings = new WPML_Translate_Link_Targets_In_Strings( $translate_link_targets_global_state, $wpdb, new WPML_WP_API(), $ICL_Pro_Translation );
|
||||
|
||||
}
|
||||
|
||||
function save_translation() {
|
||||
global $wpdb, $sitepress, $iclTranslationManagement, $wpml_post_translations;
|
||||
|
||||
$new_post_id = false;
|
||||
$is_incomplete = false;
|
||||
$data = $this->data;
|
||||
/** @var stdClass $job */
|
||||
$job = ! empty( $data['job_id'] ) ? $this->get_translation_job( $data['job_id'], true ) : null;
|
||||
$needs_second_update = $job && $job->needs_update ? 1 : 0;
|
||||
$original_post = null;
|
||||
$element_type_prefix = null;
|
||||
if ( is_object( $job ) ) {
|
||||
$element_type_prefix = $iclTranslationManagement->get_element_type_prefix_from_job( $job );
|
||||
$original_post = $iclTranslationManagement->get_post( $job->original_doc_id, $element_type_prefix );
|
||||
}
|
||||
|
||||
$is_external = apply_filters( 'wpml_is_external', false, $element_type_prefix );
|
||||
$data_to_validate = array(
|
||||
'original_post' => $original_post,
|
||||
'type_prefix' => $element_type_prefix,
|
||||
'data' => $data,
|
||||
'is_external' => $is_external,
|
||||
);
|
||||
|
||||
$validation_results = $this->get_validation_results( $job, $data_to_validate );
|
||||
|
||||
if ( ! $validation_results['is_valid'] ) {
|
||||
$this->handle_failed_validation( $validation_results, $data_to_validate );
|
||||
$res = false;
|
||||
} else {
|
||||
foreach ( $data['fields'] as $fieldname => $field ) {
|
||||
if ( substr( $fieldname, 0, 6 ) === 'field-' ) {
|
||||
$field = apply_filters( 'wpml_tm_save_translation_cf', $field, $fieldname, $data );
|
||||
}
|
||||
$this->save_translation_field( $field['tid'], $field );
|
||||
if ( ! isset( $field['finished'] ) || ! $field['finished'] ) {
|
||||
$is_incomplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
$icl_translate_job = $this->tm_records->icl_translate_job_by_job_id( $data['job_id'] );
|
||||
$rid = $icl_translate_job->rid();
|
||||
$translation_status = $this->tm_records->icl_translation_status_by_rid( $rid );
|
||||
$translation_id = $translation_status->translation_id();
|
||||
if ( ( $is_incomplete === true || empty( $data['complete'] ) ) && empty( $data['resign'] ) ) {
|
||||
$iclTranslationManagement->update_translation_status(
|
||||
array(
|
||||
'translation_id' => $translation_id,
|
||||
'status' => ICL_TM_IN_PROGRESS,
|
||||
)
|
||||
);
|
||||
$icl_translate_job->update( array( 'translated' => 0 ) );
|
||||
|
||||
self::notify_job_in_progress( $element_type_prefix, $job );
|
||||
}
|
||||
|
||||
$element_id = $translation_status->element_id();
|
||||
delete_post_meta( $element_id, '_icl_lang_duplicate_of' );
|
||||
|
||||
if ( ! empty( $data['complete'] ) && ! $is_incomplete ) {
|
||||
$icl_translate_job->update(
|
||||
array(
|
||||
'translated' => 1,
|
||||
'completed_date' => date( 'Y-m-d H:i:s' ),
|
||||
)
|
||||
);
|
||||
$job = $this->get_translation_job( $data['job_id'], true );
|
||||
|
||||
if ( $is_external ) {
|
||||
self::save_external( $element_type_prefix, $job, [ $this, 'decode_field_data' ] );
|
||||
} else {
|
||||
if ( $element_id ) {
|
||||
$postarr['ID'] = $_POST['post_ID'] = $element_id;
|
||||
} else {
|
||||
$postarr['post_status'] = ! $sitepress->get_setting( 'translated_document_status' ) ? 'draft' : $original_post->post_status;
|
||||
}
|
||||
|
||||
foreach ( $job->elements as $field ) {
|
||||
switch ( $field->field_type ) {
|
||||
case 'title':
|
||||
$postarr['post_title'] = $this->decode_field_data( $field->field_data_translated, $field->field_format );
|
||||
break;
|
||||
case 'body':
|
||||
$postarr['post_content'] = $this->decode_field_data(
|
||||
$field->field_data_translated,
|
||||
$field->field_format
|
||||
);
|
||||
break;
|
||||
case 'excerpt':
|
||||
$postarr['post_excerpt'] = $this->decode_field_data( $field->field_data_translated, $field->field_format );
|
||||
break;
|
||||
case 'URL':
|
||||
$postarr['post_name'] = $this->decode_field_data( $field->field_data_translated, $field->field_format );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$postarr['post_author'] = $original_post->post_author;
|
||||
$postarr['post_type'] = $original_post->post_type;
|
||||
|
||||
if ( $sitepress->get_setting( 'sync_comment_status' ) ) {
|
||||
$postarr['comment_status'] = $original_post->comment_status;
|
||||
}
|
||||
if ( $sitepress->get_setting( 'sync_ping_status' ) ) {
|
||||
$postarr['ping_status'] = $original_post->ping_status;
|
||||
}
|
||||
if ( $sitepress->get_setting( 'sync_page_ordering' ) ) {
|
||||
$postarr['menu_order'] = $original_post->menu_order;
|
||||
}
|
||||
if ( $sitepress->get_setting( 'sync_private_flag' ) && $original_post->post_status == 'private' ) {
|
||||
$postarr['post_status'] = 'private';
|
||||
}
|
||||
if ( $sitepress->get_setting( 'sync_password' ) && $original_post->post_password ) {
|
||||
$postarr['post_password'] = $original_post->post_password;
|
||||
}
|
||||
if ( $sitepress->get_setting( 'sync_post_date' ) ) {
|
||||
$postarr['post_date'] = $original_post->post_date;
|
||||
}
|
||||
|
||||
if ( $original_post->post_parent ) {
|
||||
$parent_id = $wpml_post_translations->element_id_in( $original_post->post_parent, $job->language_code );
|
||||
}
|
||||
|
||||
if ( isset( $parent_id ) && $sitepress->get_setting( 'sync_page_parent' ) ) {
|
||||
$_POST['post_parent'] = $postarr['post_parent'] = $parent_id;
|
||||
$_POST['parent_id'] = $postarr['parent_id'] = $parent_id;
|
||||
}
|
||||
|
||||
$_POST['trid'] = $translation_status->trid();
|
||||
$_POST['lang'] = $job->language_code;
|
||||
$_POST['skip_sitepress_actions'] = true;
|
||||
$_POST['needs_second_update'] = $needs_second_update;
|
||||
|
||||
/* @deprecated Use `wpml_pre_save_pro_translation` instead */
|
||||
$postarr = apply_filters( 'icl_pre_save_pro_translation', $postarr );
|
||||
|
||||
$postarr = apply_filters( 'wpml_pre_save_pro_translation', $postarr, $job );
|
||||
|
||||
// it's an update and user do not want to translate urls so do not change the url
|
||||
if ( $element_id ) {
|
||||
if ( $sitepress->get_setting( 'translated_document_page_url' ) !== 'translate' ) {
|
||||
$postarr['post_name'] = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT post_name
|
||||
FROM {$wpdb->posts}
|
||||
WHERE ID=%d
|
||||
LIMIT 1",
|
||||
$element_id
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$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 = wpml_get_create_post_helper()->insert_post( $postarr, $job->language_code );
|
||||
|
||||
icl_cache_clear( $postarr['post_type'] . 's_per_language' ); // clear post counter per language in cache
|
||||
|
||||
// set taxonomies for users with limited caps
|
||||
if ( ! current_user_can( 'manage-categories' ) && ! empty( $postarr['tax_input'] ) ) {
|
||||
foreach ( $postarr['tax_input'] as $taxonomy => $terms ) {
|
||||
wp_set_post_terms( $new_post_id, $terms, $taxonomy, false ); // true to append to existing tags | false to replace existing tags
|
||||
}
|
||||
}
|
||||
|
||||
$data['fields'] = apply_filters( 'wpml_tm_job_fields', $data['fields'], $job );
|
||||
|
||||
do_action( 'icl_pro_translation_saved', $new_post_id, $data['fields'], $job );
|
||||
do_action( 'wpml_translation_job_saved', $new_post_id, $data['fields'], $job );
|
||||
|
||||
// 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 $job_element ) {
|
||||
if ( $job_element->field_type === 'body' ) {
|
||||
$fields_data_translated = apply_filters( 'wpml_tm_job_data_post_content', $new_post_content );
|
||||
$fields_data_translated = $this->encode_field_data( $fields_data_translated );
|
||||
$wpdb->update(
|
||||
$wpdb->prefix . 'icl_translate',
|
||||
array( 'field_data_translated' => $fields_data_translated ),
|
||||
array(
|
||||
'job_id' => $data['job_id'],
|
||||
'field_type' => 'body',
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$sitepress->copy_custom_fields( $original_post->ID, $new_post_id );
|
||||
|
||||
// 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->get_setting( '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 );
|
||||
}
|
||||
}
|
||||
|
||||
$this->package_helper->save_job_custom_fields(
|
||||
$job,
|
||||
$new_post_id,
|
||||
\WPML\TM\Settings\Repository::getCustomFields()
|
||||
);
|
||||
|
||||
$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 ( ! $element_id ) {
|
||||
$wpdb->delete(
|
||||
$wpdb->prefix . 'icl_translations',
|
||||
array(
|
||||
'element_id' => $new_post_id,
|
||||
'element_type' => 'post_' . $postarr['post_type'],
|
||||
)
|
||||
);
|
||||
$wpdb->update( $wpdb->prefix . 'icl_translations', array( 'element_id' => $new_post_id ), array( 'translation_id' => $translation_id ) );
|
||||
$user_message = __( 'Translation added: ', 'wpml-translation-management' ) . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
|
||||
} else {
|
||||
$user_message = __( 'Translation updated: ', 'wpml-translation-management' ) . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
|
||||
}
|
||||
|
||||
// set stickiness
|
||||
// is the original post a sticky post?
|
||||
$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->get_setting( '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 stickiness has changed since the post was sent for translation
|
||||
}
|
||||
}
|
||||
|
||||
$this->add_message(
|
||||
array(
|
||||
'type' => 'updated',
|
||||
'text' => $user_message,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $this->get_tm_setting( array( 'notification', 'completed' ) ) != ICL_TM_NOTIFICATION_NONE
|
||||
&& $data['job_id']
|
||||
) {
|
||||
do_action( 'wpml_tm_complete_job_notification', $data['job_id'], ! is_null( $element_id ) );
|
||||
}
|
||||
|
||||
$iclTranslationManagement->set_page_url( $new_post_id );
|
||||
|
||||
if ( isset( $job ) && isset( $job->language_code ) && isset( $job->source_language_code ) ) {
|
||||
$this->save_terms_for_job( $data['job_id'] );
|
||||
}
|
||||
|
||||
// sync post format
|
||||
// Must be after save terms otherwise it gets lost.
|
||||
if ( $sitepress->get_setting( 'sync_post_format' ) ) {
|
||||
$_wp_post_format = get_post_format( $original_post->ID );
|
||||
set_post_format( $new_post_id, $_wp_post_format );
|
||||
}
|
||||
|
||||
do_action( 'icl_pro_translation_completed', $new_post_id, $data['fields'], $job );
|
||||
do_action( 'wpml_pro_translation_completed', $new_post_id, $data['fields'], $job );
|
||||
|
||||
$translation_status->update( [
|
||||
'status' => apply_filters( 'wpml_tm_applied_job_status', ICL_TM_COMPLETE, $job, $new_post_id ),
|
||||
'needs_update' => $needs_second_update,
|
||||
] );
|
||||
|
||||
$this->translate_link_targets_in_posts->new_content();
|
||||
$this->translate_link_targets_in_strings->new_content();
|
||||
|
||||
if ( ! defined( 'REST_REQUEST' ) && ! defined( 'XMLRPC_REQUEST' ) && ! defined( 'DOING_AJAX' ) && ! isset( $_POST['xliff_upload'] ) ) {
|
||||
$action_type = is_null( $element_id ) ? 'added' : 'updated';
|
||||
$element_id = is_null( $element_id ) ? $new_post_id : $element_id;
|
||||
$this->redirect_target = admin_url( sprintf( 'admin.php?page=%s&%s=%d&element_type=%s', WPML_TM_FOLDER . '/menu/translations-queue.php', $action_type, $element_id, $element_type_prefix ) );
|
||||
}
|
||||
} else {
|
||||
$this->add_message(
|
||||
array(
|
||||
'type' => 'updated',
|
||||
'text' => __( 'Translation (incomplete) saved.', 'wpml-translation-management' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$res = true;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false if after saving the translation no redirection is to happen or the target of the redirection
|
||||
* in case saving the data is followed by a redirect.
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
function get_redirect_target() {
|
||||
|
||||
return $this->redirect_target;
|
||||
}
|
||||
|
||||
private function save_translation_field( $tid, $field ) {
|
||||
global $wpdb;
|
||||
|
||||
$update = [];
|
||||
if ( isset( $field['data'] ) ) {
|
||||
$update['field_data_translated'] = $this->encode_field_data( $field['data'] );
|
||||
}
|
||||
$update['field_finished'] = isset( $field['finished'] ) && $field['finished'] ? 1 : 0;
|
||||
|
||||
$wpdb->update( $wpdb->prefix . 'icl_translate', $update, array( 'tid' => $tid ) );
|
||||
}
|
||||
|
||||
private function handle_failed_validation( $validation_results, $data_to_validate ) {
|
||||
if ( isset( $validation_results['messages'] ) ) {
|
||||
$messages = (array) $validation_results['messages'];
|
||||
if ( $messages ) {
|
||||
foreach ( $messages as $message ) {
|
||||
$this->add_message(
|
||||
array(
|
||||
'type' => 'error',
|
||||
'text' => $message,
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$this->add_message(
|
||||
array(
|
||||
'type' => 'error',
|
||||
'text' => __( 'Submitted data is not valid.', 'wpml-translation-management' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
do_action( 'wpml_translation_validation_failed', $validation_results, $data_to_validate );
|
||||
}
|
||||
|
||||
private function get_validation_results( $job, $data_to_validate ) {
|
||||
$is_valid = true;
|
||||
$original_post = $data_to_validate['original_post'];
|
||||
$element_type_prefix = $data_to_validate['type_prefix'];
|
||||
$validation_default_results = array(
|
||||
'is_valid' => $is_valid,
|
||||
'messages' => array(),
|
||||
);
|
||||
if ( ! $job || ! $original_post || ! $element_type_prefix ) {
|
||||
$is_valid = false;
|
||||
if ( ! $job ) {
|
||||
$validation_default_results['messages'][] = __( 'Job ID is missing', 'wpml-translation-management' );
|
||||
}
|
||||
if ( ! $original_post ) {
|
||||
$validation_default_results['messages'][] = __( 'The original post cannot be retrieved', 'wpml-translation-management' );
|
||||
}
|
||||
if ( ! $element_type_prefix ) {
|
||||
$validation_default_results['messages'][] = __( 'The type of the post cannot be retrieved', 'wpml-translation-management' );
|
||||
}
|
||||
} elseif ( ! $this->tm_records->icl_translate_job_by_job_id( $job->job_id )->is_open() ) {
|
||||
$is_valid = false;
|
||||
$validation_default_results['messages'][] = __( 'This job cannot be edited anymore because a newer job for this element exists.', 'wpml-translation-management' );
|
||||
}
|
||||
$validation_default_results['is_valid'] = $is_valid;
|
||||
$validation_results = apply_filters( 'wpml_translation_validation_data', $validation_default_results, $data_to_validate );
|
||||
$validation_results = array_merge( $validation_default_results, $validation_results );
|
||||
|
||||
if ( ! $is_valid && $validation_results['is_valid'] ) {
|
||||
$validation_results['is_valid'] = $is_valid;
|
||||
}
|
||||
|
||||
return $validation_results;
|
||||
}
|
||||
|
||||
private function save_terms_for_job( $job_id ) {
|
||||
require_once WPML_TM_PATH . '/inc/translation-jobs/wpml-translation-jobs-collection.class.php';
|
||||
|
||||
$job = new WPML_Post_Translation_Job( $job_id );
|
||||
$job->save_terms_to_post();
|
||||
}
|
||||
|
||||
private function add_message( $message ) {
|
||||
global $iclTranslationManagement;
|
||||
|
||||
$iclTranslationManagement->add_message( $message );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $element_type_prefix
|
||||
* @param object $job
|
||||
* @param callable $decoder
|
||||
*/
|
||||
private static function save_external( $element_type_prefix, $job, $decoder ) {
|
||||
do_action( 'wpml_save_external', $element_type_prefix, $job, $decoder );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $element_type_prefix
|
||||
* @param object $job
|
||||
*/
|
||||
private static function notify_job_in_progress( $element_type_prefix, $job ) {
|
||||
/**
|
||||
* The action triggered when a job is marked as in progress
|
||||
*
|
||||
* @param string $element_type_prefix
|
||||
* @param object $job
|
||||
* @since 2.10.0
|
||||
*/
|
||||
do_action( 'wpml_tm_job_in_progress', $element_type_prefix, $job );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Job_Helper_With_API extends WPML_Translation_Job_Helper {
|
||||
|
||||
/** @var WPML_Element_Translation_Package $package_helper */
|
||||
protected $package_helper;
|
||||
|
||||
function __construct() {
|
||||
$this->package_helper = new WPML_Element_Translation_Package();
|
||||
}
|
||||
|
||||
protected function get_translation_job( $job_id, $include_non_translatable_elements = false, $revisions = 0 ) {
|
||||
return wpml_tm_load_job_factory()->get_translation_job( $job_id, $include_non_translatable_elements, $revisions );
|
||||
}
|
||||
|
||||
protected function get_lang_by_rid( $rid ) {
|
||||
global $wpdb;
|
||||
|
||||
return $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT i.language_code
|
||||
FROM {$wpdb->prefix}icl_translations i
|
||||
JOIN {$wpdb->prefix}icl_translation_status s
|
||||
ON s.translation_id = i.translation_id
|
||||
WHERE s.rid = %d
|
||||
LIMIT 1",
|
||||
$rid
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
class WPML_Translation_Job_Helper {
|
||||
|
||||
public function encode_field_data( $data ) {
|
||||
return null === $data ? '' : base64_encode( $data );
|
||||
}
|
||||
|
||||
public function decode_field_data( $data, $format ) {
|
||||
return $this->get_core_translation_management()->decode_field_data( $data, $format );
|
||||
}
|
||||
|
||||
protected function get_tm_setting( $indexes ) {
|
||||
$core_tm = $this->get_core_translation_management();
|
||||
if ( empty( $core_tm->settings ) ) {
|
||||
$core_tm->init();
|
||||
}
|
||||
|
||||
$settings = $core_tm->get_settings();
|
||||
|
||||
foreach ( $indexes as $index ) {
|
||||
$settings = isset( $settings[ $index ] ) ? $settings[ $index ] : null;
|
||||
if ( ! isset( $settings ) ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TranslationManagement
|
||||
*/
|
||||
public static function get_core_translation_management() {
|
||||
/** TranslationManagement $iclTranslationManagement */
|
||||
global $iclTranslationManagement;
|
||||
|
||||
return $iclTranslationManagement;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
require_once WPML_TM_PATH . '/inc/translation-jobs/helpers/wpml-update-translation-data-action.class.php';
|
||||
|
||||
class WPML_TM_Update_External_Translation_Data_Action extends WPML_TM_Update_Translation_Data_Action {
|
||||
|
||||
protected function populate_prev_translation( $rid, array $package ) {
|
||||
list( $prev_job_id ) = $this->get_prev_job_data( $rid );
|
||||
|
||||
$prev_translation = [];
|
||||
$prev_job = $this->get_translation_job( $prev_job_id );
|
||||
/** @var stdClass $prev_job */
|
||||
if ( isset( $prev_job->original_doc_id ) ) {
|
||||
foreach ( $prev_job->elements as $element ) {
|
||||
$prev_translation[ $element->field_type ] = new WPML_TM_Translated_Field(
|
||||
$element->field_data,
|
||||
$element->field_data_translated,
|
||||
$element->field_finished
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'wpml_tm_populate_prev_translation', $prev_translation, $package, $this->get_lang_by_rid( $rid ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
require_once WPML_TM_PATH . '/inc/translation-jobs/helpers/wpml-update-translation-data-action.class.php';
|
||||
|
||||
class WPML_TM_Update_Post_Translation_Data_Action extends WPML_TM_Update_Translation_Data_Action {
|
||||
|
||||
protected function populate_prev_translation( $rid, array $package ) {
|
||||
global $wpml_post_translations;
|
||||
|
||||
$prev_translation = array();
|
||||
if ( (bool) ( $lang = $this->get_lang_by_rid( $rid ) ) === true ) {
|
||||
list( $prev_job_id ) = $this->get_prev_job_data( $rid );
|
||||
$prev_job = $this->get_translation_job( $prev_job_id );
|
||||
|
||||
if ( $prev_job ) {
|
||||
foreach ( $package['contents'] as $field_name => $field ) {
|
||||
if ( array_key_exists( 'translate', $field ) && $field['translate'] ) {
|
||||
$element = $this->get_previous_element( $prev_job, $field_name );
|
||||
if ( $element ) {
|
||||
$prev_translation[ $field_name ] = new WPML_TM_Translated_Field(
|
||||
$element->field_data,
|
||||
$element->field_data_translated,
|
||||
$element->field_finished
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$translated_post_id = $wpml_post_translations->element_id_in( $package['contents']['original_id']['data'], $lang );
|
||||
if ( $translated_post_id ) {
|
||||
$package_trans = $this->package_helper->create_translation_package( $translated_post_id );
|
||||
$translated_contents = $package_trans['contents'];
|
||||
|
||||
foreach ( $package['contents'] as $field_name => $field ) {
|
||||
if ( ! array_key_exists( $field_name, $prev_translation )
|
||||
&& array_key_exists( $field_name, $translated_contents )
|
||||
&& array_key_exists( 'data', $translated_contents[ $field_name ] )
|
||||
) {
|
||||
$prev_translation[ $field_name ] = new WPML_TM_Translated_Field(
|
||||
'',
|
||||
$translated_contents[ $field_name ]['data'],
|
||||
false );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$prev_translation = apply_filters( 'wpml_tm_populate_prev_translation', $prev_translation, $package, $lang );
|
||||
}
|
||||
|
||||
return $prev_translation;
|
||||
}
|
||||
|
||||
private function get_previous_element( $prev_job, $field_name ) {
|
||||
foreach ( $prev_job->elements as $element ) {
|
||||
if ( $element->field_type == $field_name ) {
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
use WPML\FP\Obj;
|
||||
use WPML\TM\API\Job\Map;
|
||||
|
||||
abstract class WPML_TM_Update_Translation_Data_Action extends WPML_Translation_Job_Helper_With_API {
|
||||
|
||||
function get_prev_job_data( $rid ) {
|
||||
global $wpdb;
|
||||
|
||||
// if we have a previous job_id for this rid mark it as the top (last) revision
|
||||
return $wpdb->get_row(
|
||||
$wpdb->prepare(
|
||||
"SELECT job_id, translated
|
||||
FROM {$wpdb->prefix}icl_translate_job
|
||||
WHERE rid=%d
|
||||
AND revision IS NULL
|
||||
LIMIT 1",
|
||||
$rid
|
||||
),
|
||||
ARRAY_N
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a translation job record in icl_translate_job
|
||||
*
|
||||
* @param mixed $rid
|
||||
* @param mixed $translator_id
|
||||
* @param $translation_package
|
||||
* @param array $batch_options
|
||||
*
|
||||
* @return bool|int
|
||||
*/
|
||||
function add_translation_job( $rid, $translator_id, array $translation_package, array $batch_options ) {
|
||||
global $wpdb, $current_user;
|
||||
|
||||
$previousStatus = \WPML_TM_ICL_Translation_Status::makeByRid( $rid )->previous();
|
||||
if (
|
||||
$previousStatus->map( Obj::prop( 'status' ) )->getOrElse( null ) === (string) ICL_TM_ATE_CANCELLED
|
||||
) {
|
||||
$job_id = Map::fromRid( $rid );
|
||||
} else {
|
||||
|
||||
$translation_status = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}icl_translation_status WHERE rid=%d", $rid ) );
|
||||
$prev_translation = $this->get_translated_field_values( $rid, $translation_package );
|
||||
if ( ! $current_user->ID ) {
|
||||
$manager_id = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT manager_id FROM {$wpdb->prefix}icl_translate_job WHERE rid=%d ORDER BY job_id DESC LIMIT 1",
|
||||
$rid
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$manager_id = $current_user->ID;
|
||||
}
|
||||
|
||||
$translate_job_insert_data = array(
|
||||
'rid' => $rid,
|
||||
'translator_id' => $translator_id,
|
||||
'translated' => 0,
|
||||
'manager_id' => (int) $manager_id,
|
||||
);
|
||||
|
||||
if ( isset( $batch_options['deadline_date'] ) ) {
|
||||
$translate_job_insert_data['deadline_date'] = $batch_options['deadline_date'];
|
||||
}
|
||||
|
||||
if ( isset( $translation_package['title'] ) ) {
|
||||
$translate_job_insert_data['title'] = mb_substr( $translation_package['title'], 0, 160 );
|
||||
}
|
||||
|
||||
$wpdb->insert( $wpdb->prefix . 'icl_translate_job', $translate_job_insert_data );
|
||||
$job_id = $wpdb->insert_id;
|
||||
|
||||
$this->package_helper->save_package_to_job( $translation_package, $job_id, $prev_translation );
|
||||
if ( (int) $translation_status->status !== ICL_TM_DUPLICATE ) {
|
||||
$this->fire_notification_actions( $job_id, $translation_status, $translator_id );
|
||||
}
|
||||
}
|
||||
|
||||
return $job_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $prev_id
|
||||
* @param array $package
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function populate_prev_translation( $prev_id, array $package );
|
||||
|
||||
/**
|
||||
* @param int $rid
|
||||
* @param array $package
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get_translated_field_values( $rid, array $package ) {
|
||||
global $wpdb;
|
||||
|
||||
$prev_translations = $this->populate_prev_translation( $rid, $package );
|
||||
|
||||
if ( ! $prev_translations ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// if we have a previous job_id for this rid mark it as the top (last) revision
|
||||
list( $prev_job_id, $prev_job_translated ) = $this->get_prev_job_data( $rid );
|
||||
|
||||
if ( ! is_null( $prev_job_id ) ) {
|
||||
$last_rev_prepare = $wpdb->prepare(
|
||||
"
|
||||
SELECT MAX(revision)
|
||||
FROM {$wpdb->prefix}icl_translate_job
|
||||
WHERE rid=%d
|
||||
AND ( revision IS NOT NULL OR translated = 1 )
|
||||
",
|
||||
$rid
|
||||
);
|
||||
$last_rev = $wpdb->get_var( $last_rev_prepare );
|
||||
$wpdb->update( $wpdb->prefix . 'icl_translate_job', array( 'revision' => $last_rev + 1 ), array( 'job_id' => $prev_job_id ) );
|
||||
}
|
||||
|
||||
return $prev_translations;
|
||||
}
|
||||
|
||||
protected function fire_notification_actions( $job_id, $translation_status, $translator_id ) {
|
||||
$job = wpml_tm_load_job_factory()->get_translation_job( $job_id, false, 0, true );
|
||||
if ( $job && $translation_status->translation_service === 'local' ) {
|
||||
if ( $this->get_tm_setting( array( 'notification', 'new-job' ) ) == ICL_TM_NOTIFICATION_IMMEDIATELY ) {
|
||||
if ( $job_id ) {
|
||||
if ( empty( $translator_id ) ) {
|
||||
do_action( 'wpml_tm_new_job_notification', $job );
|
||||
} else {
|
||||
do_action( 'wpml_tm_assign_job_notification', $job, $translator_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
do_action( 'wpml_added_local_translation_job', $job_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user