PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.7
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / admin / translation-cpt.php

translation-cpt.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.16.7, at inc/admin/translation-cpt.php

453 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Register wpLingua Translation CPT
11 *
12 * @return void
13 */
14 function wplng_register_post_type_translation() {
15 register_post_type(
16 'wplng_translation',
17 array(
18 'labels' => array(
19 'name' => __( 'wpLingua - Translations', 'wplingua' ),
20 'singular_name' => __( 'wpLingua - Translation', 'wplingua' ),
21 'all_items' => __( 'wpLingua - All translations', 'wplingua' ),
22 'edit_item' => __( 'wpLingua - Edit translation', 'wplingua' ),
23 'menu_name' => __( 'Translations', 'wplingua' ),
24 'search_items' => __( 'Search translations', 'wplingua' ),
25 ),
26 'public' => false,
27 'publicly_queryable' => false,
28 'show_ui' => true,
29 'exclude_from_search' => true,
30 'show_in_nav_menus' => false,
31 'show_in_menu' => false,
32 'has_archive' => false,
33 'rewrite' => false,
34 'menu_icon' => 'dashicons-translation',
35 'supports' => array(
36 'title',
37 ),
38 'capability_type' => 'post',
39 'capabilities' => array(
40 'create_posts' => false,
41 ),
42 'map_meta_cap' => true,
43 )
44 );
45 }
46
47
48 /**
49 * Remove quick edit on wpLingua translations list
50 *
51 * This function is a filter that removes the Quick Edit link on the wpLingua
52 * translations list in the WordPress admin area.
53 *
54 * @param array $actions An array of row action links.
55 * @param object $post The post object.
56 * @return array The modified array of row action links.
57 */
58 function wplng_translation_remove_quick_edit( $actions, $post ) {
59
60 // Check that the post type is a wpLingua translation
61 if ( $post->post_type !== 'wplng_translation' ) {
62 return $actions;
63 }
64
65 unset( $actions['view'] );
66 unset( $actions['inline hide-if-no-js'] );
67
68 return $actions;
69 }
70
71
72 /**
73 * Display 100 translations by default in admin area
74 *
75 * @param mixed $result
76 * @return mixed
77 */
78 function wplng_translation_per_page( $result ) {
79 if ( false === $result ) {
80 $result = '100';
81 }
82
83 return $result;
84 }
85
86
87 /**
88 * Filters the query for the 'wplng_translation' post type in the WordPress admin area.
89 *
90 * This function modifies the main query in the admin area to include a meta query
91 * that filters posts of type 'wplng_translation' based on the meta key
92 * 'wplng_translation_original_language_id'. Only posts where this meta key matches
93 * the current website's language ID (retrieved via `wplng_get_language_website_id()`)
94 * will be included in the results.
95 *
96 * @param WP_Query $query The current query instance.
97 * @return void
98 */
99 function wplng_filter_wplng_translation_posts( $query ) {
100
101 // Check if we are in the admin area, working with the main query,
102 // and the post type is 'wplng_translation'.
103 if ( is_admin()
104 && $query->is_main_query()
105 && $query->get( 'post_type' ) === 'wplng_translation'
106 ) {
107 $query->set(
108 'meta_query',
109 array(
110 array(
111 'key' => 'wplng_translation_original_language_id',
112 'value' => wplng_get_language_website_id(),
113 'compare' => '=',
114 ),
115 )
116 );
117 }
118 }
119
120
121 /**
122 * Filter translations by status: Display option on CPT list
123 *
124 * @return void
125 */
126 function wplng_restrict_manage_posts_translation_status() {
127
128 if ( empty( $_GET['post_type'] )
129 || 'wplng_translation' !== $_GET['post_type']
130 ) {
131 return;
132 }
133
134 $languages_target = wplng_get_languages_target_ids();
135 $options = array();
136 $translation_status = '';
137
138 if ( ! empty( $_GET['translation_status'] ) ) {
139 $translation_status = sanitize_title( $_GET['translation_status'] );
140 }
141
142 if ( count( $languages_target ) === 1 ) {
143 $options = array(
144 '' => __( 'All translation status', 'wplingua' ),
145 'reviewed' => __( 'Reviewed', 'wplingua' ),
146 'unreviewed' => __( 'Unreviewed', 'wplingua' ),
147 );
148 } else {
149 $options = array(
150 '' => __( 'All translation status', 'wplingua' ),
151 'full-reviewed' => __( 'Full reviewed', 'wplingua' ),
152 'partially-reviewed' => __( 'Partially reviewed', 'wplingua' ),
153 'reviewed' => __( 'Reviewed', 'wplingua' ),
154 'unreviewed' => __( 'Full unreviewed', 'wplingua' ),
155 );
156 }
157
158 $html = '<select name="translation_status">';
159
160 foreach ( $options as $value => $label ) {
161
162 $html .= '<option ';
163 $html .= 'value="' . esc_attr( $value ) . '" ';
164
165 if ( $value === $translation_status ) {
166 $html .= 'selected="selected" ';
167 }
168
169 $html .= '>';
170 $html .= esc_html( $label );
171 $html .= '</option>';
172 }
173
174 $html .= '</select>';
175
176 echo $html;
177 }
178
179
180 /**
181 * Filter translations by status: Apply custom query on CPT for translation_status.
182 *
183 * This function modifies the query for the 'wplng_translation' post type in the admin edit screen.
184 * It applies a meta query based on the specified translation status.
185 *
186 * @param WP_Query $query The current WP_Query instance.
187 * @return void
188 */
189 function wplng_posts_filter_translation_status( $query ) {
190 global $pagenow;
191
192 // Check if we are in the admin edit screen for 'wplng_translation' post type
193 if ( empty( $_GET['post_type'] )
194 || 'wplng_translation' !== $_GET['post_type']
195 || empty( $_GET['translation_status'] )
196 || ! is_admin()
197 || $pagenow !== 'edit.php'
198 ) {
199 return;
200 }
201
202 // Determine the translation status and set the corresponding meta query
203 switch ( $_GET['translation_status'] ) {
204 case 'full-reviewed':
205 $query->set(
206 'meta_query',
207 array(
208 'relation' => 'AND',
209 array(
210 'key' => 'wplng_translation_translations',
211 'value' => '"status":\d',
212 'compare' => 'REGEXP',
213 ),
214 array(
215 'key' => 'wplng_translation_translations',
216 'value' => '("status":"ungenerated"|"status":"generated")',
217 'compare' => 'NOT REGEXP',
218 ),
219 )
220 );
221 break;
222
223 case 'partially-reviewed':
224 $query->set(
225 'meta_query',
226 array(
227 'relation' => 'AND',
228 array(
229 'key' => 'wplng_translation_translations',
230 'value' => '"status":\d',
231 'compare' => 'REGEXP',
232 ),
233 array(
234 'key' => 'wplng_translation_translations',
235 'value' => '("status":"ungenerated"|"status":"generated")',
236 'compare' => 'REGEXP',
237 ),
238 )
239 );
240 break;
241
242 case 'reviewed':
243 $query->set(
244 'meta_query',
245 array(
246 'relation' => 'AND',
247 array(
248 'key' => 'wplng_translation_translations',
249 'value' => '"status":\d',
250 'compare' => 'REGEXP',
251 ),
252 )
253 );
254 break;
255
256 case 'unreviewed':
257 $query->set(
258 'meta_query',
259 array(
260 'relation' => 'AND',
261 array(
262 'key' => 'wplng_translation_translations',
263 'value' => '"status":\d',
264 'compare' => 'NOT REGEXP',
265 ),
266 )
267 );
268 break;
269 }
270 }
271
272
273 /**
274 * Add status custom column on translations
275 *
276 * Adds a custom column in the admin edit screen for the 'wplng_translation' post type.
277 * The column is called 'Translation status' and is used to show the status of the
278 * translation for each post.
279 *
280 * @param array $columns String array
281 * @return array
282 */
283 function wplng_translation_status_columns( $columns ) {
284
285 // Save the 'cb' column value if it exists
286 $cb = array();
287
288 if ( isset( $columns['cb'] ) ) {
289 $cb = array(
290 'cb' => $columns['cb'],
291 );
292 unset( $columns['cb'] );
293 }
294
295 // Add the 'wplng_status' column
296 $columns = array_merge(
297 $cb,
298 array(
299 'wplng_status' => __( 'Translation status', 'wplingua' ),
300 ),
301 $columns
302 );
303
304 return $columns;
305 }
306
307
308 /**
309 * Add translation status class on wplng_translation post list in admin area
310 *
311 * @param string[] $classes An array of post class names.
312 * @param string[] $css_class An array of additional class names added to the post.
313 * @param int $post_id The post ID.
314 * @return string[]
315 */
316 function wplng_post_class_translation_status( $classes, $css_class, $post_id ) {
317
318 global $typenow;
319
320 if ( 'wplng_translation' !== $typenow ) {
321 return $classes;
322 }
323
324 $translations = get_post_meta(
325 $post_id,
326 'wplng_translation_translations',
327 true
328 );
329
330 $translations = json_decode(
331 $translations,
332 true
333 );
334
335 if ( empty( $translations ) ) {
336 return $classes;
337 }
338
339 $languages_target_ids = wplng_get_languages_target_ids();
340 $has_a_reviewed = false;
341 $is_not_full_reviewed = false;
342 $checked_language = array();
343 $status_class = 'wplng-status-unreview';
344
345 foreach ( $languages_target_ids as $language_target_id ) {
346 foreach ( $translations as $translation ) {
347 if ( $language_target_id !== $translation['language_id']
348 || empty( $translation['status'] )
349 ) {
350 continue;
351 }
352
353 $checked_language[] = $translation['language_id'];
354
355 if ( 'generated' === $translation['status']
356 || 'ungenerated' === $translation['status']
357 ) {
358 $is_not_full_reviewed = true;
359 } elseif ( is_int( $translation['status'] ) ) {
360 $has_a_reviewed = true;
361 }
362 }
363 }
364
365 if ( ! $is_not_full_reviewed && $has_a_reviewed ) {
366
367 if ( $checked_language === $languages_target_ids ) {
368 $status_class = 'wplng-status-full-review';
369 } else {
370 $status_class = 'wplng-status-has-review';
371 }
372 } elseif ( ! $is_not_full_reviewed || $has_a_reviewed ) {
373 $status_class = 'wplng-status-has-review';
374 }
375
376 if ( 'wplng-status-full-review' === $status_class
377 && $checked_language !== $languages_target_ids
378 ) {
379 $status_class = 'wplng-status-has-review';
380 }
381
382 $classes[] = $status_class;
383
384 return $classes;
385 }
386
387
388 /**
389 * Add status items in custom column on translations
390 *
391 * @param string $column The name of the column to display.
392 * @param int $post_id The current post ID.
393 * @return void
394 */
395 function wplng_translation_status_item( $column, $post_id ) {
396
397 if ( 'wplng_status' !== $column ) {
398 return;
399 }
400
401 $html = '<span';
402 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-full-review"';
403 $html .= ' title="' . esc_attr__( 'Full reviewed', 'wplingua' ) . '"';
404 $html .= '></span>';
405
406 $html .= '<span';
407 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-has-review"';
408 $html .= ' title="' . esc_attr__( 'Partially reviewed', 'wplingua' ) . '"';
409 $html .= '></span>';
410
411 $html .= '<span';
412 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-unreview"';
413 $html .= ' title="' . esc_attr__( 'Unreviewed', 'wplingua' ) . '"';
414 $html .= '></span>';
415
416 echo $html;
417 }
418
419
420 /**
421 * Add status items text on translations
422 *
423 * Defaults $actions are 'Edit', ‘Quick Edit’, 'Restore', 'Trash', ‘Delete Permanently’, 'Preview', and 'View'.
424 *
425 * @param string[] $actions An array of row action links.
426 * @param WP_Post $post The post object.
427 * @return string[]
428 */
429 function wplng_post_row_actions_translation_status( $actions, $post ) {
430
431 if ( 'wplng_translation' !== $post->post_type ) {
432 return $actions;
433 }
434
435 $html = esc_html__( 'Translation status: ', 'wplingua' );
436
437 $html .= '<span class="wplng-status wplng-status-full-review">';
438 $html .= esc_html__( 'Full reviewed', 'wplingua' );
439 $html .= '</span>';
440
441 $html .= '<span class="wplng-status wplng-status-has-review">';
442 $html .= esc_html__( 'Partially reviewed', 'wplingua' );
443 $html .= '</span>';
444
445 $html .= '<span class="wplng-status wplng-status-unreview">';
446 $html .= esc_html__( 'Unreviewed', 'wplingua' );
447 $html .= '</span>';
448
449 $actions['wplng-status-text'] = $html;
450
451 return $actions;
452 }
453