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

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