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 |