]> _ Git - physioassist-wordpress.git/blob
fc33d297a4b946e5829147f4413a1172e7ff3946
[physioassist-wordpress.git] /
1 <?php
2
3 use function WPML\Container\make;
4 use WPML\TM\Menu\TranslationQueue\CloneJobs;
5
6 class WPML_Translations_Queue {
7
8         /** @var  SitePress $sitepress */
9         private $sitepress;
10
11         /* @var WPML_UI_Screen_Options_Pagination */
12         private $screen_options;
13
14         /** @var WPML_Admin_Table_Sort $table_sort */
15         private $table_sort;
16
17         private $must_render_the_editor = false;
18
19         /** @var WPML_Translation_Editor_UI */
20         private $translation_editor;
21
22         /**
23          * @var CloneJobs
24          */
25         private $clone_jobs;
26
27         /**
28          * @param SitePress                      $sitepress
29          * @param WPML_UI_Screen_Options_Factory $screen_options_factory
30          * @param CloneJobs                      $clone_jobs
31          */
32         public function __construct(
33                 $sitepress,
34                 $screen_options_factory,
35                 CloneJobs $clone_jobs
36         ) {
37                 $this->sitepress      = $sitepress;
38                 $this->screen_options = $screen_options_factory->create_pagination(
39                         'tm_translations_queue_per_page',
40                         ICL_TM_DOCS_PER_PAGE
41                 );
42                 $this->table_sort     = $screen_options_factory->create_admin_table_sort();
43                 $this->clone_jobs     = $clone_jobs;
44         }
45
46         public function init_hooks() {
47                 add_action( 'current_screen', array( $this, 'load' ) );
48         }
49
50         public function load() {
51                 if ( $this->must_open_the_editor() ) {
52                         $job_id     = $this->get_job_id_from_request();
53                         $job_object = wpml_tm_load_job_factory()->get_translation_job( $job_id, false, 0, true );
54                         if ( $job_object ) {
55                                 if ( $job_object->get_translator_id() <= 0 ) {
56                                         $job_object->assign_to( $this->sitepress->get_wp_api()->get_current_user_id() );
57                                 }
58
59                                 if ( (int) $job_object->get_status_value() !== ICL_TM_COMPLETE ) {
60                                         $this->mark_job_as_in_progress( $job_object );
61                                 }
62
63                                 if ( $job_object->user_can_translate( wp_get_current_user() ) ) {
64                                         $this->clone_jobs->cloneCompletedJob( $job_id, $job_object );
65                                         $this->clone_jobs->maybeCloneWPMLJob( $job_id );
66
67                                         $this->attempt_opening_ATE( $job_id );
68
69                                         wpml_tm_load_old_jobs_editor()->set( $job_id, WPML_TM_Editors::WPML );
70
71                                         global $wpdb;
72                                         $this->must_render_the_editor = true;
73                                         $this->translation_editor     = new WPML_Translation_Editor_UI(
74                                                 $wpdb,
75                                                 $this->sitepress,
76                                                 wpml_load_core_tm(),
77                                                 $job_object,
78                                                 new WPML_TM_Job_Action_Factory( wpml_tm_load_job_factory() ),
79                                                 new WPML_TM_Job_Layout( $wpdb, $this->sitepress->get_wp_api() )
80                                         );
81                                 }
82                         }
83                 }
84         }
85
86         /**
87          * @param array $icl_translation_filter
88          *
89          * @throws \InvalidArgumentException
90          */
91         public function display( array $icl_translation_filter = array() ) {
92                 if ( $this->must_render_the_editor ) {
93                         $this->translation_editor->render();
94
95                         return;
96                 }
97
98                 /**
99                  * @var TranslationManagement        $iclTranslationManagement
100                  * @var WPML_Translation_Job_Factory $wpml_translation_job_factory
101                  */
102                 global $iclTranslationManagement, $wpml_translation_job_factory;
103
104                 $translation_jobs = array();
105                 $job_types        = array();
106                 $langs_from       = array();
107                 $lang_from        = array();
108                 $langs_to         = array();
109                 $lang_to          = array();
110                 $job_id           = null;
111
112                 if ( ! empty( $_GET['resigned'] ) ) {
113                         $iclTranslationManagement->add_message(
114                                 array(
115                                         'type' => 'updated',
116                                         'text' => __(
117                                                 "You've resigned from this job.",
118                                                 'wpml-translation-management'
119                                         ),
120                                 )
121                         );
122                 }
123
124                 $cookie_filters = self::get_cookie_filters();
125
126                 if ( $cookie_filters ) {
127                         $icl_translation_filter = $cookie_filters;
128                 }
129
130                 $current_translator = $iclTranslationManagement->get_current_translator();
131                 $can_translate      = $current_translator && $current_translator->ID > 0 && $current_translator->language_pairs;
132                 $post_link_factory  = new WPML_TM_Post_Link_Factory( $this->sitepress );
133                 if ( $can_translate ) {
134                         $icl_translation_filter['translator_id']      = $current_translator->ID;
135                         $icl_translation_filter['include_unassigned'] = true;
136
137                         $element_type_prefix = isset( $_GET['element_type'] ) ? $_GET['element_type'] : 'post';
138                         if ( isset( $_GET['updated'] ) && $_GET['updated'] ) {
139                                 $tm_post_link_updated = $post_link_factory->view_link( $_GET['updated'] );
140                                 if ( $iclTranslationManagement->is_external_type( $element_type_prefix ) ) {
141                                         $tm_post_link_updated = apply_filters(
142                                                 'wpml_external_item_link',
143                                                 $tm_post_link_updated,
144                                                 $_GET['updated'],
145                                                 false
146                                         );
147                                 }
148                                 $user_message = __( 'Translation updated: ', 'wpml-translation-management' ) . $tm_post_link_updated;
149                                 $iclTranslationManagement->add_message(
150                                         array(
151                                                 'type' => 'updated',
152                                                 'text' => $user_message,
153                                         )
154                                 );
155                         } elseif ( isset( $_GET['added'] ) && $_GET['added'] ) {
156                                 $tm_post_link_added = $post_link_factory->view_link( $_GET['added'] );
157                                 if ( $iclTranslationManagement->is_external_type( $element_type_prefix ) ) {
158                                         $tm_post_link_added = apply_filters(
159                                                 'wpml_external_item_link',
160                                                 $tm_post_link_added,
161                                                 $_GET['added'],
162                                                 false
163                                         );
164                                 }
165                                 $user_message = __( 'Translation added: ', 'wpml-translation-management' ) . $tm_post_link_added;
166                                 $iclTranslationManagement->add_message(
167                                         array(
168                                                 'type' => 'updated',
169                                                 'text' => $user_message,
170                                         )
171                                 );
172                         } elseif ( isset( $_GET['job-cancelled'] ) ) {
173                                 $user_message = __( 'Translation has been removed by admin', 'wpml-translation-management' );
174                                 $iclTranslationManagement->add_message(
175                                         array(
176                                                 'type' => 'error',
177                                                 'text' => $user_message,
178                                         )
179                                 );
180                         }
181
182                         if ( isset( $_GET['title'] ) && $_GET['title'] ) {
183                                 $icl_translation_filter['title'] = filter_var( $_GET['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
184                         }
185
186                         if ( ! empty( $current_translator->language_pairs ) ) {
187                                 $_langs_to = array();
188                                 if ( 1 < count( $current_translator->language_pairs ) ) {
189                                         foreach ( $current_translator->language_pairs as $lang => $to ) {
190                                                 $langs_from[] = $this->sitepress->get_language_details( $lang );
191                                                 $_langs_to    = array_merge( (array) $_langs_to, array_keys( $to ) );
192                                         }
193                                         $_langs_to = array_unique( $_langs_to );
194                                 } else {
195                                         $_langs_to                      = array_keys( current( $current_translator->language_pairs ) );
196                                         $lang_from                      = $this->sitepress->get_language_details( key( $current_translator->language_pairs ) );
197                                         $icl_translation_filter['from'] = $lang_from['code'];
198                                 }
199
200                                 if ( 1 < count( $_langs_to ) ) {
201                                         foreach ( $_langs_to as $lang ) {
202                                                 $langs_to[] = $this->sitepress->get_language_details( $lang );
203                                         }
204                                 } else {
205                                         $lang_to                      = $this->sitepress->get_language_details( current( $_langs_to ) );
206                                         $icl_translation_filter['to'] = $lang_to['code'];
207                                 }
208                                 $job_types = $wpml_translation_job_factory->get_translation_job_types_filter(
209                                         array(),
210                                         array(
211                                                 'translator_id'      => $current_translator->ID,
212                                                 'include_unassigned' => true,
213                                         )
214                                 );
215
216                                 if ( isset( $_GET['orderby'] ) ) {
217                                         $icl_translation_filter['order_by'] = filter_var( $_GET['orderby'], FILTER_SANITIZE_STRING );
218                                 }
219
220                                 if ( isset( $_GET['order'] ) ) {
221                                         $icl_translation_filter['order'] = filter_var( $_GET['order'], FILTER_SANITIZE_STRING );
222                                 }
223
224                                 $translation_jobs = $wpml_translation_job_factory->get_translation_jobs( (array) $icl_translation_filter );
225                         }
226                 }
227                 ?>
228                 <div class="wrap">
229                         <h2><?php echo __( 'Translations queue', 'wpml-translation-management' ); ?></h2>
230
231                         <?php if ( empty( $current_translator->language_pairs ) ) : ?>
232                                 <div class="error below-h2"><p><?php _e( 'No translation languages configured for this user.', 'wpml-translation-management' ); ?></p></div>
233                         <?php endif; ?>
234                         <?php do_action( 'icl_tm_messages' ); ?>
235
236                         <?php if ( ! empty( $current_translator->language_pairs ) ) : ?>
237
238                                 <div class="alignright">
239                                         <form method="post"
240                                                   name="translation-jobs-filter"
241                                                   id="tm-queue-filter"
242                                                   action="admin.php?page=<?php echo WPML_TM_FOLDER; ?>/menu/translations-queue.php">
243                                                 <input type="hidden" name="icl_tm_action" value="ujobs_filter"/>
244                                                 <table class="">
245                                                         <tbody>
246                                                         <tr valign="top">
247                                                                 <td>
248                                                                         <select name="filter[type]">
249                                                                                 <option value=""><?php _e( 'All types', 'wpml-translation-management' ); ?></option>
250                                                                                 <?php foreach ( $job_types as $job_type => $job_type_name ) : ?>
251                                                                                         <option value="<?php echo $job_type; ?>" 
252                                                                                                                                           <?php
253                                                                                                                                                 if ( ! empty( $icl_translation_filter['type'] )
254                                                                                                                                                 && $icl_translation_filter['type']
255                                                                                                                                                 === $job_type ) :
256
257                                                                                                                                                         ?>
258                                                                                                         selected="selected"<?php endif; ?>><?php echo $job_type_name; ?></option>
259                                                                                 <?php endforeach; ?>
260                                                                         </select>&nbsp;
261                                                                         <label>
262                                                                                 <strong><?php _e( 'From', 'wpml-translation-management' ); ?></strong>
263                                                                                 <?php
264                                                                                 if ( 1 < count( $current_translator->language_pairs ) ) {
265
266                                                                                         $from_select = new WPML_Simple_Language_Selector( $this->sitepress );
267                                                                                         echo $from_select->render(
268                                                                                                 array(
269                                                                                                         'name' => 'filter[from]',
270                                                                                                         'please_select_text' => __(
271                                                                                                                 'Any language',
272                                                                                                                 'wpml-translation-management'
273                                                                                                         ),
274                                                                                                         'style' => '',
275                                                                                                         'languages' => $langs_from,
276                                                                                                         'selected' => isset( $icl_translation_filter['from'] )
277                                                                                                                         ? $icl_translation_filter['from'] : '',
278                                                                                                 )
279                                                                                         );
280                                                                                 } else {
281                                                                                         ?>
282                                                                                         <input type="hidden"
283                                                                                                    name="filter[from]"
284                                                                                                    value="<?php echo esc_attr( $lang_from['code'] ); ?>"/>
285                                                                                         <?php
286                                                                                         echo $this->sitepress->get_flag_img( $lang_from['code'] )
287                                                                                                            . ' '
288                                                                                                            . $lang_from['display_name'];
289                                                                                         ?>
290                                                                                 <?php } ?>
291                                                                         </label>&nbsp;
292                                                                         <label>
293                                                                                 <strong><?php _e( 'To', 'wpml-translation-management' ); ?></strong>
294                                                                                 <?php
295                                                                                 if ( 1 < @count( $langs_to ) ) {
296                                                                                         $to_select = new WPML_Simple_Language_Selector( $this->sitepress );
297                                                                                         echo $to_select->render(
298                                                                                                 array(
299                                                                                                         'name' => 'filter[to]',
300                                                                                                         'please_select_text' => __(
301                                                                                                                 'Any language',
302                                                                                                                 'wpml-translation-management'
303                                                                                                         ),
304                                                                                                         'style' => '',
305                                                                                                         'languages' => $langs_to,
306                                                                                                         'selected' => isset( $icl_translation_filter['to'] )
307                                                                                                                         ? $icl_translation_filter['to'] : '',
308                                                                                                 )
309                                                                                         );
310                                                                                 } else {
311                                                                                         ?>
312                                                                                         <input type="hidden" name="filter[to]"
313                                                                                                    value="<?php echo esc_attr( $lang_to['code'] ); ?>"/>
314                                                                                         <?php
315                                                                                         echo $this->sitepress->get_flag_img( $lang_to['code'] ) . ' ' . $lang_to['display_name'];
316                                                                                 }
317
318                                                                                 $translation_filter_status = null;
319                                                                                 if ( array_key_exists( 'status', $icl_translation_filter ) ) {
320                                                                                         $translation_filter_status = (int) $icl_translation_filter['status'];
321                                                                                 }
322
323                                                                                 ?>
324                                                                         </label>
325                                                                         &nbsp;
326                                                                         <select name="filter[status]">
327                                                                                 <option value=""><?php _e( 'All statuses', 'wpml-translation-management' ) ?></option>
328                                                                                 <option value="<?php echo ICL_TM_COMPLETE; ?>" 
329                                                                                                                                   <?php
330                                                                                                                                         if ( $translation_filter_status === ICL_TM_COMPLETE ) :
331
332                                                                                                                                                 ?>
333                                                                                 selected="selected"<?php endif; ?>><?php echo TranslationManagement::status2text( ICL_TM_COMPLETE ); ?></option>
334                                                                                 <option value="<?php echo ICL_TM_IN_PROGRESS; ?>" 
335                                                                                                                                   <?php
336                                                                                                                                         if ( $translation_filter_status
337                                                                                                                                         === ICL_TM_IN_PROGRESS ) :
338
339                                                                                                                                                 ?>
340                                                                                          selected="selected"<?php endif; ?>><?php echo TranslationManagement::status2text( ICL_TM_IN_PROGRESS ); ?></option>
341                                                                                 <option value="<?php echo ICL_TM_WAITING_FOR_TRANSLATOR; ?>" 
342                                                                                                                                   <?php
343                                                                                                                                         if ( $translation_filter_status === ICL_TM_WAITING_FOR_TRANSLATOR ) :
344
345                                                                                                                                                 ?>
346                                                                                 selected="selected"<?php endif; ?>><?php _e( 'Available to translate', 'wpml-translation-management' ) ?></option>
347                                                                         </select>
348                                                                         &nbsp;
349                                                                         <input class="button-secondary"
350                                                                                    type="submit"
351                                                                                    value="<?php _e( 'Filter', 'wpml-translation-management' ); ?>"/>
352                                                                 </td>
353                                                         </tr>
354                                                         </tbody>
355                                                 </table>
356                                         </form>
357                                 </div>
358                                 <?php
359                                 $actions = apply_filters( 'wpml_translation_queue_actions', array() );
360
361                                 /**
362                                  * @deprecated Use 'wpml_translation_queue_actions' instead
363                                  */
364                                 $actions = apply_filters( 'WPML_translation_queue_actions', $actions );
365                                 ?>
366                                 <?php if ( count( $actions ) > 0 ) : ?>
367                                         <form method="post" name="translation-jobs-action" action="admin.php?page=<?php echo WPML_TM_FOLDER; ?>/menu/translations-queue.php">
368                                 <?php endif; ?>
369
370                                 <?php
371                                 do_action( 'wpml_xliff_select_actions', $actions, 'action', $translation_jobs );
372
373                                 /**
374                                  * @deprecated Use 'wpml_xliff_select_actions' instead
375                                  */
376                                 do_action( 'WPML_xliff_select_actions', $actions, 'action', $translation_jobs );
377                                 ?>
378
379                                 <?php
380
381                                 $translation_queue_pagination = new WPML_Translations_Queue_Pagination_UI(
382                                         $translation_jobs,
383                                         $this->screen_options->get_items_per_page()
384                                 );
385                                 $translation_jobs             = $translation_queue_pagination->get_paged_jobs();
386
387                                 ?>
388                                 <?php // pagination - end ?>
389
390                                 <?php
391                                 $blog_translators = wpml_tm_load_blog_translators();
392                                 $tm_api           = new WPML_TM_API( $blog_translators, $iclTranslationManagement );
393
394                                 $translation_queue_jobs_model = new WPML_Translations_Queue_Jobs_Model(
395                                         $this->sitepress,
396                                         $iclTranslationManagement,
397                                         $tm_api,
398                                         \WPML\TM\Jobs\Utils\ElementLinkFactory::create(),
399                                         $translation_jobs
400                                 );
401                                 $translation_jobs             = $translation_queue_jobs_model->get();
402
403                                 $this->show_table( $translation_jobs, count( $actions ) > 0, $job_id );
404                                 ?>
405
406                                 <div id="tm-queue-pagination" class="tablenav">
407                                         <?php $translation_queue_pagination->show(); ?>
408
409                                         <?php
410                                         do_action( 'wpml_xliff_select_actions', $actions, 'action2', $translation_jobs );
411
412                                         /**
413                                          * @deprecated Use 'wpml_xliff_select_actions' instead
414                                          */
415                                         do_action( 'WPML_xliff_select_actions', $actions, 'action2', $translation_jobs );
416                                         ?>
417                                 </div>
418                                 <?php // pagination - end ?>
419
420                                 <?php if ( count( $actions ) > 0 ) : ?>
421                                         </form>
422                                 <?php endif; ?>
423
424                                 <?php do_action( 'wpml_translation_queue_after_display', $translation_jobs ); ?>
425
426                         <?php endif; ?>
427                 </div>
428
429                 <?php
430                 // Check for any bulk actions
431                 if ( isset( $_POST['action'] ) || isset( $_POST['action2'] ) ) {
432                         $xliff_version = isset( $_POST['doaction'] ) ? $_POST['action'] : $_POST['action2'];
433                         do_action( 'wpml_translation_queue_do_actions_export_xliff', $_POST, $xliff_version );
434
435                         /**
436                          * @deprecated Use 'wpml_translation_queue_do_actions_export_xliff' instead
437                          */
438                         do_action( 'WPML_translation_queue_do_actions_export_xliff', $_POST, $xliff_version );
439                 }
440         }
441
442         /**
443          * @param $translation_jobs
444          * @param $has_actions
445          * @param $open_job
446          */
447         public function show_table( $translation_jobs, $has_actions, $open_job ) {
448                 ?>
449                         <table class="widefat striped icl-translation-jobs" id="icl-translation-jobs" cellspacing="0"
450                                 data-string-complete="<?php esc_attr_e( 'Complete', 'wpml-translation-management' ); ?>"
451                                 data-string-edit="<?php esc_attr_e( 'Edit', 'wpml-translation-management' ); ?>"
452                         >
453                 <?php foreach ( array( 'thead', 'tfoot' ) as $element_type ) { ?>
454                                 <<?php echo $element_type; ?>>
455                                 <tr>
456                                         <?php if ( $has_actions ) { ?>
457                                         <td class="manage-column column-cb check-column js-check-all" scope="col">
458                                                 <input title="<?php echo esc_attr( $translation_jobs['strings']['check_all'] ); ?>"
459                                                            type="checkbox"/>
460                                         </td>
461                                         <?php } ?>
462                                         <th scope="col" class="cloumn-job_id <?php echo $this->table_sort->get_column_classes( 'job_id' ); ?>">
463                                                 <a href="<?php echo $this->table_sort->get_column_url( 'job_id' ); ?>">
464                                                         <span><?php echo esc_html( $translation_jobs['strings']['job_id'] ); ?></span>
465                                                         <span class="sorting-indicator"></span>
466                                                 </a>
467                                         </th>
468                                         <th scope="col"
469                                                 class="column-title"><?php echo esc_html( $translation_jobs['strings']['title'] ); ?></th>
470                                         <th scope="col"
471                                                 class="column-type"><?php echo esc_html( $translation_jobs['strings']['type'] ); ?></th>
472                                         <th scope="col"
473                                                 class="column-language"><?php echo esc_html( $translation_jobs['strings']['language'] ); ?></th>
474                                         <th scope="col"
475                                                 class="column-status"><?php echo esc_html( $translation_jobs['strings']['status'] ); ?></th>
476                                         <th scope="col"
477                                                 class="column-deadline <?php echo $this->table_sort->get_column_classes( 'deadline' ); ?>">
478                                                 <a href="<?php echo $this->table_sort->get_column_url( 'deadline' ); ?>">
479                                                         <span><?php echo esc_html( $translation_jobs['strings']['deadline'] ); ?></span>
480                                                         <span class="sorting-indicator"></span>
481                                                 </a>
482                                         </th>
483                                         <th scope="col"
484                                                 class="column-actions"></th>
485                                 </tr>
486                                 </<?php echo $element_type; ?>>
487                 <?php } ?>
488
489                         <tbody>
490                 <?php if ( empty( $translation_jobs['jobs'] ) ) { ?>
491                                 <tr>
492                                         <td colspan="7"
493                                                 align="center"><?php _e( 'No translation jobs found', 'wpml-translation-management' ); ?></td>
494                                 </tr>
495                         <?php
496                 } else {
497
498                         $ate_jobs = apply_filters( 'wpml_tm_ate_jobs_data', array(), $translation_jobs['jobs'] );
499
500                         foreach ( $translation_jobs['jobs'] as $index => $job ) {
501                                 ?>
502                                                 <tr<?php echo $this->get_row_css_attribute( $job ); ?>>
503                                                           <?php if ( $has_actions ) { ?>
504                                                         <td>
505                                                                         <input type="checkbox" name="job[<?php echo $job->job_id; ?>]" value="1"/>
506                                                         </td>
507                                                         <?php } ?>
508                                                         <td class="column-job_id"><?php echo $job->job_id; ?></td>
509                                                         <td class="column-title">
510                                                                   <?php echo esc_html( $job->post_title ); ?>
511                                                                 <div class="row-actions">
512                                                                         <span class="view"><?php echo $job->tm_post_link; ?></span>
513                                                                 </div>
514                                                         </td>
515                                                         <td class="column-type" data-colname=""><?php echo esc_html( $job->post_type ); ?></td>
516                                                         <td class="column-languages"><?php echo $job->lang_text_with_flags; ?></td>
517                                                         <td class="column-status"><span><i class="<?php echo esc_attr( $job->icon ); ?>"></i><?php echo esc_html( $job->status_text ); ?></span></td>
518                                                         <td class="column-deadline">
519                                                                 <?php
520                                                                 if ( $job->deadline_date ) {
521                                                                         if ( '0000-00-00 00:00:00' === $job->deadline_date ) {
522                                                                                 $deadline_day = __( 'Not set', 'wpml-translation-management' );
523                                                                         } else {
524                                                                                 $deadline_day = date( 'Y-m-d', strtotime( $job->deadline_date ) );
525                                                                         }
526                                                                         echo esc_html( $deadline_day );
527                                                                 }
528                                                                 ?>
529                                                         </td>
530                                                         <td class="column-actions">
531                                         <?php
532                                         if ( $job->original_doc_id ) {
533                                                 $ate_job_id = null;
534                                                 if ( array_key_exists( $job->job_id, $ate_jobs ) ) {
535                                                         if ( array_key_exists( 'ate_job_id', $ate_jobs[ $job->job_id ] ) ) {
536                                                                 $ate_job_id = $ate_jobs[ $job->job_id ]['ate_job_id'];
537                                                         }
538                                                 }
539                                                 ?>
540                                                                                 <a class="button-secondary js-translation-queue-edit"
541                                                                                    href="<?php echo esc_attr( $job->edit_url ); ?>"
542                                                                                    data-job-id="<?php echo $job->job_id; ?>"
543                                                                                    data-ate-job-id="<?php echo esc_attr( $ate_job_id ); ?>"
544                                                                                    data-ate-job-url="<?php echo $job->edit_url; ?>"
545                                                                                    data-ate-auto-open="<?php echo $job->job_id === $open_job; ?>"
546                                                                                 >
547                                                 <?php echo $job->button_text; ?>
548                                                                                 </a>
549                                                 <?php
550                                         }
551                                         ?>
552                                                                 <?php if ( $job->is_doing_job ) { ?>
553                                                                         <br>
554                                                                         <a class="link-resign"
555                                                                            href="<?php echo esc_attr( $job->resign_url ); ?>"
556                                                                            onclick="if ( !confirm( '<?php echo esc_js( $translation_jobs['strings']['confirm'] ); ?>' ) ) { return false; }">
557                                                                                 <?php echo $job->resign_text; ?>
558                                                                         </a>
559                                                                 <?php } ?>
560                                                                 <?php
561                                                                 if ( $job->view_link ) {
562                                                                         echo '<br>';
563                                                                         echo $job->view_link;
564                                                                 }
565                                                                 ?>
566                                                         </td>
567                                                 </tr>
568                                 <?php
569                         }
570                 }
571                 ?>
572                         </tbody>
573                         </table>
574                 <?php
575         }
576
577         private function get_job_id_from_request() {
578                 /**
579                  * @var TranslationManagement        $iclTranslationManagement
580                  * @var WPML_Post_Translation        $wpml_post_translations
581                  * @var WPML_Translation_Job_Factory $wpml_translation_job_factory
582                  */
583                 global $iclTranslationManagement, $wpml_post_translations, $wpml_translation_job_factory, $sitepress;
584
585                 $job_id = filter_var( isset( $_GET['job_id'] ) ? $_GET['job_id'] : '', FILTER_SANITIZE_NUMBER_INT );
586
587                 list( $trid, $update_needed, $language_code, $element_type ) = $this->get_job_data_for_restore( $job_id );
588                 $source_language_code                                        = filter_var( isset( $_GET['source_language_code'] ) ? $_GET['source_language_code'] : '', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
589
590                 if ( $trid && $language_code ) {
591                         if ( ! $job_id ) {
592                                 $job_id = $iclTranslationManagement->get_translation_job_id(
593                                         $trid,
594                                         $language_code
595                                 );
596                                 if ( ! $job_id ) {
597                                         if ( ! $source_language_code ) {
598                                                 $post_id = SitePress::get_original_element_id_by_trid( $trid );
599                                         } else {
600                                                 $posts_in_trid = $wpml_post_translations->get_element_translations(
601                                                         false,
602                                                         $trid
603                                                 );
604                                                 $post_id       = isset( $posts_in_trid[ $source_language_code ] ) ? $posts_in_trid[ $source_language_code ] : false;
605                                         }
606                                         $blog_translators = wpml_tm_load_blog_translators();
607                                         $args             = array(
608                                                 'lang_from' => $source_language_code,
609                                                 'lang_to'   => $language_code,
610                                                 'job_id'    => $job_id,
611                                         );
612                                         if ( $post_id && $blog_translators->is_translator(
613                                                 $sitepress->get_current_user()->ID,
614                                                 $args
615                                         )
616                                         ) {
617                                                 $job_id = $wpml_translation_job_factory->create_local_post_job(
618                                                         $post_id,
619                                                         $language_code
620                                                 );
621                                         }
622                                 }
623                         } elseif ( $update_needed ) {
624                                 $element_id = SitePress::get_original_element_id_by_trid( $trid );
625                                 $job_id     = $wpml_translation_job_factory->create_local_job( $element_id, $language_code, null, $element_type );
626                         }
627                 }
628
629                 return $job_id;
630         }
631
632         /**
633          * @param $job_id
634          *
635          * @return array ( trid, updated_needed, language_code )
636          */
637         private function get_job_data_for_restore( $job_id ) {
638                 $fields = array( 'trid', 'update_needed', 'language_code', 'element_type' );
639                 $result = array_fill_keys( $fields, false );
640
641                 if ( isset( $_GET['trid'] ) ) {
642                         $result['trid'] = filter_var( $_GET['trid'], FILTER_SANITIZE_NUMBER_INT );
643                 }
644                 if ( isset( $_GET['update_needed'] ) ) {
645                         $result['update_needed'] = filter_var( $_GET['update_needed'], FILTER_SANITIZE_NUMBER_INT );
646                 }
647                 if ( isset( $_GET['language_code'] ) ) {
648                         $result['language_code'] = filter_var( $_GET['language_code'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
649                 }
650
651                 $wpdb = $this->sitepress->get_wpdb();
652
653                 if ( isset( $result['trid'] ) ) {
654                         $element_type_query = $wpdb->prepare(
655                                 "SELECT element_type FROM {$wpdb->prefix}icl_translations WHERE trid = %d LIMIT 1",
656                                 $result['trid']
657                         );
658
659                         $result['element_type'] = $wpdb->get_var( $element_type_query );
660                 }
661
662                 if ( ! $job_id || isset( $_GET['trid'], $_GET['update_needed'], $_GET['language_code'], $result['element_type'] ) ) {
663                         return array(
664                                 $result['trid'],
665                                 $result['update_needed'],
666                                 $result['language_code'],
667                                 $result['element_type'],
668                         );
669                 }
670
671                 $sql = "
672                 SELECT t.trid, ts.needs_update as update_needed, t.language_code, t.element_type
673             FROM {$wpdb->prefix}icl_translations t
674             INNER JOIN {$wpdb->prefix}icl_translation_status ts on ts.translation_id = t.translation_id
675             INNER JOIN {$wpdb->prefix}icl_translate_job j ON j.rid = ts.rid
676             WHERE j.job_id = %d;
677             ";
678
679                 $db_result = $wpdb->get_row( $wpdb->prepare( $sql, $job_id ), ARRAY_A );
680
681                 foreach ( $fields as $field ) {
682                         if ( ! isset( $_GET[ $field ] ) ) {
683                                 $result[ $field ] = $db_result[ $field ];
684                         }
685                 }
686
687                 return array( $result['trid'], $result['update_needed'], $result['language_code'], $result['element_type'] );
688         }
689
690         /**
691          * @param stdClass $job
692          *
693          * @return string
694          */
695         private function get_row_css_attribute( $job ) {
696                 $classes = [ 'js-wpml-job-row' ];
697
698                 if ( isset( $job->deadline_date ) && ICL_TM_COMPLETE !== (int) $job->status ) {
699                         $deadline_day = date( 'Y-m-d', strtotime( $job->deadline_date ) );
700                         $today        = date( 'Y-m-d' );
701
702                         if ( $deadline_day < $today ) {
703                                 $classes[] = 'overdue';
704                         }
705                 }
706
707                 return ' class="' . esc_attr( implode( ' ', $classes ) ) . '" data-job-id="' . $job->job_id . '"';
708         }
709
710         /**
711          * @return bool
712          */
713         private function must_open_the_editor() {
714                 return ( isset( $_GET['job_id'] ) && $_GET['job_id'] > 0 )
715                            || ( isset( $_GET['trid'] ) && $_GET['trid'] > 0 );
716         }
717
718         /**
719          * @param $job_id
720          */
721         private function attempt_opening_ATE( $job_id ) {
722                 if ( ! WPML_TM_ATE_Status::is_enabled_and_activated() ) {
723                         return;
724                 }
725
726                 // a job already exists
727                 if ( isset( $_GET['job_id'] ) && $_GET['job_id'] > 0 ) {
728                         $current_editor = wpml_tm_load_old_jobs_editor()->get( $job_id );
729                         if ( WPML_TM_Editors::ATE !== $current_editor && WPML_TM_Editors::NONE !== $current_editor ) {
730                                 return;
731                         }
732                 }
733
734                 $editor_url = apply_filters( 'wpml_tm_ate_jobs_editor_url', null, $job_id, $this->get_return_url() );
735
736                 if ( $editor_url ) {
737                         make( \WPML\TM\ATE\Sync\Trigger::class )->setSyncRequiredForCurrentUser();
738                         wpml_tm_load_old_jobs_editor()->set( $job_id, WPML_TM_Editors::ATE );
739
740                         if ( wp_safe_redirect( $editor_url, 302, 'WPML' ) ) {
741                                 exit;
742                         }
743                 }
744         }
745
746         /**
747          * @return mixed|null|string
748          */
749         private function get_return_url() {
750                 $return_url = null;
751
752                 if ( array_key_exists( 'return_url', $_GET ) ) {
753                         $return_url = filter_var( $_GET['return_url'], FILTER_SANITIZE_URL );
754
755                         $return_url_parts = wp_parse_url( $return_url );
756
757                         $admin_url       = get_admin_url();
758                         $admin_url_parts = wp_parse_url( $admin_url );
759
760                         if ( strpos( $return_url_parts['path'], $admin_url_parts['path'] ) === 0 ) {
761                                 $admin_url_parts['path'] = $return_url_parts['path'];
762                         } else {
763                                 $admin_url_parts = $return_url_parts;
764                         }
765
766                         if ( array_key_exists( 'query', $return_url_parts ) ) {
767                                 $admin_url_parts['query'] = $this->filterQueryParameters( $return_url_parts['query'] );
768                         }
769
770                         $return_url = http_build_url( $admin_url_parts );
771                 }
772
773                 return $return_url;
774         }
775
776         private function filterQueryParameters( $query ) {
777                 $parameters = [];
778                 parse_str( $query, $parameters );
779
780                 unset( $parameters['ate_original_id'] );
781                 unset( $parameters['back'] );
782                 unset( $parameters['complete'] );
783
784                 return http_build_query( $parameters );
785         }
786
787         /**
788          * @return array
789          */
790         public static function get_cookie_filters() {
791                 $filters = array();
792
793                 if ( isset( $_COOKIE['wp-translation_ujobs_filter'] ) ) {
794                         parse_str( $_COOKIE['wp-translation_ujobs_filter'], $filters );
795
796                         $filters = filter_var_array(
797                                 $filters,
798                                 array(
799                                         'type'   => FILTER_SANITIZE_STRING,
800                                         'from'   => FILTER_SANITIZE_STRING,
801                                         'to'     => FILTER_SANITIZE_STRING,
802                                         'status' => FILTER_SANITIZE_NUMBER_INT,
803                                 )
804                         );
805                 }
806
807                 return $filters;
808         }
809
810         /**
811          * @param  \WPML_Translation_Job $job_object
812          */
813         private function mark_job_as_in_progress( \WPML_Translation_Job $job_object ) {
814                 wpml_load_core_tm()->update_translation_status(
815                         [
816                                 'translation_id' => $job_object->get_translation_id(),
817                                 'status'         => ICL_TM_IN_PROGRESS,
818                         ]
819                 );
820         }
821 }