class-wpml-st-upgrade.php
5.42 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* WPML_ST_Upgrade class file.
*
* @package wpml-string-translation
*/
/**
* Class WPML_ST_Upgrade
*/
class WPML_ST_Upgrade {
const TRANSIENT_UPGRADE_IN_PROGRESS = 'wpml_st_upgrade_in_progress';
/**
* SitePress instance.
*
* @var SitePress $sitepress
*/
private $sitepress;
/**
* Upgrade Command Factory instance.
*
* @var WPML_ST_Upgrade_Command_Factory
*/
private $command_factory;
/**
* Upgrade in progress flag.
*
* @var bool $upgrade_in_progress
*/
private $upgrade_in_progress;
/**
* WPML_ST_Upgrade constructor.
*
* @param SitePress $sitepress SitePress instance.
* @param WPML_ST_Upgrade_Command_Factory|null $command_factory Upgrade Command Factory instance.
*/
public function __construct( SitePress $sitepress, WPML_ST_Upgrade_Command_Factory $command_factory = null ) {
$this->sitepress = $sitepress;
$this->command_factory = $command_factory;
}
/**
* Run upgrade.
*/
public function run() {
if ( get_transient( self::TRANSIENT_UPGRADE_IN_PROGRESS ) ) {
return;
}
if ( $this->sitepress->get_wp_api()->is_admin() ) {
if ( $this->sitepress->get_wp_api()->constant( 'DOING_AJAX' ) ) {
$this->run_ajax();
} else {
$this->run_admin();
}
} else {
$this->run_front_end();
}
$this->set_upgrade_completed();
}
/**
* Run admin.
*/
private function run_admin() {
$this->maybe_run( 'WPML_ST_Upgrade_Migrate_Originals' );
$this->maybe_run( 'WPML_ST_Upgrade_Display_Strings_Scan_Notices' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_String_Packages' );
$this->maybe_run( 'WPML_ST_Upgrade_MO_Scanning' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_String_Name_Index' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_Longtext_String_Value' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_Strings_Add_Translation_Priority_Field' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_String_Packages_Word_Count' );
$this->maybe_run( '\WPML\ST\Upgrade\Command\RegenerateMoFilesWithStringNames' );
$this->maybe_run( \WPML\ST\Upgrade\Command\MigrateMultilingualWidgets::class );
}
/**
* Run ajax.
*/
private function run_ajax() {
$this->maybe_run_ajax( 'WPML_ST_Upgrade_Migrate_Originals' );
// It has to be maybe_run.
$this->maybe_run( 'WPML_ST_Upgrade_MO_Scanning' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_String_Packages_Word_Count' );
}
/**
* Run on frontend.
*/
private function run_front_end() {
$this->maybe_run( 'WPML_ST_Upgrade_MO_Scanning' );
$this->maybe_run( 'WPML_ST_Upgrade_DB_String_Packages_Word_Count' );
}
/**
* Maybe run command.
*
* @param string $class Command class name.
*/
private function maybe_run( $class ) {
if ( ! $this->has_command_been_executed( $class ) ) {
$this->set_upgrade_in_progress();
$upgrade = $this->command_factory->create( $class );
if ( $upgrade->run() ) {
$this->mark_command_as_executed( $class );
}
}
}
/**
* Maybe run command in ajax.
*
* @param string $class Command class name.
*/
private function maybe_run_ajax( $class ) {
if ( ! $this->has_command_been_executed( $class ) ) {
$this->run_ajax_command( $class );
}
}
/**
* Run command in ajax.
*
* @param string $class Command class name.
*/
private function run_ajax_command( $class ) {
if ( $this->nonce_ok( $class ) ) {
$upgrade = $this->command_factory->create( $class );
if ( $upgrade->run_ajax() ) {
$this->mark_command_as_executed( $class );
$this->sitepress->get_wp_api()->wp_send_json_success( '' );
}
}
}
/**
* Check nonce.
*
* @param string $class Command class name.
*
* @return bool
*/
private function nonce_ok( $class ) {
$ok = false;
$class = strtolower( $class );
$class = str_replace( '_', '-', $class );
if ( isset( $_POST['action'] ) && $_POST['action'] === $class ) {
$nonce = $this->filter_nonce_parameter();
if ( $this->sitepress->get_wp_api()->wp_verify_nonce( $nonce, $class . '-nonce' ) ) {
$ok = true;
}
}
return $ok;
}
/**
* Check if command was executed.
*
* @param string $class Command class name.
*
* @return bool
*/
public function has_command_been_executed( $class ) {
/** @phpstan-ignore-next-line */
$id = call_user_func( [ $class, 'get_command_id' ] );
$settings = $this->sitepress->get_setting( 'st', [] );
return isset( $settings[ $id . '_has_run' ] );
}
/**
* Mark command as executed.
*
* @param string $class Command class name.
*/
public function mark_command_as_executed( $class ) {
/** @phpstan-ignore-next-line */
$id = call_user_func( [ $class, 'get_command_id' ] );
$settings = $this->sitepress->get_setting( 'st', [] );
$settings[ $id . '_has_run' ] = true;
$this->sitepress->set_setting( 'st', $settings, true );
wp_cache_flush();
}
/**
* Filter nonce.
*
* @return mixed
*/
protected function filter_nonce_parameter() {
return filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
}
/**
* Set flag that upgrade is in process.
*/
private function set_upgrade_in_progress() {
if ( ! $this->upgrade_in_progress ) {
$this->upgrade_in_progress = true;
set_transient( self::TRANSIENT_UPGRADE_IN_PROGRESS, true, MINUTE_IN_SECONDS );
}
}
/**
* Mark upgrade as completed.
*/
private function set_upgrade_completed() {
if ( $this->upgrade_in_progress ) {
$this->upgrade_in_progress = false;
delete_transient( self::TRANSIENT_UPGRADE_IN_PROGRESS );
}
}
}