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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 / PaymentGateways / Settings / TranslationControls.php
woocommerce-multilingual / classes / PaymentGateways / Settings Last commit date
TranslationControls.php 1 week ago
TranslationControls.php
188 lines
1 <?php
2
3 namespace WCML\PaymentGateways\Settings;
4
5 use WCML\PaymentGateways\Strings;
6 use WCML\TranslationControls\Hooks as TranslationControlsBase;
7 use WCML\Utilities\WcAdminPages;
8 use WPML\FP\Obj;
9 use WPML\FP\Str;
10
11 class TranslationControls extends TranslationControlsBase {
12
13 public function add_hooks() {
14 parent::add_hooks();
15 if ( wpml_is_rest_request() ) {
16 $this->addRestHooks();
17 }
18 }
19
20 protected function isAdminPage() {
21 return WcAdminPages::isPaymentSettings() && WcAdminPages::hasSection();
22 }
23
24 protected function addAdminPageHooks() {
25 add_action( 'woocommerce_after_settings_checkout', [ $this, 'translationInstructions' ] );
26 add_action( 'woocommerce_after_settings_checkout', [ $this, 'translationControls' ] );
27 add_action( 'woocommerce_update_options_checkout', [ $this, 'registerStringsOnSave' ], 9 );
28 }
29
30 private function addRestHooks() {
31 add_filter( 'woocommerce_rest_prepare_payment_gateway', [ $this, 'registerStringsOnRestSave' ], 10, 3 );
32 }
33
34 private function isSubmitButtonHidden() {
35 return ! empty( $GLOBALS['hide_save_button'] );
36 }
37
38 protected function getInstructionsWithRegisteredStrings( $domain, $search = '' ) {
39 return Strings::getTranslationInstructions();
40 }
41
42 public function translationInstructions() {
43 if ( $this->isSubmitButtonHidden() ) {
44 return;
45 }
46
47 $gateway = $this->getCurrentGateway();
48 if ( is_null( $gateway ) ) {
49 return;
50 }
51
52 $this->pointerFactory
53 ->create( [
54 'content' => $this->getInstructions( Strings::TRANSLATION_DOMAIN, Strings::getStringName( $gateway->id, '' ) ),
55 'selectorId' => 'wpbody-content .woocommerce table.form-table:nth-of-type(1)',
56 'method' => 'before',
57 ] )->show();
58 }
59
60 private function getCurrentGateway() {
61 static $currentGateway = false;
62 if ( false !== $currentGateway ) {
63 return $currentGateway;
64 }
65
66 $gatewaysManager = \WC_Payment_Gateways::instance();
67 $gateways = $gatewaysManager->payment_gateways();
68
69 $isCurrentGatewaySection = function( $gateway ) {
70 return WcAdminPages::isSection( $gateway->id );
71 };
72
73 $currentGateway = wpml_collect( $gateways )->filter( $isCurrentGatewaySection )->first();
74 return $currentGateway;
75 }
76
77 protected function getTranslationControls() {
78 $translationControls = [];
79
80 if ( $this->isSubmitButtonHidden() ) {
81 return $translationControls;
82 }
83
84 $gateway = $this->getCurrentGateway();
85 if ( is_null( $gateway ) ) {
86 return $translationControls;
87 }
88
89 $textKeys = $this->getGatewayTextKeys();
90 foreach ( $textKeys as $textKey ) {
91 if ( '' === $textKey ) {
92 $settingValue = $gateway->description;
93 } elseif ( isset( $gateway->settings[ $textKey ] ) ) {
94 $settingValue = $gateway->settings[ $textKey ];
95 } else {
96 $settingValue = Obj::prop( $textKey, $gateway );
97 }
98
99 $gatewayKey = $gateway->plugin_id . $gateway->id;
100 $translationControls[] = $this->getTranslationControl(
101 $gatewayKey,
102 $textKey,
103 $settingValue,
104 Strings::TRANSLATION_DOMAIN,
105 $this->getStringName( $gateway->id, $textKey )
106 );
107 }
108
109 return $translationControls;
110 }
111
112 public function registerStringsOnSave() {
113 $gateway = $this->getCurrentGateway();
114 if ( is_null( $gateway ) ) {
115 return;
116 }
117
118 $gatewayKey = $gateway->plugin_id . $gateway->id;
119 $languageSelectorNamePrefix = $this->getLanguageSelectorNamePrefix( $gateway->plugin_id . $gateway->id );
120 $itemsToProcess = wpml_collect( $_POST )
121 ->filter( function( $value, $key ) use ( $languageSelectorNamePrefix ) {
122 return Str::startsWith( $languageSelectorNamePrefix, $key );
123 } )
124 ->toArray();
125
126 array_walk( $itemsToProcess, function( $language, $key ) use ( $gateway, $gatewayKey, $languageSelectorNamePrefix ) {
127 $textKey = str_replace( $languageSelectorNamePrefix, '', $key );
128 $name = $this->getStringName( $gateway->id, $textKey );
129 $stringValue = wp_kses_post( Obj::propOr(
130 '',
131 $this->getInputName( $gatewayKey, $textKey ),
132 $_POST
133 ) );
134 if ( empty( $stringValue ) ) {
135 return;
136 }
137 $this->replaceStringAndLanguage( $stringValue, Strings::TRANSLATION_DOMAIN, $name, $language );
138 } );
139 }
140
141 public function registerStringsOnRestSave( $response, $gateway, $request ) {
142 $requestMethod = $request->get_method();
143 if ( ! in_array( $requestMethod, [ 'POST', 'PUT', 'PATCH' ], true ) ) {
144 return $response;
145 }
146
147 $textKeys = $this->getGatewayTextKeys();
148 foreach ( $textKeys as $textKey ) {
149 $name = $this->getStringName( $gateway->id, $textKey );
150 if ( '' === $textKey ) {
151 $stringValue = $gateway->description;
152 } elseif ( isset( $gateway->settings[ $textKey ] ) ) {
153 $stringValue = $gateway->settings[ $textKey ];
154 } else {
155 $stringValue = Obj::prop( $textKey, $gateway );
156 }
157
158 $this->replaceStringAndLanguage( $stringValue, Strings::TRANSLATION_DOMAIN, $name );
159 }
160 return $response;
161 }
162
163 private function getGatewayTextKeys() {
164 return apply_filters( 'wcml_gateway_text_keys_to_translate', Strings::TRANSLATABLE_SETTINGS );
165 }
166
167 protected function getStringName( $gatewayId, $textKey ) {
168 return Strings::getStringName( $gatewayId, $textKey );
169 }
170
171 protected function getInputId( $gatewayKey, $textKey ) {
172 return $gatewayKey . '_' . $textKey;
173 }
174
175 protected function getLanguageSelectorId( $gatewayKey, $textKey ) {
176 return $gatewayKey . '_settings_' . $textKey . '_' . self::LANGUAGE_SELECTOR_ID_SUFFIX;
177 }
178
179 private function getLanguageSelectorNamePrefix( $gatewayKey ) {
180 return self::KEY_PREFIX . '-' . $gatewayKey . '_settings-';
181 }
182
183 protected function getLanguageSelectorName( $gatewayKey, $textKey ) {
184 return $this->getLanguageSelectorNamePrefix( $gatewayKey ) . $textKey;
185 }
186
187 }
188