| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * WPML Compatibility class |
| 8 | 5 | * Defines some WPML constants |
| @@ -10,25 +7,10 @@ | ||
| 10 | 7 | * |
| 11 | 8 | * @since 1.0.2 |
| 12 | 9 | */ |
| 13 | 10 | class PLL_WPML_Compat { |
| 14 | - /** | |
| 15 | - * Singleton instance | |
| 16 | - * | |
| 17 | - * @var PLL_WPML_Compat | |
| 18 | - */ | |
| 19 | - protected static $instance; | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * Stores the strings registered with the WPML API. | |
| 23 | - * | |
| 24 | - * @var array | |
| 25 | - */ | |
| 26 | - protected static $strings; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * @var PLL_WPML_API | |
| 30 | - */ | |
| 11 | + protected static $instance; // For singleton | |
| 12 | + protected static $strings; // Used for cache | |
| 31 | 13 | public $api; |
| 32 | 14 | |
| 33 | 15 | /** |
| 34 | 16 | * Constructor |
| @@ -36,9 +18,9 @@ | ||
| 36 | 18 | * @since 1.0.2 |
| 37 | 19 | */ |
| 38 | 20 | protected function __construct() { |
| 39 | 21 | // Load the WPML API |
| 40 | - require_once __DIR__ . '/wpml-legacy-api.php'; | |
| 22 | + require_once PLL_MODULES_INC . '/wpml/wpml-legacy-api.php'; | |
| 41 | 23 | $this->api = new PLL_WPML_API(); |
| 42 | 24 | |
| 43 | 25 | self::$strings = get_option( 'polylang_wpml_strings', array() ); |
| 44 | 26 | |
| @@ -51,9 +33,9 @@ | ||
| 51 | 33 | * Access to the single instance of the class |
| 52 | 34 | * |
| 53 | 35 | * @since 1.7 |
| 54 | 36 | * |
| 55 | - * @return PLL_WPML_Compat | |
| 37 | + * @return object | |
| 56 | 38 | */ |
| 57 | 39 | public static function instance() { |
| 58 | 40 | if ( empty( self::$instance ) ) { |
| 59 | 41 | self::$instance = new self(); |
| @@ -66,10 +48,8 @@ | ||
| 66 | 48 | * The compatibility with WPML is not perfect on admin side as the constants are defined |
| 67 | 49 | * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie ) |
| 68 | 50 | * |
| 69 | 51 | * @since 0.9.5 |
| 70 | - * | |
| 71 | - * @return void | |
| 72 | 52 | */ |
| 73 | 53 | public function define_constants() { |
| 74 | 54 | if ( ! empty( PLL()->curlang ) ) { |
| 75 | 55 | if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) { |
| @@ -96,36 +76,17 @@ | ||
| 96 | 76 | * we use a serialized option to do this |
| 97 | 77 | * |
| 98 | 78 | * @since 1.0.2 |
| 99 | 79 | * |
| 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. | |
| 103 | - * @return void | |
| 80 | + * @param string $context the group in which the string is registered, defaults to 'polylang' | |
| 81 | + * @param string $name a unique name for the string | |
| 82 | + * @param string $string the string to register | |
| 104 | 83 | */ |
| 105 | 84 | public function register_string( $context, $name, $string ) { |
| 106 | - // If a string has already been registered with the same name and context, let's replace it. | |
| 107 | - $exist_string = $this->get_string_by_context_and_name( $context, $name ); | |
| 108 | - if ( $exist_string && $exist_string !== $string ) { | |
| 109 | - $languages = PLL()->model->get_languages_list(); | |
| 110 | - | |
| 111 | - // Assign translations of the old string to the new string, except for the default language. | |
| 112 | - 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 ); | |
| 118 | - } | |
| 119 | - } | |
| 120 | - $this->unregister_string( $context, $name ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - // Registers the string if it does not exist yet (multiline as in WPML). | |
| 85 | + // Registers the string if it does not exist yet (multiline as in WPML) | |
| 124 | 86 | $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true ); |
| 125 | 87 | if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) { |
| 126 | - $key = md5( "$context | $name" ); | |
| 127 | - self::$strings[ $key ] = $to_register; | |
| 88 | + self::$strings[] = $to_register; | |
| 128 | 89 | update_option( 'polylang_wpml_strings', self::$strings ); |
| 129 | 90 | } |
| 130 | 91 | } |
| 131 | 92 | |
| @@ -133,17 +94,17 @@ | ||
| 133 | 94 | * Removes a string from the registered strings list |
| 134 | 95 | * |
| 135 | 96 | * @since 1.0.2 |
| 136 | 97 | * |
| 137 | - * @param string $context The group in which the string is registered. | |
| 138 | - * @param string $name A unique name for the string. | |
| 139 | - * @return void | |
| 98 | + * @param string $context the group in which the string is registered, defaults to 'polylang' | |
| 99 | + * @param string $name a unique name for the string | |
| 140 | 100 | */ |
| 141 | 101 | public function unregister_string( $context, $name ) { |
| 142 | - $key = md5( "$context | $name" ); | |
| 143 | - if ( isset( self::$strings[ $key ] ) ) { | |
| 144 | - unset( self::$strings[ $key ] ); | |
| 145 | - update_option( 'polylang_wpml_strings', self::$strings ); | |
| 102 | + foreach ( self::$strings as $key => $string ) { | |
| 103 | + if ( $string['context'] == $context && $string['name'] == $name ) { | |
| 104 | + unset( self::$strings[ $key ] ); | |
| 105 | + update_option( 'polylang_wpml_strings', self::$strings ); | |
| 106 | + } | |
| 146 | 107 | } |
| 147 | 108 | } |
| 148 | 109 | |
| 149 | 110 | /** |
| @@ -162,13 +123,17 @@ | ||
| 162 | 123 | * Get a registered string by its context and name |
| 163 | 124 | * |
| 164 | 125 | * @since 2.0 |
| 165 | 126 | * |
| 166 | - * @param string $context The group in which the string is registered. | |
| 167 | - * @param string $name A unique name for the string. | |
| 168 | - * @return bool|string The registered string, false if none was found. | |
| 127 | + * @param string $context the group in which the string is registered | |
| 128 | + * @param string $name a unique name for the string | |
| 129 | + * @return bool|string the registered string, false if none was found | |
| 169 | 130 | */ |
| 170 | 131 | public function get_string_by_context_and_name( $context, $name ) { |
| 171 | - $key = md5( "$context | $name" ); | |
| 172 | - return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false; | |
| 132 | + foreach ( self::$strings as $string ) { | |
| 133 | + if ( $string['context'] == $context && $string['name'] == $name ) { | |
| 134 | + return $string['string']; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + return false; | |
| 173 | 138 | } |
| 174 | 139 | } |