| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Reads and interprets the file wpml-config.xml |
| 8 | 5 | * See http://wpml.org/documentation/support/language-configuration-files/ |
| @@ -82,18 +79,13 @@ | ||
| 82 | 79 | foreach ( $this->xmls as $context => $xml ) { |
| 83 | 80 | foreach ( $xml->xpath( 'admin-texts/key' ) as $key ) { |
| 84 | 81 | $attributes = $key->attributes(); |
| 85 | 82 | $name = (string) $attributes['name']; |
| 86 | - | |
| 87 | - if ( false !== strpos( $name, '*' ) ) { | |
| 88 | - $pattern = '#^' . str_replace( '*', '(?:.+)', $name ) . '$#'; | |
| 89 | - $names = preg_grep( $pattern, array_keys( wp_load_alloptions() ) ); | |
| 90 | - | |
| 91 | - foreach ( $names as $_name ) { | |
| 92 | - $this->register_or_translate_option( $context, $_name, $key ); | |
| 93 | - } | |
| 83 | + if ( PLL() instanceof PLL_Frontend ) { | |
| 84 | + $this->options[ $name ] = $key; | |
| 85 | + add_filter( 'option_' . $name, array( $this, 'translate_strings' ) ); | |
| 94 | 86 | } else { |
| 95 | - $this->register_or_translate_option( $context, $name, $key ); | |
| 87 | + $this->register_string_recursive( $context, get_option( $name ), $key ); | |
| 96 | 88 | } |
| 97 | 89 | } |
| 98 | 90 | } |
| 99 | 91 | } |
| @@ -191,41 +183,113 @@ | ||
| 191 | 183 | return $taxonomies; |
| 192 | 184 | } |
| 193 | 185 | |
| 194 | 186 | /** |
| 195 | - * Registers or translates the strings for an option | |
| 187 | + * Translates the strings for an option | |
| 196 | 188 | * |
| 197 | - * @since 2.8 | |
| 189 | + * @since 1.0 | |
| 198 | 190 | * |
| 199 | - * @param string $context The group in which the strings will be registered. | |
| 200 | - * @param string $name Option name. | |
| 201 | - * @param object $key XML node. | |
| 191 | + * @param array|string $value Either a string to translate or a list of strings to translate | |
| 192 | + * @return array|string translated string(s) | |
| 202 | 193 | */ |
| 203 | - protected function register_or_translate_option( $context, $name, $key ) { | |
| 204 | - $option_keys = $this->xml_to_array( $key ); | |
| 205 | - new PLL_Translate_Option( $name, reset( $option_keys ), array( 'context' => $context ) ); | |
| 194 | + public function translate_strings( $value ) { | |
| 195 | + $option = substr( current_filter(), 7 ); | |
| 196 | + return $this->translate_strings_recursive( $value, $this->options[ $option ] ); | |
| 206 | 197 | } |
| 207 | 198 | |
| 208 | 199 | /** |
| 209 | - * Recursively transforms xml nodes to an array, ready for PLL_Translate_Option. | |
| 200 | + * Recursively registers strings for a serialized option | |
| 210 | 201 | * |
| 211 | - * @since 2.9 | |
| 202 | + * @since 1.0 | |
| 212 | 203 | * |
| 213 | - * @param object $key XML node. | |
| 214 | - * @param array $arr Array of option keys to translate. | |
| 215 | - * @return array | |
| 204 | + * @param string $context the group in which the strings will be registered | |
| 205 | + * @param array $options | |
| 206 | + * @param object $key XML node | |
| 216 | 207 | */ |
| 217 | - protected function xml_to_array( $key, &$arr = array() ) { | |
| 218 | - $attributes = $key->attributes(); | |
| 219 | - $name = (string) $attributes['name']; | |
| 208 | + protected function register_string_recursive( $context, $options, $key ) { | |
| 220 | 209 | $children = $key->children(); |
| 210 | + if ( count( $children ) ) { | |
| 211 | + foreach ( $children as $child ) { | |
| 212 | + $attributes = $child->attributes(); | |
| 213 | + $name = (string) $attributes['name']; | |
| 214 | + if ( '*' === $name && is_array( $options ) ) { | |
| 215 | + foreach ( $options as $n => $option ) { | |
| 216 | + $this->register_wildcard_options_recursive( $context, $option, $n ); | |
| 217 | + } | |
| 218 | + } elseif ( isset( $options[ $name ] ) ) { | |
| 219 | + $this->register_string_recursive( $context, $options[ $name ], $child ); | |
| 220 | + } | |
| 221 | + } | |
| 222 | + } else { | |
| 223 | + $attributes = $key->attributes(); | |
| 224 | + pll_register_string( (string) $attributes['name'], $options, $context, true ); // Multiline as in WPML | |
| 225 | + } | |
| 226 | + } | |
| 221 | 227 | |
| 228 | + /** | |
| 229 | + * Recursively registers strings with a wildcard | |
| 230 | + * | |
| 231 | + * @since 2.1 | |
| 232 | + * | |
| 233 | + * @param string $context the group in which the strings will be registered | |
| 234 | + * @param array $options | |
| 235 | + * @param string $name Option name | |
| 236 | + */ | |
| 237 | + protected function register_wildcard_options_recursive( $context, $options, $name ) { | |
| 238 | + if ( is_array( $options ) ) { | |
| 239 | + foreach ( $options as $n => $option ) { | |
| 240 | + $this->register_wildcard_options_recursive( $context, $option, $n ); | |
| 241 | + } | |
| 242 | + } else { | |
| 243 | + pll_register_string( $name, $options, $context ); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Recursively translates strings for a serialized option | |
| 249 | + * | |
| 250 | + * @since 1.0 | |
| 251 | + * | |
| 252 | + * @param array|string $values either a string to translate or a list of strings to translate | |
| 253 | + * @param object $key XML node | |
| 254 | + * @return array|string translated string(s) | |
| 255 | + */ | |
| 256 | + protected function translate_strings_recursive( $values, $key ) { | |
| 257 | + $children = $key->children(); | |
| 222 | 258 | if ( count( $children ) ) { |
| 223 | 259 | foreach ( $children as $child ) { |
| 224 | - $arr[ $name ] = $this->xml_to_array( $child, $arr[ $name ] ); | |
| 260 | + $attributes = $child->attributes(); | |
| 261 | + $name = (string) $attributes['name']; | |
| 262 | + if ( '*' === $name && is_array( $values ) ) { | |
| 263 | + foreach ( $values as $n => $v ) { | |
| 264 | + $values[ $n ] = $this->translate_wildcard_options_recursive( $v, $n ); | |
| 265 | + } | |
| 266 | + } elseif ( isset( $values[ $name ] ) ) { | |
| 267 | + $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 268 | + } | |
| 225 | 269 | } |
| 226 | 270 | } else { |
| 227 | - $arr[ $name ] = true; // Multiline as in WPML. | |
| 271 | + $values = pll__( $values ); | |
| 228 | 272 | } |
| 229 | - return $arr; | |
| 273 | + return $values; | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * Recursively translates strings registered by a wildcard | |
| 278 | + * | |
| 279 | + * @since 2.1 | |
| 280 | + * | |
| 281 | + * @param array|string $options Either a string to translate or a list of strings to translate | |
| 282 | + * @param string $name Option name | |
| 283 | + * @return array|string Translated string(s) | |
| 284 | + */ | |
| 285 | + protected function translate_wildcard_options_recursive( $options, $name ) { | |
| 286 | + if ( is_array( $options ) ) { | |
| 287 | + foreach ( $options as $n => $option ) { | |
| 288 | + $options[ $n ] = $this->translate_wildcard_options_recursive( $option, $n ); | |
| 289 | + } | |
| 290 | + } else { | |
| 291 | + $options = pll__( $options ); | |
| 292 | + } | |
| 293 | + return $options; | |
| 230 | 294 | } |
| 231 | 295 | } |