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 |