| @@ -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/ |
| @@ -73,10 +70,9 @@ | ||
| 73 | 70 | $this->xmls['Polylang'] = $xml; |
| 74 | 71 | } |
| 75 | 72 | |
| 76 | 73 | if ( ! empty( $this->xmls ) ) { |
| 77 | - add_filter( 'pll_copy_post_metas', array( $this, 'copy_post_metas' ), 20, 2 ); | |
| 78 | - add_filter( 'pll_copy_term_metas', array( $this, 'copy_term_metas' ), 20, 2 ); | |
| 74 | + add_filter( 'pll_copy_post_metas', array( $this, 'copy_post_metas' ), 10, 2 ); | |
| 79 | 75 | add_filter( 'pll_get_post_types', array( $this, 'translate_types' ), 10, 2 ); |
| 80 | 76 | add_filter( 'pll_get_taxonomies', array( $this, 'translate_taxonomies' ), 10, 2 ); |
| 81 | 77 | |
| 82 | 78 | foreach ( $this->xmls as $context => $xml ) { |
| @@ -82,18 +78,13 @@ | ||
| 82 | 78 | foreach ( $this->xmls as $context => $xml ) { |
| 83 | 79 | foreach ( $xml->xpath( 'admin-texts/key' ) as $key ) { |
| 84 | 80 | $attributes = $key->attributes(); |
| 85 | 81 | $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 | - } | |
| 82 | + if ( PLL() instanceof PLL_Frontend ) { | |
| 83 | + $this->options[ $name ] = $key; | |
| 84 | + add_filter( 'option_' . $name, array( $this, 'translate_strings' ) ); | |
| 94 | 85 | } else { |
| 95 | - $this->register_or_translate_option( $context, $name, $key ); | |
| 86 | + $this->register_string_recursive( $context, get_option( $name ), $key ); | |
| 96 | 87 | } |
| 97 | 88 | } |
| 98 | 89 | } |
| 99 | 90 | } |
| @@ -122,31 +113,8 @@ | ||
| 122 | 113 | return $metas; |
| 123 | 114 | } |
| 124 | 115 | |
| 125 | 116 | /** |
| 126 | - * Adds term metas to the list of metas to copy when creating a new translation | |
| 127 | - * | |
| 128 | - * @since 2.6 | |
| 129 | - * | |
| 130 | - * @param array $metas The list of term metas to copy or synchronize. | |
| 131 | - * @param bool $sync True for sync, false for copy. | |
| 132 | - * @return array The list of term metas to copy or synchronize. | |
| 133 | - */ | |
| 134 | - public function copy_term_metas( $metas, $sync ) { | |
| 135 | - foreach ( $this->xmls as $xml ) { | |
| 136 | - foreach ( $xml->xpath( 'custom-term-fields/custom-term-field' ) as $cf ) { | |
| 137 | - $attributes = $cf->attributes(); | |
| 138 | - if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) { | |
| 139 | - $metas[] = (string) $cf; | |
| 140 | - } else { | |
| 141 | - $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 142 | - } | |
| 143 | - } | |
| 144 | - } | |
| 145 | - return $metas; | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | 117 | * Language and translation management for custom post types |
| 150 | 118 | * |
| 151 | 119 | * @since 1.0 |
| 152 | 120 | * |
| @@ -191,26 +159,8 @@ | ||
| 191 | 159 | return $taxonomies; |
| 192 | 160 | } |
| 193 | 161 | |
| 194 | 162 | /** |
| 195 | - * Registers or translates the strings for an option | |
| 196 | - * | |
| 197 | - * @since 2.8 | |
| 198 | - * | |
| 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. | |
| 202 | - */ | |
| 203 | - protected function register_or_translate_option( $context, $name, $key ) { | |
| 204 | - if ( PLL() instanceof PLL_Frontend ) { | |
| 205 | - $this->options[ $name ] = $key; | |
| 206 | - add_filter( 'option_' . $name, array( $this, 'translate_strings' ) ); | |
| 207 | - } else { | |
| 208 | - $this->register_string_recursive( $context, $name, get_option( $name ), $key ); | |
| 209 | - } | |
| 210 | - } | |
| 211 | - | |
| 212 | - /** | |
| 213 | 163 | * Translates the strings for an option |
| 214 | 164 | * |
| 215 | 165 | * @since 1.0 |
| 216 | 166 | * |
| @@ -225,50 +175,49 @@ | ||
| 225 | 175 | /** |
| 226 | 176 | * Recursively registers strings for a serialized option |
| 227 | 177 | * |
| 228 | 178 | * @since 1.0 |
| 229 | - * @since 2.7 Signature modified | |
| 230 | 179 | * |
| 231 | - * @param string $context The group in which the strings will be registered. | |
| 232 | - * @param string $option Option name. | |
| 233 | - * @param array $values Option value. | |
| 234 | - * @param object $key XML node. | |
| 180 | + * @param string $context the group in which the strings will be registered | |
| 181 | + * @param array $options | |
| 182 | + * @param object $key XML node | |
| 235 | 183 | */ |
| 236 | - protected function register_string_recursive( $context, $option, $values, $key ) { | |
| 237 | - if ( is_object( $values ) ) { | |
| 238 | - $values = (array) $values; | |
| 239 | - } | |
| 240 | - | |
| 184 | + protected function register_string_recursive( $context, $options, $key ) { | |
| 241 | 185 | $children = $key->children(); |
| 242 | - | |
| 243 | - if ( is_array( $values ) ) { | |
| 244 | - if ( count( $children ) ) { | |
| 245 | - foreach ( $children as $child ) { | |
| 246 | - $attributes = $child->attributes(); | |
| 247 | - $name = (string) $attributes['name']; | |
| 248 | - | |
| 249 | - if ( isset( $values[ $name ] ) ) { | |
| 250 | - $this->register_string_recursive( $context, $name, $values[ $name ], $child ); | |
| 251 | - continue; | |
| 186 | + if ( count( $children ) ) { | |
| 187 | + foreach ( $children as $child ) { | |
| 188 | + $attributes = $child->attributes(); | |
| 189 | + $name = (string) $attributes['name']; | |
| 190 | + if ( '*' === $name && is_array( $options ) ) { | |
| 191 | + foreach ( $options as $n => $option ) { | |
| 192 | + $this->register_wildcard_options_recursive( $context, $option, $n ); | |
| 252 | 193 | } |
| 194 | + } elseif ( isset( $options[ $name ] ) ) { | |
| 195 | + $this->register_string_recursive( $context, $options[ $name ], $child ); | |
| 196 | + } | |
| 197 | + } | |
| 198 | + } else { | |
| 199 | + $attributes = $key->attributes(); | |
| 200 | + pll_register_string( (string) $attributes['name'], $options, $context, true ); // Multiline as in WPML | |
| 201 | + } | |
| 202 | + } | |
| 253 | 203 | |
| 254 | - $pattern = '#^' . str_replace( '*', '(?:.+)', $name ) . '$#'; | |
| 255 | - | |
| 256 | - foreach ( $values as $n => $value ) { | |
| 257 | - // The first case could be handled by the next one, but we avoid calls to preg_match here. | |
| 258 | - if ( '*' === $name || ( false !== strpos( $name, '*' ) && preg_match( $pattern, $n ) ) ) { | |
| 259 | - $this->register_string_recursive( $context, $n, $value, $child ); | |
| 260 | - } | |
| 261 | - } | |
| 262 | - } | |
| 263 | - } else { | |
| 264 | - foreach ( $values as $n => $value ) { | |
| 265 | - // Parent key is a wildcard and no sub-key has been whitelisted. | |
| 266 | - $this->register_string_recursive( $context, $n, $value, $key ); | |
| 267 | - } | |
| 204 | + /** | |
| 205 | + * Recursively registers strings with a wildcard | |
| 206 | + * | |
| 207 | + * @since 2.1 | |
| 208 | + * | |
| 209 | + * @param string $context the group in which the strings will be registered | |
| 210 | + * @param array $options | |
| 211 | + * @param string $name Option name | |
| 212 | + */ | |
| 213 | + protected function register_wildcard_options_recursive( $context, $options, $name ) { | |
| 214 | + if ( is_array( $options ) ) { | |
| 215 | + foreach ( $options as $n => $option ) { | |
| 216 | + $this->register_wildcard_options_recursive( $context, $option, $n ); | |
| 268 | 217 | } |
| 269 | 218 | } else { |
| 270 | - pll_register_string( $option, $values, $context, true ); // Multiline as in WPML. | |
| 219 | + pll_register_string( $name, $options, $context ); | |
| 271 | 220 | } |
| 272 | 221 | } |
| 273 | 222 | |
| 274 | 223 | /** |
| @@ -275,49 +224,48 @@ | ||
| 275 | 224 | * Recursively translates strings for a serialized option |
| 276 | 225 | * |
| 277 | 226 | * @since 1.0 |
| 278 | 227 | * |
| 279 | - * @param array|string $values Either a string to translate or a list of strings to translate. | |
| 280 | - * @param object $key XML node. | |
| 281 | - * @return array|string Translated string(s) | |
| 228 | + * @param array|string $values either a string to translate or a list of strings to translate | |
| 229 | + * @param object $key XML node | |
| 230 | + * @return array|string translated string(s) | |
| 282 | 231 | */ |
| 283 | 232 | protected function translate_strings_recursive( $values, $key ) { |
| 284 | 233 | $children = $key->children(); |
| 285 | - | |
| 286 | - if ( is_array( $values ) || is_object( $values ) ) { | |
| 287 | - if ( count( $children ) ) { | |
| 288 | - foreach ( $children as $child ) { | |
| 289 | - $attributes = $child->attributes(); | |
| 290 | - $name = (string) $attributes['name']; | |
| 291 | - | |
| 292 | - if ( is_array( $values ) && isset( $values[ $name ] ) ) { | |
| 293 | - $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 294 | - continue; | |
| 234 | + if ( count( $children ) ) { | |
| 235 | + foreach ( $children as $child ) { | |
| 236 | + $attributes = $child->attributes(); | |
| 237 | + $name = (string) $attributes['name']; | |
| 238 | + if ( '*' === $name && is_array( $values ) ) { | |
| 239 | + foreach ( $values as $n => $v ) { | |
| 240 | + $values[ $n ] = $this->translate_wildcard_options_recursive( $v, $n ); | |
| 295 | 241 | } |
| 296 | - | |
| 297 | - if ( is_object( $values ) && isset( $values->$name ) ) { | |
| 298 | - $values->$name = $this->translate_strings_recursive( $values->$name, $child ); | |
| 299 | - continue; | |
| 300 | - } | |
| 301 | - | |
| 302 | - $pattern = '#^' . str_replace( '*', '(?:.+)', $name ) . '$#'; | |
| 303 | - | |
| 304 | - foreach ( $values as $n => &$value ) { | |
| 305 | - // The first case could be handled by the next one, but we avoid calls to preg_match here. | |
| 306 | - if ( '*' === $name || ( false !== strpos( $name, '*' ) && preg_match( $pattern, $n ) ) ) { | |
| 307 | - $value = $this->translate_strings_recursive( $value, $child ); | |
| 308 | - } | |
| 309 | - } | |
| 242 | + } elseif ( isset( $values[ $name ] ) ) { | |
| 243 | + $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 310 | 244 | } |
| 311 | - } else { | |
| 312 | - // Parent key is a wildcard and no sub-key has been whitelisted. | |
| 313 | - foreach ( $values as &$value ) { | |
| 314 | - $value = $this->translate_strings_recursive( $value, $key ); | |
| 315 | - } | |
| 316 | 245 | } |
| 317 | 246 | } else { |
| 318 | 247 | $values = pll__( $values ); |
| 319 | 248 | } |
| 249 | + return $values; | |
| 250 | + } | |
| 320 | 251 | |
| 321 | - return $values; | |
| 252 | + /** | |
| 253 | + * Recursively translates strings registered by a wildcard | |
| 254 | + * | |
| 255 | + * @since 2.1 | |
| 256 | + * | |
| 257 | + * @param array|string $options Either a string to translate or a list of strings to translate | |
| 258 | + * @param string $name Option name | |
| 259 | + * @return array|string Translated string(s) | |
| 260 | + */ | |
| 261 | + protected function translate_wildcard_options_recursive( $options, $name ) { | |
| 262 | + if ( is_array( $options ) ) { | |
| 263 | + foreach ( $options as $n => $option ) { | |
| 264 | + $options[ $n ] = $this->translate_wildcard_options_recursive( $option, $n ); | |
| 265 | + } | |
| 266 | + } else { | |
| 267 | + $options = pll__( $options ); | |
| 268 | + } | |
| 269 | + return $options; | |
| 322 | 270 | } |
| 323 | 271 | } |