class-wpml-tm-rest-apply-tp-translation.php
1.69 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
70
71
<?php
use WPML\LIB\WP\User;
class WPML_TM_REST_Apply_TP_Translation extends WPML_REST_Base {
/** @var WPML_TP_Apply_Translations */
private $apply_translations;
public function __construct( WPML_TP_Apply_Translations $apply_translations ) {
parent::__construct( 'wpml/tm/v1' );
$this->apply_translations = $apply_translations;
}
public function add_hooks() {
$this->register_routes();
}
public function register_routes() {
parent::register_route(
'/tp/apply-translations',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'apply_translations' ),
)
);
}
/**
* @return WP_Error|int|array
*/
public function apply_translations( WP_REST_Request $request ) {
try {
$params = $request->get_json_params();
if ( $params ) {
if ( ! isset( $params['original_element_id'] ) ) {
$params = array_filter( $params, array( $this, 'validate_job' ) );
if ( ! $params ) {
return array();
}
}
}
return $this->apply_translations->apply( $params )->map( array( $this, 'map_jobs_to_array' ) );
} catch ( Exception $e ) {
return new WP_Error( 400, $e->getMessage() );
}
}
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [ User::CAP_ADMINISTRATOR, User::CAP_MANAGE_TRANSLATIONS, User::CAP_TRANSLATE ];
}
public function map_jobs_to_array( WPML_TM_Job_Entity $job ) {
return [
'id' => $job->get_id(),
'type' => $job->get_type(),
'status' => $job->get_status(),
];
}
/**
* @param array $job
*
* @return bool
*/
private function validate_job( array $job ) {
return isset( $job['id'], $job['type'] ) && \WPML_TM_Job_Entity::is_type_valid( $job['type'] );
}
}