| 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 once the global $wp_query object has been instantiated |
| 67 |
if ( method_exists( PLL()->links, 'get_translation_url' ) && ! empty( $GLOBALS['wp_query'] ) ) { |
| 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 ) ) && ( 'term' === $pll_type || PLL()->model->post->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; // phpcs:ignore WordPress.Security.EscapeOutput |
| 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 |
* |
| 214 |
* @see original WPML code at https://wpml.org/forums/topic/canonical-urls-for-wpml-duplicated-posts/#post-52198 |
| 215 |
* |
| 216 |
* @since 1.8 |
| 217 |
* |
| 218 |
* @param null $empty optional, not used |
| 219 |
* @param int $post_id optional, post id, defaults to current post |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
function wpml_get_language_information( $empty = null, $post_id = null ) { |
| 223 |
if ( empty( $post_id ) ) { |
| 224 |
$post_id = get_the_ID(); |
| 225 |
} |
| 226 |
|
| 227 |
// FIXME WPML may return a WP_Error object |
| 228 |
return false === ( $lang = PLL()->model->post->get_language( $post_id ) ) ? array() : array( |
| 229 |
'language_code' => $lang->slug, |
| 230 |
'locale' => $lang->locale, |
| 231 |
'text_direction' => (bool) $lang->is_rtl, |
| 232 |
'display_name' => $lang->name, // Seems to be the post language name displayed in the current language, not a feature in Polylang |
| 233 |
'native_name' => $lang->name, |
| 234 |
'different_language' => pll_current_language() !== $lang->slug, |
| 235 |
); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if ( ! function_exists( 'icl_register_string' ) ) { |
| 240 |
/** |
| 241 |
* Registers a string for translation in the "strings translation" panel |
| 242 |
* |
| 243 |
* @since 0.9.3 |
| 244 |
* |
| 245 |
* @param string $context the group in which the string is registered, defaults to 'polylang' |
| 246 |
* @param string $name a unique name for the string |
| 247 |
* @param string $string the string to register |
| 248 |
* @param bool $allow_empty_value not used |
| 249 |
* @param string $source_lang not used by Polylang |
| 250 |
*/ |
| 251 |
function icl_register_string( $context, $name, $string, $allow_empty_value = false, $source_lang = '' ) { |
| 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 |
*/ |
| 265 |
function icl_unregister_string( $context, $name ) { |
| 266 |
PLL_WPML_Compat::instance()->unregister_string( $context, $name ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if ( ! function_exists( 'icl_t' ) ) { |
| 271 |
/** |
| 272 |
* Gets the translated value of a string ( previously registered with icl_register_string or pll_register_string ) |
| 273 |
* |
| 274 |
* @since 0.9.3 |
| 275 |
* @since 1.9.2 argument 3 is optional |
| 276 |
* @since 2.0 add support for arguments 4 to 6 |
| 277 |
* |
| 278 |
* @param string $context the group in which the string is registered |
| 279 |
* @param string $name a unique name for the string |
| 280 |
* @param string $string the string to translate, optional for strings registered with icl_register_string |
| 281 |
* @param bool|null $has_translation optional, not supported in Polylang |
| 282 |
* @param bool $bool optional, not used |
| 283 |
* @param string|null $lang optional, return the translation in this language, defaults to current language |
| 284 |
* @return string the translated string |
| 285 |
*/ |
| 286 |
function icl_t( $context, $name, $string = false, &$has_translation = null, $bool = false, $lang = null ) { |
| 287 |
return icl_translate( $context, $name, $string, false, $has_translation, $lang ); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! function_exists( 'icl_translate' ) ) { |
| 292 |
/** |
| 293 |
* Undocumented function used by NextGen Gallery |
| 294 |
* used in PLL_Plugins_Compat for Jetpack with only 3 arguments |
| 295 |
* |
| 296 |
* @since 1.0.2 |
| 297 |
* @since 2.0 add support for arguments 5 and 6, strings are no more automatically registered |
| 298 |
* |
| 299 |
* @param string $context the group in which the string is registered |
| 300 |
* @param string $name a unique name for the string |
| 301 |
* @param string $string the string to translate, optional for strings registered with icl_register_string |
| 302 |
* @param bool $bool optional, not used |
| 303 |
* @param bool|null $has_translation optional, not supported in Polylang |
| 304 |
* @param string|null $lang optional, return the translation in this language, defaults to current language |
| 305 |
* @return string the translated string |
| 306 |
*/ |
| 307 |
function icl_translate( $context, $name, $string = false, $bool = false, &$has_translation = null, $lang = null ) { |
| 308 |
// FIXME WPML can automatically registers the string based on an option |
| 309 |
if ( empty( $string ) ) { |
| 310 |
$string = PLL_WPML_Compat::instance()->get_string_by_context_and_name( $context, $name ); |
| 311 |
} |
| 312 |
return empty( $lang ) ? pll__( $string ) : pll_translate_string( $string, $lang ); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if ( ! function_exists( 'wpml_get_copied_fields_for_post_edit' ) ) { |
| 317 |
/** |
| 318 |
* Undocumented function used by Types |
| 319 |
* FIXME: tested only with Types |
| 320 |
* probably incomplete as Types locks the custom fields for a new post, but not when edited |
| 321 |
* This is probably linked to the fact that WPML has always an original post in the default language and not Polylang :) |
| 322 |
* |
| 323 |
* @since 1.1.2 |
| 324 |
* |
| 325 |
* @return array |
| 326 |
*/ |
| 327 |
function wpml_get_copied_fields_for_post_edit() { |
| 328 |
if ( empty( $_GET['from_post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 329 |
return array(); |
| 330 |
} |
| 331 |
|
| 332 |
$arr['original_post_id'] = (int) $_GET['from_post']; // phpcs:ignore WordPress.Security.NonceVerification |
| 333 |
|
| 334 |
// Don't know what WPML does but Polylang does copy all public meta keys by default |
| 335 |
foreach ( $keys = array_unique( array_keys( get_post_custom( $arr['original_post_id'] ) ) ) as $k => $meta_key ) { |
| 336 |
if ( is_protected_meta( $meta_key ) ) { |
| 337 |
unset( $keys[ $k ] ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// Apply our filter and fill the expected output ( see /types/embedded/includes/fields-post.php ) |
| 342 |
/** This filter is documented in modules/sync/admin-sync.php */ |
| 343 |
$arr['fields'] = array_unique( apply_filters( 'pll_copy_post_metas', empty( $keys ) ? array() : $keys, false ) ); |
| 344 |
return $arr; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if ( ! function_exists( 'icl_get_default_language' ) ) { |
| 349 |
/** |
| 350 |
* Undocumented function used by Warp 6 by Yootheme |
| 351 |
* |
| 352 |
* @since 1.0.5 |
| 353 |
* |
| 354 |
* @return string default language code |
| 355 |
*/ |
| 356 |
function icl_get_default_language() { |
| 357 |
return pll_default_language(); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
if ( ! function_exists( 'wpml_get_default_language' ) ) { |
| 362 |
/** |
| 363 |
* Undocumented function reported to be used by Table Rate Shipping for WooCommerce |
| 364 |
* |
| 365 |
* @see https://wordpress.org/support/topic/add-wpml-compatibility-function |
| 366 |
* |
| 367 |
* @since 1.8.2 |
| 368 |
* |
| 369 |
* @return string default language code |
| 370 |
*/ |
| 371 |
function wpml_get_default_language() { |
| 372 |
return pll_default_language(); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if ( ! function_exists( 'icl_get_current_language' ) ) { |
| 377 |
/** |
| 378 |
* Undocumented function used by Ultimate Member |
| 379 |
* |
| 380 |
* @since 2.2.4 |
| 381 |
* |
| 382 |
* @return string Current language code |
| 383 |
*/ |
| 384 |
function icl_get_current_language() { |
| 385 |
return pll_current_language(); |
| 386 |
} |
| 387 |
} |
| 388 |
|