| @@ -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/ |
| @@ -10,9 +7,9 @@ | ||
| 10 | 7 | * |
| 11 | 8 | * @since 1.0 |
| 12 | 9 | */ |
| 13 | 10 | class PLL_WPML_Config { |
| 14 | - protected static $instance; // For singleton | |
| 11 | + static protected $instance; // For singleton | |
| 15 | 12 | protected $xmls, $options; |
| 16 | 13 | |
| 17 | 14 | /** |
| 18 | 15 | * Constructor |
| @@ -31,9 +28,9 @@ | ||
| 31 | 28 | * @since 1.7 |
| 32 | 29 | * |
| 33 | 30 | * @return object |
| 34 | 31 | */ |
| 35 | - public static function instance() { | |
| 32 | + static public function instance() { | |
| 36 | 33 | if ( empty( self::$instance ) ) { |
| 37 | 34 | self::$instance = new self(); |
| 38 | 35 | } |
| 39 | 36 | return self::$instance; |
| @@ -46,12 +43,22 @@ | ||
| 46 | 43 | */ |
| 47 | 44 | public function init() { |
| 48 | 45 | $this->xmls = array(); |
| 49 | 46 | |
| 47 | + // Theme | |
| 48 | + if ( file_exists( $file = ( $template = get_template_directory() ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 49 | + $this->xmls[ get_template() ] = $xml; | |
| 50 | + } | |
| 51 | + | |
| 52 | + // Child theme | |
| 53 | + if ( ( $stylesheet = get_stylesheet_directory() ) !== $template && file_exists( $file = $stylesheet . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 54 | + $this->xmls[ get_stylesheet() ] = $xml; | |
| 55 | + } | |
| 56 | + | |
| 50 | 57 | // Plugins |
| 51 | 58 | // 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 |
| 52 | 59 | $plugins = ( is_multisite() && $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); |
| 53 | - $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); | |
| 60 | + $plugins = array_merge( $plugins, get_option( 'active_plugins' ) ); | |
| 54 | 61 | |
| 55 | 62 | foreach ( $plugins as $plugin ) { |
| 56 | 63 | if ( file_exists( $file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { |
| 57 | 64 | $this->xmls[ dirname( $plugin ) ] = $xml; |
| @@ -57,18 +64,8 @@ | ||
| 57 | 64 | $this->xmls[ dirname( $plugin ) ] = $xml; |
| 58 | 65 | } |
| 59 | 66 | } |
| 60 | 67 | |
| 61 | - // Theme | |
| 62 | - if ( file_exists( $file = ( $template = get_template_directory() ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 63 | - $this->xmls[ get_template() ] = $xml; | |
| 64 | - } | |
| 65 | - | |
| 66 | - // Child theme | |
| 67 | - if ( ( $stylesheet = get_stylesheet_directory() ) !== $template && file_exists( $file = $stylesheet . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 68 | - $this->xmls[ get_stylesheet() ] = $xml; | |
| 69 | - } | |
| 70 | - | |
| 71 | 68 | // Custom |
| 72 | 69 | if ( file_exists( $file = PLL_LOCAL_DIR . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { |
| 73 | 70 | $this->xmls['Polylang'] = $xml; |
| 74 | 71 | } |
| @@ -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 | } |
| @@ -111,12 +102,12 @@ | ||
| 111 | 102 | public function copy_post_metas( $metas, $sync ) { |
| 112 | 103 | foreach ( $this->xmls as $xml ) { |
| 113 | 104 | foreach ( $xml->xpath( 'custom-fields/custom-field' ) as $cf ) { |
| 114 | 105 | $attributes = $cf->attributes(); |
| 115 | - if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) { | |
| 106 | + if ( 'copy' == $attributes['action'] || ( ! $sync && 'translate' == $attributes['action'] ) ) { | |
| 116 | 107 | $metas[] = (string) $cf; |
| 117 | 108 | } else { |
| 118 | - $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 109 | + $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 119 | 110 | } |
| 120 | 111 | } |
| 121 | 112 | } |
| 122 | 113 | return $metas; |
| @@ -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,31 +159,13 @@ | ||
| 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 | * |
| 217 | - * @param array|string $value Either a string to translate or a list of strings to translate | |
| 167 | + * @param array|string either a string to translate or a list of strings to translate | |
| 218 | 168 | * @return array|string translated string(s) |
| 219 | 169 | */ |
| 220 | 170 | public function translate_strings( $value ) { |
| 221 | 171 | $option = substr( current_filter(), 7 ); |
| @@ -225,50 +175,26 @@ | ||
| 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; | |
| 252 | - } | |
| 253 | - | |
| 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 | - } | |
| 186 | + if ( count( $children ) ) { | |
| 187 | + foreach ( $children as $child ) { | |
| 188 | + $attributes = $child->attributes(); | |
| 189 | + $name = (string) $attributes['name']; | |
| 190 | + if ( isset( $options[ $name ] ) ) { | |
| 191 | + $this->register_string_recursive( $context, $options[ $name ], $child ); | |
| 262 | 192 | } |
| 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 | - } | |
| 268 | 193 | } |
| 269 | 194 | } else { |
| 270 | - pll_register_string( $option, $values, $context, true ); // Multiline as in WPML. | |
| 195 | + $attributes = $key->attributes(); | |
| 196 | + pll_register_string( (string) $attributes['name'], $options, $context ); | |
| 271 | 197 | } |
| 272 | 198 | } |
| 273 | 199 | |
| 274 | 200 | /** |
| @@ -275,49 +201,24 @@ | ||
| 275 | 201 | * Recursively translates strings for a serialized option |
| 276 | 202 | * |
| 277 | 203 | * @since 1.0 |
| 278 | 204 | * |
| 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) | |
| 205 | + * @param array|string $values either a string to translate or a list of strings to translate | |
| 206 | + * @param object $key XML node | |
| 207 | + * @return array|string translated string(s) | |
| 282 | 208 | */ |
| 283 | 209 | protected function translate_strings_recursive( $values, $key ) { |
| 284 | 210 | $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; | |
| 295 | - } | |
| 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 | - } | |
| 211 | + if ( count( $children ) ) { | |
| 212 | + foreach ( $children as $child ) { | |
| 213 | + $attributes = $child->attributes(); | |
| 214 | + $name = (string) $attributes['name']; | |
| 215 | + if ( isset( $values[ $name ] ) ) { | |
| 216 | + $values[ $name ] = $this->translate_strings_recursive( $values[ $name ], $child ); | |
| 310 | 217 | } |
| 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 | 218 | } |
| 317 | 219 | } else { |
| 318 | 220 | $values = pll__( $values ); |
| 319 | 221 | } |
| 320 | - | |
| 321 | 222 | return $values; |
| 322 | 223 | } |
| 323 | 224 | } |