AutoTranslate.php
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Left;
use WPML\FP\Obj;
use WPML\FP\Right;
use WPML\TM\API\Jobs;
class AutoTranslate implements IHandler {
public function run( Collection $data ) {
global $wpml_translation_job_factory;
$trid = $data->get( 'trid' );
$language_code = $data->get( 'language' );
if ( $trid && $language_code ) {
$post_id = \SitePress::get_original_element_id_by_trid( $trid );
if ( ! $post_id ) {
return Either::left( 'Post cannot be found by trid' );
}
$job_id = $wpml_translation_job_factory->create_local_post_job( $post_id, $language_code );
$job = Jobs::get( (int) $job_id );
if ( ! $job ) {
return Either::left( 'Job could not be created' );
}
if ( Obj::prop( 'automatic', $job ) ) {
return Right::of( [ 'jobId' => $job_id, 'automatic' => 1 ] );
} else {
return Right::of( [ 'jobId' => $job_id, 'automatic' => 0, 'editUrl' => Jobs::getEditUrl( $data->get( 'currentUrl' ), $job_id ) ] );
}
}
return Left::of( 'invalid data' );
}
}