| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A class to handle the WPML API based on hooks, introduced since WPML 3.2 |
| 5 |
* It partly relies on the legacy API |
| 6 |
* |
| 7 |
* @see https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/ |
| 8 |
* |
| 9 |
* @since 2.0 |
| 10 |
*/ |
| 11 |
class PLL_WPML_API { |
| 12 |
private static $original_language = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* |
| 17 |
* @since 2.0 |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
// Site Wide Language informations |
| 21 |
|
| 22 |
add_filter( 'wpml_active_languages', array( $this, 'wpml_active_languages' ), 10, 2 ); |
| 23 |
add_filter( 'wpml_display_language_names', array( $this, 'wpml_display_language_names' ), 10, 5 ); |
| 24 |
// wpml_translated_language_name => not applicable |
| 25 |
add_filter( 'wpml_current_language', 'pll_current_language', 10, 0 ); |
| 26 |
add_filter( 'wpml_default_language', 'pll_default_language', 10, 0 ); |
| 27 |
// wpml_add_language_selector => not implemented |
| 28 |
// wpml_footer_language_selector => not applicable |
| 29 |
add_action( 'wpml_add_language_form_field', array( $this, 'wpml_add_language_form_field' ) ); |
| 30 |
add_filter( 'wpml_language_is_active', array( $this, 'wpml_language_is_active' ), 10, 2 ); |
| 31 |
add_filter( 'wpml_is_rtl', array( $this, 'wpml_is_rtl' ) ); |
| 32 |
// wpml_language_form_input_field => See wpml_add_language_form_field |
| 33 |
// wpml_language_has_switched => See wpml_switch_language |
| 34 |
// wpml_element_trid => not implemented |
| 35 |
// wpml_get_element_translations => not implemented |
| 36 |
// wpml_language_switcher => not implemented |
| 37 |
// wpml_browser_redirect_language_params => not implemented |
| 38 |
// wpml_enqueue_browser_redirect_language => not applicable |
| 39 |
// wpml_enqueued_browser_redirect_language => not applicable |
| 40 |
// wpml_encode_string => not applicable |
| 41 |
// wpml_decode_string => not applicable |
| 42 |
|
| 43 |
// Retrieving Language Information for Content |
| 44 |
|
| 45 |
add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 ); |
| 46 |
add_action( 'wpml_switch_language', array( __CLASS__, 'wpml_switch_language' ), 10, 2 ); |
| 47 |
add_filter( 'wpml_element_language_code', array( $this, 'wpml_element_language_code' ), 10, 3 ); |
| 48 |
// wpml_element_language_details => not applicable |
| 49 |
|
| 50 |
// Retrieving Localized Content |
| 51 |
|
| 52 |
add_filter( 'wpml_home_url', 'pll_home_url', 10, 0 ); |
| 53 |
add_filter( 'wpml_element_link', 'icl_link_to_element', 10, 7 ); |
| 54 |
add_filter( 'wpml_object_id', 'icl_object_id', 10, 4 ); |
| 55 |
add_filter( 'wpml_translate_single_string', array( $this, 'wpml_translate_single_string' ), 10, 4 ); |
| 56 |
// wpml_translate_string => not applicable |
| 57 |
// wpml_unfiltered_admin_string => not implemented |
| 58 |
add_filter( 'wpml_permalink', array( $this, 'wpml_permalink' ), 10, 2 ); |
| 59 |
// wpml_elements_without_translations => not implemented |
| 60 |
add_filter( 'wpml_get_translated_slug', array( $this, 'wpml_get_translated_slug' ), 10, 3 ); |
| 61 |
|
| 62 |
// Finding the Translation State of Content |
| 63 |
|
| 64 |
// wpml_element_translation_type => not implemented |
| 65 |
add_filter( 'wpml_element_has_translations', array( $this, 'wpml_element_has_translations' ), 10, 3 ); |
| 66 |
// wpml_master_post_from_duplicate => not applicable |
| 67 |
// wpml_post_duplicates => not applicable |
| 68 |
|
| 69 |
// Inserting Content |
| 70 |
|
| 71 |
// wpml_admin_make_post_duplicates => not applicable |
| 72 |
// wpml_make_post_duplicates => not applicable |
| 73 |
add_action( 'wpml_register_single_string', 'icl_register_string', 10, 3 ); |
| 74 |
// wpml_register_string => not applicable |
| 75 |
// wpml_register_string_packages => not applicable |
| 76 |
// wpml_delete_package_action => not applicable |
| 77 |
// wpml_show_package_language_ui => not applicable |
| 78 |
// wpml_set_element_language_details => not implemented |
| 79 |
// wpml_multilingual_options => not applicable |
| 80 |
|
| 81 |
// Miscellaneous |
| 82 |
|
| 83 |
// wpml_element_type => not applicable |
| 84 |
// wpml_setting => not applicable |
| 85 |
// wpml_sub_setting => not applicable |
| 86 |
// wpml_editor_cf_to_display => not applicable |
| 87 |
// wpml_tm_save_translation_cf => not implemented |
| 88 |
// wpml_tm_xliff_export_translated_cf => not applicable |
| 89 |
// wpml_tm_xliff_export_original_cf => not applicable |
| 90 |
// wpml_duplicate_generic_string => not applicable |
| 91 |
// wpml_translatable_user_meta_fields => not implemented |
| 92 |
// wpml_cross_domain_language_data => not applicable |
| 93 |
// wpml_get_cross_domain_language_data => not applicable |
| 94 |
// wpml_loaded => not applicable |
| 95 |
// wpml_st_loaded => not applicable |
| 96 |
// wpml_tm_loaded => not applicable |
| 97 |
// wpml_hide_management_column (3.4.1) => not applicable |
| 98 |
// wpml_ls_directories_to_scan => not applicable |
| 99 |
// wpml_ls_model_css_classes => not applicable |
| 100 |
// wpml_ls_model_language_css_classes => not applicable |
| 101 |
// wpml_tf_feedback_open_link => not applicable |
| 102 |
// wpml_sync_custom_field => not implemented |
| 103 |
// wpml_sync_all_custom_fields => not implemented |
| 104 |
// wpml_is_redirected => not implemented |
| 105 |
|
| 106 |
// Updating Content |
| 107 |
|
| 108 |
// wpml_set_translation_mode_for_post_type => not implemented |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get a list of the languages enabled for a site |
| 113 |
* |
| 114 |
* @since 2.0 |
| 115 |
* |
| 116 |
* @param mixed $null Not used |
| 117 |
* @param array| string $args See arguments of icl_get_languages() |
| 118 |
* @return array Array of arrays per language |
| 119 |
*/ |
| 120 |
public function wpml_active_languages( $null, $args = '' ) { |
| 121 |
return icl_get_languages( $args ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* In WPML, get a language's native and translated name for display in a custom language switcher |
| 126 |
* Since Polylang does not implement the translated name, always returns only the native name |
| 127 |
* |
| 128 |
* @since 2.2 |
| 129 |
* |
| 130 |
* @param mixed $null Not used. |
| 131 |
* @param string $native_name The language native name. |
| 132 |
* @param string|bool $translated_name The language translated name. Not used. |
| 133 |
* @param bool $native_hidden Whether to hide the language native name or not. Not used. |
| 134 |
* @param bool $translated_hidden Whether to hide the language translated name or not. Not used. |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function wpml_display_language_names( $null, $native_name, $translated_name = false, $native_hidden = false, $translated_hidden = false ) { |
| 138 |
return $native_name; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns an HTML hidden input field with name=”lang” and as value the current language |
| 143 |
* |
| 144 |
* @since 2.0 |
| 145 |
*/ |
| 146 |
public function wpml_add_language_form_field() { |
| 147 |
$lang = pll_current_language(); |
| 148 |
$field = sprintf( '<input type="hidden" name="lang" value="%s" />', esc_attr( $lang ) ); |
| 149 |
$field = apply_filters( 'wpml_language_form_input_field', $field, $lang ); |
| 150 |
echo $field; // phpcs:ignore WordPress.Security.EscapeOutput |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Find out if a specific language is enabled for the site |
| 155 |
* |
| 156 |
* @since 2.0 |
| 157 |
* |
| 158 |
* @param mixed $null Not used |
| 159 |
* @param string $slug Language code |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function wpml_language_is_active( $null, $slug ) { |
| 163 |
$language = PLL()->model->get_language( $slug ); |
| 164 |
return empty( $language->active ) || true === $language->active; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Find out whether the current language text direction is RTL or not |
| 169 |
* |
| 170 |
* @since 2.0 |
| 171 |
* |
| 172 |
* @param mixed $null Not used |
| 173 |
* @return bool |
| 174 |
*/ |
| 175 |
public function wpml_is_rtl( $null ) { |
| 176 |
return pll_current_language( 'is_rtl' ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Switches whole site to the given language or restores the language that was set when first calling this function. |
| 181 |
* Unlike the WPML original action, it is not possible to set the current language and the cookie to different values. |
| 182 |
* |
| 183 |
* @since 2.7 |
| 184 |
* |
| 185 |
* @param null|string $lang Language code to switch into, restores the original language if null. |
| 186 |
* @param bool|string $cookie Optionally also switches the cookie. |
| 187 |
*/ |
| 188 |
public static function wpml_switch_language( $lang = null, $cookie = false ) { |
| 189 |
if ( null === self::$original_language ) { |
| 190 |
self::$original_language = PLL()->curlang; |
| 191 |
} |
| 192 |
|
| 193 |
if ( empty( $lang ) ) { |
| 194 |
PLL()->curlang = self::$original_language; |
| 195 |
} elseif ( 'all' === $lang ) { |
| 196 |
PLL()->curlang = null; |
| 197 |
} elseif ( in_array( $lang, pll_languages_list() ) ) { |
| 198 |
PLL()->curlang = PLL()->model->get_language( $lang ); |
| 199 |
} |
| 200 |
|
| 201 |
if ( $cookie && isset( PLL()->choose_lang ) ) { |
| 202 |
PLL()->choose_lang->maybe_setcookie(); |
| 203 |
} |
| 204 |
|
| 205 |
do_action( 'wpml_language_has_switched', $lang, $cookie, self::$original_language ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the language code for a translatable element |
| 210 |
* |
| 211 |
* @since 2.0 |
| 212 |
* |
| 213 |
* @param mixed $language_code |
| 214 |
* @param array $args An array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
public function wpml_element_language_code( $language_code, $args ) { |
| 218 |
$type = $args['element_type']; |
| 219 |
$id = $args['element_id']; |
| 220 |
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ); |
| 221 |
if ( 'term' === $pll_type && $term = get_term_by( 'term_taxonomy_id', $id ) ) { |
| 222 |
$id = $term->term_id; |
| 223 |
} |
| 224 |
return $pll_type ? call_user_func( "pll_get_{$pll_type}_language", $id ) : $language_code; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Translates a string |
| 229 |
* |
| 230 |
* @since 2.0 |
| 231 |
* |
| 232 |
* @param string $string The string's original value |
| 233 |
* @param string $context The string's registered context |
| 234 |
* @param string $name The string's registered name |
| 235 |
* @param null|string $lang Optional, return the translation in this language, defaults to current language |
| 236 |
* @return string The translated string |
| 237 |
*/ |
| 238 |
public function wpml_translate_single_string( $string, $context, $name, $lang = null ) { |
| 239 |
$has_translation = null; // Passed by reference |
| 240 |
return icl_translate( $context, $name, $string, false, $has_translation, $lang ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Converts a permalink to a language specific permalink |
| 245 |
* |
| 246 |
* @since 2.2 |
| 247 |
* |
| 248 |
* @param string $url The url to filter |
| 249 |
* @param null|string $lang Langage code, optional, defaults to the current language |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
public function wpml_permalink( $url, $lang = '' ) { |
| 253 |
$lang = PLL()->model->get_language( $lang ); |
| 254 |
|
| 255 |
if ( empty( $lang ) && ! empty( PLL()->curlang ) ) { |
| 256 |
$lang = PLL()->curlang; |
| 257 |
} |
| 258 |
|
| 259 |
return empty( $lang ) ? $url : PLL()->links_model->switch_language_in_link( $url, $lang ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Translates a post type slug |
| 264 |
* |
| 265 |
* @since 2.2 |
| 266 |
* |
| 267 |
* @param string $slug Post type slug |
| 268 |
* @param string $post_type Post type name |
| 269 |
* @param string $lang Optional language code (defaults to current language) |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
public function wpml_get_translated_slug( $slug, $post_type, $lang = null ) { |
| 273 |
if ( isset( PLL()->translate_slugs ) ) { |
| 274 |
if ( empty( $lang ) ) { |
| 275 |
$lang = pll_current_language(); |
| 276 |
} |
| 277 |
|
| 278 |
$slug = PLL()->translate_slugs->slugs_model->get_translated_slug( $post_type, $lang ); |
| 279 |
} |
| 280 |
return $slug; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Find out whether a post type or a taxonomy term is translated |
| 285 |
* |
| 286 |
* @since 2.0 |
| 287 |
* |
| 288 |
* @param mixed $null |
| 289 |
* @param int $id The post_id or term_id |
| 290 |
* @param string $type The post type or taxonomy |
| 291 |
* @return bool |
| 292 |
*/ |
| 293 |
public function wpml_element_has_translations( $null, $id, $type ) { |
| 294 |
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ); |
| 295 |
return ( $pll_type && $translations = call_user_func( "pll_get_{$pll_type}_translations", $id ) ) ? count( $translations ) > 1 : false; |
| 296 |
} |
| 297 |
} |
| 298 |
|