PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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
polylang / modules / wpml / wpml-compat.php

wpml-compat.php in Polylang 3.7.1, at modules/wpml/wpml-compat.php

194 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * WPML Compatibility class
8 * Defines some WPML constants
9 * Registers strings in a persistent way as done by WPML
10 *
11 * @since 1.0.2
12 */
13 class PLL_WPML_Compat {
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 */
31 public $api;
32
33 /**
34 * Constructor.
35 *
36 * @since 1.0.2
37 */
38 protected function __construct() {
39 // Load the WPML API.
40 require_once __DIR__ . '/wpml-legacy-api.php';
41 $this->api = new PLL_WPML_API();
42
43 $strings = get_option( 'polylang_wpml_strings' );
44
45 if ( is_array( $strings ) ) {
46 self::$strings = $strings;
47 add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
48 }
49
50 add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
51 add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
52 }
53
54 /**
55 * Access to the single instance of the class
56 *
57 * @since 1.7
58 *
59 * @return PLL_WPML_Compat
60 */
61 public static function instance() {
62 if ( empty( self::$instance ) ) {
63 self::$instance = new self();
64 }
65 return self::$instance;
66 }
67
68 /**
69 * Defines two WPML constants once the language has been defined
70 * The compatibility with WPML is not perfect on admin side as the constants are defined
71 * in 'setup_theme' by Polylang (based on user info) and 'plugins_loaded' by WPML (based on cookie).
72 *
73 * @since 0.9.5
74 *
75 * @return void
76 */
77 public function define_constants() {
78 if ( ! empty( PLL()->curlang ) ) {
79 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
80 define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
81 }
82
83 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
84 define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
85 }
86 } elseif ( ! PLL() instanceof PLL_Frontend ) {
87 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
88 define( 'ICL_LANGUAGE_CODE', 'all' );
89 }
90
91 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
92 define( 'ICL_LANGUAGE_NAME', '' );
93 }
94 }
95 }
96
97 /**
98 * Unlike pll_register_string, icl_register_string stores the string in database
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.
101 *
102 * @since 1.0.2
103 *
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
108 */
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
124 // If a string has already been registered with the same name and context, let's replace it.
125 $exist_string = $this->get_string_by_context_and_name( $context, $name );
126 if ( $exist_string && $exist_string !== $string ) {
127 $languages = PLL()->model->get_languages_list();
128
129 // Assign translations of the old string to the new string, except for the default language.
130 foreach ( $languages as $language ) {
131 if ( $language->is_default ) {
132 continue;
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 );
138 }
139 $this->unregister_string( $context, $name );
140 }
141
142 // Registers the string if it does not exist yet (multiline as in WPML).
143 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
144 if ( ! in_array( $to_register, self::$strings ) ) {
145 $key = md5( "$context | $name" );
146 self::$strings[ $key ] = $to_register;
147 update_option( 'polylang_wpml_strings', self::$strings );
148 }
149 }
150
151 /**
152 * Removes a string from the registered strings list
153 *
154 * @since 1.0.2
155 *
156 * @param string $context The group in which the string is registered.
157 * @param string $name A unique name for the string.
158 * @return void
159 */
160 public function unregister_string( $context, $name ) {
161 $key = md5( "$context | $name" );
162 if ( isset( self::$strings[ $key ] ) ) {
163 unset( self::$strings[ $key ] );
164 update_option( 'polylang_wpml_strings', self::$strings );
165 }
166 }
167
168 /**
169 * Adds strings registered by icl_register_string to those registered by pll_register_string
170 *
171 * @since 1.0.2
172 *
173 * @param array $strings existing registered strings
174 * @return array registered strings with added strings through WPML API
175 */
176 public function get_strings( $strings ) {
177 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
178 }
179
180 /**
181 * Get a registered string by its context and name
182 *
183 * @since 2.0
184 *
185 * @param string $context The group in which the string is registered.
186 * @param string $name A unique name for the string.
187 * @return string The registered string, empty if none was found.
188 */
189 public function get_string_by_context_and_name( $context, $name ) {
190 $key = md5( "$context | $name" );
191 return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : '';
192 }
193 }
194