PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.13.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.13.1
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 / slug-cpt.php

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

478 lines 11.4 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 Slug CPT
11 *
12 * @return void
13 */
14 function wplng_register_post_type_slug() {
15 register_post_type(
16 'wplng_slug',
17 array(
18 'labels' => array(
19 'name' => __( 'wpLingua - Website slugs', 'wplingua' ),
20 'singular_name' => __( 'wpLingua - Website slug', 'wplingua' ),
21 'all_items' => __( 'wpLingua - All slugs', 'wplingua' ),
22 'edit_item' => __( 'wpLingua - Edit slug', 'wplingua' ),
23 'menu_name' => __( 'Website slugs', 'wplingua' ),
24 'search_items' => __( 'Search slugs', '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 slugs list.
50 *
51 * This function is a filter on post_row_actions and is used to remove the
52 * quick edit action from the list of actions on the slugs list page.
53 *
54 * @param array $actions An array of row action links.
55 * @param WP_Post $post The post object.
56 *
57 * @return array
58 */
59 function wplng_slug_remove_quick_edit( $actions, $post ) {
60
61 // Check if the post type is 'wplng_slug'.
62 if ( $post->post_type !== 'wplng_slug' ) {
63 return $actions;
64 }
65
66 unset( $actions['view'] );
67 unset( $actions['inline hide-if-no-js'] );
68
69 return $actions;
70 }
71
72
73 /**
74 * Display 100 translations by default in admin area
75 *
76 * This function is a filter on the posts_per_page option and is used to
77 * display 100 translations by default in the admin area.
78 *
79 * @param mixed $result
80 * @return mixed
81 */
82 function wplng_slug_per_page( $result ) {
83
84 if ( false === $result ) {
85 $result = '100';
86 }
87
88 return $result;
89 }
90
91
92 /**
93 * Filters the query for the 'wplng_slug' post type in the WordPress admin area.
94 *
95 * This function modifies the main query in the admin area to include a meta query
96 * that filters posts of type 'wplng_slug' based on the meta key
97 * 'wplng_slug_original_language_id'. Only posts where this meta key matches
98 * the current website's language ID (retrieved via `wplng_get_language_website_id()`)
99 * will be included in the results.
100 *
101 * @param WP_Query $query The current query instance.
102 * @return void
103 */
104 function wplng_filter_wplng_slug_posts( $query ) {
105
106 // Check if we are in the admin area, working with the main query,
107 // and the post type is 'wplng_translation'.
108 if ( is_admin()
109 && $query->is_main_query()
110 && $query->get( 'post_type' ) === 'wplng_slug'
111 ) {
112 $query->set(
113 'meta_query',
114 array(
115 array(
116 'key' => 'wplng_slug_original_language_id',
117 'value' => wplng_get_language_website_id(),
118 'compare' => '=',
119 ),
120 )
121 );
122 }
123 }
124
125
126 /**
127 * Filter slugs by status: Display option on CPT list
128 *
129 * @return void
130 */
131 function wplng_restrict_manage_posts_slug_status() {
132
133 // Check if we are on the right post type.
134 if ( empty( $_GET['post_type'] )
135 || 'wplng_slug' !== $_GET['post_type']
136 ) {
137 return;
138 }
139
140 // Get languages target IDs.
141 $languages_target = wplng_get_languages_target_ids();
142
143 // Set default options for the select.
144 $options = array(
145 '' => __( 'All slug status', 'wplingua' ),
146 );
147
148 // Set slug status value from URL parameter if set.
149 $slug_status = '';
150
151 if ( ! empty( $_GET['slug_status'] ) ) {
152 $slug_status = sanitize_title( $_GET['slug_status'] );
153 }
154
155 // If only one language target id, then use simple status options.
156 if ( count( $languages_target ) === 1 ) {
157 $options = array_merge(
158 $options,
159 array(
160 'reviewed' => __( 'Reviewed', 'wplingua' ),
161 'unreviewed' => __( 'Unreviewed', 'wplingua' ),
162 )
163 );
164 } else {
165 // If more than one language target id,
166 // then use full, partially and reviewed status options.
167 $options = array_merge(
168 $options,
169 array(
170 'full-reviewed' => __( 'Full reviewed', 'wplingua' ),
171 'partially-reviewed' => __( 'Partially reviewed', 'wplingua' ),
172 'reviewed' => __( 'Reviewed', 'wplingua' ),
173 'unreviewed' => __( 'Full unreviewed', 'wplingua' ),
174 )
175 );
176 }
177
178 // Display the select with options.
179 $html = '<select name="slug_status">';
180
181 foreach ( $options as $value => $label ) {
182
183 $html .= '<option ';
184 $html .= 'value="' . esc_attr( $value ) . '" ';
185
186 if ( $value === $slug_status ) {
187 $html .= 'selected="selected" ';
188 }
189
190 $html .= '>';
191 $html .= esc_html( $label );
192 $html .= '</option>';
193 }
194
195 $html .= '</select>';
196
197 echo $html;
198 }
199
200
201 /**
202 * Filter slugs by status: Apply custom query on CPT for slug_status.
203 *
204 * This function modifies the query to filter slugs based on their review status.
205 * It is hooked into the WordPress query system for 'wplng_slug' post type
206 * on the admin edit screen.
207 *
208 * @param object $query The current WP_Query instance.
209 * @return void
210 */
211 function wplng_posts_filter_slug_status( $query ) {
212
213 global $pagenow;
214
215 if ( empty( $_GET['post_type'] )
216 || 'wplng_slug' !== $_GET['post_type']
217 || empty( $_GET['slug_status'] )
218 || ! is_admin()
219 || $pagenow !== 'edit.php'
220 ) {
221 return;
222 }
223
224 // Determine the slug status and set the corresponding meta query.
225 switch ( $_GET['slug_status'] ) {
226 case 'full-reviewed':
227 $query->set(
228 'meta_query',
229 array(
230 'relation' => 'AND',
231 array(
232 'key' => 'wplng_slug_translations',
233 'value' => '"status":\d',
234 'compare' => 'REGEXP',
235 ),
236 array(
237 'key' => 'wplng_slug_translations',
238 'value' => '("status":"ungenerated"|"status":"generated")',
239 'compare' => 'NOT REGEXP',
240 ),
241 )
242 );
243 break;
244
245 case 'partially-reviewed':
246 $query->set(
247 'meta_query',
248 array(
249 'relation' => 'AND',
250 array(
251 'key' => 'wplng_slug_translations',
252 'value' => '"status":\d',
253 'compare' => 'REGEXP',
254 ),
255 array(
256 'key' => 'wplng_slug_translations',
257 'value' => '("status":"ungenerated"|"status":"generated")',
258 'compare' => 'REGEXP',
259 ),
260 )
261 );
262 break;
263
264 case 'reviewed':
265 $query->set(
266 'meta_query',
267 array(
268 'relation' => 'AND',
269 array(
270 'key' => 'wplng_slug_translations',
271 'value' => '"status":\d',
272 'compare' => 'REGEXP',
273 ),
274 )
275 );
276 break;
277
278 case 'unreviewed':
279 $query->set(
280 'meta_query',
281 array(
282 'relation' => 'AND',
283 array(
284 'key' => 'wplng_slug_translations',
285 'value' => '"status":\d',
286 'compare' => 'NOT REGEXP',
287 ),
288 )
289 );
290 break;
291 }
292 }
293
294
295
296
297 /**
298 * Add status custom column on translations
299 *
300 * @param array $columns String array
301 * @return array
302 */
303 function wplng_slug_status_columns( $columns ) {
304
305 // Save the 'cb' column value if it exists
306 $cb = array();
307 if ( isset( $columns['cb'] ) ) {
308 $cb = array(
309 'cb' => $columns['cb'],
310 );
311 unset( $columns['cb'] );
312 }
313
314 // Add the 'wplng_status' column
315 $columns = array_merge(
316 $cb,
317 array(
318 'wplng_status' => __( 'Slug status', 'wplingua' ),
319 ),
320 $columns
321 );
322
323 return $columns;
324 }
325
326
327 /**
328 * Add slug status class on wplng_slug post list in admin area
329 *
330 * @param string[] $classes An array of post class names.
331 * @param string[] $css_class An array of additional class names added to the post.
332 * @param int $post_id The post ID.
333 * @return string[]
334 */
335 function wplng_post_class_slug_status( $classes, $css_class, $post_id ) {
336
337 global $typenow;
338
339 if ( 'wplng_slug' !== $typenow ) {
340 return $classes;
341 }
342
343 $translations = get_post_meta(
344 $post_id,
345 'wplng_slug_translations',
346 true
347 );
348
349 $translations = json_decode(
350 $translations,
351 true
352 );
353
354 if ( empty( $translations ) ) {
355 return $classes;
356 }
357
358 $languages_target_ids = wplng_get_languages_target_ids();
359 $has_a_reviewed = false;
360 $is_not_full_reviewed = false;
361 $checked_language = array();
362 $status_class = 'wplng-status-unreview';
363
364 foreach ( $languages_target_ids as $language_target_id ) {
365 foreach ( $translations as $translation ) {
366 if ( $language_target_id !== $translation['language_id']
367 || empty( $translation['status'] )
368 ) {
369 continue;
370 }
371
372 $checked_language[] = $translation['language_id'];
373
374 if ( 'generated' === $translation['status']
375 || 'ungenerated' === $translation['status']
376 ) {
377 $is_not_full_reviewed = true;
378 } elseif ( is_int( $translation['status'] ) ) {
379 $has_a_reviewed = true;
380 }
381 }
382 }
383
384 if ( ! $is_not_full_reviewed && $has_a_reviewed ) {
385
386 if ( $checked_language === $languages_target_ids ) {
387 $status_class = 'wplng-status-full-review';
388 } else {
389 $status_class = 'wplng-status-has-review';
390 }
391 } elseif ( ! $is_not_full_reviewed || $has_a_reviewed ) {
392 $status_class = 'wplng-status-has-review';
393 }
394
395 if ( 'wplng-status-full-review' === $status_class
396 && $checked_language !== $languages_target_ids
397 ) {
398 $status_class = 'wplng-status-has-review';
399 }
400
401 $classes[] = $status_class;
402
403 return $classes;
404 }
405
406
407 /**
408 * Add status items in custom column on slugs
409 *
410 * @param string $column The name of the column to display.
411 * @param int $post_id The current post ID.
412 * @return void
413 */
414 function wplng_slug_status_item( $column, $post_id ) {
415
416 if ( 'wplng_status' !== $column ) {
417 return;
418 }
419
420 $html = '<span';
421 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-full-review"';
422 $html .= ' title="' . esc_attr__( 'Full reviewed', 'wplingua' ) . '"';
423 $html .= '></span>';
424
425 $html .= '<span';
426 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-has-review"';
427 $html .= ' title="' . esc_attr__( 'Partially reviewed', 'wplingua' ) . '"';
428 $html .= '></span>';
429
430 $html .= '<span';
431 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-unreview"';
432 $html .= ' title="' . esc_attr__( 'Unreviewed', 'wplingua' ) . '"';
433 $html .= '></span>';
434
435 echo $html;
436 }
437
438
439 /**
440 * Add status items text on slugs
441 *
442 * Defaults $actions are 'Edit', ‘Quick Edit’, 'Restore', 'Trash', ‘Delete Permanently’, 'Preview', and 'View'.
443 *
444 * @param string[] $actions An array of row action links.
445 * @param WP_Post $post The post object.
446 * @return string[]
447 */
448 function wplng_post_row_actions_slug_status( $actions, $post ) {
449
450 if ( 'wplng_slug' !== $post->post_type ) {
451 return $actions;
452 }
453
454 $html = esc_html__( 'Slug status: ', 'wplingua' );
455
456 $html .= '<span';
457 $html .= ' class="wplng-status wplng-status-full-review"';
458 $html .= '>';
459 $html .= esc_html__( 'Full reviewed', 'wplingua' );
460 $html .= '</span>';
461
462 $html .= '<span';
463 $html .= ' class="wplng-status wplng-status-has-review"';
464 $html .= '>';
465 $html .= esc_html__( 'Partially reviewed', 'wplingua' );
466 $html .= '</span>';
467
468 $html .= '<span';
469 $html .= ' class="wplng-status wplng-status-unreview"';
470 $html .= '>';
471 $html .= esc_html__( 'Unreviewed', 'wplingua' );
472 $html .= '</span>';
473
474 $actions['wplng-status-text'] = $html;
475
476 return $actions;
477 }
478