admin-menus
1 week ago
currencies
1 week ago
template-classes
1 week ago
translation-editor
1 week ago
class-wcml-ajax-setup.php
1 week ago
class-wcml-attributes.php
1 week ago
class-wcml-capabilities.php
1 week ago
class-wcml-cart-removed-items-widget.php
1 year ago
class-wcml-cart-switch-lang-functions.php
1 week ago
class-wcml-cart-sync-warnings.php
1 week ago
class-wcml-cart.php
1 week ago
class-wcml-comments.php
1 week ago
class-wcml-compatibility.php
1 week ago
class-wcml-coupons.php
1 week ago
class-wcml-dependencies.php
1 week ago
class-wcml-emails.php
1 week ago
class-wcml-endpoints.php
1 week ago
class-wcml-install.php
1 week ago
class-wcml-languages-upgrader.php
1 week ago
class-wcml-locale.php
1 week ago
class-wcml-orders.php
1 week ago
class-wcml-products-screen-options.php
1 week ago
class-wcml-products.php
1 week ago
class-wcml-reports.php
1 week ago
class-wcml-requests.php
1 week ago
class-wcml-resources.php
1 week ago
class-wcml-store-pages.php
1 week ago
class-wcml-terms.php
1 week ago
class-wcml-tp-support.php
1 week ago
class-wcml-troubleshooting.php
1 week ago
class-wcml-upgrade.php
1 week ago
class-wcml-url-translation.php
1 week ago
class-wcml-wc-gateways.php
1 week ago
class-wcml-wc-shipping.php
1 week ago
class-wcml-wc-strings.php
1 week ago
class-wcml-widgets.php
1 week ago
constants.php
1 week ago
functions-pure.php
1 week ago
functions.php
1 week ago
installer-loader.php
1 week ago
missing-php-functions.php
1 week ago
wcml-core-functions.php
1 week ago
wcml-switch-lang-request.php
1 week ago
class-wcml-endpoints.php
547 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Endpoints { |
| 4 | |
| 5 | private $woocommerce_wpml; |
| 6 | private $sitepress; |
| 7 | private $wpdb; |
| 8 | |
| 9 | private $originalEndpoints = []; |
| 10 | private $translatedEndpoints = []; |
| 11 | private $endpointsToTranslations = []; |
| 12 | private $translatedEndpointsInDatabase = []; |
| 13 | private $editAddressSlugs = []; |
| 14 | |
| 15 | private $endpointKeysToOptions = [ |
| 16 | 'order-pay' => 'woocommerce_checkout_pay_endpoint', |
| 17 | 'order-received' => 'woocommerce_checkout_order_received_endpoint', |
| 18 | 'orders' => 'woocommerce_myaccount_orders_endpoint', |
| 19 | 'view-order' => 'woocommerce_myaccount_view_order_endpoint', |
| 20 | 'downloads' => 'woocommerce_myaccount_downloads_endpoint', |
| 21 | 'edit-account' => 'woocommerce_myaccount_edit_account_endpoint', |
| 22 | 'edit-address' => 'woocommerce_myaccount_edit_address_endpoint', |
| 23 | 'payment-methods' => 'woocommerce_myaccount_payment_methods_endpoint', |
| 24 | 'lost-password' => 'woocommerce_myaccount_lost_password_endpoint', |
| 25 | 'customer-logout' => 'woocommerce_logout_endpoint', |
| 26 | 'add-payment-method' => 'woocommerce_myaccount_add_payment_method_endpoint', |
| 27 | 'delete-payment-method' => 'woocommerce_myaccount_delete_payment_method_endpoint', |
| 28 | 'set-default-payment-method' => 'woocommerce_myaccount_set_default_payment_method_endpoint', |
| 29 | ]; |
| 30 | |
| 31 | const STRING_CONTEXT = 'WP Endpoints'; |
| 32 | |
| 33 | public function __construct( woocommerce_wpml $woocommerce_wpml, SitePress $sitepress, wpdb $wpdb ) { |
| 34 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 35 | $this->sitepress = $sitepress; |
| 36 | $this->wpdb = $wpdb; |
| 37 | } |
| 38 | |
| 39 | public function add_hooks() { |
| 40 | add_action( 'init', [ $this, 'migrate_ones_string_translations' ], 8 ); |
| 41 | |
| 42 | add_filter( 'wpml_registered_endpoints', [ $this, 'unregisterWcEndpointsFromWpml' ] ); |
| 43 | |
| 44 | if ( ! is_admin() ) { |
| 45 | add_action( 'init', [ $this, 'initQueryVars' ], 9 ); |
| 46 | add_action( 'init', [ $this, 'translateOptions' ] ); |
| 47 | add_filter( 'woocommerce_get_query_vars', [ $this, 'registerAndTranslate' ], 99 ); |
| 48 | add_filter( 'option_rewrite_rules', [ $this, 'adjustRewriteRules' ], 0 ); |
| 49 | } |
| 50 | |
| 51 | add_filter( 'wpml_endpoint_url_value', [ $this, 'filter_endpoint_url_value' ], 10, 2 ); |
| 52 | add_filter( 'wpml_current_ls_language_url_endpoint', [ $this, 'add_endpoint_to_current_ls_language_url' ], 10, 4 ); |
| 53 | |
| 54 | add_filter( 'wpml_sl_blacklist_requests', [ $this, 'reserved_requests' ] ); |
| 55 | add_filter( 'woocommerce_get_endpoint_url', [ $this, 'filter_get_endpoint_url' ], 10, 4 ); |
| 56 | } |
| 57 | |
| 58 | public function migrate_ones_string_translations() { |
| 59 | if ( ! get_option( 'wcml_endpoints_context_updated' ) ) { |
| 60 | |
| 61 | $endpoint_keys = [ |
| 62 | 'order-pay', |
| 63 | 'order-received', |
| 64 | 'view-order', |
| 65 | 'edit-account', |
| 66 | 'edit-address', |
| 67 | 'lost-password', |
| 68 | 'customer-logout', |
| 69 | 'add-payment-method', |
| 70 | 'set-default-payment-method', |
| 71 | 'delete-payment-method', |
| 72 | 'payment-methods', |
| 73 | 'downloads', |
| 74 | 'orders', |
| 75 | ]; |
| 76 | |
| 77 | foreach ( $endpoint_keys as $endpoint_key ) { |
| 78 | |
| 79 | $existing_string_id = $this->wpdb->get_var( |
| 80 | $this->wpdb->prepare( "SELECT id FROM {$this->wpdb->prefix}icl_strings |
| 81 | WHERE context = %s AND name = %s", |
| 82 | WPML_Endpoints_Support::STRING_CONTEXT, $endpoint_key ) |
| 83 | ); |
| 84 | |
| 85 | if ( $existing_string_id ) { |
| 86 | |
| 87 | $existing_wcml_string_id = $this->wpdb->get_var( |
| 88 | $this->wpdb->prepare( |
| 89 | "SELECT id FROM {$this->wpdb->prefix}icl_strings WHERE context = %s AND name = %s", |
| 90 | WCML_Url_Translation::WC_STRING_CONTEXT, |
| 91 | $endpoint_key |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | if ( $existing_wcml_string_id ) { |
| 96 | $wcml_string_translations = icl_get_string_translations_by_id( $existing_wcml_string_id ); |
| 97 | |
| 98 | foreach ( $wcml_string_translations as $language_code => $translation_data ) { |
| 99 | icl_add_string_translation( $existing_string_id, $language_code, $translation_data['value'], ICL_STRING_TRANSLATION_COMPLETE ); |
| 100 | } |
| 101 | |
| 102 | wpml_unregister_string_multi( [ $existing_wcml_string_id ] ); |
| 103 | } |
| 104 | } else { |
| 105 | |
| 106 | $this->wpdb->query( |
| 107 | $this->wpdb->prepare( |
| 108 | "UPDATE {$this->wpdb->prefix}icl_strings |
| 109 | SET context = %s |
| 110 | WHERE context = %s AND name = %s", |
| 111 | WPML_Endpoints_Support::STRING_CONTEXT, |
| 112 | WCML_Url_Translation::WC_STRING_CONTEXT, |
| 113 | $endpoint_key |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | $string_id = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT id FROM {$this->wpdb->prefix}icl_strings WHERE context = %s AND name = %s", WPML_Endpoints_Support::STRING_CONTEXT, $endpoint_key ) ); |
| 118 | |
| 119 | if ( $string_id ) { |
| 120 | $this->wpdb->query( |
| 121 | $this->wpdb->prepare( "UPDATE {$this->wpdb->prefix}icl_strings |
| 122 | SET domain_name_context_md5 = %s |
| 123 | WHERE id = %d", |
| 124 | md5( $endpoint_key . WPML_Endpoints_Support::STRING_CONTEXT ), $string_id ) |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | update_option( 'wcml_endpoints_context_updated', true ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public function initQueryVars() { |
| 134 | WC()->query->get_query_vars(); |
| 135 | } |
| 136 | |
| 137 | public function registerAndTranslate( $queryVars ) { |
| 138 | if ( empty( $this->originalEndpoints ) ) { |
| 139 | $this->originalEndpoints = $queryVars; |
| 140 | } |
| 141 | |
| 142 | $currentLanguage = $this->sitepress->get_current_language(); |
| 143 | $pendingQueryVars = []; |
| 144 | $originalQueryVars = $queryVars; |
| 145 | |
| 146 | foreach ( $queryVars as $key => $endpoint ) { |
| 147 | if ( in_array( $endpoint, $this->translatedEndpoints, true ) ) { |
| 148 | continue; |
| 149 | } |
| 150 | if ( array_key_exists( $endpoint, $this->endpointsToTranslations ) ) { |
| 151 | $queryVars[ $key ] = $this->endpointsToTranslations[ $endpoint ]; |
| 152 | continue; |
| 153 | } |
| 154 | if ( $key !== $endpoint && $this->isRegisteredEndpointString( $endpoint, $endpoint ) ) { |
| 155 | $this->migrateEndpointStringName( $key, $endpoint ); |
| 156 | } elseif ( ! $this->isRegisteredEndpointString( $key, $endpoint ) ) { |
| 157 | $this->registerEndpointString( $key, $endpoint ); |
| 158 | } |
| 159 | $pendingQueryVars[ $key ] = $endpoint; |
| 160 | } |
| 161 | |
| 162 | foreach ( $pendingQueryVars as $key => $endpoint ) { |
| 163 | $queryVars[ $key ] = $this->translateEndpoint( $key, $endpoint, $currentLanguage ); |
| 164 | $this->translatedEndpoints[ $key ] = $queryVars[ $key ]; |
| 165 | $this->endpointsToTranslations[ $endpoint ] = $queryVars[ $key ]; |
| 166 | } |
| 167 | |
| 168 | $additionalQueryVars = apply_filters( 'wcml_register_endpoints_query_vars', $queryVars, $originalQueryVars, $this ); |
| 169 | |
| 170 | return array_merge( $queryVars, $additionalQueryVars ); |
| 171 | } |
| 172 | |
| 173 | public function get_endpoint_translation( $key, $endpointOrLanguage = null, $language = null ) { |
| 174 | $endpoint = $key; |
| 175 | |
| 176 | $activeLanguages = array_keys( $this->sitepress->get_active_languages() ); |
| 177 | |
| 178 | if ( in_array( $endpointOrLanguage, $activeLanguages, true ) ) { |
| 179 | $language = $endpointOrLanguage; |
| 180 | } elseif ( $endpointOrLanguage ) { |
| 181 | $endpoint = $endpointOrLanguage; |
| 182 | } |
| 183 | |
| 184 | return $this->translateEndpoint( $key, $endpoint, $language ); |
| 185 | } |
| 186 | |
| 187 | public function unregisterWcEndpointsFromWpml( $endpoints ) { |
| 188 | foreach ( $this->originalEndpoints as $key => $value ) { |
| 189 | unset( $endpoints[ $key ] ); |
| 190 | unset( $endpoints[ $value ] ); |
| 191 | } |
| 192 | foreach ( $this->translatedEndpoints as $key => $value ) { |
| 193 | unset( $endpoints[ $value ] ); |
| 194 | } |
| 195 | return $endpoints; |
| 196 | } |
| 197 | |
| 198 | private function isRegisteredEndpointString( $key, $endpoint ) { |
| 199 | $endpoints = wp_cache_get( self::STRING_CONTEXT, __CLASS__ ); |
| 200 | |
| 201 | if ( false === $endpoints ) { |
| 202 | $endpointsByname = $this->wpdb->get_results( |
| 203 | $this->wpdb->prepare( |
| 204 | " |
| 205 | SELECT name, value FROM {$this->wpdb->prefix}icl_strings WHERE context = %s |
| 206 | ", |
| 207 | self::STRING_CONTEXT |
| 208 | ) |
| 209 | ); |
| 210 | |
| 211 | $endpoints = wp_list_pluck( $endpointsByname, 'value', 'name' ); |
| 212 | |
| 213 | wp_cache_set( self::STRING_CONTEXT, $endpoints, __CLASS__ ); |
| 214 | } |
| 215 | |
| 216 | return array_key_exists( $key, $endpoints ) && $endpoints[ $key ] === $endpoint; |
| 217 | } |
| 218 | |
| 219 | private function migrateEndpointStringName( $key, $endpoint ) { |
| 220 | $keyBackup = $key . '-bak-' . wp_rand( 0, 1000 ); |
| 221 | $this->wpdb->query( |
| 222 | $this->wpdb->prepare( |
| 223 | " |
| 224 | UPDATE {$this->wpdb->prefix}icl_strings |
| 225 | SET name = %s, domain_name_context_md5 = %s |
| 226 | WHERE context = %s AND name = %s |
| 227 | ", |
| 228 | $keyBackup, |
| 229 | md5( $keyBackup . self::STRING_CONTEXT ), |
| 230 | self::STRING_CONTEXT, |
| 231 | $key |
| 232 | ) |
| 233 | ); |
| 234 | $this->wpdb->query( |
| 235 | $this->wpdb->prepare( |
| 236 | " |
| 237 | UPDATE {$this->wpdb->prefix}icl_strings |
| 238 | SET name = %s, domain_name_context_md5 = %s |
| 239 | WHERE context = %s AND name = %s and value = %s |
| 240 | ", |
| 241 | $key, |
| 242 | md5( $key . self::STRING_CONTEXT ), |
| 243 | self::STRING_CONTEXT, |
| 244 | $endpoint, |
| 245 | $endpoint |
| 246 | ) |
| 247 | ); |
| 248 | |
| 249 | do_action( 'wpml_st_string_updated' ); |
| 250 | wp_cache_delete( self::STRING_CONTEXT, __CLASS__ ); |
| 251 | } |
| 252 | |
| 253 | private function registerEndpointString( $key, $endpoint ) { |
| 254 | do_action( 'wpml_register_single_string', self::STRING_CONTEXT, $key, $endpoint ); |
| 255 | wp_cache_delete( self::STRING_CONTEXT, __CLASS__ ); |
| 256 | } |
| 257 | |
| 258 | public function adjustRewriteRules( $rewriteRules ) { |
| 259 | if ( empty( $rewriteRules ) ) { |
| 260 | return $rewriteRules; |
| 261 | } |
| 262 | |
| 263 | foreach ( $this->endpointsToTranslations as $endpoint => $endpointTranslation ) { |
| 264 | if ( $endpoint === $endpointTranslation ) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | $adjustedRewriteRules = []; |
| 269 | foreach ( $rewriteRules as $k => $v ) { |
| 270 | if ( array_key_exists( $k, $adjustedRewriteRules ) ) { |
| 271 | continue; |
| 272 | } |
| 273 | $adjustedRewriteRules[ $k ] = $v; |
| 274 | |
| 275 | $newKey = false; |
| 276 | if ( 0 === strpos( $k, $endpoint . '(/(.*))?/?$' ) ) { |
| 277 | $newKey = str_replace( |
| 278 | $endpoint . '(/(.*))?/?$', |
| 279 | $endpointTranslation . '(/(.*))?/?$', |
| 280 | $k |
| 281 | ); |
| 282 | } elseif ( false !== strpos( $k, '/' . $endpoint . '(/(.*))?/?$' ) ) { |
| 283 | $newKey = str_replace( |
| 284 | '/' . $endpoint . '(/(.*))?/?$', |
| 285 | '/' . $endpointTranslation . '(/(.*))?/?$', |
| 286 | $k |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | if ( ! $newKey ) { |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | preg_match( |
| 295 | '/&' . $endpoint . '=\$matches\[(\d+)\]/', |
| 296 | $v, |
| 297 | $matches |
| 298 | ); |
| 299 | $matchesValue = isset( $matches[1] ) ? '$matches[' . $matches[1] . ']' : ''; |
| 300 | $newValue = str_replace( |
| 301 | '&' . $endpoint . '=' . $matchesValue, |
| 302 | '&' . $endpointTranslation . '=' . $matchesValue . '&' . $endpoint . '=' . $matchesValue, |
| 303 | $v |
| 304 | ); |
| 305 | |
| 306 | $adjustedRewriteRules[ $newKey ] = $newValue; |
| 307 | } |
| 308 | $rewriteRules = $adjustedRewriteRules; |
| 309 | } |
| 310 | |
| 311 | return $rewriteRules; |
| 312 | } |
| 313 | |
| 314 | public function translateOptions() { |
| 315 | $keysToOptions = apply_filters( |
| 316 | 'wcml_endpoint_keys_to_options', |
| 317 | $this->endpointKeysToOptions |
| 318 | ); |
| 319 | |
| 320 | foreach ( $keysToOptions as $key => $option ) { |
| 321 | add_filter( |
| 322 | 'option_' . $option, |
| 323 | function( $originalEndpoint ) use ( $key ) { |
| 324 | if ( array_key_exists( $key, $this->translatedEndpoints ) ) { |
| 325 | return $this->translatedEndpoints[ $key ]; |
| 326 | } |
| 327 | return $originalEndpoint; |
| 328 | }, |
| 329 | 99 |
| 330 | ); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | public function translateEndpoint( $key, $endpoint, $language = null, $encode = true ) { |
| 335 | if ( null === $language ) { |
| 336 | $language = $this->sitepress->get_current_language(); |
| 337 | } |
| 338 | $translatedEndpointsInDatabase = $this->getTranslatedEndpointsInDatabase( $language ); |
| 339 | $endpointTranslation = $translatedEndpointsInDatabase[ $key ] ?? $endpoint; |
| 340 | |
| 341 | if ( ! empty( $endpointTranslation ) ) { |
| 342 | return $encode |
| 343 | ? implode( '/', array_map( 'rawurlencode', explode( '/', $endpointTranslation ) ) ) |
| 344 | : $endpointTranslation; |
| 345 | } else { |
| 346 | return $endpoint; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private function getTranslatedEndpointsInDatabase( $language, $refreshCache = false ) { |
| 351 | if ( ! $refreshCache && array_key_exists( $language, $this->translatedEndpointsInDatabase ) ) { |
| 352 | return $this->translatedEndpointsInDatabase[ $language ]; |
| 353 | } |
| 354 | |
| 355 | $translatedEndpointsInDatabase = $this->wpdb->get_results( |
| 356 | $this->wpdb->prepare( |
| 357 | " |
| 358 | SELECT s.name as name, st.value as value |
| 359 | FROM {$this->wpdb->prefix}icl_strings s |
| 360 | JOIN {$this->wpdb->prefix}icl_string_translations st |
| 361 | WHERE s.context = %s |
| 362 | AND s.id = st.string_id |
| 363 | AND st.language = %s |
| 364 | ", |
| 365 | self::STRING_CONTEXT, |
| 366 | $language |
| 367 | ) |
| 368 | ); |
| 369 | |
| 370 | $this->translatedEndpointsInDatabase[ $language ] = wp_list_pluck( $translatedEndpointsInDatabase, 'value', 'name' ); |
| 371 | |
| 372 | return $this->translatedEndpointsInDatabase[ $language ]; |
| 373 | } |
| 374 | |
| 375 | public function reserved_requests( $requests ) { |
| 376 | $cache_key = 'reserved_requests'; |
| 377 | $cache_group = 'wpml-endpoints'; |
| 378 | |
| 379 | $found = null; |
| 380 | $reserved_requests = wp_cache_get( $cache_key, $cache_group, false, $found ); |
| 381 | |
| 382 | if ( ! $found || ! $reserved_requests ) { |
| 383 | $reserved_requests = []; |
| 384 | |
| 385 | $current_language = $this->sitepress->get_current_language(); |
| 386 | $languages = $this->sitepress->get_active_languages(); |
| 387 | $languages_codes = array_keys( $languages ); |
| 388 | foreach ( $languages_codes as $language_code ) { |
| 389 | $this->sitepress->switch_lang( $language_code ); |
| 390 | |
| 391 | $my_account_page_id = wc_get_page_id( 'myaccount' ); |
| 392 | |
| 393 | if ( $my_account_page_id ) { |
| 394 | |
| 395 | $account_base = get_page_uri( $my_account_page_id ); |
| 396 | |
| 397 | if ( $account_base ) { |
| 398 | $reserved_requests[] = $account_base; |
| 399 | $reserved_requests[] = '/^' . str_replace( '/', '\/', $account_base ) . '/'; |
| 400 | $is_page_display_as_translated = $this->sitepress->is_display_as_translated_post_type( 'page' ); |
| 401 | |
| 402 | if ( ! $is_page_display_as_translated ) { |
| 403 | $wcQueryVars = WC()->query->get_query_vars(); |
| 404 | foreach ( $wcQueryVars as $key => $endpoint ) { |
| 405 | $translated_endpoint = $this->translateEndpoint( $key, $endpoint, $language_code ); |
| 406 | $reserved_requests[] = $account_base . '/' . $translated_endpoint; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | $this->sitepress->switch_lang( $current_language ); |
| 414 | |
| 415 | $reserved_requests[] = '/' . get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ) . '/'; |
| 416 | |
| 417 | wp_cache_set( $cache_key, $reserved_requests, $cache_group ); |
| 418 | |
| 419 | } |
| 420 | |
| 421 | if ( $reserved_requests ) { |
| 422 | $requests = array_unique( array_merge( $requests, $reserved_requests ) ); |
| 423 | } |
| 424 | |
| 425 | return $requests; |
| 426 | } |
| 427 | |
| 428 | private function get_translated_edit_address_slug( $slug, $language = false ) { |
| 429 | if ( $language && isset( $this->editAddressSlugs[ $language ][ $slug ] ) ) { |
| 430 | return $this->editAddressSlugs[ $language ][ $slug ]; |
| 431 | } |
| 432 | |
| 433 | $strings = $this->woocommerce_wpml->strings; |
| 434 | $strings_language = $strings->get_string_language( $slug, 'woocommerce', 'edit-address-slug: ' . $slug ); |
| 435 | if ( $strings_language === $language ) { |
| 436 | return $slug; |
| 437 | } |
| 438 | |
| 439 | $translated_slug = apply_filters( 'wpml_translate_single_string', $slug, 'woocommerce', 'edit-address-slug: ' . $slug, $language ); |
| 440 | if ( $translated_slug === $slug ) { |
| 441 | if ( $language ) { |
| 442 | $translated_slug = $strings->get_translation_from_woocommerce_mo_file( $strings->get_msgid_for_mo( $slug, 'edit-address-slug' ), $language ); |
| 443 | } else { |
| 444 | $translated_slug = _x( $slug, 'edit-address-slug', 'woocommerce' ); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if ( $language ) { |
| 449 | $this->editAddressSlugs[ $language ][ $slug ] = $translated_slug; |
| 450 | } |
| 451 | |
| 452 | return $translated_slug; |
| 453 | } |
| 454 | |
| 455 | public function filter_get_endpoint_url( $url, $endpoint, $value, $permalink ) { |
| 456 | remove_filter( 'woocommerce_get_endpoint_url', [ $this, 'filter_get_endpoint_url' ], 10 ); |
| 457 | |
| 458 | $key = array_search( $endpoint, $this->translatedEndpoints, true ); |
| 459 | if ( false === $key ) { |
| 460 | $key = array_search( $endpoint, $this->originalEndpoints, true ); |
| 461 | } |
| 462 | |
| 463 | if ( false !== $key ) { |
| 464 | $currentLanguage = $this->sitepress->get_current_language(); |
| 465 | $translated_endpoint = $this->translateEndpoint( $key, $endpoint, $currentLanguage ); |
| 466 | $url = wc_get_endpoint_url( $translated_endpoint, $value, $this->sitepress->convert_url( $permalink ) ); |
| 467 | } |
| 468 | |
| 469 | add_filter( 'woocommerce_get_endpoint_url', [ $this, 'filter_get_endpoint_url' ], 10, 4 ); |
| 470 | return $url; |
| 471 | } |
| 472 | |
| 473 | public function filter_endpoint_url_value( $value, $page_lang ) { |
| 474 | if ( $page_lang ) { |
| 475 | $edit_address_shipping = $this->get_translated_edit_address_slug( 'shipping', $page_lang ); |
| 476 | $edit_address_billing = $this->get_translated_edit_address_slug( 'billing', $page_lang ); |
| 477 | |
| 478 | if ( $edit_address_shipping == urldecode( $value ) ) { |
| 479 | $value = $this->get_translated_edit_address_slug( 'shipping', $this->sitepress->get_current_language() ); |
| 480 | } elseif ( $edit_address_billing == urldecode( $value ) ) { |
| 481 | $value = $this->get_translated_edit_address_slug( 'billing', $this->sitepress->get_current_language() ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return $value; |
| 486 | } |
| 487 | |
| 488 | private function adjustCurrentLsEndpointValue( $value, $switcherLanguage, $postLanguage ) { |
| 489 | if ( $postLanguage ) { |
| 490 | $edit_address_shipping = sanitize_title( $this->get_translated_edit_address_slug( 'shipping', $postLanguage ) ); |
| 491 | $edit_address_billing = sanitize_title( $this->get_translated_edit_address_slug( 'billing', $postLanguage ) ); |
| 492 | |
| 493 | if ( urldecode( $value ) === $edit_address_shipping ) { |
| 494 | $value = sanitize_title( $this->get_translated_edit_address_slug( 'shipping', $switcherLanguage ) ); |
| 495 | } elseif ( urldecode( $value ) === $edit_address_billing ) { |
| 496 | $value = sanitize_title( $this->get_translated_edit_address_slug( 'billing', $switcherLanguage ) ); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return $value; |
| 501 | } |
| 502 | |
| 503 | public function add_endpoint_to_current_ls_language_url( $url, $postLanguage, $data, $current_endpoint ) { |
| 504 | $current_endpoint = $this->getCurrentLsEndpoint( $data['code'], $postLanguage ); |
| 505 | |
| 506 | if ( $current_endpoint ) { |
| 507 | $url = apply_filters( 'wpml_get_endpoint_url', $current_endpoint['endpoint'], $current_endpoint['value'], $url ); |
| 508 | } |
| 509 | |
| 510 | return esc_url_raw( $url ); |
| 511 | } |
| 512 | |
| 513 | private function getCurrentLsEndpoint( $switcherLanguage, $postLanguage ) { |
| 514 | global $wp; |
| 515 | |
| 516 | $current_endpoint = []; |
| 517 | |
| 518 | foreach ( $this->translatedEndpoints as $key => $endpointTranslation ) { |
| 519 | if ( |
| 520 | isset( $wp->query_vars[ $endpointTranslation ] ) |
| 521 | && array_key_exists( $key, $this->originalEndpoints ) |
| 522 | ) { |
| 523 | $current_endpoint['key'] = $key; |
| 524 | $current_endpoint['endpoint'] = $this->translateEndpoint( $key, $this->originalEndpoints[ $key ], $switcherLanguage, false ); |
| 525 | $current_endpoint['value'] = $this->adjustCurrentLsEndpointValue( $wp->query_vars[ $endpointTranslation ], $switcherLanguage, $postLanguage ); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if ( $current_endpoint ) { |
| 531 | return $current_endpoint; |
| 532 | } |
| 533 | |
| 534 | foreach ( $this->originalEndpoints as $key => $endpoint ) { |
| 535 | if ( isset( $wp->query_vars[ $endpoint ] ) ) { |
| 536 | $current_endpoint['key'] = $key; |
| 537 | $current_endpoint['endpoint'] = $this->translateEndpoint( $key, $endpoint, $switcherLanguage, false ); |
| 538 | $current_endpoint['value'] = $this->adjustCurrentLsEndpointValue( $wp->query_vars[ $endpoint ], $switcherLanguage, $postLanguage ); |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return $current_endpoint; |
| 544 | } |
| 545 | |
| 546 | } |
| 547 |