first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?php
class WPML_TP_Apply_Single_Job {
/** @var WPML_TP_Translations_Repository */
private $translations_repository;
/** @var WPML_TP_Apply_Translation_Strategies */
private $strategy_dispatcher;
/**
* @param WPML_TP_Translations_Repository $translations_repository
* @param WPML_TP_Apply_Translation_Strategies $strategy_dispatcher
*/
public function __construct(
WPML_TP_Translations_Repository $translations_repository,
WPML_TP_Apply_Translation_Strategies $strategy_dispatcher
) {
$this->translations_repository = $translations_repository;
$this->strategy_dispatcher = $strategy_dispatcher;
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return WPML_TM_Job_Entity
* @throws WPML_TP_API_Exception
*/
public function apply( WPML_TM_Job_Entity $job ) {
$translations = $this->translations_repository->get_job_translations_by_job_entity( $job );
$this->strategy_dispatcher->get( $job )->apply( $job, $translations );
$job->set_status( ICL_TM_COMPLETE );
return $job;
}
}

View File

@@ -0,0 +1,69 @@
<?php
class WPML_TP_Apply_Translation_Post_Strategy implements WPML_TP_Apply_Translation_Strategy {
/** @var WPML_TP_Jobs_API */
private $jobs_api;
/** @var wpdb */
private $wpdb;
/**
* @param WPML_TP_Jobs_API $jobs_api
*/
public function __construct( WPML_TP_Jobs_API $jobs_api ) {
$this->jobs_api = $jobs_api;
global $wpdb;
$this->wpdb = $wpdb;
}
/**
* @param WPML_TM_Job_Entity $job
* @param WPML_TP_Translation_Collection $translations
*
* @return void
* @throws WPML_TP_API_Exception
*/
public function apply( WPML_TM_Job_Entity $job, WPML_TP_Translation_Collection $translations ) {
if ( ! $job instanceof WPML_TM_Post_Job_Entity ) {
throw new InvalidArgumentException( 'A job must have post type' );
}
kses_remove_filters();
wpml_tm_save_data( $this->build_data( $job, $translations ) );
kses_init();
$this->jobs_api->update_job_state( $job, 'delivered' );
}
/**
* @param WPML_TM_Job_Entity $job
* @param WPML_TP_Translation_Collection $translations
*
* @return array
*/
private function build_data( WPML_TM_Post_Job_Entity $job, WPML_TP_Translation_Collection $translations ) {
$data = array(
'job_id' => $job->get_translate_job_id(),
'fields' => array(),
'complete' => 1
);
/** @var WPML_TP_Translation $translation */
foreach ( $translations as $translation ) {
foreach ( $job->get_elements() as $element ) {
if ( $element->get_type() === $translation->get_field() ) {
$data['fields'][ $element->get_type() ] = array(
'data' => $translation->get_target(),
'finished' => 1,
'tid' => $element->get_id(),
'field_type' => $element->get_type(),
'format' => $element->get_format()
);
}
}
}
return $data;
}
}

View File

@@ -0,0 +1,62 @@
<?php
class WPML_TP_Apply_Translation_Strategies {
/** @var WPML_TP_Apply_Translation_Post_Strategy */
private $post_strategy;
/** @var WPML_TP_Apply_Translation_String_Strategy */
private $string_strategy;
/** @var wpdb */
private $wpdb;
/**
* @param wpdb $wpdb
*/
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return WPML_TP_Apply_Translation_Strategy
*/
public function get( WPML_TM_Job_Entity $job ) {
switch ( $job->get_type() ) {
case WPML_TM_Job_Entity::STRING_TYPE:
return $this->get_string_strategy();
case WPML_TM_Job_Entity::POST_TYPE:
case WPML_TM_Job_Entity::PACKAGE_TYPE:
case WPML_TM_Job_Entity::STRING_BATCH:
return $this->get_post_strategy();
default:
throw new InvalidArgumentException( 'Job type: ' . $job->get_type() . ' is not supported' );
}
}
/**
* @return WPML_TP_Apply_Translation_Post_Strategy
*/
private function get_post_strategy() {
if ( ! $this->post_strategy ) {
$this->post_strategy = new WPML_TP_Apply_Translation_Post_Strategy( wpml_tm_get_tp_jobs_api() );
}
return $this->post_strategy;
}
/**
* @return WPML_TP_Apply_Translation_String_Strategy
*/
private function get_string_strategy() {
if ( ! $this->string_strategy ) {
$this->string_strategy = new WPML_TP_Apply_Translation_String_Strategy(
wpml_tm_get_tp_jobs_api(),
$this->wpdb
);
}
return $this->string_strategy;
}
}

View File

@@ -0,0 +1,11 @@
<?php
interface WPML_TP_Apply_Translation_Strategy {
/**
* @param WPML_TM_Job_Entity $job
* @param WPML_TP_Translation_Collection $translations
*
* @return void
*/
public function apply( WPML_TM_Job_Entity $job, WPML_TP_Translation_Collection $translations );
}

View File

@@ -0,0 +1,62 @@
<?php
class WPML_TP_Apply_Translation_String_Strategy implements WPML_TP_Apply_Translation_Strategy {
/** @var WPML_TP_Jobs_API */
private $jobs_api;
/** @var wpdb */
private $wpdb;
/**
* @param WPML_TP_Jobs_API $jobs_api
* @param wpdb $wpdb
*/
public function __construct( WPML_TP_Jobs_API $jobs_api, wpdb $wpdb ) {
$this->jobs_api = $jobs_api;
$this->wpdb = $wpdb;
}
/**
* @param WPML_TM_Job_Entity $job
* @param WPML_TP_Translation_Collection $translations
*
* @return void
* @throws WPML_TP_API_Exception
*/
public function apply( WPML_TM_Job_Entity $job, WPML_TP_Translation_Collection $translations ) {
if ( ! icl_translation_add_string_translation(
$job->get_tp_id(),
$this->map_translations_to_legacy_array( $translations ),
$translations->get_target_language()
)
) {
throw new WPML_TP_API_Exception( 'Could not apply string translation!' );
}
$this->update_local_job_status( $job, ICL_TM_COMPLETE );
$this->jobs_api->update_job_state( $job, 'delivered' );
}
/**
* @param WPML_TM_Job_Entity $job
* @param int $status
*/
private function update_local_job_status( WPML_TM_Job_Entity $job, $status ) {
$this->wpdb->update(
$this->wpdb->prefix . 'icl_core_status',
array( 'status' => $status ),
array( 'id' => $job->get_id() )
);
}
private function map_translations_to_legacy_array( WPML_TP_Translation_Collection $translations ) {
$result = array();
/** @var WPML_TP_Translation $translation */
foreach ( $translations as $translation ) {
$result[ $translation->get_field() ] = $translation->get_target();
}
return $result;
}
}