Factory.php
4 weeks ago
MulticurrencyHooks.php
4 weeks ago
OptionIterator.php
4 weeks ago
class-wcml-checkout-addons.php
4 weeks ago
class-wcml-checkout-addons.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | use WCML\Compatibility\WcCheckoutAddons\OptionIterator; |
| 4 | |
| 5 | class WCML_Checkout_Addons implements \IWPML_Action { |
| 6 | |
| 7 | const PACKAGE_KIND = 'WooCommerce Checkout Add-On'; |
| 8 | const PACKAGE_KIND_SLUG = 'woocommerce-checkout-add-on'; |
| 9 | const PACKAGE_NAME = 'wc-checkout-woocommerce-addons-%s'; |
| 10 | const PACKAGE_TITLE = 'WooCommerce Checkout Add-On: %s'; |
| 11 | |
| 12 | private $packages; |
| 13 | |
| 14 | public function createPackage( $checkoutAddOnId, $checkoutAddOnName ): stdClass { |
| 15 | if ( ! isset( $this->packages[ $checkoutAddOnId ][ $checkoutAddOnName ] ) ) { |
| 16 | return $this->packages[ $checkoutAddOnId ][ $checkoutAddOnName ] = (object) [ |
| 17 | 'kind' => self::PACKAGE_KIND, |
| 18 | 'kind_slug' => self::PACKAGE_KIND_SLUG, |
| 19 | 'name' => sprintf( self::PACKAGE_NAME, $checkoutAddOnId ), |
| 20 | 'title' => sprintf( self::PACKAGE_TITLE, $checkoutAddOnName ), |
| 21 | ]; |
| 22 | } |
| 23 | |
| 24 | return $this->packages[ $checkoutAddOnId ][ $checkoutAddOnName ]; |
| 25 | } |
| 26 | |
| 27 | public function add_hooks() { |
| 28 | add_filter( 'option_wc_checkout_add_ons', [ $this, 'option_wc_checkout_add_ons' ] ); |
| 29 | } |
| 30 | |
| 31 | public function option_wc_checkout_add_ons( $option_value ) { |
| 32 | return OptionIterator::apply( [ $this, 'handle_option_part' ], $option_value ); |
| 33 | } |
| 34 | |
| 35 | public function handle_option_part( $index, $conf, $checkoutAddOnName, $checkoutAddOnId ) { |
| 36 | $conf = $this->register_or_translate( 'label', $conf, $index, $checkoutAddOnName, $checkoutAddOnId ); |
| 37 | $conf = $this->register_or_translate( 'description', $conf, $index, $checkoutAddOnName, $checkoutAddOnId ); |
| 38 | |
| 39 | return $conf; |
| 40 | } |
| 41 | |
| 42 | private function register_or_translate( $element, $conf, $index, $checkoutAddOnName, $checkoutAddOnId ) { |
| 43 | if ( isset( $conf[ $element ] ) ) { |
| 44 | $package = $this->createPackage( $checkoutAddOnId, $checkoutAddOnName ); |
| 45 | $string = $conf[ $element ]; |
| 46 | $key = sprintf( '%s_%s', $index, $element ); |
| 47 | if ( $this->is_default_language() ) { |
| 48 | do_action( |
| 49 | 'wpml_register_string', |
| 50 | $string, |
| 51 | $key, |
| 52 | $package, |
| 53 | sprintf( '%s %s', $string, ucfirst( $element ) ), |
| 54 | $package->kind |
| 55 | ); |
| 56 | |
| 57 | } else { |
| 58 | $conf[ $element ] = $this->translate( $string, $key, $package ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $conf; |
| 63 | } |
| 64 | |
| 65 | private function translate( $text, $key, $package ) { |
| 66 | $translation = apply_filters( |
| 67 | 'wpml_translate_string', |
| 68 | $text, |
| 69 | $key, |
| 70 | $package |
| 71 | ); |
| 72 | |
| 73 | if ( $text === $translation ) { |
| 74 | $key .= '_' . md5( $text ); |
| 75 | |
| 76 | $translation = apply_filters( |
| 77 | 'wpml_translate_single_string', |
| 78 | $text, |
| 79 | 'wc_checkout_addons', |
| 80 | $key |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return $translation; |
| 85 | } |
| 86 | |
| 87 | private function is_default_language() { |
| 88 | return apply_filters( 'wpml_current_language', null ) === apply_filters( 'wpml_default_language', null ); |
| 89 | } |
| 90 | } |
| 91 |