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 / slug-cpt.php

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

476 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 * Add status custom column on translations
297 *
298 * @param array $columns String array
299 * @return array
300 */
301 function wplng_slug_status_columns( $columns ) {
302
303 // Save the 'cb' column value if it exists
304 $cb = array();
305 if ( isset( $columns['cb'] ) ) {
306 $cb = array(
307 'cb' => $columns['cb'],
308 );
309 unset( $columns['cb'] );
310 }
311
312 // Add the 'wplng_status' column
313 $columns = array_merge(
314 $cb,
315 array(
316 'wplng_status' => __( 'Slug status', 'wplingua' ),
317 ),
318 $columns
319 );
320
321 return $columns;
322 }
323
324
325 /**
326 * Add slug status class on wplng_slug post list in admin area
327 *
328 * @param string[] $classes An array of post class names.
329 * @param string[] $css_class An array of additional class names added to the post.
330 * @param int $post_id The post ID.
331 * @return string[]
332 */
333 function wplng_post_class_slug_status( $classes, $css_class, $post_id ) {
334
335 global $typenow;
336
337 if ( 'wplng_slug' !== $typenow ) {
338 return $classes;
339 }
340
341 $translations = get_post_meta(
342 $post_id,
343 'wplng_slug_translations',
344 true
345 );
346
347 $translations = json_decode(
348 $translations,
349 true
350 );
351
352 if ( empty( $translations ) ) {
353 return $classes;
354 }
355
356 $languages_target_ids = wplng_get_languages_target_ids();
357 $has_a_reviewed = false;
358 $is_not_full_reviewed = false;
359 $checked_language = array();
360 $status_class = 'wplng-status-unreview';
361
362 foreach ( $languages_target_ids as $language_target_id ) {
363 foreach ( $translations as $translation ) {
364 if ( $language_target_id !== $translation['language_id']
365 || empty( $translation['status'] )
366 ) {
367 continue;
368 }
369
370 $checked_language[] = $translation['language_id'];
371
372 if ( 'generated' === $translation['status']
373 || 'ungenerated' === $translation['status']
374 ) {
375 $is_not_full_reviewed = true;
376 } elseif ( is_int( $translation['status'] ) ) {
377 $has_a_reviewed = true;
378 }
379 }
380 }
381
382 if ( ! $is_not_full_reviewed && $has_a_reviewed ) {
383
384 if ( $checked_language === $languages_target_ids ) {
385 $status_class = 'wplng-status-full-review';
386 } else {
387 $status_class = 'wplng-status-has-review';
388 }
389 } elseif ( ! $is_not_full_reviewed || $has_a_reviewed ) {
390 $status_class = 'wplng-status-has-review';
391 }
392
393 if ( 'wplng-status-full-review' === $status_class
394 && $checked_language !== $languages_target_ids
395 ) {
396 $status_class = 'wplng-status-has-review';
397 }
398
399 $classes[] = $status_class;
400
401 return $classes;
402 }
403
404
405 /**
406 * Add status items in custom column on slugs
407 *
408 * @param string $column The name of the column to display.
409 * @param int $post_id The current post ID.
410 * @return void
411 */
412 function wplng_slug_status_item( $column, $post_id ) {
413
414 if ( 'wplng_status' !== $column ) {
415 return;
416 }
417
418 $html = '<span';
419 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-full-review"';
420 $html .= ' title="' . esc_attr__( 'Full reviewed', 'wplingua' ) . '"';
421 $html .= '></span>';
422
423 $html .= '<span';
424 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-has-review"';
425 $html .= ' title="' . esc_attr__( 'Partially reviewed', 'wplingua' ) . '"';
426 $html .= '></span>';
427
428 $html .= '<span';
429 $html .= ' class="dashicons dashicons-yes-alt wplng-status wplng-status-unreview"';
430 $html .= ' title="' . esc_attr__( 'Unreviewed', 'wplingua' ) . '"';
431 $html .= '></span>';
432
433 echo $html;
434 }
435
436
437 /**
438 * Add status items text on slugs
439 *
440 * Defaults $actions are 'Edit', ‘Quick Edit’, 'Restore', 'Trash', ‘Delete Permanently’, 'Preview', and 'View'.
441 *
442 * @param string[] $actions An array of row action links.
443 * @param WP_Post $post The post object.
444 * @return string[]
445 */
446 function wplng_post_row_actions_slug_status( $actions, $post ) {
447
448 if ( 'wplng_slug' !== $post->post_type ) {
449 return $actions;
450 }
451
452 $html = esc_html__( 'Slug status: ', 'wplingua' );
453
454 $html .= '<span';
455 $html .= ' class="wplng-status wplng-status-full-review"';
456 $html .= '>';
457 $html .= esc_html__( 'Full reviewed', 'wplingua' );
458 $html .= '</span>';
459
460 $html .= '<span';
461 $html .= ' class="wplng-status wplng-status-has-review"';
462 $html .= '>';
463 $html .= esc_html__( 'Partially reviewed', 'wplingua' );
464 $html .= '</span>';
465
466 $html .= '<span';
467 $html .= ' class="wplng-status wplng-status-unreview"';
468 $html .= '>';
469 $html .= esc_html__( 'Unreviewed', 'wplingua' );
470 $html .= '</span>';
471
472 $actions['wplng-status-text'] = $html;
473
474 return $actions;
475 }
476