| @@ -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,41 +159,113 @@ | ||
| 191 | 159 | return $taxonomies; |
| 192 | 160 | } |
| 193 | 161 | |
| 194 | 162 | /** |
| 195 | - * Registers or translates the strings for an option | |
| 163 | + * Translates the strings for an option | |
| 196 | 164 | * |
| 197 | - * @since 2.8 | |
| 165 | + * @since 1.0 | |
| 198 | 166 | * |
| 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. | |
| 167 | + * @param array|string $value Either a string to translate or a list of strings to translate | |
| 168 | + * @return array|string translated string(s) | |
| 202 | 169 | */ |
| 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 ) ); | |
| 170 | + public function translate_strings( $value ) { | |
| 171 | + $option = substr( current_filter(), 7 ); | |
| 172 | + return $this->translate_strings_recursive( $value, $this->options[ $option ] ); | |
| 206 | 173 | } |
| 207 | 174 | |
| 208 | 175 | /** |
| 209 | - * Recursively transforms xml nodes to an array, ready for PLL_Translate_Option. | |
| 176 | + * Recursively registers strings for a serialized option | |
| 210 | 177 | * |
| 211 | - * @since 2.9 | |
| 178 | + * @since 1.0 | |
| 212 | 179 | * |
| 213 | - * @param object $key XML node. | |
| 214 | - * @param array $arr Array of option keys to translate. | |
| 215 | - * @return array | |
| 180 | + * @param string $context the group in which the strings will be registered | |
| 181 | + * @param array $options | |
| 182 | + * @param object $key XML node | |
| 216 | 183 | */ |
| 217 | - protected function xml_to_array( $key, &$arr = array() ) { | |
| 218 | - $attributes = $key->attributes(); | |
| 219 | - $name = (string) $attributes['name']; | |
| 184 | + protected function register_string_recursive( $context, $options, $key ) { | |
| 220 | 185 | $children = $key->children(); |
| 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 ); | |
| 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 | + } | |
| 221 | 203 | |
| 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 ); | |
| 217 | + } | |
| 218 | + } else { | |
| 219 | + pll_register_string( $name, $options, $context ); | |
| 220 | + } | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Recursively translates strings for a serialized option | |
| 225 | + * | |
| 226 | + * @since 1.0 | |
| 227 | + * | |
| 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) | |
| 231 | + */ | |
| 232 | + protected function translate_strings_recursive( $values, $key ) { | |
| 233 | + $children = $key->children(); | |
| 222 | 234 | if ( count( $children ) ) { |
| 223 | 235 | foreach ( $children as $child ) { |
| 224 | - $arr[ $name ] = $this->xml_to_array( $child, $arr[ $name ] ); | |
| 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 ); | |
| 241 | + } | |
| 242 | + } elseif ( isset( $values[ $name ] ) ) { | |
| 243 | + $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 244 | + } | |
| 225 | 245 | } |
| 226 | 246 | } else { |
| 227 | - $arr[ $name ] = true; // Multiline as in WPML. | |
| 247 | + $values = pll__( $values ); | |
| 228 | 248 | } |
| 229 | - return $arr; | |
| 249 | + return $values; | |
| 250 | + } | |
| 251 | + | |
| 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; | |
| 230 | 270 | } |
| 231 | 271 | } |