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