class-wpml-tm-icl20-migrate-local.php
5.07 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Migrate_Local {
const JOBS_TYPES_DOCUMENTS = 'icl_translation_status';
const JOBS_TYPES_STRINGS = 'icl_string_translations';
private $progress;
private $sitepress;
private $status;
/** @var WPML_TP_Services */
private $tp_services;
/**
* WPML_TM_ICL20 constructor.
*
* @param WPML_TP_Services $tp_services
* @param WPML_TM_ICL20_Migration_Status $status
* @param WPML_TM_ICL20_Migration_Progress $progress
* @param SitePress $sitepress
*
* @internal param SitePress $sitepress
*/
public function __construct(
WPML_TP_Services $tp_services,
WPML_TM_ICL20_Migration_Status $status,
WPML_TM_ICL20_Migration_Progress $progress,
SitePress $sitepress
) {
$this->tp_services = $tp_services;
$this->status = $status;
$this->progress = $progress;
$this->sitepress = $sitepress;
}
public function migrate_jobs( $table ) {
$result = false;
$current_service = $this->tp_services->get_current_service();
if ( $this->status->get_ICL_20_TS_ID() === $current_service->id ) {
$step = null;
if ( self::JOBS_TYPES_DOCUMENTS === $table ) {
$step = WPML_TM_ICL20_Migration_Progress::STEP_MIGRATE_JOBS_DOCUMENTS;
}
if ( self::JOBS_TYPES_STRINGS === $table ) {
$step = WPML_TM_ICL20_Migration_Progress::STEP_MIGRATE_JOBS_STRINGS;
}
if ( null !== $step ) {
$update = $this->update_table( $table );
$result = false !== $update;
$this->progress->set_completed_step( $step, $result );
} else {
$this->progress->log_failed_attempt( __METHOD__ . ' - ' . 'Wrong "' . $table . '"' );
}
}
return $result;
}
/**
* @param $table
*
* @return false|int
*/
private function update_table( $table ) {
$wpdb = $this->sitepress->get_wpdb();
$update = $wpdb->update(
$wpdb->prefix . $table,
array(
'translator_id' => 0,
'translation_service' => $this->status->get_ICL_20_TS_ID(),
),
array(
'translation_service' => $this->status->get_ICL_LEGACY_TS_ID(),
),
array( '%d', '%d' ),
array( '%d' )
);
if ( false === $update ) {
$this->progress->log_failed_attempt( __METHOD__ . ' - ' . $wpdb->last_error );
}
return $update;
}
public function migrate_project() {
$old_index = $this->progress->get_project_to_migrate();
// icl_translation_projects
$migrated = false;
if ( $old_index ) {
$current_service = $this->tp_services->get_current_service();
if ( $current_service && null !== $current_service->id ) {
$new_index = md5( $current_service->id . serialize( $current_service->custom_fields_data ) );
$migrated = $this->update_project_index( $old_index, $new_index );
}
}
$this->progress->set_completed_step( WPML_TM_ICL20_Migration_Progress::STEP_MIGRATE_LOCAL_PROJECT, $migrated );
return $migrated;
}
private function update_project_index( $old_service_index, $new_service_index ) {
$updated = false;
$projects = $this->sitepress->get_setting( 'icl_translation_projects', null );
$old_index_exists = array_key_exists( $old_service_index, $projects );
$new_index_does_not_exists = ! array_key_exists( $new_service_index, $projects );
if ( $projects && $old_index_exists && $new_index_does_not_exists ) {
$project = $projects[ $old_service_index ];
$projects[ $new_service_index ] = $project;
unset( $projects[ $old_service_index ] );
$this->sitepress->set_setting( 'icl_translation_projects', $projects, true );
$updated = true;
}
if ( false === $updated ) {
$error = '';
if ( ! $old_index_exists ) {
$error = 'The old project does not exists';
}
if ( ! $new_index_does_not_exists ) {
$error = 'The new project index already exists';
}
$this->progress->log_failed_attempt( __METHOD__ . ' - ' . $error );
}
return $updated;
}
public function migrate_service( $new_token ) {
$current_service = $this->tp_services->get_current_service();
$migrated = false;
if ( $current_service ) {
$old_index = md5( $current_service->id . serialize( $current_service->custom_fields_data ) );
$this->progress->set_project_to_migrate( $old_index );
$icl20_service_id = $this->status->get_ICL_20_TS_ID();
$this->tp_services->select_service(
$icl20_service_id,
array(
'api_token' => $new_token,
)
);
$active_service = $this->tp_services->get_current_service();
$migrated = $active_service->id === $icl20_service_id;
}
$this->progress->set_completed_step( WPML_TM_ICL20_Migration_Progress::STEP_MIGRATE_LOCAL_SERVICE, $migrated );
return $migrated;
}
public function rollback_service() {
$current_service = $this->tp_services->get_current_service();
$rolled_back = false;
if ( $current_service ) {
$this->tp_services->select_service( $this->status->get_ICL_LEGACY_TS_ID() );
$active_service = $this->tp_services->get_current_service();
$rolled_back = $active_service->id === $this->status->get_ICL_LEGACY_TS_ID();
}
$this->progress->set_completed_step( WPML_TM_ICL20_Migration_Progress::STEP_MIGRATE_LOCAL_SERVICE, false );
return $rolled_back;
}
}