| @@ -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,10 +7,10 @@ | ||
| 10 | 7 | * |
| 11 | 8 | * @since 1.0.2 |
| 12 | 9 | */ |
| 13 | 10 | class PLL_WPML_Compat { |
| 14 | - protected static $instance; // For singleton | |
| 15 | - protected static $strings; // Used for cache | |
| 11 | + static protected $instance; // For singleton | |
| 12 | + static protected $strings; // Used for cache | |
| 16 | 13 | public $api; |
| 17 | 14 | |
| 18 | 15 | /** |
| 19 | 16 | * Constructor |
| @@ -21,9 +18,9 @@ | ||
| 21 | 18 | * @since 1.0.2 |
| 22 | 19 | */ |
| 23 | 20 | protected function __construct() { |
| 24 | 21 | // Load the WPML API |
| 25 | - require_once __DIR__ . '/wpml-legacy-api.php'; | |
| 22 | + require_once PLL_MODULES_INC . '/wpml/wpml-legacy-api.php'; | |
| 26 | 23 | $this->api = new PLL_WPML_API(); |
| 27 | 24 | |
| 28 | 25 | self::$strings = get_option( 'polylang_wpml_strings', array() ); |
| 29 | 26 | |
| @@ -38,9 +35,9 @@ | ||
| 38 | 35 | * @since 1.7 |
| 39 | 36 | * |
| 40 | 37 | * @return object |
| 41 | 38 | */ |
| 42 | - public static function instance() { | |
| 39 | + static public function instance() { | |
| 43 | 40 | if ( empty( self::$instance ) ) { |
| 44 | 41 | self::$instance = new self(); |
| 45 | 42 | } |
| 46 | 43 | return self::$instance; |
| @@ -79,35 +76,17 @@ | ||
| 79 | 76 | * we use a serialized option to do this |
| 80 | 77 | * |
| 81 | 78 | * @since 1.0.2 |
| 82 | 79 | * |
| 83 | - * @param string $context The group in which the string is registered. | |
| 84 | - * @param string $name A unique name for the string. | |
| 85 | - * @param string $string The string to register. | |
| 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 | |
| 86 | 83 | */ |
| 87 | 84 | public function register_string( $context, $name, $string ) { |
| 88 | - // If a string has already been registered with the same name and context, let's replace it. | |
| 89 | - $exist_string = $this->get_string_by_context_and_name( $context, $name ); | |
| 90 | - if ( $exist_string && $exist_string !== $string ) { | |
| 91 | - $languages = PLL()->model->get_languages_list(); | |
| 92 | - | |
| 93 | - // Assign translations of the old string to the new string, except for the default language. | |
| 94 | - foreach ( $languages as $language ) { | |
| 95 | - if ( pll_default_language() !== $language->slug ) { | |
| 96 | - $mo = new PLL_MO(); | |
| 97 | - $mo->import_from_db( $language ); | |
| 98 | - $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) ); | |
| 99 | - $mo->export_to_db( $language ); | |
| 100 | - } | |
| 101 | - } | |
| 102 | - $this->unregister_string( $context, $name ); | |
| 103 | - } | |
| 104 | - | |
| 105 | - // 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) | |
| 106 | 86 | $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true ); |
| 107 | 87 | if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) { |
| 108 | - $key = md5( "$context | $name" ); | |
| 109 | - self::$strings[ $key ] = $to_register; | |
| 88 | + self::$strings[] = $to_register; | |
| 110 | 89 | update_option( 'polylang_wpml_strings', self::$strings ); |
| 111 | 90 | } |
| 112 | 91 | } |
| 113 | 92 | |
| @@ -115,16 +94,17 @@ | ||
| 115 | 94 | * Removes a string from the registered strings list |
| 116 | 95 | * |
| 117 | 96 | * @since 1.0.2 |
| 118 | 97 | * |
| 119 | - * @param string $context The group in which the string is registered. | |
| 120 | - * @param string $name A unique name for the string. | |
| 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 | |
| 121 | 100 | */ |
| 122 | 101 | public function unregister_string( $context, $name ) { |
| 123 | - $key = md5( "$context | $name" ); | |
| 124 | - if ( isset( self::$strings[ $key ] ) ) { | |
| 125 | - unset( self::$strings[ $key ] ); | |
| 126 | - 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 | + } | |
| 127 | 107 | } |
| 128 | 108 | } |
| 129 | 109 | |
| 130 | 110 | /** |
| @@ -143,13 +123,17 @@ | ||
| 143 | 123 | * Get a registered string by its context and name |
| 144 | 124 | * |
| 145 | 125 | * @since 2.0 |
| 146 | 126 | * |
| 147 | - * @param string $context The group in which the string is registered. | |
| 148 | - * @param string $name A unique name for the string. | |
| 149 | - * @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 | |
| 150 | 130 | */ |
| 151 | 131 | public function get_string_by_context_and_name( $context, $name ) { |
| 152 | - $key = md5( "$context | $name" ); | |
| 153 | - 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; | |
| 154 | 138 | } |
| 155 | 139 | } |