class-wpml-config-update.php
9.4 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* Fetch the wpml config files for known plugins and themes
*
* @package wpml-core
*/
class WPML_Config_Update {
/** @var bool */
private $has_errors;
private $log;
/** @var SitePress $sitepress */
protected $sitepress;
/**
* @var WP_Http $http
*/
private $http;
/**
* @var WPML_Active_Plugin_Provider
*/
private $active_plugin_provider;
/**
* WPML_Config_Update constructor.
*
* @param SitePress $sitepress
* @param WP_Http $http
* @param WPML_Log|null $log
*/
public function __construct( $sitepress, $http, WPML_Log $log = null ) {
$this->sitepress = $sitepress;
$this->http = $http;
$this->log = $log;
}
/**
* @param WPML_Active_Plugin_Provider $active_plugin_provider
*/
public function set_active_plugin_provider( WPML_Active_Plugin_Provider $active_plugin_provider ) {
$this->active_plugin_provider = $active_plugin_provider;
}
/**
* @return WPML_Active_Plugin_Provider
*/
public function get_active_plugin_provider() {
if ( null === $this->active_plugin_provider ) {
if ( ! class_exists( 'WPML_Active_Plugin_Provider' ) ) {
require_once WPML_PLUGIN_PATH . '/classes/class-wpml-active-plugin-provider.php';
}
$this->active_plugin_provider = new WPML_Active_Plugin_Provider();
}
return $this->active_plugin_provider;
}
public function run() {
if ( ! $this->is_config_update_disabled() ) {
$this->has_errors = false;
$request_args = array( 'timeout' => 45 );
$index_response = $this->http->get( ICL_REMOTE_WPML_CONFIG_FILES_INDEX . 'wpml-config/config-index.json', $request_args );
if ( ! $this->is_a_valid_remote_response( $index_response ) ) {
$this->log_response( $index_response, 'index', 'wpml-config/config-index.json' );
} else {
$arr = json_decode( $index_response['body'] );
$plugins = isset( $arr->plugins ) ? $arr->plugins : array();
$themes = isset( $arr->themes ) ? $arr->themes : array();
if ( $plugins || $themes ) {
update_option( 'wpml_config_index', $arr, false );
update_option( 'wpml_config_index_updated', time(), false );
$config_files_original = get_option( 'wpml_config_files_arr', null );
$config_files = maybe_unserialize( $config_files_original );
$config_files_for_themes = array();
$deleted_configs_for_themes = array();
$config_files_for_plugins = array();
$deleted_configs_for_plugins = array();
if ( $config_files ) {
if ( isset( $config_files->themes ) && $config_files->themes ) {
$config_files_for_themes = $config_files->themes;
$deleted_configs_for_themes = $config_files->themes;
}
if ( isset( $config_files->plugins ) && $config_files->plugins ) {
$config_files_for_plugins = $config_files->plugins;
$deleted_configs_for_plugins = $config_files->plugins;
}
}
$current_theme_name = $this->sitepress->get_wp_api()
->get_theme_name();
$current_theme_parent = '';
if ( method_exists( $this->sitepress->get_wp_api(), 'get_theme_parent_name' ) ) {
$current_theme_parent = $this->sitepress->get_wp_api()
->get_theme_parent_name();
}
$active_theme_names = array( $current_theme_name );
if ( $current_theme_parent ) {
$active_theme_names[] = $current_theme_parent;
}
foreach ( $themes as $theme ) {
if ( in_array( $theme->name, $active_theme_names, true ) ) {
unset( $deleted_configs_for_themes[ $theme->name ] );
if ( ! isset( $config_files_for_themes[ $theme->name ] ) || md5( $config_files_for_themes[ $theme->name ] ) !== $theme->hash ) {
$theme_response = $this->http->get( ICL_REMOTE_WPML_CONFIG_FILES_INDEX . $theme->path, $request_args );
if ( ! $this->is_a_valid_remote_response( $theme_response ) ) {
$this->log_response( $theme_response, 'index', $theme->name );
} else {
$config_files_for_themes[ $theme->name ] = $theme_response['body'];
}
}
}
}
foreach ( $deleted_configs_for_themes as $key => $deleted_config ) {
unset( $config_files_for_themes[ $key ] );
}
$active_plugins_names = $this->get_active_plugin_provider()
->get_active_plugin_names();
foreach ( $plugins as $plugin ) {
if ( in_array( $plugin->name, $active_plugins_names, true ) ) {
unset( $deleted_configs_for_plugins[ $plugin->name ] );
if ( ! isset( $config_files_for_plugins[ $plugin->name ] ) || md5( $config_files_for_plugins[ $plugin->name ] ) !== $plugin->hash ) {
$plugin_response = $this->http->get( ICL_REMOTE_WPML_CONFIG_FILES_INDEX . $plugin->path, $request_args );
if ( ! $this->is_a_valid_remote_response( $plugin_response ) ) {
$this->log_response( $plugin_response, 'index', $plugin->name );
} else {
$config_files_for_plugins[ $plugin->name ] = $plugin_response['body'];
}
}
}
}
foreach ( $deleted_configs_for_plugins as $key => $deleted_config ) {
unset( $config_files_for_plugins[ $key ] );
}
if ( ! $config_files ) {
$config_files = new stdClass();
}
$config_files->themes = $config_files_for_themes;
$config_files->plugins = $config_files_for_plugins;
update_option( 'wpml_config_files_arr', $config_files, false );
}
}
$wpml_config_files_arr = maybe_unserialize( get_option( 'wpml_config_files_arr', null ) );
if ( ! $wpml_config_files_arr ) {
$this->log_response( 'Missing data', 'get_option', 'wpml_config_files_arr' );
}
if ( ! $this->has_errors && $this->log ) {
$this->log->clear();
}
}
return ! $this->has_errors;
}
private function is_valid_wpml_config_files_arr( $wpml_config_files_arr ) {
$is_valid = true;
$is_valid &= is_object( $wpml_config_files_arr );
$at_least_plugins_or_themes = false;
$at_least_plugins_or_themes |= isset( $wpml_config_files_arr->themes ) && is_array( $wpml_config_files_arr->themes ) && $wpml_config_files_arr->themes;
$at_least_plugins_or_themes |= isset( $wpml_config_files_arr->plugins ) && is_array( $wpml_config_files_arr->plugins ) && $wpml_config_files_arr->plugins;
return $is_valid && $at_least_plugins_or_themes;
}
/**
* @param array|WP_Error $response
*
* @return bool
*/
private function is_a_valid_remote_response( $response ) {
return $response && ! is_wp_error( $response ) && ! $this->is_http_error( $response );
}
private function is_http_error( $response ) {
return $response && is_array( $response )
&& ( ( array_key_exists( 'response', $response )
&& array_key_exists( 'code', $response['response'] )
&& 200 !== (int) $response['response']['code'] )
|| ! array_key_exists( 'body', $response )
|| '' === trim( $response['body'] ) );
}
/**
* @param string|array|WP_Error $response
* @param string $request_type
* @param ?string $component
* @param array|stdClass|null $extra_data
*/
private function log_response( $response, $request_type = 'unknown', $component = null, $extra_data = null ) {
if ( ! $this->log ) {
return;
}
$message_type = 'message';
if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
// Fallback -> Introduced in PHP 5.4.0
define( 'JSON_PRETTY_PRINT', 128 );
}
$response_data = null;
if ( is_scalar( $response ) ) {
$message_type = 'app_error';
$response_data = $response;
} elseif ( is_wp_error( $response ) ) {
$message_type = 'wp_error';
$response_data = array(
'code' => $response->get_error_code(),
'message' => $response->get_error_message(),
);
} elseif ( $this->is_http_error( $response ) ) {
$message_type = 'http_error';
if ( array_key_exists( 'response', $response ) ) {
if ( array_key_exists( 'code', $response['response'] ) ) {
$response_data['code'] = $response['response']['code'];
}
if ( array_key_exists( 'message', $response['response'] ) ) {
$response_data['message'] = $response['response']['message'];
}
}
$response_data['body'] = 'Missing!';
if ( array_key_exists( 'body', $response ) ) {
$response_data['body'] = 'Empty!';
if ( $response['body'] ) {
$body_encode = wp_json_encode( simplexml_load_string( $response['body'] ) );
if ( $body_encode ) {
$response_data['body'] = json_decode( $body_encode, true );
}
}
}
} elseif ( is_array( $response ) ) {
$response_data = $response;
} else {
$response_data = array( wp_json_encode( $response, JSON_PRETTY_PRINT ) );
}
$serialized_extra_data = null;
if ( $extra_data ) {
$serialized_extra_data = $extra_data;
if ( is_object( $serialized_extra_data ) ) {
$serialized_extra_data = get_object_vars( $serialized_extra_data );
}
if ( ! is_array( $serialized_extra_data ) ) {
$serialized_extra_data = array( wp_json_encode( $serialized_extra_data, JSON_PRETTY_PRINT ) );
}
}
$entry = array(
'request' => $request_type,
'type' => $message_type,
'component' => $component,
'response' => $response_data,
'extra' => $serialized_extra_data,
);
$this->log->insert( microtime( true ), $entry );
$this->has_errors = true;
}
private function is_config_update_disabled() {
if ( $this->sitepress->get_wp_api()
->constant( 'ICL_REMOTE_WPML_CONFIG_DISABLED' ) ) {
delete_option( 'wpml_config_index' );
delete_option( 'wpml_config_index_updated' );
delete_option( 'wpml_config_files_arr' );
return true;
}
return false;
}
}