PluginProbe
Polylang / 2.7.1
Polylang v2.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 2.7.1, at modules/wpml/wpml-compat.php

153 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WPML Compatibility class
5 * Defines some WPML constants
6 * Registers strings in a persistent way as done by WPML
7 *
8 * @since 1.0.2
9 */
10 class PLL_WPML_Compat {
11 protected static $instance; // For singleton
12 protected static $strings; // Used for cache
13 public $api;
14
15 /**
16 * Constructor
17 *
18 * @since 1.0.2
19 */
20 protected function __construct() {
21 // Load the WPML API
22 require_once PLL_MODULES_INC . '/wpml/wpml-legacy-api.php';
23 $this->api = new PLL_WPML_API();
24
25 self::$strings = get_option( 'polylang_wpml_strings', array() );
26
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' ) );
30 }
31
32 /**
33 * Access to the single instance of the class
34 *
35 * @since 1.7
36 *
37 * @return object
38 */
39 public static function instance() {
40 if ( empty( self::$instance ) ) {
41 self::$instance = new self();
42 }
43 return self::$instance;
44 }
45
46 /**
47 * Defines two WPML constants once the language has been defined
48 * The compatibility with WPML is not perfect on admin side as the constants are defined
49 * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
50 *
51 * @since 0.9.5
52 */
53 public function define_constants() {
54 if ( ! empty( PLL()->curlang ) ) {
55 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
56 define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
57 }
58
59 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
60 define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
61 }
62 } elseif ( ! PLL() instanceof PLL_Frontend ) {
63 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
64 define( 'ICL_LANGUAGE_CODE', 'all' );
65 }
66
67 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
68 define( 'ICL_LANGUAGE_NAME', '' );
69 }
70 }
71 }
72
73 /**
74 * Unlike pll_register_string, icl_register_string stores the string in database
75 * so we need to do the same as some plugins or themes may expect this
76 * we use a serialized option to do this
77 *
78 * @since 1.0.2
79 *
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.
83 */
84 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 );
104 if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
105 $key = md5( "$context | $name" );
106 self::$strings[ $key ] = $to_register;
107 update_option( 'polylang_wpml_strings', self::$strings );
108 }
109 }
110
111 /**
112 * Removes a string from the registered strings list
113 *
114 * @since 1.0.2
115 *
116 * @param string $context The group in which the string is registered.
117 * @param string $name A unique name for the string.
118 */
119 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 );
124 }
125 }
126
127 /**
128 * Adds strings registered by icl_register_string to those registered by pll_register_string
129 *
130 * @since 1.0.2
131 *
132 * @param array $strings existing registered strings
133 * @return array registered strings with added strings through WPML API
134 */
135 public function get_strings( $strings ) {
136 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
137 }
138
139 /**
140 * Get a registered string by its context and name
141 *
142 * @since 2.0
143 *
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.
147 */
148 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;
151 }
152 }
153