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

155 lines 4.6 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.
91 foreach ( $languages as $language ) {
92 $mo = new PLL_MO();
93 $mo->import_from_db( $language );
94 $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) );
95 $mo->export_to_db( $language );
96 }
97 $this->unregister_string( $context, $name );
98 }
99
100 // Registers the string if it does not exist yet (multiline as in WPML).
101 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
102 if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
103 self::$strings[] = $to_register;
104 update_option( 'polylang_wpml_strings', self::$strings );
105 }
106 }
107
108 /**
109 * Removes a string from the registered strings list
110 *
111 * @since 1.0.2
112 *
113 * @param string $context the group in which the string is registered, defaults to 'polylang'
114 * @param string $name a unique name for the string
115 */
116 public function unregister_string( $context, $name ) {
117 foreach ( self::$strings as $key => $string ) {
118 if ( $string['context'] == $context && $string['name'] == $name ) {
119 unset( self::$strings[ $key ] );
120 update_option( 'polylang_wpml_strings', self::$strings );
121 }
122 }
123 }
124
125 /**
126 * Adds strings registered by icl_register_string to those registered by pll_register_string
127 *
128 * @since 1.0.2
129 *
130 * @param array $strings existing registered strings
131 * @return array registered strings with added strings through WPML API
132 */
133 public function get_strings( $strings ) {
134 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
135 }
136
137 /**
138 * Get a registered string by its context and name
139 *
140 * @since 2.0
141 *
142 * @param string $context the group in which the string is registered
143 * @param string $name a unique name for the string
144 * @return bool|string the registered string, false if none was found
145 */
146 public function get_string_by_context_and_name( $context, $name ) {
147 foreach ( self::$strings as $string ) {
148 if ( $string['context'] == $context && $string['name'] == $name ) {
149 return $string['string'];
150 }
151 }
152 return false;
153 }
154 }
155