TranslationControls.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Permalinks\Settings; |
| 4 | |
| 5 | use WCML\Permalinks\Strings; |
| 6 | use WCML\TranslationControls\Hooks as TranslationControlsBase; |
| 7 | use WCML\Utilities\AdminUrl; |
| 8 | use WCML\Utilities\WpAdminPages; |
| 9 | |
| 10 | class TranslationControls extends TranslationControlsBase { |
| 11 | |
| 12 | const PERMALINK_BASES = [ |
| 13 | 'tag_base' => 'product_tag', |
| 14 | 'category_base' => 'product_cat', |
| 15 | 'attribute_base' => 'attribute', |
| 16 | 'product_base' => 'product', |
| 17 | ]; |
| 18 | |
| 19 | const INSTRUCTIONS_FOR_PRODUCTS = 'products'; |
| 20 | const INSTRUCTIONS_FOR_PRODUCT_TAXONOMIES = 'product-taxonomies'; |
| 21 | |
| 22 | protected function addAdminPageHooks() { |
| 23 | add_action( 'admin_footer', [ $this, 'translationInstructions' ] ); |
| 24 | add_action( 'admin_footer', [ $this, 'translationControls' ] ); |
| 25 | $this->registerStringsOnSave(); |
| 26 | } |
| 27 | |
| 28 | protected function isAdminPage() { |
| 29 | return WpAdminPages::isPermalinksSettings(); |
| 30 | } |
| 31 | |
| 32 | protected function getInstructionsLink( $domain, $search = '' ) { |
| 33 | return AdminUrl::getStoreURLTab(); |
| 34 | } |
| 35 | |
| 36 | private function getInstructionsVariation( $variation ) { |
| 37 | if ( ! $this->hasStringsInDomain( Strings::TRANSLATION_DOMAIN ) ) { |
| 38 | return $this->getInstructionsWithoutRegisteredStrings( Strings::TRANSLATION_DOMAIN ); |
| 39 | } |
| 40 | switch ( $variation ) { |
| 41 | case self::INSTRUCTIONS_FOR_PRODUCT_TAXONOMIES: |
| 42 | return sprintf( |
| 43 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 44 | esc_html__( 'To translate permalinks for product taxonomies, go to %1$sWPML Multilingual & Multicurrency for WooCommerce → Store URLs%2$s.', 'woocommerce-multilingual' ), |
| 45 | '<a href="' . esc_url( $this->getInstructionsLink( Strings::TRANSLATION_DOMAIN ) ) . '">', |
| 46 | '</a>' |
| 47 | ); |
| 48 | case self::INSTRUCTIONS_FOR_PRODUCTS: |
| 49 | default: |
| 50 | return sprintf( |
| 51 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 52 | esc_html__( 'To translate product permalinks, go to %1$sWPML Multilingual & Multicurrency for WooCommerce → Store URLs%2$s.', 'woocommerce-multilingual' ), |
| 53 | '<a href="' . esc_url( $this->getInstructionsLink( Strings::TRANSLATION_DOMAIN ) ) . '">', |
| 54 | '</a>' |
| 55 | ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function translationInstructions() { |
| 60 | $this->pointerFactory |
| 61 | ->create( [ |
| 62 | 'anchor' => esc_html__( 'How to translate permalinks for product taxonomies', 'woocommerce-multilingual' ), |
| 63 | 'content' => $this->getInstructionsVariation( self::INSTRUCTIONS_FOR_PRODUCT_TAXONOMIES ), |
| 64 | 'selectorId' => 'wpbody-content h2:nth-of-type(2) + p', |
| 65 | ] )->show(); |
| 66 | |
| 67 | $this->pointerFactory |
| 68 | ->create( [ |
| 69 | 'anchor' => esc_html__( 'How to translate product permalinks', 'woocommerce-multilingual' ), |
| 70 | 'content' => $this->getInstructionsVariation( self::INSTRUCTIONS_FOR_PRODUCTS ), |
| 71 | 'selectorId' => 'wpbody-content table.form-table.wc-permalink-structure', |
| 72 | 'method' => 'before', |
| 73 | ] )->show(); |
| 74 | } |
| 75 | |
| 76 | protected function getTranslationControls() { |
| 77 | $translationControls = []; |
| 78 | $permalink_options = get_option( 'woocommerce_permalinks' ); |
| 79 | |
| 80 | foreach ( self::PERMALINK_BASES as $baseKey => $base ) { |
| 81 | |
| 82 | switch ( $base ) { |
| 83 | case 'product_tag': |
| 84 | $value = ! empty( $permalink_options['tag_base'] ) |
| 85 | ? $permalink_options['tag_base'] |
| 86 | : Strings::DEFAULT_PRODUCT_TAG_BASE; |
| 87 | break; |
| 88 | case 'product_cat': |
| 89 | $value = ! empty( $permalink_options['category_base'] ) |
| 90 | ? $permalink_options['category_base'] |
| 91 | : Strings::DEFAULT_PRODUCT_CATEGORY_BASE; |
| 92 | break; |
| 93 | case 'attribute': |
| 94 | $value = ! empty( $permalink_options['attribute_base'] ) |
| 95 | ? $permalink_options['attribute_base'] |
| 96 | : Strings::DEFAULT_PRODUCT_ATTRIBUTE_BASE; |
| 97 | break; |
| 98 | case 'product': |
| 99 | $value = ! empty( $permalink_options['product_base'] ) |
| 100 | ? trim( $permalink_options['product_base'], '/' ) |
| 101 | : _x( Strings::DEFAULT_PRODUCT_BASE, 'default-slug', 'woocommerce' ); |
| 102 | break; |
| 103 | default: |
| 104 | $value = ''; |
| 105 | } |
| 106 | |
| 107 | $translationControls[] = $this->getTranslationControl( |
| 108 | '', |
| 109 | $baseKey, |
| 110 | trim( $value, '/' ), |
| 111 | Strings::TRANSLATION_DOMAIN, |
| 112 | $this->getStringName( '', $base ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | return $translationControls; |
| 117 | } |
| 118 | |
| 119 | public function registerStringsOnSave() { |
| 120 | add_filter( 'pre_update_option_woocommerce_permalinks', [ $this, 'registerStringsOnPreUpdate' ] ); |
| 121 | } |
| 122 | |
| 123 | public function registerStringsOnPreUpdate( $wcPermalinks ) { |
| 124 | return $this->wcmlStrings->getUrlTranslation()->register_product_and_taxonomy_bases( $wcPermalinks ); |
| 125 | } |
| 126 | |
| 127 | protected function getStringName( $dummyContext, $base ) { |
| 128 | return Strings::getStringName( $base ); |
| 129 | } |
| 130 | |
| 131 | protected function getInputName( $dummyContext, $baseKey ) { |
| 132 | switch ( $baseKey ) { |
| 133 | case 'tag_base': |
| 134 | return 'woocommerce_product_tag_slug'; |
| 135 | case 'category_base': |
| 136 | return 'woocommerce_product_category_slug'; |
| 137 | case 'attribute_base': |
| 138 | return 'woocommerce_product_attribute_slug'; |
| 139 | case 'product_base': |
| 140 | return 'product_permalink_structure'; |
| 141 | } |
| 142 | |
| 143 | return ''; |
| 144 | } |
| 145 | |
| 146 | protected function getInputId( $dummyContext, $baseKey ) { |
| 147 | return ''; |
| 148 | } |
| 149 | |
| 150 | protected function getLanguageSelectorId( $dummyContext, $baseKey ) { |
| 151 | return $baseKey . '_' . self::LANGUAGE_SELECTOR_ID_SUFFIX; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | protected function getLanguageSelectorName( $dummy, $baseKey ) { |
| 157 | return $baseKey . '_language'; |
| 158 | } |
| 159 | |
| 160 | } |
| 161 |