admin-menus
4 weeks ago
currencies
4 weeks ago
template-classes
4 weeks ago
translation-editor
4 weeks ago
class-wcml-ajax-setup.php
4 weeks ago
class-wcml-attributes.php
4 weeks ago
class-wcml-capabilities.php
4 weeks ago
class-wcml-cart-removed-items-widget.php
1 year ago
class-wcml-cart-switch-lang-functions.php
4 weeks ago
class-wcml-cart-sync-warnings.php
4 weeks ago
class-wcml-cart.php
4 weeks ago
class-wcml-comments.php
4 weeks ago
class-wcml-compatibility.php
4 weeks ago
class-wcml-coupons.php
4 weeks ago
class-wcml-dependencies.php
4 weeks ago
class-wcml-emails.php
4 weeks ago
class-wcml-endpoints.php
4 weeks ago
class-wcml-install.php
4 weeks ago
class-wcml-languages-upgrader.php
4 weeks ago
class-wcml-locale.php
4 weeks ago
class-wcml-orders.php
4 weeks ago
class-wcml-products-screen-options.php
4 weeks ago
class-wcml-products.php
4 weeks ago
class-wcml-reports.php
4 weeks ago
class-wcml-requests.php
4 weeks ago
class-wcml-resources.php
4 weeks ago
class-wcml-store-pages.php
4 weeks ago
class-wcml-terms.php
4 weeks ago
class-wcml-tp-support.php
4 weeks ago
class-wcml-troubleshooting.php
4 weeks ago
class-wcml-upgrade.php
4 weeks ago
class-wcml-url-translation.php
4 weeks ago
class-wcml-wc-gateways.php
4 weeks ago
class-wcml-wc-shipping.php
4 weeks ago
class-wcml-wc-strings.php
4 weeks ago
class-wcml-widgets.php
4 weeks ago
constants.php
4 weeks ago
functions-pure.php
4 weeks ago
functions.php
4 weeks ago
installer-loader.php
4 weeks ago
missing-php-functions.php
4 weeks ago
wcml-core-functions.php
4 weeks ago
wcml-switch-lang-request.php
4 weeks ago
class-wcml-wc-gateways.php
314 lines
| 1 | <?php |
| 2 | |
| 3 | use function WCML\functions\isStandAlone; |
| 4 | use WCML\PaymentGateways\Strings; |
| 5 | use WCML\StandAlone\NullSitePress; |
| 6 | use WCML\Utilities\WcAdminPages; |
| 7 | use WPML\API\Sanitize; |
| 8 | use WPML\Collect\Support\Collection; |
| 9 | use WPML\Core\ISitePress; |
| 10 | use WPML\FP\Fns; |
| 11 | |
| 12 | class WCML_WC_Gateways { |
| 13 | |
| 14 | const WCML_BACS_ACCOUNTS_CURRENCIES_OPTION = 'wcml_bacs_accounts_currencies'; |
| 15 | |
| 16 | private $current_language; |
| 17 | |
| 18 | private $woocommerce_wpml; |
| 19 | private $sitepress; |
| 20 | |
| 21 | public function __construct( woocommerce_wpml $woocommerce_wpml, ISitePress $sitepress ) { |
| 22 | $this->sitepress = $sitepress; |
| 23 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 24 | |
| 25 | $this->current_language = $this->sitepress->get_current_language(); |
| 26 | if ( 'all' === $this->current_language ) { |
| 27 | $this->current_language = $this->sitepress->get_default_language(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public function add_hooks() { |
| 32 | if ( isStandAlone() ) { |
| 33 | if ( WcAdminPages::isPaymentSettings() ) { |
| 34 | add_action( 'init', [ $this, 'load_bacs_gateway_currency_selector_hooks' ], 11 ); |
| 35 | } |
| 36 | } else { |
| 37 | add_action( 'init', [ $this, 'on_init_hooks' ], 11 ); |
| 38 | add_filter( 'woocommerce_payment_gateways', Fns::withoutRecursion( Fns::identity(), [ $this, 'loaded_woocommerce_payment_gateways' ] ) ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function on_init_hooks() { |
| 43 | add_filter( 'woocommerce_gateway_title', [ $this, 'translate_gateway_title' ], 10, 2 ); |
| 44 | add_filter( 'woocommerce_gateway_description', [ $this, 'translate_gateway_description' ], 10, 2 ); |
| 45 | add_filter( 'woocommerce_paypal_payments_gateway_description', [ $this, 'translate_paypal_payments_gateway_description' ], 10, 2 ); |
| 46 | |
| 47 | if ( WcAdminPages::isPaymentSettings() ) { |
| 48 | $this->load_bacs_gateway_currency_selector_hooks(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public function load_bacs_gateway_currency_selector_hooks() { |
| 53 | if ( WcAdminPages::isSection( WcAdminPages::SECTION_BACS ) && wcml_is_multi_currency_on() ) { |
| 54 | $this->set_bacs_gateway_currency(); |
| 55 | add_action( 'admin_footer', [ $this, 'append_currency_selector_to_bacs_account_settings' ] ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function loaded_woocommerce_payment_gateways( $load_gateways ) { |
| 60 | |
| 61 | foreach ( $load_gateways as $key => $gateway ) { |
| 62 | |
| 63 | $load_gateway = $gateway; |
| 64 | |
| 65 | if ( is_string( $gateway ) ) { |
| 66 | if ( class_exists( $gateway ) ) { |
| 67 | $load_gateway = new $gateway(); |
| 68 | } else { |
| 69 | continue; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $this->register_gateway_settings_strings( $load_gateway->id, $load_gateway->settings ); |
| 74 | $this->payment_gateways_filters( $load_gateway ); |
| 75 | $load_gateways[ $key ] = $load_gateway; |
| 76 | } |
| 77 | |
| 78 | return $load_gateways; |
| 79 | } |
| 80 | |
| 81 | public function register_gateway_settings_strings( $gateway_id, $settings ) { |
| 82 | if ( isset( $settings['enabled'] ) && 'yes' === $settings['enabled'] ) { |
| 83 | foreach ( $this->get_gateway_text_keys_to_translate() as $text_key ) { |
| 84 | if ( isset( $settings[ $text_key ] ) && ! $this->get_gateway_string_id( $settings[ $text_key ], $gateway_id, $text_key ) ) { |
| 85 | icl_register_string( |
| 86 | Strings::TRANSLATION_DOMAIN, |
| 87 | Strings::getStringName( $gateway_id, $text_key ), |
| 88 | $settings[ $text_key ], |
| 89 | false, |
| 90 | $this->sitepress->get_default_language() |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public function payment_gateways_filters( $gateway ) { |
| 98 | if ( isset( $gateway->id ) ) { |
| 99 | $this->translate_gateway_strings( $gateway ); |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | public function translate_gateway_strings( WC_Payment_Gateway $gateway ) { |
| 105 | if ( isset( $gateway->enabled ) && 'no' !== $gateway->enabled ) { |
| 106 | if ( isset( $gateway->instructions ) ) { |
| 107 | $gateway->instructions = $this->translate_gateway_instructions( $gateway->instructions, $gateway->id ); |
| 108 | } |
| 109 | if ( isset( $gateway->description ) ) { |
| 110 | $gateway->description = $this->translate_gateway_description( $gateway->description, $gateway->id ); |
| 111 | } |
| 112 | if ( isset( $gateway->title ) ) { |
| 113 | $gateway->title = $this->translate_gateway_title( $gateway->title, $gateway->id ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | public function translate_gateway_title( $title, $gateway_id ) { |
| 120 | return $this->get_translated_gateway_string( $title, $gateway_id, 'title' ); |
| 121 | } |
| 122 | |
| 123 | public function translate_paypal_payments_gateway_description( $description, $gateway ) { |
| 124 | $id = \WPML\FP\Obj::prop( 'id', $gateway ); |
| 125 | if ( is_null( $id ) ) { |
| 126 | return $description; |
| 127 | } |
| 128 | |
| 129 | return $this->translate_gateway_description( $description, $id ); |
| 130 | } |
| 131 | |
| 132 | public function translate_gateway_description( $description, $gateway_id ) { |
| 133 | return $this->get_translated_gateway_string( $description, $gateway_id, 'description' ); |
| 134 | } |
| 135 | |
| 136 | public function translate_gateway_instructions( $instructions, $gateway_id ) { |
| 137 | return $this->get_translated_gateway_string( $instructions, $gateway_id, 'instructions' ); |
| 138 | } |
| 139 | |
| 140 | public function get_translated_gateway_string( $string, $gateway_id, $name ) { |
| 141 | if ( ! is_string( $string ) ) { |
| 142 | return $string; |
| 143 | } |
| 144 | |
| 145 | $gatewayLanguage = $this->get_current_gateway_language(); |
| 146 | $translatedString = apply_filters( |
| 147 | 'wpml_translate_single_string', |
| 148 | $string, |
| 149 | Strings::TRANSLATION_DOMAIN, |
| 150 | Strings::getStringName( $gateway_id, $name ), |
| 151 | $gatewayLanguage |
| 152 | ); |
| 153 | |
| 154 | if ( $translatedString !== $string ) { |
| 155 | return $translatedString; |
| 156 | } |
| 157 | |
| 158 | if ( $this->isSendingOrderDetails() ) { |
| 159 | $this->sitepress->switch_lang( $gatewayLanguage ); |
| 160 | } |
| 161 | |
| 162 | if ( 'cheque' === $gateway_id && 'title' === $name ) { |
| 163 | $translatedString = _x( $string, 'Check payment method', 'woocommerce' ); |
| 164 | } else { |
| 165 | $translatedString = __( $string, 'woocommerce' ); |
| 166 | } |
| 167 | |
| 168 | return $translatedString; |
| 169 | } |
| 170 | |
| 171 | private function get_current_gateway_language() { |
| 172 | $postData = wpml_collect( $_POST ); |
| 173 | if ( $postData->isNotEmpty() ) { |
| 174 | if ( $this->is_user_order_note( $postData ) ) { |
| 175 | $current_gateway_language = WCML_Orders::getLanguage( (int) $postData->get( 'post_id' ) ); |
| 176 | } elseif ( $this->is_refund_line_item( $postData ) ) { |
| 177 | $current_gateway_language = WCML_Orders::getLanguage( (int) $postData->get( 'order_id' ) ); |
| 178 | } else { |
| 179 | $current_gateway_language = $this->get_order_action_gateway_language( $postData ); |
| 180 | } |
| 181 | } else { |
| 182 | $current_gateway_language = $this->get_order_ajax_action_gateway_language(); |
| 183 | } |
| 184 | |
| 185 | return apply_filters( 'wcml_current_gateway_language', $current_gateway_language ); |
| 186 | } |
| 187 | |
| 188 | private function is_user_order_note( Collection $postData ) { |
| 189 | return 'woocommerce_add_order_note' === $postData->get( 'action' ) && 'customer' === $postData->get( 'note_type' ); |
| 190 | } |
| 191 | |
| 192 | private function is_refund_line_item( Collection $postData ){ |
| 193 | return 'woocommerce_refund_line_items' === $postData->get( 'action' ); |
| 194 | } |
| 195 | |
| 196 | private function isSendingOrderDetails() { |
| 197 | $postData = wpml_collect( $_POST ); |
| 198 | return $postData->get( 'post_ID' ) |
| 199 | && 'shop_order' === $postData->get( 'post_type' ) |
| 200 | && 'send_order_details' === $postData->get( 'wc_order_action' ); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | private function get_order_action_gateway_language( Collection $postData ) { |
| 205 | |
| 206 | if ( $postData->get( 'post_ID' ) ) { |
| 207 | |
| 208 | $is_saving_new_order = wpml_collect( [ |
| 209 | 'auto-draft', |
| 210 | 'draft' |
| 211 | ] )->contains( $postData->get( 'post_status' ) ) |
| 212 | && 'editpost' === $postData->get( 'action' ) |
| 213 | && $postData->get( 'save' ); |
| 214 | if ( $is_saving_new_order && isset( $_COOKIE[ WCML_Orders::DASHBOARD_COOKIE_NAME ] ) ) { |
| 215 | return $_COOKIE[ WCML_Orders::DASHBOARD_COOKIE_NAME ]; |
| 216 | } |
| 217 | |
| 218 | $is_order_emails_status = wpml_collect( [ |
| 219 | 'wc-completed', |
| 220 | 'wc-processing', |
| 221 | 'wc-refunded', |
| 222 | 'wc-on-hold' |
| 223 | ] )->contains( $postData->get( 'order_status' ) ); |
| 224 | |
| 225 | $is_send_order_details_action = $this->isSendingOrderDetails(); |
| 226 | if ( $is_order_emails_status || $is_send_order_details_action ) { |
| 227 | return WCML_Orders::getLanguage( (int) $postData->get( 'post_ID' ) ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return $this->current_language; |
| 232 | } |
| 233 | |
| 234 | private function get_order_ajax_action_gateway_language(){ |
| 235 | |
| 236 | $getData = wpml_collect( $_GET ); |
| 237 | if ( $getData->isNotEmpty() ) { |
| 238 | $is_order_ajax_action = 'woocommerce_mark_order_status' === $getData->get( 'action' ) && wpml_collect( [ |
| 239 | 'completed', |
| 240 | 'processing' |
| 241 | ] )->contains( $getData->get( 'status' ) ); |
| 242 | if ( $is_order_ajax_action && $getData->get( 'order_id' ) ) { |
| 243 | return WCML_Orders::getLanguage( (int) $getData->get( 'order_id' ) ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return $this->current_language; |
| 248 | } |
| 249 | |
| 250 | private function get_gateway_string_id( $value, $gateway_id, $name ) { |
| 251 | return icl_get_string_id( $value, Strings::TRANSLATION_DOMAIN, $gateway_id . '_gateway_' . $name ); |
| 252 | } |
| 253 | |
| 254 | public function set_bacs_gateway_currency() { |
| 255 | foreach ( $_POST as $key => $value ) { |
| 256 | |
| 257 | if ( '_enabled' === substr( $key, -8 ) ) { |
| 258 | $gateway = str_replace( '_enabled', '', $key ); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if ( isset( $gateway ) ) { |
| 263 | if ( 'woocommerce_bacs' === $gateway && isset( $_POST['bacs-currency'] ) ) { |
| 264 | update_option( self::WCML_BACS_ACCOUNTS_CURRENCIES_OPTION, array_map( [ Sanitize::class, 'string' ], $_POST['bacs-currency'] ) ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | public function get_gateway_text_keys_to_translate() { |
| 271 | return apply_filters( 'wcml_gateway_text_keys_to_translate', Strings::TRANSLATABLE_SETTINGS ); |
| 272 | } |
| 273 | |
| 274 | public function append_currency_selector_to_bacs_account_settings() { |
| 275 | |
| 276 | $template_loader = new WPML_Twig_Template_Loader( [ $this->sitepress->get_wp_api()->constant( 'WCML_PLUGIN_PATH' ) . '/templates/multi-currency/' ] ); |
| 277 | $currencies_dropdown_ui = new WCML_Currencies_Dropdown_UI( $template_loader ); |
| 278 | |
| 279 | list( $default_dropdown, $currencies_output ) = $this->get_dropdown( $currencies_dropdown_ui ); |
| 280 | |
| 281 | wp_enqueue_script( 'wcml-bacs-accounts-currencies', WCML_PLUGIN_URL . '/res/js/bacs-accounts-currencies' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true ); |
| 282 | wp_localize_script( |
| 283 | 'wcml-bacs-accounts-currencies', |
| 284 | 'wcml_data', |
| 285 | [ |
| 286 | 'currencies_dropdown' => $currencies_output, |
| 287 | 'label' => __( 'Currency', 'woocommerce-multilingual' ), |
| 288 | 'default_dropdown' => $default_dropdown, |
| 289 | ] |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | public function get_dropdown( $currencies_dropdown_ui ) { |
| 294 | |
| 295 | $bacs_settings = get_option( 'woocommerce_bacs_accounts', [] ); |
| 296 | $active_currencies = $this->woocommerce_wpml->multi_currency->get_currency_codes(); |
| 297 | $default_currency = wcml_get_woocommerce_currency_option(); |
| 298 | $bacs_accounts_currencies = get_option( self::WCML_BACS_ACCOUNTS_CURRENCIES_OPTION, [] ); |
| 299 | $currencies_output = []; |
| 300 | |
| 301 | $default_dropdown = $currencies_dropdown_ui->get( $active_currencies, $default_currency ); |
| 302 | |
| 303 | if ( $bacs_settings ) { |
| 304 | foreach ( $bacs_settings as $id => $account_settings ) { |
| 305 | $currencies_output[ $id ] = isset( $bacs_accounts_currencies[ $id ] ) ? $currencies_dropdown_ui->get( $active_currencies, $bacs_accounts_currencies[ $id ] ) : $default_dropdown; |
| 306 | } |
| 307 | } else { |
| 308 | $currencies_output[] = $default_dropdown; |
| 309 | } |
| 310 | |
| 311 | return [ $default_dropdown, $currencies_output ]; |
| 312 | } |
| 313 | } |
| 314 |