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

179 lines 4.9 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
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;
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 add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
46 add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
47 add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
48 }
49
50 /**
51 * Access to the single instance of the class
52 *
53 * @since 1.7
54 *
55 * @return PLL_WPML_Compat
56 */
57 public static function instance() {
58 if ( empty( self::$instance ) ) {
59 self::$instance = new self();
60 }
61 return self::$instance;
62 }
63
64 /**
65 * Defines two WPML constants once the language has been defined
66 * The compatibility with WPML is not perfect on admin side as the constants are defined
67 * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
68 *
69 * @since 0.9.5
70 *
71 * @return void
72 */
73 public function define_constants() {
74 if ( ! empty( PLL()->curlang ) ) {
75 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
76 define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
77 }
78
79 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
80 define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
81 }
82 } elseif ( ! PLL() instanceof PLL_Frontend ) {
83 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
84 define( 'ICL_LANGUAGE_CODE', 'all' );
85 }
86
87 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
88 define( 'ICL_LANGUAGE_NAME', '' );
89 }
90 }
91 }
92
93 /**
94 * Unlike pll_register_string, icl_register_string stores the string in database
95 * so we need to do the same as some plugins or themes may expect this
96 * we use a serialized option to do this
97 *
98 * @since 1.0.2
99 *
100 * @param string $context The group in which the string is registered.
101 * @param string $name A unique name for the string.
102 * @param string $string The string to register.
103 * @return void
104 */
105 public function register_string( $context, $name, $string ) {
106 if ( ! $string || ! is_scalar( $string ) ) {
107 return;
108 }
109
110 // If a string has already been registered with the same name and context, let's replace it.
111 $exist_string = $this->get_string_by_context_and_name( $context, $name );
112 if ( $exist_string && $exist_string !== $string ) {
113 $languages = PLL()->model->get_languages_list();
114
115 // Assign translations of the old string to the new string, except for the default language.
116 foreach ( $languages as $language ) {
117 if ( pll_default_language() !== $language->slug ) {
118 $mo = new PLL_MO();
119 $mo->import_from_db( $language );
120 $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) );
121 $mo->export_to_db( $language );
122 }
123 }
124 $this->unregister_string( $context, $name );
125 }
126
127 // Registers the string if it does not exist yet (multiline as in WPML).
128 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
129 if ( ! in_array( $to_register, self::$strings ) ) {
130 $key = md5( "$context | $name" );
131 self::$strings[ $key ] = $to_register;
132 update_option( 'polylang_wpml_strings', self::$strings );
133 }
134 }
135
136 /**
137 * Removes a string from the registered strings list
138 *
139 * @since 1.0.2
140 *
141 * @param string $context The group in which the string is registered.
142 * @param string $name A unique name for the string.
143 * @return void
144 */
145 public function unregister_string( $context, $name ) {
146 $key = md5( "$context | $name" );
147 if ( isset( self::$strings[ $key ] ) ) {
148 unset( self::$strings[ $key ] );
149 update_option( 'polylang_wpml_strings', self::$strings );
150 }
151 }
152
153 /**
154 * Adds strings registered by icl_register_string to those registered by pll_register_string
155 *
156 * @since 1.0.2
157 *
158 * @param array $strings existing registered strings
159 * @return array registered strings with added strings through WPML API
160 */
161 public function get_strings( $strings ) {
162 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
163 }
164
165 /**
166 * Get a registered string by its context and name
167 *
168 * @since 2.0
169 *
170 * @param string $context The group in which the string is registered.
171 * @param string $name A unique name for the string.
172 * @return bool|string The registered string, false if none was found.
173 */
174 public function get_string_by_context_and_name( $context, $name ) {
175 $key = md5( "$context | $name" );
176 return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false;
177 }
178 }
179