PluginProbe
Polylang / 2.1
Polylang v2.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 2.1, at modules/wpml/wpml-legacy-api.php

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