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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Email / Settings / TranslationControls.php
woocommerce-multilingual / classes / Email / Settings Last commit date
TranslationControls.php 1 week ago
TranslationControls.php
190 lines
1 <?php
2
3 namespace WCML\Email\Settings;
4
5 use WCML\TranslationControls\Hooks as TranslationControlsBase;
6 use WCML\Utilities\WcAdminPages;
7 use WCML_Tracking_Link;
8 use WPML\FP\Obj;
9
10 class TranslationControls extends TranslationControlsBase {
11
12 const OPTION_NAMES = [
13 'woocommerce_new_order_settings',
14 'woocommerce_cancelled_order_settings',
15 'woocommerce_failed_order_settings',
16 'woocommerce_customer_failed_order_settings',
17 'woocommerce_customer_on_hold_order_settings',
18 'woocommerce_customer_processing_order_settings',
19 'woocommerce_customer_completed_order_settings',
20 'woocommerce_customer_refunded_order_settings',
21 'woocommerce_customer_invoice_settings',
22 'woocommerce_customer_note_settings',
23 'woocommerce_customer_reset_password_settings',
24 'woocommerce_customer_new_account_settings',
25 ];
26
27 const EMAIL_TEXT_KEYS = [
28 'subject',
29 'heading',
30 'subject_downloadable',
31 'heading_downloadable',
32 'subject_full',
33 'subject_partial',
34 'heading_full',
35 'heading_partial',
36 'subject_paid',
37 'heading_paid',
38 'additional_content',
39 ];
40
41 protected function addAdminPageHooks() {
42 add_action( 'woocommerce_settings_email', [ $this, 'translationInstructions' ] );
43 add_action( 'woocommerce_after_settings_email', [ $this, 'translationControls' ] );
44 add_action( 'woocommerce_update_options_email', [ $this, 'registerStringsOnSave' ] );
45 }
46
47 protected function isAdminPage() {
48 return WcAdminPages::isEmailSettings() && WcAdminPages::hasSection();
49 }
50
51 private function getCurrentEmailOptionName() {
52 static $currentEmailOptionName = null;
53 if ( ! is_null( $currentEmailOptionName ) ) {
54 return $currentEmailOptionName;
55 }
56
57 $optionNames = apply_filters( 'wcml_emails_options_to_translate', self::OPTION_NAMES );
58
59 $isCurrentOptionSection = function( $option ) {
60 $sectionPrefix = apply_filters( 'wcml_emails_section_name_prefix', 'wc_email_', $option );
61 $sectionName = str_replace( 'woocommerce_', $sectionPrefix, $option );
62 $sectionName = str_replace( '_settings', '', $sectionName );
63 $sectionName = apply_filters( 'wcml_emails_section_name_to_translate', $sectionName );
64 return WcAdminPages::isSection( $sectionName );
65 };
66
67 $currentEmailOptionName = (string) wpml_collect( $optionNames )->filter( $isCurrentOptionSection )->first();
68 return $currentEmailOptionName;
69 }
70
71 protected function getInstructionsWithRegisteredStrings( $domain, $search = '' ) {
72 return sprintf(
73 /* translators: %1$s and %2$s are opening and closing HTML link tags */
74 esc_html__( 'To translate custom WooCommerce email notifications, go to the %1$sTranslation Dashboard%2$s.', 'woocommerce-multilingual' ),
75 '<a href="' . esc_url( $this->getInstructionsLink( $domain, $search ) ) . '">',
76 '</a>'
77 );
78 }
79
80 public function translationInstructions() {
81 $optionName = $this->getCurrentEmailOptionName();
82 if ( ! $optionName ) {
83 return;
84 }
85
86 $this->pointerFactory
87 ->create( [
88 'content' => $this->getInstructions( $this->getStringDomain( $optionName ) ),
89 'selectorId' => 'wpbody-content .woocommerce table.form-table:nth-of-type(1)',
90 'method' => 'before',
91 'docLink' => WCML_Tracking_Link::getWcmlTranslateEmailsDoc(),
92 ] )->show();
93 }
94
95 protected function getTranslationControls() {
96 $translationControls = [];
97 $optionName = $this->getCurrentEmailOptionName();
98 if ( ! $optionName ) {
99 return $translationControls;
100 }
101
102 $emailSettings = $this->getEmailSettings( $optionName );
103
104 foreach ( $emailSettings as $settingKey => $settingValue ) {
105 $translationControls[] = $this->getTranslationControl(
106 $optionName,
107 $settingKey,
108 $settingValue,
109 $this->getStringDomain( $optionName ),
110 $this->getStringName( $optionName, $settingKey )
111 );
112 }
113
114 return $translationControls;
115 }
116
117 public function registerStringsOnSave() {
118 $itemsToProcess = wpml_collect( $_POST )
119 ->filter( function( $value, $key ) {
120 return substr( $key, 0, 9 ) === self::KEY_PREFIX;
121 } )
122 ->toArray();
123
124 array_walk( $itemsToProcess, function( $language, $key ) {
125 $keyParts = explode( '-', $key );
126 if ( count( $keyParts ) < 3 ) {
127 return;
128 }
129
130 list( , $optionName, $emailTextKey ) = $keyParts;
131 $domain = $this->getStringDomain( $optionName );
132 $name = $this->getStringName( $optionName, $emailTextKey );
133 $stringValue = wp_kses_post( Obj::propOr(
134 '',
135 $this->getInputName( $optionName, $emailTextKey ),
136 $_POST
137 ) );
138 if ( empty( $stringValue ) ) {
139 return;
140 }
141 $this->replaceStringAndLanguage( $stringValue, $domain, $name, $language );
142 } );
143 }
144
145 private function getEmailTextKeys() {
146 return apply_filters( 'wcml_emails_text_keys_to_translate', self::EMAIL_TEXT_KEYS );
147 }
148
149 private function getEmailSettings( $optionName ) {
150 $emailSettings = get_option( $optionName, [] );
151 if ( ! is_array( $emailSettings ) ) {
152 return [];
153 }
154
155 if ( ! isset( $emailSettings['additional_content'] ) ) {
156 $emailSettings['additional_content'] = '';
157 }
158
159 $emailTextKeys = $this->getEmailTextKeys();
160 $relevantSettings = wpml_collect( $emailSettings )
161 ->filter( function( $settingValue, $setttingKey ) use ( $emailTextKeys ) {
162 return in_array( $setttingKey, $emailTextKeys );
163 } )
164 ->toArray();
165
166 return $relevantSettings;
167 }
168
169 protected function getStringDomain( $optionName ) {
170 return 'admin_texts_' . $optionName;
171 }
172
173 protected function getStringName( $optionName, $emailTextKey ) {
174 return '[' . $optionName . ']' . $emailTextKey;
175 }
176
177 protected function getInputId( $optionName, $emailTextKey ) {
178 return str_replace( '_settings', '', $optionName ) . '_' . $emailTextKey;
179 }
180
181 protected function getLanguageSelectorId( $optionName, $settingKey ) {
182 return $optionName . '_' . $settingKey . '_' . self::LANGUAGE_SELECTOR_ID_SUFFIX;
183 }
184
185 protected function getLanguageSelectorName( $optionName, $settingKey ) {
186 return self::KEY_PREFIX . '-' . $optionName . '-' . $settingKey;
187 }
188
189 }
190