Skip to content

Commit

Permalink
Merge branch 'BWPM-118' into pre-1.7.13
Browse files Browse the repository at this point in the history
  • Loading branch information
ponddeja committed Jan 29, 2025
2 parents 181d2bf + 781a0f0 commit e3f0fe1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
8 changes: 8 additions & 0 deletions includes/Gateways/ReepayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Reepay\Checkout\Integrations\PWGiftCardsIntegration;
use Reepay\Checkout\Integrations\WCGiftCardsIntegration;
use Reepay\Checkout\Integrations\WPCProductBundlesWooCommerceIntegration;
use Reepay\Checkout\Integrations\PolylangIntegration;
use SitePress;
use Reepay\Checkout\Utils\LoggingTrait;
use Reepay\Checkout\Tokens\TokenReepay;
Expand Down Expand Up @@ -1621,6 +1622,13 @@ protected function get_language(): string {
}
}

// Polylang support.
if ( function_exists( 'pll_current_language' ) ) {
if ( isset( $_SESSION['pll_current_language_session'] ) ) {
$locale = $_SESSION['pll_current_language_session'];
}
}

if ( in_array(
$locale,
array( 'en_US', 'da_DK', 'sv_SE', 'no_NO', 'de_DE', 'es_ES', 'fr_FR', 'it_IT', 'nl_NL' ),
Expand Down
1 change: 1 addition & 0 deletions includes/Integrations/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class Main {
public function __construct() {
new WooBlocksIntegration();
new WCGiftCardsIntegration();
new PolylangIntegration();
}
}
95 changes: 95 additions & 0 deletions includes/Integrations/PolylangIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Integration with Polylang plugin https://wordpress.org/plugins/polylang/
*
* @package Reepay\Checkout\Integrations
*/

namespace Reepay\Checkout\Integrations;

/**
* Class integration
*
* @package Reepay\Checkout\Integrations
*/
class PolylangIntegration {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'start_session' ), 1 );
add_action( 'plugins_loaded', array( $this, 'initialize' ) );
}

/**
* Start the session if it hasn't been started already.
*/
public function start_session() {
if ( ! session_id() ) {
session_start();
}
}

/**
* Initialize the integration.
*/
public function initialize() {
// Check if the request is an AJAX call.
if ( wp_doing_ajax() ) {
return;
}

// Check if Polylang function exists.
if ( ! function_exists( 'pll_current_language' ) ) {
return;
}

add_filter( 'woocommerce_get_script_data', array( $this, 'polylang_ajax_handler_fix_translation' ), 10, 1 );
}

/**
* Return params for script handles.
*
* @param string $params Script handle the data will be attached to.
* @return array|bool
*/
public function polylang_ajax_handler_fix_translation( $params ) {
if ( ! function_exists( 'ajax_handler_fix_translation' ) ) {
// Get the current language.
$locale = determine_locale();
// Take just the first part of the $locale.
$lang = ( ! empty( $locale ) ) ? strstr( $locale, '_', true ) : '';
if ( empty( $lang ) ) {
// If there is no $lang parameter, just return to standard.
return $params;
}
if ( isset( $params['wc_ajax_url'] ) ) {
$locale = $this->get_locale_from_language_code( $lang );
$_SESSION['pll_current_language_session'] = $locale;
}

return $params;
}
}

/**
* Get the locale from the language code.
*
* @param string $lang Language code.
* @return string Locale.
*/
private function get_locale_from_language_code( $lang ) {
// Get the list of languages.
$languages = PLL()->model->get_languages_list();

// Find the locale corresponding to the language code.
foreach ( $languages as $language ) {
if ( $language->slug === $lang ) {
return $language->locale;
}
}

// Fallback to default locale if not found.
return pll_default_language( 'locale' );
}
}

0 comments on commit e3f0fe1

Please sign in to comment.