wpml-post-status-display.class.php
7.91 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
use WPML\Setup\Option;
class WPML_Post_Status_Display {
const ICON_TRANSLATION_EDIT = 'otgs-ico-edit';
const ICON_TRANSLATION_NEEDS_UPDATE = 'otgs-ico-refresh';
const ICON_TRANSLATION_ADD = 'otgs-ico-add';
const ICON_TRANSLATION_ADD_DISABLED = 'otgs-ico-add-disabled';
const ICON_TRANSLATION_EDIT_DISABLED = 'otgs-ico-edit-disabled';
const ICON_TRANSLATION_IN_PROGRESS = 'otgs-ico-in-progress';
private $active_langs;
public function __construct( $active_languages ) {
$this->active_langs = $active_languages;
}
/**
* Returns the html of a status icon.
*
* @param string $link Link the status icon is to point to.
* @param string $text Hover text for the status icon.
* @param string $css_class
*
* @return string
*/
private function render_status_icon( $link, $text, $css_class ) {
$icon = $this->get_action_icon( $css_class, $text );
if ( strpos( $icon, 'disabled' ) ) {
$link = null;
}
if ( $link ) {
$icon_html = '<a href="' . esc_url( $link ) . '" class="js-wpml-translate-link">';
} else {
$icon_html = '<a class="js-wpml-translate-link">';
}
$icon_html .= $icon;
$icon_html .= $link ? '</a>' : '</span>';
return $icon_html;
}
private function get_action_icon( $css_class, $label ) {
return '<i class="' . $css_class . ' js-otgs-popover-tooltip" title="' . esc_attr( $label ) . '" data-original-title="' . esc_attr( $label ) . '"></i>';
}
/**
* This function takes a post ID and a language as input.
* It will always return the status icon,
* of the version of the input post ID in the language given as the second parameter.
*
* @param int $post_id original post ID
* @param string $lang language of the translation
*
* @return string
*/
public function get_status_html( $post_id, $lang ) {
list( $text, $link, $trid, $css_class, $status ) = $this->get_status_data( $post_id, $lang );
if ( Option::isTMAllowed() ) {
$wpml_tm_element_translations = wpml_tm_load_element_translations();
$review_status = $wpml_tm_element_translations->get_translation_review_status( $trid, $lang );
} else {
// Blog License.
$review_status = null;
}
if ( ! did_action( 'wpml_pre_status_icon_display' ) ) {
do_action( 'wpml_pre_status_icon_display' );
}
/**
* Filters the translation edit link.
*
* @param string $link
* @param int $post_id
* @param string $lang
* @param int $trid
* @param string $css_class
* @param int $status
* @param ?string $review_status
*/
$link = apply_filters( 'wpml_link_to_translation', $link, $post_id, $lang, $trid, $css_class, $status, $review_status );
/**
* Filters the translation status text.
*
* @param string $text
* @param int $post_id
* @param string $lang
* @param int $trid
* @param string $css_class
* @param int $status
* @param ?string $review_status
*/
$text = apply_filters( 'wpml_text_to_translation', $text, $post_id, $lang, $trid, $css_class, $status, $review_status );
/**
* Filter the CSS class for the status icon.
*
* @since 4.2.0
*
* @param string $css_class
* @param int $post_id
* @param string $lang
* @param int $trid
* @param int $status
* @param ?string $review_status
*/
$css_class = apply_filters( 'wpml_css_class_to_translation', $css_class, $post_id, $lang, $trid, $status, $review_status );
$css_class = $this->map_old_icon_filter_to_css_class( $css_class, $post_id, $lang, $trid );
/**
* Filter the HTML link to edit the translation
*
* @since 4.2.0
*
* @param string $html_link
* @param int $post_id
* @param string $lang
* @param int $trid
*/
return apply_filters(
'wpml_post_status_display_html',
$this->render_status_icon( $link, $text, $css_class ),
$post_id,
$lang,
$trid
);
}
/**
* @param string $css_class
* @param int $post_id
* @param string $lang
* @param int $trid
*
* @return string
*/
private function map_old_icon_filter_to_css_class( $css_class, $post_id, $lang, $trid ) {
$map = array(
'edit_translation.png' => self::ICON_TRANSLATION_EDIT,
'needs-update.png' => self::ICON_TRANSLATION_NEEDS_UPDATE,
'add_translation.png' => self::ICON_TRANSLATION_ADD,
'in_progress.png' => self::ICON_TRANSLATION_IN_PROGRESS,
'add_translation_disabled.png' => self::ICON_TRANSLATION_ADD_DISABLED,
'edit_translation_disabled.png' => self::ICON_TRANSLATION_EDIT_DISABLED,
);
$old_icon = array_search( $css_class, $map, true );
/**
* Filters the old icon image
*
* @deprecated since 4.2.0, use `wpml_css_class_to_translation` instead
*
* @param string|false $old_icon
* @param int $post_id
* @param string $lang
* @param int $trid
* @param string $css_class
*
*/
$old_icon = apply_filters( 'wpml_icon_to_translation', $old_icon, $post_id, $lang, $trid, $css_class );
if ( $old_icon && array_key_exists( $old_icon, $map ) ) {
$css_class = $map[ $old_icon ];
}
return $css_class;
}
public function get_status_data( $post_id, $lang ) {
global $wpml_post_translations;
$status_helper = wpml_get_post_status_helper();
$trid = $wpml_post_translations->get_element_trid( $post_id );
$status = $status_helper->get_status( false, $trid, $lang );
$source_language_code = $wpml_post_translations->get_element_lang_code( $post_id );
$correct_id = $wpml_post_translations->element_id_in( $post_id, $lang );
if ( $status && $correct_id ) {
list( $text, $link, $css_class ) = $this->generate_edit_allowed_data( $correct_id, $status_helper->needs_update( $correct_id ) );
} else {
list( $text, $link, $css_class ) = $this->generate_add_data( $trid, $lang, $source_language_code, $post_id );
}
if ($status === ICL_TM_ATE_NEEDS_RETRY) {
list( $text, $link, $css_class ) = $this->generate_retry_data();
}
return array( $text, $link, $trid, $css_class, $status );
}
/**
* @param int $post_id
* @param bool $update true if the translation in questions is in need of an update,
* false otherwise.
*
* @return array
*/
private function generate_edit_allowed_data( $post_id, $update = false ) {
global $wpml_post_translations;
$lang_code = $wpml_post_translations->get_element_lang_code( $post_id );
$post_type = $wpml_post_translations->get_type( $post_id );
$css_class = self::ICON_TRANSLATION_EDIT;
if ( $update && ! $wpml_post_translations->is_a_duplicate( $post_id ) ) {
$css_class = self::ICON_TRANSLATION_NEEDS_UPDATE;
}
if ( $update ) {
$text = __( 'Update %s translation', 'sitepress' );
} else {
$text = __( 'Edit the %s translation', 'sitepress' );
}
$text = sprintf( $text, $this->active_langs[ $lang_code ]['display_name'] );
$link = 'post.php?' . http_build_query (
array( 'lang' => $lang_code,
'action' => 'edit',
'post_type' => $post_type,
'post' => $post_id
)
);
return array( $text, $link, $css_class );
}
/**
* Generates the data for displaying a link element pointing towards a translation, that the current user can
* create.
*
* @param int $trid
* @param int $original_id
* @param string $lang_code
* @param string $source_language
*
* @return array
*/
private function generate_add_data( $trid, $lang_code, $source_language, $original_id ) {
$link = 'post-new.php?' . http_build_query (
array(
'lang' => $lang_code,
'post_type' => get_post_type ( $original_id ),
'trid' => $trid,
'source_lang' => $source_language
)
);
return array(
sprintf( __( 'Add translation to %s', 'sitepress' ), $this->active_langs[ $lang_code ]['display_name'] ),
$link,
self::ICON_TRANSLATION_ADD,
);
}
private function generate_retry_data( ) {
return array(
null,
null,
self::ICON_TRANSLATION_IN_PROGRESS,
);
}
}