PluginProbe
Polylang / 2.3
Polylang v2.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-legacy-api.php

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

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