class-wpml-tp-sync-update-job.php
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use WPML\FP\Relation;
use \WPML\LIB\WP\Post;
class WPML_TP_Sync_Update_Job {
private $strategies = array(
WPML_TM_Job_Entity::POST_TYPE => 'update_post_job',
WPML_TM_Job_Entity::STRING_TYPE => 'update_string_job',
WPML_TM_Job_Entity::PACKAGE_TYPE => 'update_post_job',
WPML_TM_Job_Entity::STRING_BATCH => 'update_post_job',
);
/** @var wpdb */
private $wpdb;
/** @var SitePress */
private $sitepress;
/**
* @param wpdb $wpdb
*/
public function __construct( wpdb $wpdb, SitePress $sitepress ) {
$this->wpdb = $wpdb;
$this->sitepress = $sitepress;
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return WPML_TM_Job_Entity
*/
public function update_state( WPML_TM_Job_Entity $job ) {
if ( ! array_key_exists( $job->get_type(), $this->strategies ) ) {
return $job;
}
$method = $this->strategies[ $job->get_type() ];
return call_user_func( array( $this, $method ), $job );
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return WPML_TM_Job_Entity
*/
private function update_string_job( WPML_TM_Job_Entity $job ) {
if ( $job->get_tp_id() ) {
$this->wpdb->update(
$this->wpdb->prefix . 'icl_core_status',
array(
'status' => $job->get_status(),
'tp_revision' => $job->get_revision(),
'ts_status' => $this->get_ts_status_in_ts_format( $job ),
),
array( 'rid' => $job->get_tp_id() )
);
}
$data = array(
'status' => $job->get_status(),
);
if ( ICL_TM_NOT_TRANSLATED === $job->get_status() ) {
$data['translator_id'] = null;
}
$this->wpdb->update(
$this->wpdb->prefix . 'icl_string_translations',
$data,
array( 'id' => $job->get_id() )
);
icl_update_string_status( $job->get_original_element_id() );
return $job;
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return WPML_TM_Job_Entity
*/
private function update_post_job( WPML_TM_Job_Entity $job ) {
$job_id = $job->get_id();
if ( $job->get_status() === ICL_TM_NOT_TRANSLATED ) {
$prev_status = $this->get_job_prev_status( $job_id );
if ( $prev_status && Relation::propEq( 'needs_update', '1', $prev_status ) ) {
$this->wpdb->update( $this->wpdb->prefix . 'icl_translation_status', $prev_status, [ 'rid' => $job_id ] );
$job->set_needs_update( true );
return $job;
}
}
$this->wpdb->update(
$this->wpdb->prefix . 'icl_translation_status',
array(
'status' => $job->get_status(),
'tp_revision' => $job->get_revision(),
'ts_status' => $this->get_ts_status_in_ts_format( $job ),
),
array( 'rid' => $job_id )
);
if (
ICL_TM_NOT_TRANSLATED === $job->get_status()
&& ( $post_type = Post::getType( $job->get_original_element_id() ) )
) {
$this->sitepress->delete_orphan_element(
$job->get_original_element_id(),
'post_' . $post_type,
$job->get_target_language()
);
}
return $job;
}
private function get_job_prev_status( $job_id ) {
$previous_state = $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT _prevstate
FROM {$this->wpdb->prefix}icl_translation_status
WHERE rid=%d
LIMIT 1",
$job_id
)
);
return unserialize( $previous_state );
}
/**
* In the db, we store the exact json format that we get from TS. It includes an extra ts_status key
*
* @param WPML_TM_Job_Entity $job
*
* @return string
*/
private function get_ts_status_in_ts_format( WPML_TM_Job_Entity $job ) {
$ts_status = $job->get_ts_status();
return $ts_status ? wp_json_encode( array( 'ts_status' => json_decode( (string) $ts_status ) ) ) : null;
}
}