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