| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | /** |
| 27 | 27 | * Used for building custom language selectors |
| 28 | 28 | * available only on frontend |
| 29 | 29 | * |
| 30 | - * List of paramaters accepted in $args: | |
| 30 | + * List of parameters accepted in $args: | |
| 31 | 31 | * |
| 32 | 32 | * skip_missing => whether to skip missing translation or not, 0 or 1, defaults to 0 |
| 33 | 33 | * orderby => 'id', 'code', 'name', defaults to 'id' |
| 34 | 34 | * order => 'ASC' or 'DESC', defaults to 'ASC' |
| @@ -62,9 +62,9 @@ | ||
| 62 | 62 | $languages = wp_list_sort( $languages, $orderby, $order ); // Since WP 4.7 |
| 63 | 63 | |
| 64 | 64 | foreach ( $languages as $lang ) { |
| 65 | 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'] ) ) { | |
| 66 | + if ( PLL()->links instanceof PLL_Frontend_Links && ! empty( $GLOBALS['wp_query'] ) ) { | |
| 67 | 67 | $url = PLL()->links->get_translation_url( $lang ); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // It seems that WPML does not bother of skip_missing parameter on admin side and before the $wp_query object has been filled |
| @@ -80,9 +80,9 @@ | ||
| 80 | 80 | 'translated_name' => '', // Does not exist in Polylang |
| 81 | 81 | 'language_code' => $lang->slug, |
| 82 | 82 | 'country_flag_url' => $lang->get_display_flag_url(), |
| 83 | 83 | 'url' => ! empty( $url ) ? $url : |
| 84 | - ( empty( $args['link_empty_to'] ) ? PLL()->links->get_home_url( $lang ) : | |
| 84 | + ( empty( $args['link_empty_to'] ) ? $lang->get_home_url() : | |
| 85 | 85 | str_replace( '{$lang}', $lang->slug, $args['link_empty_to'] ) ), |
| 86 | 86 | ); |
| 87 | 87 | } |
| 88 | 88 | |
| @@ -127,9 +127,9 @@ | ||
| 127 | 127 | $text = get_the_title( $id ); |
| 128 | 128 | } |
| 129 | 129 | } elseif ( taxonomy_exists( $type ) ) { |
| 130 | 130 | $link = get_term_link( $id, $type ); |
| 131 | - if ( empty( $text ) && ( $term = get_term( $id, $type ) ) && ! empty( $term ) && ! is_wp_error( $term ) ) { | |
| 131 | + if ( empty( $text ) && ( $term = get_term( $id, $type ) ) && $term instanceof WP_Term ) { | |
| 132 | 132 | $text = $term->name; |
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
| @@ -156,36 +156,69 @@ | ||
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | if ( ! function_exists( 'icl_object_id' ) ) { |
| 159 | 159 | /** |
| 160 | - * Used for calculating the IDs of objects (usually categories) in the current language | |
| 160 | + * Returns an element’s ID in the current language or in another specified language. | |
| 161 | 161 | * |
| 162 | 162 | * @since 0.9.5 |
| 163 | 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 | |
| 164 | + * @param int $element_id Object id. | |
| 165 | + * @param string $element_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|null $ulanguage_code Optional, language code, defaults to the 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 | 169 | */ |
| 170 | - function icl_object_id( $id, $type = 'post', $return_original_if_missing = false, $lang = false ) { | |
| 171 | - $lang = $lang ? $lang : pll_current_language(); | |
| 170 | + function icl_object_id( $element_id, $element_type = 'post', $return_original_if_missing = false, $ulanguage_code = null ) { | |
| 171 | + if ( empty( $element_id ) ) { | |
| 172 | + return null; | |
| 173 | + } | |
| 172 | 174 | |
| 173 | - if ( 'nav_menu' === $type ) { | |
| 175 | + $element_id = (int) $element_id; | |
| 176 | + | |
| 177 | + if ( 'any' === $element_type ) { | |
| 178 | + $element_type = get_post_type( $element_id ); | |
| 179 | + } | |
| 180 | + | |
| 181 | + if ( empty( $element_type ) ) { | |
| 182 | + return null; | |
| 183 | + } | |
| 184 | + | |
| 185 | + if ( empty( $ulanguage_code ) ) { | |
| 186 | + $ulanguage_code = pll_current_language(); | |
| 187 | + } | |
| 188 | + | |
| 189 | + if ( empty( $ulanguage_code ) ) { | |
| 190 | + return $return_original_if_missing ? $element_id : null; | |
| 191 | + } | |
| 192 | + | |
| 193 | + if ( 'nav_menu' === $element_type ) { | |
| 194 | + $tr_id = false; | |
| 174 | 195 | $theme = get_option( 'stylesheet' ); |
| 175 | 196 | if ( isset( PLL()->options['nav_menus'][ $theme ] ) ) { |
| 176 | 197 | foreach ( PLL()->options['nav_menus'][ $theme ] as $menu ) { |
| 177 | - if ( array_search( $id, $menu ) && ! empty( $menu[ $lang ] ) ) { | |
| 178 | - $tr_id = $menu[ $lang ]; | |
| 198 | + if ( array_search( $element_id, $menu ) && ! empty( $menu[ $ulanguage_code ] ) ) { | |
| 199 | + $tr_id = $menu[ $ulanguage_code ]; | |
| 179 | 200 | break; |
| 180 | 201 | } |
| 181 | 202 | } |
| 182 | 203 | } |
| 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 ); | |
| 204 | + } elseif ( pll_is_translated_post_type( $element_type ) ) { | |
| 205 | + $tr_id = PLL()->model->post->get_translation( $element_id, $ulanguage_code ); | |
| 206 | + } elseif ( pll_is_translated_taxonomy( $element_type ) ) { | |
| 207 | + $tr_id = PLL()->model->term->get_translation( $element_id, $ulanguage_code ); | |
| 208 | + } else { | |
| 209 | + return $element_id; // WPML doesn't honor $return_original_if_missing if the post type or taxonomy is not translated, @see {SitePress::get_object_id()}. | |
| 185 | 210 | } |
| 186 | 211 | |
| 187 | - return ! empty( $tr_id ) ? $tr_id : ( $return_original_if_missing ? $id : null ); | |
| 212 | + if ( empty( $tr_id ) ) { | |
| 213 | + if ( $return_original_if_missing ) { | |
| 214 | + return $element_id; | |
| 215 | + } | |
| 216 | + | |
| 217 | + return null; | |
| 218 | + } | |
| 219 | + | |
| 220 | + return (int) $tr_id; | |
| 188 | 221 | } |
| 189 | 222 | } |
| 190 | 223 | |
| 191 | 224 | if ( ! function_exists( 'wpml_object_id_filter' ) ) { |
| @@ -215,17 +248,28 @@ | ||
| 215 | 248 | * @since 1.8 |
| 216 | 249 | * |
| 217 | 250 | * @param null $empty optional, not used |
| 218 | 251 | * @param int $post_id optional, post id, defaults to current post |
| 219 | - * @return array | |
| 252 | + * @return array|WP_Error | |
| 220 | 253 | */ |
| 221 | 254 | function wpml_get_language_information( $empty = null, $post_id = null ) { |
| 255 | + if ( null === $post_id ) { | |
| 256 | + $post_id = get_the_ID(); | |
| 257 | + } | |
| 222 | 258 | if ( empty( $post_id ) ) { |
| 223 | - $post_id = get_the_ID(); | |
| 259 | + return new WP_Error( 'missing_id', __( 'Missing post ID', 'polylang' ) ); | |
| 224 | 260 | } |
| 225 | 261 | |
| 262 | + $post = get_post( $post_id ); | |
| 263 | + if ( empty( $post ) ) { | |
| 264 | + // translators: Post id. | |
| 265 | + return new WP_Error( 'missing_post', sprintf( __( 'No such post for ID = %d', 'polylang' ), $post_id ) ); | |
| 266 | + } | |
| 267 | + | |
| 268 | + $lang = PLL()->model->post->get_language( $post_id ); | |
| 269 | + | |
| 226 | 270 | // FIXME WPML may return a WP_Error object |
| 227 | - return false === ( $lang = PLL()->model->post->get_language( $post_id ) ) ? array() : array( | |
| 271 | + return false === $lang ? array() : array( | |
| 228 | 272 | 'language_code' => $lang->slug, |
| 229 | 273 | 'locale' => $lang->locale, |
| 230 | 274 | 'text_direction' => (bool) $lang->is_rtl, |
| 231 | 275 | 'display_name' => $lang->name, // Seems to be the post language name displayed in the current language, not a feature in Polylang |
| @@ -245,8 +289,9 @@ | ||
| 245 | 289 | * |
| 246 | 290 | * @param string $context the group in which the string is registered, defaults to 'polylang' |
| 247 | 291 | * @param string $name a unique name for the string |
| 248 | 292 | * @param string $string the string to register |
| 293 | + * @return void | |
| 249 | 294 | */ |
| 250 | 295 | function icl_register_string( $context, $name, $string ) { |
| 251 | 296 | PLL_WPML_Compat::instance()->register_string( $context, $name, $string ); |
| 252 | 297 | } |
| @@ -259,8 +304,9 @@ | ||
| 259 | 304 | * @since 1.0.2 |
| 260 | 305 | * |
| 261 | 306 | * @param string $context the group in which the string is registered, defaults to 'polylang' |
| 262 | 307 | * @param string $name a unique name for the string |
| 308 | + * @return void | |
| 263 | 309 | */ |
| 264 | 310 | function icl_unregister_string( $context, $name ) { |
| 265 | 311 | PLL_WPML_Compat::instance()->unregister_string( $context, $name ); |
| 266 | 312 | } |
| @@ -281,9 +327,9 @@ | ||
| 281 | 327 | * @param bool $bool optional, not used |
| 282 | 328 | * @param string|null $lang optional, return the translation in this language, defaults to current language |
| 283 | 329 | * @return string the translated string |
| 284 | 330 | */ |
| 285 | - function icl_t( $context, $name, $string = false, &$has_translation = null, $bool = false, $lang = null ) { | |
| 331 | + function icl_t( $context, $name, $string = '', &$has_translation = null, $bool = false, $lang = null ) { | |
| 286 | 332 | return icl_translate( $context, $name, $string, false, $has_translation, $lang ); |
| 287 | 333 | } |
| 288 | 334 | } |
| 289 | 335 | |
| @@ -302,9 +348,9 @@ | ||
| 302 | 348 | * @param bool|null $has_translation optional, not supported in Polylang |
| 303 | 349 | * @param string|null $lang optional, return the translation in this language, defaults to current language |
| 304 | 350 | * @return string the translated string |
| 305 | 351 | */ |
| 306 | - function icl_translate( $context, $name, $string = false, $bool = false, &$has_translation = null, $lang = null ) { | |
| 352 | + function icl_translate( $context, $name, $string = '', $bool = false, &$has_translation = null, $lang = null ) { | |
| 307 | 353 | // FIXME WPML can automatically registers the string based on an option |
| 308 | 354 | if ( empty( $string ) ) { |
| 309 | 355 | $string = PLL_WPML_Compat::instance()->get_string_by_context_and_name( $context, $name ); |
| 310 | 356 | } |
| @@ -329,12 +375,15 @@ | ||
| 329 | 375 | } |
| 330 | 376 | |
| 331 | 377 | $arr = array( 'original_post_id' => (int) $_GET['from_post'] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 332 | 378 | |
| 333 | - // Don't know what WPML does but Polylang does copy all public meta keys by default | |
| 334 | - foreach ( $keys = array_unique( array_keys( get_post_custom( $arr['original_post_id'] ) ) ) as $k => $meta_key ) { | |
| 335 | - if ( is_protected_meta( $meta_key ) ) { | |
| 336 | - unset( $keys[ $k ] ); | |
| 379 | + // Don't know what WPML does but Polylang does copy all public meta keys by default. | |
| 380 | + $keys = get_post_custom_keys( $arr['original_post_id'] ); | |
| 381 | + if ( is_array( $keys ) ) { | |
| 382 | + foreach ( $keys as $k => $meta_key ) { | |
| 383 | + if ( is_protected_meta( $meta_key ) ) { | |
| 384 | + unset( $keys[ $k ] ); | |
| 385 | + } | |
| 337 | 386 | } |
| 338 | 387 | } |
| 339 | 388 | |
| 340 | 389 | // Apply our filter and fill the expected output ( see /types/embedded/includes/fields-post.php ) |
| @@ -349,9 +398,9 @@ | ||
| 349 | 398 | * Undocumented function used by Warp 6 by Yootheme |
| 350 | 399 | * |
| 351 | 400 | * @since 1.0.5 |
| 352 | 401 | * |
| 353 | - * @return string default language code | |
| 402 | + * @return string|false default language code | |
| 354 | 403 | */ |
| 355 | 404 | function icl_get_default_language() { |
| 356 | 405 | return pll_default_language(); |
| 357 | 406 | } |
| @@ -364,9 +413,9 @@ | ||
| 364 | 413 | * @see https://wordpress.org/support/topic/add-wpml-compatibility-function |
| 365 | 414 | * |
| 366 | 415 | * @since 1.8.2 |
| 367 | 416 | * |
| 368 | - * @return string default language code | |
| 417 | + * @return string|false default language code | |
| 369 | 418 | */ |
| 370 | 419 | function wpml_get_default_language() { |
| 371 | 420 | return pll_default_language(); |
| 372 | 421 | } |
| @@ -380,7 +429,7 @@ | ||
| 380 | 429 | * |
| 381 | 430 | * @return string Current language code |
| 382 | 431 | */ |
| 383 | 432 | function icl_get_current_language() { |
| 384 | - return pll_current_language(); | |
| 433 | + return (string) pll_current_language(); | |
| 385 | 434 | } |
| 386 | 435 | } |