MultilingualHooks.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\WcEmailSettings; |
| 4 | |
| 5 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 6 | use WPML\LIB\WP\Hooks as WPHooks; |
| 7 | |
| 8 | use function WPML\FP\spreadArgs; |
| 9 | |
| 10 | class MultilingualHooks implements \IWPML_Frontend_Action, \IWPML_Backend_Action { |
| 11 | const WCML_EMAIL_HEADER_ALIGNMENT_ID = 'wcml_email_header_alignment'; |
| 12 | |
| 13 | const MULTILINGUAL_HEADER_ALIGNMENT_OFF = 'off'; |
| 14 | const MULTILINGUAL_HEADER_ALIGNMENT_ON = 'on'; |
| 15 | |
| 16 | public function add_hooks() { |
| 17 | WPHooks::onFilter( 'woocommerce_email_settings' ) |
| 18 | ->then( spreadArgs( [ $this, 'woocommerce_email_settings_add_wcml_header_alignment' ] ) ); |
| 19 | |
| 20 | WPHooks::onFilter( 'pre_option_woocommerce_email_header_alignment' ) |
| 21 | ->then( spreadArgs( [ $this, 'pre_option_woocommerce_email_header_alignment' ] ) ); |
| 22 | } |
| 23 | |
| 24 | public function woocommerce_email_settings_add_wcml_header_alignment( $settings ) { |
| 25 | $email_improvements_enabled = FeaturesUtil::feature_is_enabled( 'email_improvements' ); |
| 26 | |
| 27 | $new_settings = []; |
| 28 | |
| 29 | $wcml_option = [ |
| 30 | 'title' => __( 'Header alignment for translations', 'woocommerce-multilingual' ), |
| 31 | 'id' => self::WCML_EMAIL_HEADER_ALIGNMENT_ID, |
| 32 | /* translators: %s is an HTML break line - after about 65 characters the text will no longer be visible */ |
| 33 | 'desc' => sprintf( __( 'Automatically align email headers based on%slanguage direction (right-to-left or left-to-right)."', 'woocommerce-multilingual' ), '<br >' ), |
| 34 | 'default' => self::MULTILINGUAL_HEADER_ALIGNMENT_OFF, |
| 35 | 'type' => 'radio', |
| 36 | 'options' => [ |
| 37 | self::MULTILINGUAL_HEADER_ALIGNMENT_OFF => __( 'Off – use the "Header alignment" setting above', 'woocommerce-multilingual' ), |
| 38 | self::MULTILINGUAL_HEADER_ALIGNMENT_ON => __( 'On – align automatically', 'woocommerce-multilingual' ), |
| 39 | ], |
| 40 | 'row_class' => $email_improvements_enabled ? '' : 'disabled', |
| 41 | ]; |
| 42 | |
| 43 | foreach ( $settings as $setting ) { |
| 44 | $new_settings[] = $setting; |
| 45 | |
| 46 | $settingId = $setting['id'] ?? null; |
| 47 | if ( 'woocommerce_email_header_alignment' === $settingId ) { |
| 48 | $new_settings[] = $wcml_option; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return $new_settings; |
| 53 | } |
| 54 | |
| 55 | public function pre_option_woocommerce_email_header_alignment( $value ) { |
| 56 | if ( self::MULTILINGUAL_HEADER_ALIGNMENT_ON !== get_option( self::WCML_EMAIL_HEADER_ALIGNMENT_ID ) ) { |
| 57 | return $value; |
| 58 | } |
| 59 | |
| 60 | return is_rtl() ? 'right' : 'left'; |
| 61 | } |
| 62 | } |
| 63 |