| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | class PLL_WPML_Compat { |
| 14 | 14 | /** |
| 15 | 15 | * Singleton instance |
| 16 | 16 | * |
| 17 | - * @var PLL_WPML_Compat | |
| 17 | + * @var PLL_WPML_Compat|null | |
| 18 | 18 | */ |
| 19 | 19 | protected static $instance; |
| 20 | 20 | |
| 21 | 21 | /** |
| @@ -22,9 +22,9 @@ | ||
| 22 | 22 | * Stores the strings registered with the WPML API. |
| 23 | 23 | * |
| 24 | 24 | * @var array |
| 25 | 25 | */ |
| 26 | - protected static $strings; | |
| 26 | + protected static $strings = array(); | |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * @var PLL_WPML_API |
| 30 | 30 | */ |
| @@ -30,22 +30,26 @@ | ||
| 30 | 30 | */ |
| 31 | 31 | public $api; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | - * Constructor | |
| 34 | + * Constructor. | |
| 35 | 35 | * |
| 36 | 36 | * @since 1.0.2 |
| 37 | 37 | */ |
| 38 | 38 | protected function __construct() { |
| 39 | - // Load the WPML API | |
| 39 | + // Load the WPML API. | |
| 40 | 40 | require_once __DIR__ . '/wpml-legacy-api.php'; |
| 41 | 41 | $this->api = new PLL_WPML_API(); |
| 42 | 42 | |
| 43 | - self::$strings = get_option( 'polylang_wpml_strings', array() ); | |
| 43 | + $strings = get_option( 'polylang_wpml_strings' ); | |
| 44 | 44 | |
| 45 | + if ( is_array( $strings ) ) { | |
| 46 | + self::$strings = $strings; | |
| 47 | + add_filter( 'pll_get_strings', array( $this, 'get_strings' ) ); | |
| 48 | + } | |
| 49 | + | |
| 45 | 50 | add_action( 'pll_language_defined', array( $this, 'define_constants' ) ); |
| 46 | 51 | add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) ); |
| 47 | - add_filter( 'pll_get_strings', array( $this, 'get_strings' ) ); | |
| 48 | 52 | } |
| 49 | 53 | |
| 50 | 54 | /** |
| 51 | 55 | * Access to the single instance of the class |
| @@ -63,9 +67,9 @@ | ||
| 63 | 67 | |
| 64 | 68 | /** |
| 65 | 69 | * Defines two WPML constants once the language has been defined |
| 66 | 70 | * The compatibility with WPML is not perfect on admin side as the constants are defined |
| 67 | - * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie ) | |
| 71 | + * in 'setup_theme' by Polylang (based on user info) and 'plugins_loaded' by WPML (based on cookie). | |
| 68 | 72 | * |
| 69 | 73 | * @since 0.9.5 |
| 70 | 74 | * |
| 71 | 75 | * @return void |
| @@ -91,19 +95,33 @@ | ||
| 91 | 95 | } |
| 92 | 96 | |
| 93 | 97 | /** |
| 94 | 98 | * Unlike pll_register_string, icl_register_string stores the string in database |
| 95 | - * so we need to do the same as some plugins or themes may expect this | |
| 96 | - * we use a serialized option to do this | |
| 99 | + * so we need to do the same as some plugins or themes may expect this. | |
| 100 | + * We use a serialized option to store these strings. | |
| 97 | 101 | * |
| 98 | 102 | * @since 1.0.2 |
| 99 | 103 | * |
| 100 | - * @param string $context The group in which the string is registered. | |
| 101 | - * @param string $name A unique name for the string. | |
| 102 | - * @param string $string The string to register. | |
| 104 | + * @param string|string[] $context The group in which the string is registered. | |
| 105 | + * @param string $name A unique name for the string. | |
| 106 | + * @param string $string The string to register. | |
| 103 | 107 | * @return void |
| 104 | 108 | */ |
| 105 | 109 | public function register_string( $context, $name, $string ) { |
| 110 | + if ( ! $string || ! is_scalar( $string ) ) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /* | |
| 115 | + * WPML accepts arrays as context and internally converts them to strings. | |
| 116 | + * See WPML_Register_String_Filter::truncate_name_and_context(). | |
| 117 | + * This possibility is used by Types. | |
| 118 | + */ | |
| 119 | + if ( is_array( $context ) ) { | |
| 120 | + $name = isset( $context['context'] ) ? $name . $context['context'] : $name; | |
| 121 | + $context = $context['domain'] ?? ''; | |
| 122 | + } | |
| 123 | + | |
| 106 | 124 | // If a string has already been registered with the same name and context, let's replace it. |
| 107 | 125 | $exist_string = $this->get_string_by_context_and_name( $context, $name ); |
| 108 | 126 | if ( $exist_string && $exist_string !== $string ) { |
| 109 | 127 | $languages = PLL()->model->get_languages_list(); |
| @@ -109,14 +127,15 @@ | ||
| 109 | 127 | $languages = PLL()->model->get_languages_list(); |
| 110 | 128 | |
| 111 | 129 | // Assign translations of the old string to the new string, except for the default language. |
| 112 | 130 | foreach ( $languages as $language ) { |
| 113 | - if ( pll_default_language() !== $language->slug ) { | |
| 114 | - $mo = new PLL_MO(); | |
| 115 | - $mo->import_from_db( $language ); | |
| 116 | - $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) ); | |
| 117 | - $mo->export_to_db( $language ); | |
| 131 | + if ( $language->is_default ) { | |
| 132 | + continue; | |
| 118 | 133 | } |
| 134 | + $mo = new PLL_MO(); | |
| 135 | + $mo->import_from_db( $language ); | |
| 136 | + $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) ); | |
| 137 | + $mo->export_to_db( $language ); | |
| 119 | 138 | } |
| 120 | 139 | $this->unregister_string( $context, $name ); |
| 121 | 140 | } |
| 122 | 141 | |
| @@ -121,9 +140,9 @@ | ||
| 121 | 140 | } |
| 122 | 141 | |
| 123 | 142 | // Registers the string if it does not exist yet (multiline as in WPML). |
| 124 | 143 | $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true ); |
| 125 | - if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) { | |
| 144 | + if ( ! in_array( $to_register, self::$strings ) ) { | |
| 126 | 145 | $key = md5( "$context | $name" ); |
| 127 | 146 | self::$strings[ $key ] = $to_register; |
| 128 | 147 | update_option( 'polylang_wpml_strings', self::$strings ); |
| 129 | 148 | } |
| @@ -164,11 +183,11 @@ | ||
| 164 | 183 | * @since 2.0 |
| 165 | 184 | * |
| 166 | 185 | * @param string $context The group in which the string is registered. |
| 167 | 186 | * @param string $name A unique name for the string. |
| 168 | - * @return bool|string The registered string, false if none was found. | |
| 187 | + * @return string The registered string, empty if none was found. | |
| 169 | 188 | */ |
| 170 | 189 | public function get_string_by_context_and_name( $context, $name ) { |
| 171 | 190 | $key = md5( "$context | $name" ); |
| 172 | - return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false; | |
| 191 | + return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : ''; | |
| 173 | 192 | } |
| 174 | 193 | } |