class-wpml-config.php
15.5 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
use WPML\Settings\PostType\Automatic;
class WPML_Config {
const PATH_TO_XSD = WPML_PLUGIN_PATH . '/res/xsd/wpml-config.xsd';
static $wpml_config_files = array();
static $active_plugins = array();
static function load_config() {
global $pagenow, $sitepress;
if ( ! is_admin() || wpml_is_ajax() || ( isset( $_POST['action'] ) && $_POST['action'] === 'heartbeat' ) || ! $sitepress || ! $sitepress->get_default_language() ) {
return;
}
$white_list_pages = array(
'theme_options',
'plugins.php',
'themes.php',
WPML_PLUGIN_FOLDER . '/menu/languages.php',
WPML_PLUGIN_FOLDER . '/menu/theme-localization.php',
WPML_PLUGIN_FOLDER . '/menu/translation-options.php',
);
if ( defined( 'WPML_ST_FOLDER' ) ) {
$white_list_pages[] = WPML_ST_FOLDER . '/menu/string-translation.php';
}
$white_list_pages = apply_filters( 'wpml_config_white_list_pages', $white_list_pages );
// Runs the load config process only on specific pages
$current_page = isset( $_GET['page'] ) ? $_GET['page'] : null;
if ( ( isset( $current_page ) && in_array( $current_page, $white_list_pages ) ) || ( isset( $pagenow ) && in_array( $pagenow, $white_list_pages ) ) ) {
self::load_config_run();
}
}
static function load_config_run() {
global $sitepress;
self::load_config_pre_process();
self::load_plugins_wpml_config();
self::load_theme_wpml_config();
self::parse_wpml_config_files();
self::load_config_post_process();
$sitepress->save_settings();
}
static function get_custom_fields_translation_settings( $translation_actions = array( 0 ) ) {
$iclTranslationManagement = wpml_load_core_tm();
$section = 'custom_fields_translation';
$result = array();
$tm_settings = $iclTranslationManagement->settings;
if ( isset( $tm_settings[ $section ] ) ) {
foreach ( $tm_settings[ $section ] as $meta_key => $translation_type ) {
if ( in_array( $translation_type, $translation_actions ) ) {
$result[] = $meta_key;
}
}
}
return $result;
}
static function parse_wpml_config_post_process( $config ) {
self::parse_custom_fields( $config );
self::parseTaxonomies( $config );
self::parsePostTypes( $config );
do_action( 'wpml_reset_ls_settings', $config['wpml-config']['language-switcher-settings'] );
return $config;
}
static function parseTaxonomies( $config ) {
self::parseTMSetting( 'taxonomy', 'taxonomies', $config );
}
static function parsePostTypes( $config ) {
self::parseTMSetting( 'custom-type', 'custom-types', $config );
Automatic::saveFromConfig( $config );
}
static function parseTMSetting( $singular, $plural, $config ) {
global $sitepress, $iclTranslationManagement;
$tm_settings = new WPML_TM_Settings_Update(
$singular,
$plural,
$iclTranslationManagement,
$sitepress,
wpml_load_settings_helper()
);
$tm_settings->update_from_config( $config['wpml-config'] );
}
static function load_config_post_process() {
global $iclTranslationManagement;
$post_process = new WPML_TM_Settings_Post_Process( $iclTranslationManagement );
$post_process->run();
}
static function load_config_pre_process() {
global $iclTranslationManagement;
$tm_settings = $iclTranslationManagement->settings;
if ( ( isset( $tm_settings['custom_types_readonly_config'] ) && is_array( $tm_settings['custom_types_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_types_readonly_config_prev'] = $tm_settings['custom_types_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_types_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_types_readonly_config'] = array();
if ( ( isset( $tm_settings['custom_fields_readonly_config'] ) && is_array( $tm_settings['custom_fields_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_fields_readonly_config_prev'] = $tm_settings['custom_fields_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_fields_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_fields_readonly_config'] = array();
if ( ( isset( $tm_settings['custom_term_fields_readonly_config'] ) && is_array( $tm_settings['custom_term_fields_readonly_config'] ) ) ) {
$iclTranslationManagement->settings['__custom_term_fields_readonly_config_prev'] = $tm_settings['custom_term_fields_readonly_config'];
} else {
$iclTranslationManagement->settings['__custom_term_fields_readonly_config_prev'] = array();
}
$iclTranslationManagement->settings['custom_term_fields_readonly_config'] = array();
}
static function load_plugins_wpml_config() {
if ( is_multisite() ) {
// Get multi site plugins
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $p => $dummy ) {
if ( ! self::check_on_config_file( $p ) ) {
continue;
}
$plugin_slug = dirname( $p );
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
if ( trim( $plugin_slug, '\/.' ) && file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
}
}
// Get single site or current blog active plugins
$plugins = get_option( 'active_plugins' );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $p ) {
if ( ! self::check_on_config_file( $p ) ) {
continue;
}
$plugin_slug = dirname( $p );
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
if ( trim( $plugin_slug, '\/.' ) && file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
}
// Get the must-use plugins
$mu_plugins = wp_get_mu_plugins();
if ( ! empty( $mu_plugins ) ) {
foreach ( $mu_plugins as $mup ) {
if ( ! self::check_on_config_file( $mup ) ) {
continue;
}
$plugin_dir_name = dirname( $mup );
$plugin_base_name = basename( $mup, '.php' );
$plugin_sub_dir = $plugin_dir_name . '/' . $plugin_base_name;
if ( file_exists( $plugin_sub_dir . '/wpml-config.xml' ) ) {
$config_file = $plugin_sub_dir . '/wpml-config.xml';
self::$wpml_config_files[] = $config_file;
}
}
}
return self::$wpml_config_files;
}
static function check_on_config_file( $name ) {
if ( empty( self::$active_plugins ) ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
self::$active_plugins = get_plugins();
}
$config_index_file_data = maybe_unserialize( get_option( 'wpml_config_index' ) );
$config_files_arr = maybe_unserialize( get_option( 'wpml_config_files_arr' ) );
if ( ! $config_index_file_data || ! $config_files_arr ) {
return true;
}
if ( isset( self::$active_plugins[ $name ] ) ) {
$plugin_info = self::$active_plugins[ $name ];
$plugin_slug = dirname( $name );
$name = $plugin_info['Name'];
$config_data = $config_index_file_data->plugins;
$config_files_arr = $config_files_arr->plugins;
$config_file = WPML_PLUGINS_DIR . '/' . $plugin_slug . '/wpml-config.xml';
$type = 'plugin';
} else {
$config_data = $config_index_file_data->themes;
$config_files_arr = $config_files_arr->themes;
$config_file = get_template_directory() . '/wpml-config.xml';
$type = 'theme';
}
foreach ( $config_data as $item ) {
if ( $name == $item->name && isset( $config_files_arr[ $item->name ] ) ) {
if ( $item->override_local || ! file_exists( $config_file ) ) {
end( self::$wpml_config_files );
$key = key( self::$wpml_config_files ) + 1;
self::$wpml_config_files[ $key ] = new stdClass();
self::$wpml_config_files[ $key ]->config = icl_xml2array( $config_files_arr[ $item->name ] );
self::$wpml_config_files[ $key ]->type = $type;
self::$wpml_config_files[ $key ]->admin_text_context = basename( dirname( $config_file ) );
return false;
} else {
return true;
}
}
}
return true;
}
static function load_theme_wpml_config() {
$theme_data = wp_get_theme();
if ( ! self::check_on_config_file( $theme_data->get( 'Name' ) ) ) {
return self::$wpml_config_files;
}
$parent_theme = $theme_data->parent_theme;
if ( $parent_theme && ! self::check_on_config_file( $parent_theme ) ) {
return self::$wpml_config_files;
}
if ( get_template_directory() != get_stylesheet_directory() ) {
$config_file = get_stylesheet_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
}
$config_file = get_template_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
self::$wpml_config_files[] = $config_file;
}
return self::$wpml_config_files;
}
static function get_theme_wpml_config_file() {
if ( get_template_directory() != get_stylesheet_directory() ) {
$config_file = get_stylesheet_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
return $config_file;
}
}
$config_file = get_template_directory() . '/wpml-config.xml';
if ( file_exists( $config_file ) ) {
return $config_file;
}
return false;
}
static function parse_wpml_config_files() {
$config_all['wpml-config'] = array(
'custom-fields' => array(),
'custom-fields-texts' => array(),
'custom-term-fields' => array(),
'custom-types' => array(),
'taxonomies' => array(),
'admin-texts' => array(),
'language-switcher-settings' => array(),
'shortcodes' => array(),
'shortcode-list' => array(),
'gutenberg-blocks' => array(),
'built-with-page-builder' => array(),
);
$config_all_updated = false;
$validate = new WPML_XML_Config_Validate(); // Validate with no XSD file (see wpmlcore-8444).
$transform = new WPML_XML2Array();
if ( ! empty( self::$wpml_config_files ) ) {
foreach ( self::$wpml_config_files as $file ) {
if ( is_object( $file ) ) {
$config = $file->config;
} else {
$xml_config_file = new WPML_XML_Config_Read_File( $file, $validate, $transform );
$config = $xml_config_file->get();
}
do_action( 'wpml_parse_config_file', $file );
$config_all = self::merge_with( $config_all, $config );
$config_all_updated = true;
}
}
$config_all = self::append_custom_xml_config( $config_all, $config_all_updated );
if ( $config_all_updated ) {
$config_all = apply_filters( 'icl_wpml_config_array', $config_all );
$config_all = apply_filters( 'wpml_config_array', $config_all );
}
$config_all = WPML_Config_Display_As_Translated::merge_to_translate_mode( $config_all );
self::parse_wpml_config_post_process( $config_all );
}
/**
* @param array<string,array<string,mixed>> $config_files
* @param bool|null $updated
*
* @return array
*/
private static function append_custom_xml_config( $config_files, &$updated = null ) {
$validate = new WPML_XML_Config_Validate( self::PATH_TO_XSD );
$transform = new WPML_XML2Array();
$custom_config = self::get_custom_xml_config( $validate, $transform );
if ( $custom_config ) {
$config_files = self::merge_with( $config_files, $custom_config );
$updated = true;
}
return $config_files;
}
/**
* @param \WPML_XML_Config_Validate $validate
* @param \WPML_XML_Transform $transform
*
* @return mixed
*/
private static function get_custom_xml_config( $validate, $transform ) {
if ( class_exists( 'WPML_Custom_XML' ) ) {
$custom_xml_option = new WPML_Custom_XML();
$custom_xml_config = new WPML_XML_Config_Read_Option( $custom_xml_option, $validate, $transform );
$custom_config = $custom_xml_config->get();
if ( $custom_config ) {
$config_object = (object) array(
'config' => $custom_config,
'type' => 'wpml-custom-xml',
'admin_text_context' => 'wpml-custom-xml',
);
do_action( 'wpml_parse_custom_config', $config_object );
return $custom_config;
}
}
return null;
}
/**
* @param array<string,array<string,mixed>> $all_configs
* @param array<string,array<string,mixed>> $config
*
* @return mixed
*/
private static function merge_with( $all_configs, $config ) {
if ( isset( $config['wpml-config'] ) ) {
$wpml_config = $config['wpml-config'];
$wpml_config_all = $all_configs['wpml-config'];
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-field', 'custom-fields' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-term-field', 'custom-term-fields' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'custom-type', 'custom-types' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'taxonomy', 'taxonomies' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'shortcode', 'shortcodes' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'gutenberg-block', 'gutenberg-blocks' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'key', 'custom-fields-texts' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'elementor-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'beaver-builder-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'cornerstone-widgets' );
$wpml_config_all = self::parse_config_index( $wpml_config_all, $wpml_config, 'widget', 'siteorigin-widgets' );
// language-switcher-settings
if ( isset( $wpml_config['language-switcher-settings']['key'] ) ) {
if ( ! is_numeric( key( $wpml_config['language-switcher-settings']['key'] ) ) ) { // single
$wpml_config_all['language-switcher-settings']['key'][] = $wpml_config['language-switcher-settings']['key'];
} else {
foreach ( $wpml_config['language-switcher-settings']['key'] as $cf ) {
$wpml_config_all['language-switcher-settings']['key'][] = $cf;
}
}
}
if ( isset( $wpml_config['shortcode-list']['value'] ) ) {
$wpml_config_all['shortcode-list'] = array_merge( $wpml_config_all['shortcode-list'], explode( ',', $wpml_config['shortcode-list']['value'] ) );
}
if ( isset( $wpml_config['built-with-page-builder']['value'] ) ) {
$wpml_config_all['built-with-page-builder'] = $wpml_config['built-with-page-builder']['value'];
}
$all_configs['wpml-config'] = $wpml_config_all;
}
return $all_configs;
}
/**
* @param array<string,array<string,mixed>> $config
*/
protected static function parse_custom_fields( $config ) {
/** @var TranslationManagement $iclTranslationManagement */
global $iclTranslationManagement;
$setting_factory = $iclTranslationManagement->settings_factory();
$import = new WPML_Custom_Field_XML_Settings_Import( $setting_factory, $config['wpml-config'] );
$import->run();
}
private static function parse_config_index( $config_all, $wpml_config, $index_sing, $index_plur ) {
if ( isset( $wpml_config[ $index_plur ][ $index_sing ] ) ) {
if ( isset( $wpml_config[ $index_plur ][ $index_sing ]['value'] ) ) { // single
$config_all[ $index_plur ][ $index_sing ][] = $wpml_config[ $index_plur ][ $index_sing ];
} else {
foreach ( (array) $wpml_config[ $index_plur ][ $index_sing ] as $cf ) {
$config_all[ $index_plur ][ $index_sing ][] = $cf;
}
}
}
return $config_all;
}
}