PluginProbe
Polylang / 3.4.2
Polylang v3.4.2
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.4.2, at modules/wpml/wpml-compat.php

184 lines 5.1 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 self::$strings = get_option( 'polylang_wpml_strings', array() );
44
45 if ( ! is_array( self::$strings ) ) {
46 self::$strings = array(); // In case the serialized option is corrupted.
47 }
48
49 add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
50 add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
51 add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
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 do this
101 *
102 * @since 1.0.2
103 *
104 * @param 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 // If a string has already been registered with the same name and context, let's replace it.
115 $exist_string = $this->get_string_by_context_and_name( $context, $name );
116 if ( $exist_string && $exist_string !== $string ) {
117 $languages = PLL()->model->get_languages_list();
118
119 // Assign translations of the old string to the new string, except for the default language.
120 foreach ( $languages as $language ) {
121 if ( $language->is_default ) {
122 continue;
123 }
124 $mo = new PLL_MO();
125 $mo->import_from_db( $language );
126 $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) );
127 $mo->export_to_db( $language );
128 }
129 $this->unregister_string( $context, $name );
130 }
131
132 // Registers the string if it does not exist yet (multiline as in WPML).
133 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
134 if ( ! in_array( $to_register, self::$strings ) ) {
135 $key = md5( "$context | $name" );
136 self::$strings[ $key ] = $to_register;
137 update_option( 'polylang_wpml_strings', self::$strings );
138 }
139 }
140
141 /**
142 * Removes a string from the registered strings list
143 *
144 * @since 1.0.2
145 *
146 * @param string $context The group in which the string is registered.
147 * @param string $name A unique name for the string.
148 * @return void
149 */
150 public function unregister_string( $context, $name ) {
151 $key = md5( "$context | $name" );
152 if ( isset( self::$strings[ $key ] ) ) {
153 unset( self::$strings[ $key ] );
154 update_option( 'polylang_wpml_strings', self::$strings );
155 }
156 }
157
158 /**
159 * Adds strings registered by icl_register_string to those registered by pll_register_string
160 *
161 * @since 1.0.2
162 *
163 * @param array $strings existing registered strings
164 * @return array registered strings with added strings through WPML API
165 */
166 public function get_strings( $strings ) {
167 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
168 }
169
170 /**
171 * Get a registered string by its context and name
172 *
173 * @since 2.0
174 *
175 * @param string $context The group in which the string is registered.
176 * @param string $name A unique name for the string.
177 * @return bool|string The registered string, false if none was found.
178 */
179 public function get_string_by_context_and_name( $context, $name ) {
180 $key = md5( "$context | $name" );
181 return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false;
182 }
183 }
184