wpml-tf-backend-document-information.php
4.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Class WPML_TF_Backend_Document_Information
*
* @author OnTheGoSystems
*/
class WPML_TF_Backend_Document_Information extends WPML_TF_Document_Information {
/** @var WPML_TP_Client_Factory|null $tp_client_factory */
private $tp_client_factory;
/** @var WPML_TP_Client|null $tp_client */
private $tp_client;
/**
* WPML_TF_Backend_Document_Information constructor.
*
* @param SitePress $sitepress
* @param WPML_TP_Client_Factory $tp_client_factory
*/
public function __construct( SitePress $sitepress, WPML_TP_Client_Factory $tp_client_factory = null ) {
parent::__construct( $sitepress );
$this->tp_client_factory = $tp_client_factory;
}
/**
* @return false|null|string
*/
public function get_url() {
$url = null;
if ( $this->is_post_document() ) {
$url = get_permalink( $this->id );
}
return $url;
}
/**
* @return null|string
*/
public function get_title() {
$title = null;
if ( $this->is_post_document() ) {
$title = $this->get_post_title( $this->id );
}
return $title;
}
/**
* @return bool
*/
private function is_post_document() {
return 0 === strpos( $this->type, 'post_' );
}
/**
* @param string $language_code
*
* @return string
*/
public function get_flag_url( $language_code ) {
return $this->sitepress->get_flag_url( $language_code );
}
/**
* @return string
*/
public function get_edit_url() {
$this->load_link_to_translation_tm_filters();
$url = 'post.php?' . http_build_query(
array(
'lang' => $this->get_language(),
'action' => 'edit',
'post_type' => $this->type,
'post' => $this->id,
)
);
return apply_filters( 'wpml_link_to_translation', $url, $this->get_source_id(), $this->get_language(), $this->get_trid() );
}
private function load_link_to_translation_tm_filters() {
if ( ! did_action( 'wpml_pre_status_icon_display' ) ) {
do_action( 'wpml_pre_status_icon_display' );
}
}
/**
* @return null|int
*/
public function get_source_id() {
$source_id = null;
$translations = $this->get_translations();
if ( isset( $translations[ $this->get_source_language() ]->element_id ) ) {
$source_id = (int) $translations[ $this->get_source_language() ]->element_id;
}
return $source_id;
}
/**
* @return null|string
*/
public function get_source_url() {
$url = null;
if ( $this->get_source_id() ) {
$url = get_the_permalink( $this->get_source_id() );
}
return $url;
}
/**
* @return null|string
*/
public function get_source_title() {
$title = null;
if ( $this->get_source_id() ) {
$title = $this->get_post_title( $this->get_source_id() );
}
return $title;
}
/**
* @return array|bool|mixed
*/
private function get_translations() {
return $this->sitepress->get_element_translations( $this->get_trid(), $this->type );
}
/**
* @param int $job_id
*
* @return string
*/
public function get_translator_name( $job_id ) {
$translation_job = $this->get_translation_job( $job_id );
if ( isset( $translation_job->translation_service ) ) {
if ( 'local' === $translation_job->translation_service && isset( $translation_job->translator_id ) ) {
$translator_name = get_the_author_meta( 'display_name', $translation_job->translator_id );
$translator_name .= ' (' . __( 'local translator', 'sitepress' ) . ')';
} else {
$translator_name = __( 'Unknown remote translation service', 'sitepress' );
$tp_client = $this->get_tp_client();
if ( $tp_client ) {
$translator_name = $tp_client->services()->get_name( $translation_job->translation_service );
}
}
} else {
$post_author_id = get_post_field( 'post_author', $this->id );
$translator_name = get_the_author_meta( 'display_name', (int) $post_author_id );
}
return $translator_name;
}
/**
* @param string $from
* @param string $to
*
* @return array
*/
public function get_available_translators( $from, $to ) {
$translators = array();
if ( function_exists( 'wpml_tm_load_blog_translators' ) ) {
$args = array(
'from' => $from,
'to' => $to,
);
$users = wpml_tm_load_blog_translators()->get_blog_translators( $args );
$translators = wp_list_pluck( $users, 'display_name', 'ID' );
}
return $translators;
}
/**
* @param int $post_id
*
* @return null|string
*/
private function get_post_title( $post_id ) {
$title = null;
$post = get_post( $post_id );
if ( isset( $post->post_title ) ) {
$title = $post->post_title;
}
return $title;
}
/** @return null|WPML_TP_Client */
private function get_tp_client() {
if ( ! $this->tp_client && $this->tp_client_factory ) {
$this->tp_client = $this->tp_client_factory->create();
}
return $this->tp_client;
}
}