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

139 lines 4.0 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 static protected $instance; // For singleton
12 static protected $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_get_strings', array( $this, 'get_strings' ) );
29 }
30
31 /**
32 * Access to the single instance of the class
33 *
34 * @since 1.7
35 *
36 * @return object
37 */
38 static public function instance() {
39 if ( empty( self::$instance ) ) {
40 self::$instance = new self();
41 }
42 return self::$instance;
43 }
44
45 /**
46 * Defines two WPML constants once the language has been defined
47 * The compatibility with WPML is not perfect on admin side as the constants are defined
48 * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
49 *
50 * @since 0.9.5
51 */
52 public function define_constants() {
53 if ( ! empty( PLL()->curlang ) ) {
54 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
55 define( 'ICL_LANGUAGE_CODE', PLL()->curlang->slug );
56 }
57
58 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
59 define( 'ICL_LANGUAGE_NAME', PLL()->curlang->name );
60 }
61 } elseif ( ! PLL() instanceof PLL_Frontend ) {
62 if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
63 define( 'ICL_LANGUAGE_CODE', 'all' );
64 }
65
66 if ( ! defined( 'ICL_LANGUAGE_NAME' ) ) {
67 define( 'ICL_LANGUAGE_NAME', '' );
68 }
69 }
70 }
71
72 /**
73 * Unlike pll_register_string, icl_register_string stores the string in database
74 * so we need to do the same as some plugins or themes may expect this
75 * we use a serialized option to do this
76 *
77 * @since 1.0.2
78 *
79 * @param string $context the group in which the string is registered, defaults to 'polylang'
80 * @param string $name a unique name for the string
81 * @param string $string the string to register
82 */
83 public function register_string( $context, $name, $string ) {
84 // Registers the string if it does not exist yet
85 $to_register = array( 'context' => $context, 'name' => $name, 'string' => $string, 'multiline' => false, 'icl' => true );
86 if ( ! in_array( $to_register, self::$strings ) && $to_register['string'] ) {
87 self::$strings[] = $to_register;
88 update_option( 'polylang_wpml_strings', self::$strings );
89 }
90 }
91
92 /**
93 * Removes a string from the registered strings list
94 *
95 * @since 1.0.2
96 *
97 * @param string $context the group in which the string is registered, defaults to 'polylang'
98 * @param string $name a unique name for the string
99 */
100 public function unregister_string( $context, $name ) {
101 foreach ( self::$strings as $key => $string ) {
102 if ( $string['context'] == $context && $string['name'] == $name ) {
103 unset( self::$strings[ $key ] );
104 update_option( 'polylang_wpml_strings', self::$strings );
105 }
106 }
107 }
108
109 /**
110 * Adds strings registered by icl_register_string to those registered by pll_register_string
111 *
112 * @since 1.0.2
113 *
114 * @param array $strings existing registered strings
115 * @return array registered strings with added strings through WPML API
116 */
117 public function get_strings( $strings ) {
118 return empty( self::$strings ) ? $strings : array_merge( $strings, self::$strings );
119 }
120
121 /**
122 * Get a registered string by its context and name
123 *
124 * @since 2.0
125 *
126 * @param string $context the group in which the string is registered
127 * @param string $name a unique name for the string
128 * @return bool|string the registered string, false if none was found
129 */
130 public function get_string_by_context_and_name( $context, $name ) {
131 foreach ( self::$strings as $string ) {
132 if ( $string['context'] == $context && $string['name'] == $name ) {
133 return $string['string'];
134 }
135 }
136 return false;
137 }
138 }
139