| @@ -83,9 +83,9 @@ | ||
| 83 | 83 | if ( PLL() instanceof PLL_Frontend ) { |
| 84 | 84 | $this->options[ $name ] = $key; |
| 85 | 85 | add_filter( 'option_' . $name, array( $this, 'translate_strings' ) ); |
| 86 | 86 | } else { |
| 87 | - $this->register_string_recursive( $context, $name, get_option( $name ), $key ); | |
| 87 | + $this->register_string_recursive( $context, get_option( $name ), $key ); | |
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | } |
| @@ -199,50 +199,49 @@ | ||
| 199 | 199 | /** |
| 200 | 200 | * Recursively registers strings for a serialized option |
| 201 | 201 | * |
| 202 | 202 | * @since 1.0 |
| 203 | - * @since 2.7 Signature modified | |
| 204 | 203 | * |
| 205 | - * @param string $context The group in which the strings will be registered. | |
| 206 | - * @param string $option Option name. | |
| 207 | - * @param array $values Option value. | |
| 208 | - * @param object $key XML node. | |
| 204 | + * @param string $context the group in which the strings will be registered | |
| 205 | + * @param array $options | |
| 206 | + * @param object $key XML node | |
| 209 | 207 | */ |
| 210 | - protected function register_string_recursive( $context, $option, $values, $key ) { | |
| 211 | - if ( is_object( $values ) ) { | |
| 212 | - $values = (array) $values; | |
| 213 | - } | |
| 214 | - | |
| 208 | + protected function register_string_recursive( $context, $options, $key ) { | |
| 215 | 209 | $children = $key->children(); |
| 216 | - | |
| 217 | - if ( is_array( $values ) ) { | |
| 218 | - if ( count( $children ) ) { | |
| 219 | - foreach ( $children as $child ) { | |
| 220 | - $attributes = $child->attributes(); | |
| 221 | - $name = (string) $attributes['name']; | |
| 222 | - | |
| 223 | - if ( isset( $values[ $name ] ) ) { | |
| 224 | - $this->register_string_recursive( $context, $name, $values[ $name ], $child ); | |
| 225 | - continue; | |
| 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 ); | |
| 226 | 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 | + } | |
| 227 | 227 | |
| 228 | - $pattern = '#^' . str_replace( '*', '(?:.+)', $name ) . '$#'; | |
| 229 | - | |
| 230 | - foreach ( $values as $n => $value ) { | |
| 231 | - // The first case could be handled by the next one, but we avoid calls to preg_match here. | |
| 232 | - if ( '*' === $name || ( false !== strpos( $name, '*' ) && preg_match( $pattern, $n ) ) ) { | |
| 233 | - $this->register_string_recursive( $context, $n, $value, $child ); | |
| 234 | - } | |
| 235 | - } | |
| 236 | - } | |
| 237 | - } else { | |
| 238 | - foreach ( $values as $n => $value ) { | |
| 239 | - // Parent key is a wildcard and no sub-key has been whitelisted. | |
| 240 | - $this->register_string_recursive( $context, $n, $value, $key ); | |
| 241 | - } | |
| 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 ); | |
| 242 | 241 | } |
| 243 | 242 | } else { |
| 244 | - pll_register_string( $option, $values, $context, true ); // Multiline as in WPML. | |
| 243 | + pll_register_string( $name, $options, $context ); | |
| 245 | 244 | } |
| 246 | 245 | } |
| 247 | 246 | |
| 248 | 247 | /** |
| @@ -249,49 +248,48 @@ | ||
| 249 | 248 | * Recursively translates strings for a serialized option |
| 250 | 249 | * |
| 251 | 250 | * @since 1.0 |
| 252 | 251 | * |
| 253 | - * @param array|string $values Either a string to translate or a list of strings to translate. | |
| 254 | - * @param object $key XML node. | |
| 255 | - * @return array|string Translated string(s) | |
| 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) | |
| 256 | 255 | */ |
| 257 | 256 | protected function translate_strings_recursive( $values, $key ) { |
| 258 | 257 | $children = $key->children(); |
| 259 | - | |
| 260 | - if ( is_array( $values ) || is_object( $values ) ) { | |
| 261 | - if ( count( $children ) ) { | |
| 262 | - foreach ( $children as $child ) { | |
| 263 | - $attributes = $child->attributes(); | |
| 264 | - $name = (string) $attributes['name']; | |
| 265 | - | |
| 266 | - if ( is_array( $values ) && isset( $values[ $name ] ) ) { | |
| 267 | - $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 268 | - continue; | |
| 258 | + if ( count( $children ) ) { | |
| 259 | + foreach ( $children as $child ) { | |
| 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 ); | |
| 269 | 265 | } |
| 270 | - | |
| 271 | - if ( is_object( $values ) && isset( $values->$name ) ) { | |
| 272 | - $values->$name = $this->translate_strings_recursive( $values->$name, $child ); | |
| 273 | - continue; | |
| 274 | - } | |
| 275 | - | |
| 276 | - $pattern = '#^' . str_replace( '*', '(?:.+)', $name ) . '$#'; | |
| 277 | - | |
| 278 | - foreach ( $values as $n => &$value ) { | |
| 279 | - // The first case could be handled by the next one, but we avoid calls to preg_match here. | |
| 280 | - if ( '*' === $name || ( false !== strpos( $name, '*' ) && preg_match( $pattern, $n ) ) ) { | |
| 281 | - $value = $this->translate_strings_recursive( $value, $child ); | |
| 282 | - } | |
| 283 | - } | |
| 266 | + } elseif ( isset( $values[ $name ] ) ) { | |
| 267 | + $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 284 | 268 | } |
| 285 | - } else { | |
| 286 | - // Parent key is a wildcard and no sub-key has been whitelisted. | |
| 287 | - foreach ( $values as &$value ) { | |
| 288 | - $value = $this->translate_strings_recursive( $value, $key ); | |
| 289 | - } | |
| 290 | 269 | } |
| 291 | 270 | } else { |
| 292 | 271 | $values = pll__( $values ); |
| 293 | 272 | } |
| 273 | + return $values; | |
| 274 | + } | |
| 294 | 275 | |
| 295 | - return $values; | |
| 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; | |
| 296 | 294 | } |
| 297 | 295 | } |