| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * @since 1.0 |
| 9 | 9 | */ |
| 10 | 10 | class PLL_WPML_Config { |
| 11 | - protected static $instance; // For singleton | |
| 11 | + static protected $instance; // For singleton | |
| 12 | 12 | protected $xmls, $options; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Constructor |
| @@ -28,9 +28,9 @@ | ||
| 28 | 28 | * @since 1.7 |
| 29 | 29 | * |
| 30 | 30 | * @return object |
| 31 | 31 | */ |
| 32 | - public static function instance() { | |
| 32 | + static public function instance() { | |
| 33 | 33 | if ( empty( self::$instance ) ) { |
| 34 | 34 | self::$instance = new self(); |
| 35 | 35 | } |
| 36 | 36 | return self::$instance; |
| @@ -43,12 +43,22 @@ | ||
| 43 | 43 | */ |
| 44 | 44 | public function init() { |
| 45 | 45 | $this->xmls = array(); |
| 46 | 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 | + | |
| 47 | 57 | // Plugins |
| 48 | 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 |
| 49 | 59 | $plugins = ( is_multisite() && $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); |
| 50 | - $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); | |
| 60 | + $plugins = array_merge( $plugins, get_option( 'active_plugins' ) ); | |
| 51 | 61 | |
| 52 | 62 | foreach ( $plugins as $plugin ) { |
| 53 | 63 | if ( file_exists( $file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { |
| 54 | 64 | $this->xmls[ dirname( $plugin ) ] = $xml; |
| @@ -54,18 +64,8 @@ | ||
| 54 | 64 | $this->xmls[ dirname( $plugin ) ] = $xml; |
| 55 | 65 | } |
| 56 | 66 | } |
| 57 | 67 | |
| 58 | - // Theme | |
| 59 | - if ( file_exists( $file = ( $template = get_template_directory() ) . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 60 | - $this->xmls[ get_template() ] = $xml; | |
| 61 | - } | |
| 62 | - | |
| 63 | - // Child theme | |
| 64 | - if ( ( $stylesheet = get_stylesheet_directory() ) !== $template && file_exists( $file = $stylesheet . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { | |
| 65 | - $this->xmls[ get_stylesheet() ] = $xml; | |
| 66 | - } | |
| 67 | - | |
| 68 | 68 | // Custom |
| 69 | 69 | if ( file_exists( $file = PLL_LOCAL_DIR . '/wpml-config.xml' ) && false !== $xml = simplexml_load_file( $file ) ) { |
| 70 | 70 | $this->xmls['Polylang'] = $xml; |
| 71 | 71 | } |
| @@ -70,10 +70,9 @@ | ||
| 70 | 70 | $this->xmls['Polylang'] = $xml; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | if ( ! empty( $this->xmls ) ) { |
| 74 | - add_filter( 'pll_copy_post_metas', array( $this, 'copy_post_metas' ), 20, 2 ); | |
| 75 | - 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 ); | |
| 76 | 75 | add_filter( 'pll_get_post_types', array( $this, 'translate_types' ), 10, 2 ); |
| 77 | 76 | add_filter( 'pll_get_taxonomies', array( $this, 'translate_taxonomies' ), 10, 2 ); |
| 78 | 77 | |
| 79 | 78 | foreach ( $this->xmls as $context => $xml ) { |
| @@ -83,9 +82,9 @@ | ||
| 83 | 82 | if ( PLL() instanceof PLL_Frontend ) { |
| 84 | 83 | $this->options[ $name ] = $key; |
| 85 | 84 | add_filter( 'option_' . $name, array( $this, 'translate_strings' ) ); |
| 86 | 85 | } else { |
| 87 | - $this->register_string_recursive( $context, $name, get_option( $name ), $key ); | |
| 86 | + $this->register_string_recursive( $context, get_option( $name ), $key ); | |
| 88 | 87 | } |
| 89 | 88 | } |
| 90 | 89 | } |
| 91 | 90 | } |
| @@ -103,12 +102,12 @@ | ||
| 103 | 102 | public function copy_post_metas( $metas, $sync ) { |
| 104 | 103 | foreach ( $this->xmls as $xml ) { |
| 105 | 104 | foreach ( $xml->xpath( 'custom-fields/custom-field' ) as $cf ) { |
| 106 | 105 | $attributes = $cf->attributes(); |
| 107 | - if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) { | |
| 106 | + if ( 'copy' == $attributes['action'] || ( ! $sync && 'translate' == $attributes['action'] ) ) { | |
| 108 | 107 | $metas[] = (string) $cf; |
| 109 | 108 | } else { |
| 110 | - $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 109 | + $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 111 | 110 | } |
| 112 | 111 | } |
| 113 | 112 | } |
| 114 | 113 | return $metas; |
| @@ -114,31 +113,8 @@ | ||
| 114 | 113 | return $metas; |
| 115 | 114 | } |
| 116 | 115 | |
| 117 | 116 | /** |
| 118 | - * Adds term metas to the list of metas to copy when creating a new translation | |
| 119 | - * | |
| 120 | - * @since 2.6 | |
| 121 | - * | |
| 122 | - * @param array $metas The list of term metas to copy or synchronize. | |
| 123 | - * @param bool $sync True for sync, false for copy. | |
| 124 | - * @return array The list of term metas to copy or synchronize. | |
| 125 | - */ | |
| 126 | - public function copy_term_metas( $metas, $sync ) { | |
| 127 | - foreach ( $this->xmls as $xml ) { | |
| 128 | - foreach ( $xml->xpath( 'custom-term-fields/custom-term-field' ) as $cf ) { | |
| 129 | - $attributes = $cf->attributes(); | |
| 130 | - if ( 'copy' == $attributes['action'] || ( ! $sync && in_array( $attributes['action'], array( 'translate', 'copy-once' ) ) ) ) { | |
| 131 | - $metas[] = (string) $cf; | |
| 132 | - } else { | |
| 133 | - $metas = array_diff( $metas, array( (string) $cf ) ); | |
| 134 | - } | |
| 135 | - } | |
| 136 | - } | |
| 137 | - return $metas; | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | 117 | * Language and translation management for custom post types |
| 142 | 118 | * |
| 143 | 119 | * @since 1.0 |
| 144 | 120 | * |
| @@ -187,9 +163,9 @@ | ||
| 187 | 163 | * Translates the strings for an option |
| 188 | 164 | * |
| 189 | 165 | * @since 1.0 |
| 190 | 166 | * |
| 191 | - * @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 | |
| 192 | 168 | * @return array|string translated string(s) |
| 193 | 169 | */ |
| 194 | 170 | public function translate_strings( $value ) { |
| 195 | 171 | $option = substr( current_filter(), 7 ); |
| @@ -199,50 +175,26 @@ | ||
| 199 | 175 | /** |
| 200 | 176 | * Recursively registers strings for a serialized option |
| 201 | 177 | * |
| 202 | 178 | * @since 1.0 |
| 203 | - * @since 2.7 Signature modified | |
| 204 | 179 | * |
| 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. | |
| 180 | + * @param string $context the group in which the strings will be registered | |
| 181 | + * @param array $options | |
| 182 | + * @param object $key XML node | |
| 209 | 183 | */ |
| 210 | - protected function register_string_recursive( $context, $option, $values, $key ) { | |
| 211 | - if ( is_object( $values ) ) { | |
| 212 | - $values = (array) $values; | |
| 213 | - } | |
| 214 | - | |
| 184 | + protected function register_string_recursive( $context, $options, $key ) { | |
| 215 | 185 | $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; | |
| 226 | - } | |
| 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 | - } | |
| 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 ); | |
| 236 | 192 | } |
| 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 | - } | |
| 242 | 193 | } |
| 243 | 194 | } else { |
| 244 | - pll_register_string( $option, $values, $context, true ); // Multiline as in WPML. | |
| 195 | + $attributes = $key->attributes(); | |
| 196 | + pll_register_string( (string) $attributes['name'], $options, $context ); | |
| 245 | 197 | } |
| 246 | 198 | } |
| 247 | 199 | |
| 248 | 200 | /** |
| @@ -249,49 +201,24 @@ | ||
| 249 | 201 | * Recursively translates strings for a serialized option |
| 250 | 202 | * |
| 251 | 203 | * @since 1.0 |
| 252 | 204 | * |
| 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) | |
| 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) | |
| 256 | 208 | */ |
| 257 | 209 | protected function translate_strings_recursive( $values, $key ) { |
| 258 | 210 | $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; | |
| 269 | - } | |
| 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 | - } | |
| 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 ); | |
| 284 | 217 | } |
| 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 | 218 | } |
| 291 | 219 | } else { |
| 292 | 220 | $values = pll__( $values ); |
| 293 | 221 | } |
| 294 | - | |
| 295 | 222 | return $values; |
| 296 | 223 | } |
| 297 | 224 | } |