PluginProbe
Polylang / 3.2.8
Polylang v3.2.8
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-api.php

wpml-api.php in Polylang 3.2.8, at modules/wpml/wpml-api.php

320 lines 11.3 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 * A class to handle the WPML API based on hooks, introduced since WPML 3.2
8 * It partly relies on the legacy API
9 *
10 * @see https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
11 *
12 * @since 2.0
13 */
14 class PLL_WPML_API {
15 /**
16 * Stores the original language when the language is switched.
17 *
18 * @var PLL_Language
19 */
20 private static $original_language = null;
21
22 /**
23 * Constructor
24 *
25 * @since 2.0
26 */
27 public function __construct() {
28 // Site Wide Language informations
29
30 add_filter( 'wpml_active_languages', array( $this, 'wpml_active_languages' ), 10, 2 );
31 add_filter( 'wpml_display_language_names', array( $this, 'wpml_display_language_names' ), 10, 5 );
32 // wpml_translated_language_name => not applicable
33 add_filter( 'wpml_current_language', 'pll_current_language', 10, 0 );
34 add_filter( 'wpml_default_language', 'pll_default_language', 10, 0 );
35 // wpml_add_language_selector => not implemented
36 // wpml_footer_language_selector => not applicable
37 add_action( 'wpml_add_language_form_field', array( $this, 'wpml_add_language_form_field' ) );
38 add_filter( 'wpml_language_is_active', array( $this, 'wpml_language_is_active' ), 10, 2 );
39 add_filter( 'wpml_is_rtl', array( $this, 'wpml_is_rtl' ) );
40 // wpml_language_form_input_field => See wpml_add_language_form_field
41 // wpml_language_has_switched => See wpml_switch_language
42 // wpml_element_trid => not implemented
43 // wpml_get_element_translations => not implemented
44 // wpml_language_switcher => not implemented
45 // wpml_browser_redirect_language_params => not implemented
46 // wpml_enqueue_browser_redirect_language => not applicable
47 // wpml_enqueued_browser_redirect_language => not applicable
48 // wpml_encode_string => not applicable
49 // wpml_decode_string => not applicable
50
51 // Retrieving Language Information for Content
52
53 add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 );
54 add_action( 'wpml_switch_language', array( __CLASS__, 'wpml_switch_language' ), 10, 2 );
55 add_filter( 'wpml_element_language_code', array( $this, 'wpml_element_language_code' ), 10, 3 );
56 // wpml_element_language_details => not applicable
57
58 // Retrieving Localized Content
59
60 add_filter( 'wpml_home_url', 'pll_home_url', 10, 0 );
61 add_filter( 'wpml_element_link', 'icl_link_to_element', 10, 7 );
62 add_filter( 'wpml_object_id', 'icl_object_id', 10, 4 );
63 add_filter( 'wpml_translate_single_string', array( $this, 'wpml_translate_single_string' ), 10, 4 );
64 // wpml_translate_string => not applicable
65 // wpml_unfiltered_admin_string => not implemented
66 add_filter( 'wpml_permalink', array( $this, 'wpml_permalink' ), 10, 2 );
67 // wpml_elements_without_translations => not implemented
68 add_filter( 'wpml_get_translated_slug', array( $this, 'wpml_get_translated_slug' ), 10, 3 );
69
70 // Finding the Translation State of Content
71
72 // wpml_element_translation_type => not implemented
73 add_filter( 'wpml_element_has_translations', array( $this, 'wpml_element_has_translations' ), 10, 3 );
74 // wpml_master_post_from_duplicate => not applicable
75 // wpml_post_duplicates => not applicable
76
77 // Inserting Content
78
79 // wpml_admin_make_post_duplicates => not applicable
80 // wpml_make_post_duplicates => not applicable
81 add_action( 'wpml_register_single_string', 'icl_register_string', 10, 3 );
82 // wpml_register_string => not applicable
83 // wpml_register_string_packages => not applicable
84 // wpml_delete_package_action => not applicable
85 // wpml_show_package_language_ui => not applicable
86 // wpml_set_element_language_details => not implemented
87 // wpml_multilingual_options => not applicable
88
89 // Miscellaneous
90
91 // wpml_element_type => not applicable
92 // wpml_setting => not applicable
93 // wpml_sub_setting => not applicable
94 // wpml_editor_cf_to_display => not applicable
95 // wpml_tm_save_translation_cf => not implemented
96 // wpml_tm_xliff_export_translated_cf => not applicable
97 // wpml_tm_xliff_export_original_cf => not applicable
98 // wpml_duplicate_generic_string => not applicable
99 // wpml_translatable_user_meta_fields => not implemented
100 // wpml_cross_domain_language_data => not applicable
101 // wpml_get_cross_domain_language_data => not applicable
102 // wpml_loaded => not applicable
103 // wpml_st_loaded => not applicable
104 // wpml_tm_loaded => not applicable
105 // wpml_hide_management_column (3.4.1) => not applicable
106 // wpml_ls_directories_to_scan => not applicable
107 // wpml_ls_model_css_classes => not applicable
108 // wpml_ls_model_language_css_classes => not applicable
109 // wpml_tf_feedback_open_link => not applicable
110 // wpml_sync_custom_field => not implemented
111 // wpml_sync_all_custom_fields => not implemented
112 // wpml_is_redirected => not implemented
113
114 // Updating Content
115
116 // wpml_set_translation_mode_for_post_type => not implemented
117 }
118
119 /**
120 * Get a list of the languages enabled for a site
121 *
122 * @since 2.0
123 *
124 * @param mixed $null Not used
125 * @param array| string $args See arguments of icl_get_languages()
126 * @return array Array of arrays per language
127 */
128 public function wpml_active_languages( $null, $args = '' ) {
129 return icl_get_languages( $args );
130 }
131
132 /**
133 * In WPML, get a language's native and translated name for display in a custom language switcher
134 * Since Polylang does not implement the translated name, always returns only the native name,
135 * so the 3rd, 4th and 5th parameters are not used.
136 *
137 * @since 2.2
138 *
139 * @param mixed $null Not used.
140 * @param string $native_name The language native name.
141 * @return string
142 */
143 public function wpml_display_language_names( $null, $native_name ) {
144 return $native_name;
145 }
146
147 /**
148 * Returns an HTML hidden input field with name=”lang” and as value the current language
149 *
150 * @since 2.0
151 *
152 * @return void
153 */
154 public function wpml_add_language_form_field() {
155 $lang = pll_current_language();
156 $field = sprintf( '<input type="hidden" name="lang" value="%s" />', esc_attr( $lang ) );
157 $field = apply_filters( 'wpml_language_form_input_field', $field, $lang );
158 echo $field; // phpcs:ignore WordPress.Security.EscapeOutput
159 }
160
161 /**
162 * Find out if a specific language is enabled for the site
163 *
164 * @since 2.0
165 *
166 * @param mixed $null Not used
167 * @param string $slug Language code
168 * @return bool
169 */
170 public function wpml_language_is_active( $null, $slug ) {
171 $language = PLL()->model->get_language( $slug );
172 return empty( $language->active ) || true === $language->active;
173 }
174
175 /**
176 * Find out whether the current language text direction is RTL or not
177 *
178 * @since 2.0
179 *
180 * @return bool
181 */
182 public function wpml_is_rtl() {
183 return pll_current_language( 'is_rtl' );
184 }
185
186 /**
187 * Switches whole site to the given language or restores the language that was set when first calling this function.
188 * Unlike the WPML original action, it is not possible to set the current language and the cookie to different values.
189 *
190 * @since 2.7
191 *
192 * @param null|string $lang Language code to switch into, restores the original language if null.
193 * @param bool|string $cookie Optionally also switches the cookie.
194 * @return void
195 */
196 public static function wpml_switch_language( $lang = null, $cookie = false ) {
197 if ( null === self::$original_language ) {
198 self::$original_language = PLL()->curlang;
199 }
200
201 if ( empty( $lang ) ) {
202 PLL()->curlang = self::$original_language;
203 } elseif ( 'all' === $lang ) {
204 PLL()->curlang = null;
205 } elseif ( in_array( $lang, pll_languages_list() ) ) {
206 PLL()->curlang = PLL()->model->get_language( $lang );
207 }
208
209 if ( $cookie && isset( PLL()->choose_lang ) ) {
210 PLL()->choose_lang->maybe_setcookie();
211 }
212
213 do_action( 'wpml_language_has_switched', $lang, $cookie, self::$original_language );
214 }
215
216 /**
217 * Get the language code for a translatable element
218 *
219 * @since 2.0
220 *
221 * @param mixed $language_code
222 * @param array $args An array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy
223 * @return string
224 */
225 public function wpml_element_language_code( $language_code, $args ) {
226 $type = $args['element_type'];
227 $id = $args['element_id'];
228
229 if ( 'post' === $type || pll_is_translated_post_type( $type ) ) {
230 return pll_get_post_language( $id );
231 }
232
233 if ( 'term' === $type || pll_is_translated_taxonomy( $type ) ) {
234 $term = get_term_by( 'term_taxonomy_id', $id );
235 if ( $term instanceof WP_Term ) {
236 $id = $term->term_id;
237 }
238 return pll_get_term_language( $id );
239 }
240
241 return $language_code;
242 }
243
244 /**
245 * Translates a string
246 *
247 * @since 2.0
248 *
249 * @param string $string The string's original value
250 * @param string $context The string's registered context
251 * @param string $name The string's registered name
252 * @param null|string $lang Optional, return the translation in this language, defaults to current language
253 * @return string The translated string
254 */
255 public function wpml_translate_single_string( $string, $context, $name, $lang = null ) {
256 $has_translation = null; // Passed by reference
257 return icl_translate( $context, $name, $string, false, $has_translation, $lang );
258 }
259
260 /**
261 * Converts a permalink to a language specific permalink
262 *
263 * @since 2.2
264 *
265 * @param string $url The url to filter
266 * @param null|string $lang Langage code, optional, defaults to the current language
267 * @return string
268 */
269 public function wpml_permalink( $url, $lang = '' ) {
270 $lang = PLL()->model->get_language( $lang );
271
272 if ( empty( $lang ) && ! empty( PLL()->curlang ) ) {
273 $lang = PLL()->curlang;
274 }
275
276 return empty( $lang ) ? $url : PLL()->links_model->switch_language_in_link( $url, $lang );
277 }
278
279 /**
280 * Translates a post type slug
281 *
282 * @since 2.2
283 *
284 * @param string $slug Post type slug
285 * @param string $post_type Post type name
286 * @param string $lang Optional language code (defaults to current language)
287 * @return string
288 */
289 public function wpml_get_translated_slug( $slug, $post_type, $lang = null ) {
290 if ( isset( PLL()->translate_slugs ) ) {
291 if ( empty( $lang ) ) {
292 $lang = pll_current_language();
293 }
294
295 $slug = PLL()->translate_slugs->slugs_model->get_translated_slug( $post_type, $lang );
296 }
297 return $slug;
298 }
299
300 /**
301 * Find out whether a post type or a taxonomy term is translated
302 *
303 * @since 2.0
304 *
305 * @param mixed $null
306 * @param int $id The post_id or term_id
307 * @param string $type The post type or taxonomy
308 * @return bool
309 */
310 public function wpml_element_has_translations( $null, $id, $type ) {
311 if ( 'post' === $type || pll_is_translated_post_type( $type ) ) {
312 return count( pll_get_post_translations( $id ) ) > 1;
313 } elseif ( 'term' === $type || pll_is_translated_taxonomy( $type ) ) {
314 return count( pll_get_term_translations( $id ) ) > 1;
315 }
316
317 return false;
318 }
319 }
320