PluginProbe
Polylang / 3.1
Polylang v3.1
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-legacy-api.php

wpml-legacy-api.php in Polylang 3.1, at modules/wpml/wpml-legacy-api.php

389 lines 14.8 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 * Compatibility with WPML legacy API
8 * Deprecated since WPML 3.2 and no more documented
9 * But a lot of 3rd party plugins / themes are still using these functions
10 */
11
12 if ( ! function_exists( 'icl_get_home_url' ) ) {
13 /**
14 * Link to the home page in the active language
15 *
16 * @since 0.9.4
17 *
18 * @return string
19 */
20 function icl_get_home_url() {
21 return pll_home_url();
22 }
23 }
24
25 if ( ! function_exists( 'icl_get_languages' ) ) {
26 /**
27 * Used for building custom language selectors
28 * available only on frontend
29 *
30 * List of paramaters accepted in $args:
31 *
32 * skip_missing => whether to skip missing translation or not, 0 or 1, defaults to 0
33 * orderby => 'id', 'code', 'name', defaults to 'id'
34 * order => 'ASC' or 'DESC', defaults to 'ASC'
35 * link_empty_to => link to use when the translation is missing {$lang} is replaced by the language code
36 *
37 * List of parameters returned per language:
38 *
39 * id => the language id
40 * active => whether this is the active language or no, 0 or 1
41 * native_name => the language name
42 * missing => whether the translation is missing or not, 0 or 1
43 * translated_name => empty, does not exist in Polylang
44 * language_code => the language code ( slug )
45 * country_flag_url => the url of the flag
46 * url => the url of the translation
47 *
48 * @since 1.0
49 *
50 * @param string|array $args optional
51 * @return array array of arrays per language
52 */
53 function icl_get_languages( $args = '' ) {
54 $args = wp_parse_args( $args, array( 'skip_missing' => 0, 'orderby' => 'id', 'order' => 'ASC' ) );
55 $orderby = ( isset( $args['orderby'] ) && 'code' == $args['orderby'] ) ? 'slug' : ( isset( $args['orderby'] ) && 'name' == $args['orderby'] ? 'name' : 'id' );
56 $order = ( ! empty( $args['order'] ) && 'desc' == $args['order'] ) ? 'DESC' : 'ASC';
57
58 $arr = array();
59
60 // NB: When 'skip_missing' is false, WPML returns all languages even if there is no content
61 $languages = PLL()->model->get_languages_list( array( 'hide_empty' => $args['skip_missing'] ) );
62 $languages = wp_list_sort( $languages, $orderby, $order ); // Since WP 4.7
63
64 foreach ( $languages as $lang ) {
65 // We can find a translation only on frontend once the global $wp_query object has been instantiated
66 if ( method_exists( PLL()->links, 'get_translation_url' ) && ! empty( $GLOBALS['wp_query'] ) ) {
67 $url = PLL()->links->get_translation_url( $lang );
68 }
69
70 // It seems that WPML does not bother of skip_missing parameter on admin side and before the $wp_query object has been filled
71 if ( empty( $url ) && ! empty( $args['skip_missing'] ) && ! is_admin() && did_action( 'parse_query' ) ) {
72 continue;
73 }
74
75 $arr[ $lang->slug ] = array(
76 'id' => $lang->term_id,
77 'active' => isset( PLL()->curlang->slug ) && PLL()->curlang->slug == $lang->slug ? 1 : 0,
78 'native_name' => $lang->name,
79 'missing' => empty( $url ) ? 1 : 0,
80 'translated_name' => '', // Does not exist in Polylang
81 'language_code' => $lang->slug,
82 'country_flag_url' => $lang->get_display_flag_url(),
83 'url' => ! empty( $url ) ? $url :
84 ( empty( $args['link_empty_to'] ) ? PLL()->links->get_home_url( $lang ) :
85 str_replace( '{$lang}', $lang->slug, $args['link_empty_to'] ) ),
86 );
87 }
88
89 // Apply undocumented WPML filter
90 $arr = apply_filters( 'icl_ls_languages', $arr );
91
92 return $arr;
93 }
94 }
95
96 if ( ! function_exists( 'icl_link_to_element' ) ) {
97 /**
98 * Used for creating language dependent links in themes
99 *
100 * @since 1.0
101 * @since 2.0 add support for arguments 6 and 7
102 *
103 * @param int $id object id
104 * @param string $type optional, post type or taxonomy name of the object, defaults to 'post'
105 * @param string $text optional, the link text. If not specified will produce the name of the element in the current language
106 * @param array $args optional, an array of arguments to add to the link, defaults to empty
107 * @param string $anchor optional, the anchor to add to the link, defaults to empty
108 * @param bool $echo optional, whether to echo the link, defaults to true
109 * @param bool $return_original_if_missing optional, whether to return a value if the translation is missing
110 * @return string a language dependent link
111 */
112 function icl_link_to_element( $id, $type = 'post', $text = '', $args = array(), $anchor = '', $echo = true, $return_original_if_missing = true ) {
113 if ( 'tag' == $type ) {
114 $type = 'post_tag';
115 }
116
117 $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
118 if ( $pll_type && ( $lang = pll_current_language() ) && ( $tr_id = PLL()->model->$pll_type->get_translation( $id, $lang ) ) && ( 'term' === $pll_type || PLL()->model->post->current_user_can_read( $tr_id ) ) ) {
119 $id = $tr_id;
120 } elseif ( ! $return_original_if_missing ) {
121 return '';
122 }
123
124 if ( post_type_exists( $type ) ) {
125 $link = get_permalink( $id );
126 if ( empty( $text ) ) {
127 $text = get_the_title( $id );
128 }
129 } elseif ( taxonomy_exists( $type ) ) {
130 $link = get_term_link( $id, $type );
131 if ( empty( $text ) && ( $term = get_term( $id, $type ) ) && $term instanceof WP_Term ) {
132 $text = $term->name;
133 }
134 }
135
136 if ( empty( $link ) || is_wp_error( $link ) ) {
137 return '';
138 }
139
140 if ( ! empty( $args ) ) {
141 $link .= ( false === strpos( $link, '?' ) ? '?' : '&' ) . http_build_query( $args );
142 }
143
144 if ( ! empty( $anchor ) ) {
145 $link .= '#' . $anchor;
146 }
147
148 $link = sprintf( '<a href="%s">%s</a>', esc_url( $link ), esc_html( $text ) );
149
150 if ( $echo ) {
151 echo $link; // phpcs:ignore WordPress.Security.EscapeOutput
152 }
153
154 return $link;
155 }
156 }
157
158 if ( ! function_exists( 'icl_object_id' ) ) {
159 /**
160 * Used for calculating the IDs of objects (usually categories) in the current language
161 *
162 * @since 0.9.5
163 *
164 * @param int $id Object id
165 * @param string $type Optional, post type or taxonomy name of the object, defaults to 'post'
166 * @param bool $return_original_if_missing Optional, true if Polylang should return the original id if the translation is missing, defaults to false
167 * @param string $lang Optional, language code, defaults to current language
168 * @return int|null The object id of the translation, null if the translation is missing and $return_original_if_missing set to false
169 */
170 function icl_object_id( $id, $type = 'post', $return_original_if_missing = false, $lang = '' ) {
171 $lang = $lang ? $lang : pll_current_language();
172
173 if ( 'nav_menu' === $type ) {
174 $theme = get_option( 'stylesheet' );
175 if ( isset( PLL()->options['nav_menus'][ $theme ] ) ) {
176 foreach ( PLL()->options['nav_menus'][ $theme ] as $menu ) {
177 if ( array_search( $id, $menu ) && ! empty( $menu[ $lang ] ) ) {
178 $tr_id = $menu[ $lang ];
179 break;
180 }
181 }
182 }
183 } elseif ( $pll_type = ( 'post' === $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' === $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ) ) {
184 $tr_id = PLL()->model->$pll_type->get_translation( $id, $lang );
185 }
186
187 return ! empty( $tr_id ) ? $tr_id : ( $return_original_if_missing ? $id : null );
188 }
189 }
190
191 if ( ! function_exists( 'wpml_object_id_filter' ) ) {
192 /**
193 * Undocumented alias of `icl_object_id` introduced in WPML 3.2, used by Yith WooCommerce compare
194 *
195 * @since 2.2.4
196 *
197 * @param int $id object id
198 * @param string $type optional, post type or taxonomy name of the object, defaults to 'post'
199 * @param bool $return_original_if_missing optional, true if Polylang should return the original id if the translation is missing, defaults to false
200 * @param string $lang optional, language code, defaults to current language
201 * @return int|null the object id of the translation, null if the translation is missing and $return_original_if_missing set to false
202 */
203 function wpml_object_id_filter( $id, $type = 'post', $return_original_if_missing = false, $lang = null ) {
204 return icl_object_id( $id, $type, $return_original_if_missing, $lang );
205 }
206 }
207
208 if ( ! function_exists( 'wpml_get_language_information' ) ) {
209 /**
210 * Undocumented function used by the theme Maya
211 * returns the post language
212 *
213 * @see https://wpml.org/forums/topic/canonical-urls-for-wpml-duplicated-posts/#post-52198 for the original WPML code
214 *
215 * @since 1.8
216 *
217 * @param null $empty optional, not used
218 * @param int $post_id optional, post id, defaults to current post
219 * @return array
220 */
221 function wpml_get_language_information( $empty = null, $post_id = null ) {
222 if ( empty( $post_id ) ) {
223 $post_id = get_the_ID();
224 }
225
226 // FIXME WPML may return a WP_Error object
227 return false === ( $lang = PLL()->model->post->get_language( $post_id ) ) ? array() : array(
228 'language_code' => $lang->slug,
229 'locale' => $lang->locale,
230 'text_direction' => (bool) $lang->is_rtl,
231 'display_name' => $lang->name, // Seems to be the post language name displayed in the current language, not a feature in Polylang
232 'native_name' => $lang->name,
233 'different_language' => pll_current_language() !== $lang->slug,
234 );
235 }
236 }
237
238 if ( ! function_exists( 'icl_register_string' ) ) {
239 /**
240 * Registers a string for translation in the "strings translation" panel
241 *
242 * The 4th and 5th parameters $allow_empty_value and $source_lang are not used by Polylang.
243 *
244 * @since 0.9.3
245 *
246 * @param string $context the group in which the string is registered, defaults to 'polylang'
247 * @param string $name a unique name for the string
248 * @param string $string the string to register
249 * @return void
250 */
251 function icl_register_string( $context, $name, $string ) {
252 PLL_WPML_Compat::instance()->register_string( $context, $name, $string );
253 }
254 }
255
256 if ( ! function_exists( 'icl_unregister_string' ) ) {
257 /**
258 * Removes a string from the "strings translation" panel
259 *
260 * @since 1.0.2
261 *
262 * @param string $context the group in which the string is registered, defaults to 'polylang'
263 * @param string $name a unique name for the string
264 * @return void
265 */
266 function icl_unregister_string( $context, $name ) {
267 PLL_WPML_Compat::instance()->unregister_string( $context, $name );
268 }
269 }
270
271 if ( ! function_exists( 'icl_t' ) ) {
272 /**
273 * Gets the translated value of a string ( previously registered with icl_register_string or pll_register_string )
274 *
275 * @since 0.9.3
276 * @since 1.9.2 argument 3 is optional
277 * @since 2.0 add support for arguments 4 to 6
278 *
279 * @param string $context the group in which the string is registered
280 * @param string $name a unique name for the string
281 * @param string $string the string to translate, optional for strings registered with icl_register_string
282 * @param bool|null $has_translation optional, not supported in Polylang
283 * @param bool $bool optional, not used
284 * @param string|null $lang optional, return the translation in this language, defaults to current language
285 * @return string the translated string
286 */
287 function icl_t( $context, $name, $string = '', &$has_translation = null, $bool = false, $lang = null ) {
288 return icl_translate( $context, $name, $string, false, $has_translation, $lang );
289 }
290 }
291
292 if ( ! function_exists( 'icl_translate' ) ) {
293 /**
294 * Undocumented function used by NextGen Gallery
295 * used in PLL_Plugins_Compat for Jetpack with only 3 arguments
296 *
297 * @since 1.0.2
298 * @since 2.0 add support for arguments 5 and 6, strings are no more automatically registered
299 *
300 * @param string $context the group in which the string is registered
301 * @param string $name a unique name for the string
302 * @param string $string the string to translate, optional for strings registered with icl_register_string
303 * @param bool $bool optional, not used
304 * @param bool|null $has_translation optional, not supported in Polylang
305 * @param string|null $lang optional, return the translation in this language, defaults to current language
306 * @return string the translated string
307 */
308 function icl_translate( $context, $name, $string = '', $bool = false, &$has_translation = null, $lang = null ) {
309 // FIXME WPML can automatically registers the string based on an option
310 if ( empty( $string ) ) {
311 $string = PLL_WPML_Compat::instance()->get_string_by_context_and_name( $context, $name );
312 }
313 return empty( $lang ) ? pll__( $string ) : pll_translate_string( $string, $lang );
314 }
315 }
316
317 if ( ! function_exists( 'wpml_get_copied_fields_for_post_edit' ) ) {
318 /**
319 * Undocumented function used by Types
320 * FIXME: tested only with Types
321 * probably incomplete as Types locks the custom fields for a new post, but not when edited
322 * This is probably linked to the fact that WPML has always an original post in the default language and not Polylang :)
323 *
324 * @since 1.1.2
325 *
326 * @return array
327 */
328 function wpml_get_copied_fields_for_post_edit() {
329 if ( empty( $_GET['from_post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
330 return array();
331 }
332
333 $arr = array( 'original_post_id' => (int) $_GET['from_post'] ); // phpcs:ignore WordPress.Security.NonceVerification
334
335 // Don't know what WPML does but Polylang does copy all public meta keys by default
336 foreach ( $keys = array_unique( array_keys( get_post_custom( $arr['original_post_id'] ) ) ) as $k => $meta_key ) {
337 if ( is_protected_meta( $meta_key ) ) {
338 unset( $keys[ $k ] );
339 }
340 }
341
342 // Apply our filter and fill the expected output ( see /types/embedded/includes/fields-post.php )
343 /** This filter is documented in modules/sync/admin-sync.php */
344 $arr['fields'] = array_unique( apply_filters( 'pll_copy_post_metas', empty( $keys ) ? array() : $keys, false ) );
345 return $arr;
346 }
347 }
348
349 if ( ! function_exists( 'icl_get_default_language' ) ) {
350 /**
351 * Undocumented function used by Warp 6 by Yootheme
352 *
353 * @since 1.0.5
354 *
355 * @return string default language code
356 */
357 function icl_get_default_language() {
358 return pll_default_language();
359 }
360 }
361
362 if ( ! function_exists( 'wpml_get_default_language' ) ) {
363 /**
364 * Undocumented function reported to be used by Table Rate Shipping for WooCommerce
365 *
366 * @see https://wordpress.org/support/topic/add-wpml-compatibility-function
367 *
368 * @since 1.8.2
369 *
370 * @return string default language code
371 */
372 function wpml_get_default_language() {
373 return pll_default_language();
374 }
375 }
376
377 if ( ! function_exists( 'icl_get_current_language' ) ) {
378 /**
379 * Undocumented function used by Ultimate Member
380 *
381 * @since 2.2.4
382 *
383 * @return string Current language code
384 */
385 function icl_get_current_language() {
386 return pll_current_language();
387 }
388 }
389