class-wpml-tp-translations-repository.php
2.1 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
class WPML_TP_Translations_Repository {
/** @var WPML_TP_XLIFF_API */
private $xliff_api;
/** @var WPML_TM_Jobs_Repository */
private $jobs_repository;
/**
* @param WPML_TP_XLIFF_API $xliff_api
* @param WPML_TM_Jobs_Repository $jobs_repository
*/
public function __construct( WPML_TP_XLIFF_API $xliff_api, WPML_TM_Jobs_Repository $jobs_repository ) {
$this->xliff_api = $xliff_api;
$this->jobs_repository = $jobs_repository;
}
/**
* @param int $job_id
* @param int $job_type
* @param bool $parse When true, it returns the parsed translation, otherwise, it returns the raw XLIFF.
*
* @return WPML_TP_Translation_Collection|string
* @throws WPML_TP_API_Exception|InvalidArgumentException
*/
public function get_job_translations( $job_id, $job_type, $parse = true ) {
$job = $this->jobs_repository->get_job( $job_id, $job_type );
if ( ! $job ) {
throw new InvalidArgumentException( 'Cannot find job' );
}
return $this->get_job_translations_by_job_entity( $job, $parse );
}
/**
* @param WPML_TM_Job_Entity $job
* @param bool $parse When true, it returns the parsed translation, otherwise, it returns the raw XLIFF.
*
* @return WPML_TP_Translation_Collection|string
* @throws WPML_TP_API_Exception
*/
public function get_job_translations_by_job_entity( WPML_TM_Job_Entity $job, $parse = true ) {
$correct_states = array( ICL_TM_TRANSLATION_READY_TO_DOWNLOAD, ICL_TM_COMPLETE );
if ( ! in_array( $job->get_status(), $correct_states, true ) ) {
throw new InvalidArgumentException( 'Job\'s translation is not ready.' );
}
if ( ! $job->get_tp_id() ) {
throw new InvalidArgumentException( 'This is only a local job.' );
}
$translations = $this->xliff_api->get_remote_translations( $job->get_tp_id(), $parse );
if ( $parse ) {
/**
* It filters translations coming from the Translation Proxy.
*
* @param \WPML_TP_Translation_Collection|string $translations
* @param \WPML_TM_Job_Entity $job
*/
$translations = apply_filters( 'wpml_tm_proxy_translations', $translations, $job );
}
return $translations;
}
}