class-wpml-tm-rest-jobs-element-info.php
3.57 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
use WPML\FP\Obj;
class WPML_TM_Rest_Jobs_Element_Info {
/** @var WPML_TM_Rest_Jobs_Package_Helper_Factory */
private $package_helper_factory;
/** @var array|null */
private $post_types;
/**
* @param WPML_TM_Rest_Jobs_Package_Helper_Factory $package_helper_factory
*/
public function __construct( WPML_TM_Rest_Jobs_Package_Helper_Factory $package_helper_factory ) {
$this->package_helper_factory = $package_helper_factory;
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return array
*/
public function get( WPML_TM_Job_Entity $job ) {
$type = $job->get_type();
$id = $job->get_original_element_id();
$result = [];
switch ( $type ) {
case WPML_TM_Job_Entity::POST_TYPE:
/** @var WPML_TM_Post_Job_Entity $job */
$result = $this->get_for_post( $id, $job->get_element_id() );
break;
case WPML_TM_Job_Entity::STRING_TYPE:
case WPML_TM_Job_Entity::STRING_BATCH:
$result = $this->get_for_title( $job->get_title() );
break;
case WPML_TM_Job_Entity::PACKAGE_TYPE:
$result = $this->get_for_package( $id );
break;
}
if ( empty( $result ) ) {
$result = array(
'name' => '',
'url' => null,
);
do_action( 'wpml_tm_jobs_log', 'WPML_TM_Rest_Jobs_Element_Info::get', array( $id, $type ), 'Empty result' );
}
$result['url'] = apply_filters( 'wpml_tm_job_list_element_url', $result['url'], $id, $type );
if ( $job instanceof WPML_TM_Post_Job_Entity ) {
$result['type'] = $this->get_type_info( $job );
}
return $result;
}
/**
* @param int $originalPostId
* @param int $translatedPostId
*
* @return array
*/
private function get_for_post( $originalPostId, $translatedPostId ) {
$result = array();
$post = get_post( $originalPostId );
if ( $post ) {
$permalink = get_permalink( $post );
$result = [
'name' => $post->post_title,
'url' => $permalink,
'status' => Obj::propOr( 'draft', 'post_status', get_post( $translatedPostId ) ),
];
}
return $result;
}
/**
* @param int $id
*
* @return array
*/
private function get_for_package( $id ) {
$result = array();
$helper = $this->package_helper_factory->create();
if ( ! $helper ) {
return array(
'name' => __( 'String package job', 'wpml-translation-management' ),
'url' => null,
);
}
$package = $helper->get_translatable_item( null, $id );
if ( $package ) {
$result = array(
'name' => $package->title,
'url' => $package->edit_link,
);
}
return $result;
}
/**
* @param string $title
*
* @return array
*/
private function get_for_title( $title ) {
return [
'name' => $title,
'url' => null,
];
}
/**
* @param WPML_TM_Post_Job_Entity $job
*
* @return array
*/
private function get_type_info( WPML_TM_Post_Job_Entity $job ) {
$generalType = substr(
$job->get_element_type(),
0,
strpos( $job->get_element_type(), '_' ) ?: 0
);
switch ( $generalType ) {
case 'post':
case 'package':
$specificType = substr( $job->get_element_type(), strlen( $generalType ) + 1 );
$label = Obj::pathOr(
$job->get_element_type(),
[ $specificType, 'labels', 'singular_name' ],
$this->get_post_types()
);
break;
case 'st-batch':
$label = __( 'Strings', 'wpml-translation-management' );
break;
default:
$label = $job->get_element_type();
}
return [
'value' => $job->get_element_type(),
'label' => $label,
];
}
private function get_post_types() {
if ( $this->post_types === null ) {
$this->post_types = \WPML\API\PostTypes::getTranslatableWithInfo();
}
return $this->post_types;
}
}