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-legacy-api.php

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

328 lines 12.5 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 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, $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 'locale' => $lang->locale,
186 'text_direction' => $lang->is_rtl,
187 'display_name' => $lang->name, // Seems to be the post language name displayed in the current language, not a feature in Polylang
188 'native_name' => $lang->name,
189 'different_language' => $lang->slug != pll_current_language(),
190 );
191 }
192 }
193
194 /**
195 * Registers a string for translation in the "strings translation" panel
196 *
197 * @since 0.9.3
198 *
199 * @param string $context the group in which the string is registered, defaults to 'polylang'
200 * @param string $name a unique name for the string
201 * @param string $string the string to register
202 * @param bool $allow_empty_value not used
203 * @param string $source_lang not used by Polylang
204 */
205 if ( ! function_exists( 'icl_register_string' ) ) {
206 function icl_register_string( $context, $name, $string, $allow_empty_value = false, $source_lang = '' ) {
207 PLL_WPML_Compat::instance()->register_string( $context, $name, $string );
208 }
209 }
210
211 /**
212 * Removes a string from the "strings translation" panel
213 *
214 * @since 1.0.2
215 *
216 * @param string $context the group in which the string is registered, defaults to 'polylang'
217 * @param string $name a unique name for the string
218 */
219 if ( ! function_exists( 'icl_unregister_string' ) ) {
220 function icl_unregister_string( $context, $name ) {
221 PLL_WPML_Compat::instance()->unregister_string( $context, $name );
222 }
223 }
224
225 /**
226 * Gets the translated value of a string ( previously registered with icl_register_string or pll_register_string )
227 *
228 * @since 0.9.3
229 * @since 1.9.2 argument 3 is optional
230 * @since 2.0 add support for arguments 4 to 6
231 *
232 * @param string $context the group in which the string is registered
233 * @param string $name a unique name for the string
234 * @param string $string the string to translate, optional for strings registered with icl_register_string
235 * @param bool|null $has_translation optional, not supported in Polylang
236 * @param bool $bool optional, not used
237 * @param string|null $lang optional, return the translation in this language, defaults to current language
238 * @return string the translated string
239 */
240 if ( ! function_exists( 'icl_t' ) ) {
241 function icl_t( $context, $name, $string = false, &$has_translation = null, $bool = false, $lang = null ) {
242 return icl_translate( $context, $name, $string, false, $has_translation, $lang );
243 }
244 }
245
246 /**
247 * Undocumented function used by NextGen Gallery
248 * used in PLL_Plugins_Compat for Jetpack with only 3 arguments
249 *
250 * @since 1.0.2
251 * @since 2.0 add support for arguments 5 and 6, strings are no more automatically registered
252 *
253 * @param string $context the group in which the string is registered
254 * @param string $name a unique name for the string
255 * @param string $string the string to translate, optional for strings registered with icl_register_string
256 * @param bool $bool optional, not used
257 * @param bool|null $has_translation optional, not supported in Polylang
258 * @param string|null $lang optional, return the translation in this language, defaults to current language
259 * @return string the translated string
260 */
261 if ( ! function_exists( 'icl_translate' ) ) {
262 function icl_translate( $context, $name, $string = false, $bool = false, &$has_translation = null, $lang = null ) {
263 // FIXME WPML can automatically registers the string based on an option
264 if ( empty( $string ) ) {
265 $string = PLL_WPML_Compat::instance()->get_string_by_context_and_name( $context, $name );
266 }
267 return empty( $lang ) ? pll__( $string ) : pll_translate_string( $string, $lang );
268 }
269 }
270
271 /**
272 * Undocumented function used by Types
273 * FIXME: tested only with Types
274 * probably incomplete as Types locks the custom fields for a new post, but not when edited
275 * This is probably linked to the fact that WPML has always an original post in the default language and not Polylang :)
276 *
277 * @since 1.1.2
278 *
279 * @return array
280 */
281 if ( ! function_exists( 'wpml_get_copied_fields_for_post_edit' ) ) {
282 function wpml_get_copied_fields_for_post_edit() {
283 if ( empty( $_GET['from_post'] ) ) {
284 return array();
285 }
286
287 // Don't know what WPML does but Polylang does copy all public meta keys by default
288 foreach ( $keys = array_unique( array_keys( get_post_custom( (int) $_GET['from_post'] ) ) ) as $k => $meta_key ) {
289 if ( is_protected_meta( $meta_key ) ) {
290 unset( $keys[ $k ] );
291 }
292 }
293
294 // Apply our filter and fill the expected output ( see /types/embedded/includes/fields-post.php )
295 /** This filter is documented in modules/sync/admin-sync.php */
296 $arr['fields'] = array_unique( apply_filters( 'pll_copy_post_metas', empty( $keys ) ? array() : $keys, false ) );
297 $arr['original_post_id'] = (int) $_GET['from_post'];
298 return $arr;
299 }
300 }
301
302 /**
303 * Undocumented function used by Warp 6 by Yootheme
304 *
305 * @since 1.0.5
306 *
307 * @return string default language code
308 */
309 if ( ! function_exists( 'icl_get_default_language' ) ) {
310 function icl_get_default_language() {
311 return pll_default_language();
312 }
313 }
314
315 /**
316 * Undocumented function reported to be used by Table Rate Shipping for WooCommerce
317 * @see https://wordpress.org/support/topic/add-wpml-compatibility-function
318 *
319 * @since 1.8.2
320 *
321 * @return string default language code
322 */
323 if ( ! function_exists( 'wpml_get_default_language' ) ) {
324 function wpml_get_default_language() {
325 return pll_default_language();
326 }
327 }
328