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

156 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 * @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 protected static $instance; // For singleton
15 protected static $strings; // Used for cache
16 public $api;
17
18 /**
19 * Constructor
20 *
21 * @since 1.0.2
22 */
23 protected function __construct() {
24 // Load the WPML API
25 require_once __DIR__ . '/wpml-legacy-api.php';
26 $this->api = new PLL_WPML_API();
27
28 self::$strings = get_option( 'polylang_wpml_strings', array() );
29
30 add_action( 'pll_language_defined', array( $this, 'define_constants' ) );
31 add_action( 'pll_no_language_defined', array( $this, 'define_constants' ) );
32 add_filter( 'pll_get_strings', array( $this, 'get_strings' ) );
33 }
34
35 /**
36 * Access to the single instance of the class
37 *
38 * @since 1.7
39 *
40 * @return object
41 */
42 public static function instance() {
43 if ( empty( self::$instance ) ) {
44 self::$instance = new self();
45 }
46 return self::$instance;
47 }
48
49 /**
50 * Defines two WPML constants once the language has been defined
51 * The compatibility with WPML is not perfect on admin side as the constants are defined
52 * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
53 *
54 * @since 0.9.5
55 */
56 public function define_constants() {
57 if ( ! empty( PLL()->curlang ) ) {
58 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
59 define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
60 }
61
62 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
63 define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
64 }
65 } elseif ( ! PLL() instanceof PLL_Frontend ) {
66 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
67 define( 'ICL_LANGUAGE_CODE', 'all' );
68 }
69
70 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
71 define( 'ICL_LANGUAGE_NAME', '' );
72 }
73 }
74 }
75
76 /**
77 * Unlike pll_register_string, icl_register_string stores the string in database
78 * so we need to do the same as some plugins or themes may expect this
79 * we use a serialized option to do this
80 *
81 * @since 1.0.2
82 *
83 * @param string $context The group in which the string is registered.
84 * @param string $name A unique name for the string.
85 * @param string $string The string to register.
86 */
87 public function register_string( $context, $name, $string ) {
88 // If a string has already been registered with the same name and context, let's replace it.
89 $exist_string = $this->get_string_by_context_and_name( $context, $name );
90 if ( $exist_string && $exist_string !== $string ) {
91 $languages = PLL()->model->get_languages_list();
92
93 // Assign translations of the old string to the new string, except for the default language.
94 foreach ( $languages as $language ) {
95 if ( pll_default_language() !== $language->slug ) {
96 $mo = new PLL_MO();
97 $mo->import_from_db( $language );
98 $mo->add_entry( $mo->make_entry( $string, $mo->translate( $exist_string ) ) );
99 $mo->export_to_db( $language );
100 }
101 }
102 $this->unregister_string( $context, $name );
103 }
104
105 // Registers the string if it does not exist yet (multiline as in WPML).
106 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => true, 'icl' => true );
107 if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
108 $key = md5( "$context | $name" );
109 self::$strings[ $key ] = $to_register;
110 update_option( 'polylang_wpml_strings', self::$strings );
111 }
112 }
113
114 /**
115 * Removes a string from the registered strings list
116 *
117 * @since 1.0.2
118 *
119 * @param string $context The group in which the string is registered.
120 * @param string $name A unique name for the string.
121 */
122 public function unregister_string( $context, $name ) {
123 $key = md5( "$context | $name" );
124 if ( isset( self::$strings[ $key ] ) ) {
125 unset( self::$strings[ $key ] );
126 update_option( 'polylang_wpml_strings', self::$strings );
127 }
128 }
129
130 /**
131 * Adds strings registered by icl_register_string to those registered by pll_register_string
132 *
133 * @since 1.0.2
134 *
135 * @param array $strings existing registered strings
136 * @return array registered strings with added strings through WPML API
137 */
138 public function get_strings( $strings ) {
139 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
140 }
141
142 /**
143 * Get a registered string by its context and name
144 *
145 * @since 2.0
146 *
147 * @param string $context The group in which the string is registered.
148 * @param string $name A unique name for the string.
149 * @return bool|string The registered string, false if none was found.
150 */
151 public function get_string_by_context_and_name( $context, $name ) {
152 $key = md5( "$context | $name" );
153 return isset( self::$strings[ $key ] ) ? self::$strings[ $key ]['string'] : false;
154 }
155 }
156