PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | modules/wpml/wpml-compat.php +59 -21 2.93.7.7 View file →
@@ -10,27 +10,46 @@
10 10 *
11 11 * @since 1.0.2
12 12 */
13 13 class PLL_WPML_Compat {
14 - protected static $instance; // For singleton
15 - protected static $strings; // Used for cache
14 + /**
15 + * Singleton instance
16 + *
17 + * @var PLL_WPML_Compat|null
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 = array();
27 +
28 + /**
29 + * @var PLL_WPML_API
30 + */
16 31 public $api;
17 32
18 33 /**
19 - * Constructor
34 + * Constructor.
20 35 *
21 36 * @since 1.0.2
22 37 */
23 38 protected function __construct() {
24 - // Load the WPML API
39 + // Load the WPML API.
25 40 require_once __DIR__ . '/wpml-legacy-api.php';
26 41 $this->api = new PLL_WPML_API();
27 42
28 - self::$strings = get_option( 'polylang_wpml_strings', array() );
43 + $strings = get_option( 'polylang_wpml_strings' );
29 44
45 + if ( is_array( $strings ) ) {
46 + self::$strings = $strings;
47 + add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
48 + }
49 +
30 50 add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
31 51 add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
32 - add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
33 52 }
34 53
35 54 /**
36 55 * Access to the single instance of the class
@@ -36,9 +55,9 @@
36 55 * Access to the single instance of the class
37 56 *
38 57 * @since 1.7
39 58 *
40 - * @return object
59 + * @return PLL_WPML_Compat
41 60 */
42 61 public static function instance() {
43 62 if ( empty( self::$instance ) ) {
44 63 self::$instance = new self();
@@ -48,11 +67,13 @@
48 67
49 68 /**
50 69 * Defines two WPML constants once the language has been defined
51 70 * The compatibility with WPML is not perfect on admin side as the constants are defined
52 - * 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).
53 72 *
54 73 * @since 0.9.5
74 + *
75 + * @return void
55 76 */
56 77 public function define_constants() {
57 78 if ( ! empty( PLL()->curlang ) ) {
58 79 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
@@ -74,18 +95,33 @@
74 95 }
75 96
76 97 /**
77 98 * Unlike pll_register_string, icl_register_string stores the string in database
78 - * so we need to do the same as some plugins or themes may expect this
79 - * 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.
80 101 *
81 102 * @since 1.0.2
82 103 *
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.
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.
107 + * @return void
86 108 */
87 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 +
88 124 // If a string has already been registered with the same name and context, let's replace it.
89 125 $exist_string = $this->get_string_by_context_and_name( $context, $name );
90 126 if ( $exist_string && $exist_string !== $string ) {
91 127 $languages = PLL()->model->get_languages_list();
@@ -91,14 +127,15 @@
91 127 $languages = PLL()->model->get_languages_list();
92 128
93 129 // Assign translations of the old string to the new string, except for the default language.
94 130 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 );
131 + if ( $language->is_default ) {
132 + continue;
100 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 );
101 138 }
102 139 $this->unregister_string( $context, $name );
103 140 }
104 141
@@ -103,9 +140,9 @@
103 140 }
104 141
105 142 // Registers the string if it does not exist yet (multiline as in WPML).
106 143 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
107 - if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
144 + if ( ! in_array( $to_register, self::$strings ) ) {
108 145 $key = md5( "$context | $name" );
109 146 self::$strings[ $key ] = $to_register;
110 147 update_option( 'polylang_wpml_strings', self::$strings );
111 148 }
@@ -117,8 +154,9 @@
117 154 * @since 1.0.2
118 155 *
119 156 * @param string $context The group in which the string is registered.
120 157 * @param string $name A unique name for the string.
158 + * @return void
121 159 */
122 160 public function unregister_string( $context, $name ) {
123 161 $key = md5( "$context | $name" );
124 162 if ( isset( self::$strings[ $key ] ) ) {
@@ -145,11 +183,11 @@
145 183 * @since 2.0
146 184 *
147 185 * @param string $context The group in which the string is registered.
148 186 * @param string $name A unique name for the string.
149 - * @return bool|string The registered string, false if none was found.
187 + * @return string The registered string, empty if none was found.
150 188 */
151 189 public function get_string_by_context_and_name( $context, $name ) {
152 190 $key = md5( "$context | $name" );
153 - return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false;
191 + return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : '';
154 192 }
155 193 }