This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwpml-config.php
More file actions
342 lines (304 loc) · 10.1 KB
/
wpml-config.php
File metadata and controls
342 lines (304 loc) · 10.1 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
<?php
/*
* reads and interprets the file wpml-config.xml
* see http://wpml.org/documentation/support/language-configuration-files/
* the language switcher configuration is not interpreted
* the xml parser has been adapted from http://php.net/manual/en/function.xml-parse-into-struct.php#84261
* many thanks to wickedfather at hotmail dot com
*
* @since 1.0
*/
class PLL_WPML_Config {
static protected $instance; // for singleton
protected $values, $index, $strings;
public $tags;
/*
* constructor
*
* @since 1.0
*/
public function __construct() {
$this->init();
}
/*
* access to the single instance of the class
*
* @since 1.7
*
* @return object
*/
static public function instance() {
if (empty(self::$instance))
self::$instance = new self();
return self::$instance;
}
/*
* parses the wpml-config.xml file
*
* @since 1.0
*
* @param string wpml-config.xml file content
* @param string $context identifies where the file was found
*/
protected function xml_parse($xml, $context) {
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xml, $this->values);
xml_parser_free($parser);
$this->index = 0;
$arr = $this->xml_parse_recursive();
$arr = $arr['wpml-config'];
$keys = array(
array('custom-fields', 'custom-field'),
array('custom-types','custom-type'),
array('taxonomies','taxonomy'),
array('admin-texts','key')
);
foreach ($keys as $k) {
if (isset($arr[$k[0]])) {
if (!isset($arr[$k[0]][$k[1]][0])) {
$elem = $arr[$k[0]][$k[1]];
unset($arr[$k[0]][$k[1]]);
$arr[$k[0]][$k[1]][0] = $elem;
}
$this->tags[$k[0]][$context] = $arr[$k[0]];
}
}
}
/*
* recursively parses the wpml-config.xml file
*
* @since 1.0
*
* @return array
*/
protected function xml_parse_recursive() {
$found = array();
$tagCount = array();
while (isset($this->values[$this->index])) {
extract($this->values[$this->index]);
$this->index++;
if ($type == 'close')
return $found;
if (isset($tagCount[$tag])) {
if ($tagCount[$tag] == 1)
$found[$tag] = array($found[$tag]);
$tagRef = &$found[$tag][$tagCount[$tag]];
$tagCount[$tag]++;
}
else {
$tagCount[$tag] = 1;
$tagRef = &$found[$tag];
}
if ($type == 'open') {
$tagRef = $this->xml_parse_recursive();
if (isset($attributes))
$tagRef['attributes'] = $attributes;
}
if ($type == 'complete') {
if (isset($attributes)) {
$tagRef['attributes'] = $attributes;
$tagRef = &$tagRef['value'];
}
if (isset($value))
$tagRef = $value;
}
}
return $found;
}
/*
* finds the wpml-config.xml files to parse and setup filters
*
* @since 1.0
*/
public function init() {
$this->tags = array();
// theme
if (file_exists($file = ($template = get_template_directory()) .'/wpml-config.xml'))
$this->xml_parse(file_get_contents($file), get_template()); // FIXME fopen + fread + fclose quicker ?
// child theme
if ($template != ($stylesheet = get_stylesheet_directory()) && file_exists($file = $stylesheet.'/wpml-config.xml'))
$this->xml_parse(file_get_contents($file), get_stylesheet());
// plugins
// don't forget sitewide active plugins thanks to Reactorshop http://wordpress.org/support/topic/polylang-and-yoast-seo-plugin/page/2?replies=38#post-4801829
$plugins = (is_multisite() && $sitewide_plugins = get_site_option('active_sitewide_plugins')) && is_array($sitewide_plugins) ? array_keys($sitewide_plugins) : array();
$plugins = array_merge($plugins, get_option('active_plugins'));
foreach ($plugins as $plugin) {
if (file_exists($file = WP_PLUGIN_DIR.'/'.dirname($plugin).'/wpml-config.xml'))
$this->xml_parse(file_get_contents($file), dirname($plugin));
}
// custom
if (file_exists($file = PLL_LOCAL_DIR.'/wpml-config.xml'))
$this->xml_parse(file_get_contents($file), 'polylang');
if (isset($this->tags['custom-fields']))
add_filter('pll_copy_post_metas', array(&$this, 'copy_post_metas'), 10, 2);
if (isset($this->tags['custom-types']))
add_filter('pll_get_post_types', array(&$this, 'translate_types'), 10, 2);
if (isset($this->tags['taxonomies']))
add_filter('pll_get_taxonomies', array(&$this, 'translate_taxonomies'), 10, 2);
if (!isset($this->tags['admin-texts']))
return;
// get a cleaner array for easy manipulation
foreach ($this->tags['admin-texts'] as $context => $arr)
foreach ($arr as $keys)
$this->strings[$context] = $this->admin_texts_recursive($keys);
foreach ($this->strings as $context => $options) {
foreach ($options as $option_name => $value) {
if (PLL_ADMIN) { // backend
$option = get_option($option_name);
if (is_string($option) && $value == 1)
pll_register_string($option_name, $option, $context);
elseif (is_array($option) && is_array($value))
$this->register_string_recursive($context, $value, $option); // for a serialized option
}
else
add_filter('option_'.$option_name, array(&$this, 'translate_strings'));
}
}
}
/*
* arranges strings in a cleaner way
*
* @since 1.0
*
* @param array $keys
* @return array
*/
protected function admin_texts_recursive($keys) {
if (!isset($keys[0])) {
$elem = $keys;
unset($keys);
$keys[0] = $elem;
}
foreach ($keys as $key)
$strings[$key['attributes']['name']] = isset($key['key']) ? $this->admin_texts_recursive($key['key']) : 1;
return $strings;
}
/*
* recursively registers strings for a serialized option
*
* @since 1.0
*
* @param string $context the group in which the strings will be registered
* @param array $strings
* @param array $options
*/
protected function register_string_recursive($context, $strings, $options) {
foreach ($options as $name => $value) {
if (isset($strings[$name])) {
// allow numeric values to be translated
// https://wordpress.org/support/topic/wpml-configxml-strings-skipped-when-numbers-ids
if ((is_numeric($value) || is_string($value)) && $strings[$name] == 1)
pll_register_string($name, $value, $context);
elseif (is_array($value) && is_array($strings[$name]))
$this->register_string_recursive($context, $strings[$name], $value);
}
}
}
/*
* adds custom fields to the list of metas to copy when creating a new translation
*
* @since 1.0
*
* @param array $metas the list of custom fields to copy or synchronize
* @param bool $sync true for sync, false for copy
* @return array the list of custom fields to copy or synchronize
*/
public function copy_post_metas($metas, $sync) {
foreach ($this->tags['custom-fields'] as $context) {
foreach ($context['custom-field'] as $cf) {
// copy => copy and synchronize
// translate => copy but don't synchronize
// ignore => don't copy
// see http://wordpress.org/support/topic/custom-field-values-override-other-translation-values?replies=8#post-4655563
if ('copy' == $cf['attributes']['action'] || (!$sync && 'translate' == $cf['attributes']['action']))
$metas[] = $cf['value'];
else
if (isset($cf['value']))
$metas = array_diff($metas, array($cf['value']));
}
}
return $metas;
}
/*
* language and translation management for custom post types
*
* @since 1.0
*
* @param array $types list of post type names for which Polylang manages language and translations
* @param bool $hide true when displaying the list in Polylang settings
* @return array list of post type names for which Polylang manages language and translations
*/
public function translate_types($types, $hide) {
foreach ($this->tags['custom-types'] as $context) {
foreach ($context['custom-type'] as $pt) {
if ($pt['attributes']['translate'] == 1 && !$hide)
$types[$pt['value']] = $pt['value'];
else
unset ($types[$pt['value']]); // the author decided what to do with the post type so don't allow the user to change this
}
}
return $types;
}
/*
* language and translation management for custom taxonomies
*
* @since 1.0
*
* @param array $taxonomies list of taxonomy names for which Polylang manages language and translations
* @param bool $hide true when displaying the list in Polylang settings
* @return array list of taxonomy names for which Polylang manages language and translations
*/
public function translate_taxonomies($taxonomies, $hide) {
foreach ($this->tags['taxonomies'] as $context) {
foreach ($context['taxonomy'] as $tax) {
if ($tax['attributes']['translate'] == 1 && !$hide)
$taxonomies[$tax['value']] = $tax['value'];
else
unset ($taxonomies[$tax['value']]); // the author decided what to do with the taxonomy so don't allow the user to change this
}
}
return $taxonomies;
}
/*
* translates the strings for an option
*
* @since 1.0
*
* @param array|string either a string to translate or a list of strings to translate
* @return array|string translated string(s)
*/
public function translate_strings($value) {
if (is_array($value)) {
$option = substr(current_filter(), 7);
foreach ($this->strings as $context => $options) {
if (array_key_exists($option, $options))
return $this->translate_strings_recursive($options[$option], $value); // for a serialized option
}
}
return pll__($value);
}
/*
* recursively translates strings for a serialized option
*
* @since 1.0
*
* @param array $strings
* @param array|string $values either a string to translate or a list of strings to translate
* @return array|string translated string(s)
*/
protected function translate_strings_recursive($strings, $values) {
foreach ($values as $name => $value) {
if (isset($strings[$name])) {
// allow numeric values to be translated
// https://wordpress.org/support/topic/wpml-configxml-strings-skipped-when-numbers-ids
if ((is_numeric($value) || is_string($value)) && $strings[$name] == 1)
$values[$name] = pll__($value);
elseif (is_array($value) && is_array($strings[$name]))
$values[$name] = $this->translate_strings_recursive($strings[$name], $value);
}
}
return $values;
}
} // class PLL_WPML_Config