| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | +namespace WPDeveloper\BetterDocs\Core; | |
| 2 | 3 | |
| 3 | -namespace WPDeveloper\BetterDocs\Core; | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 4 | 7 | |
| 8 | + | |
| 5 | 9 | use WP_Post; |
| 6 | 10 | use WPDeveloper\BetterDocs\Utils\Base; |
| 7 | 11 | use WPDeveloper\BetterDocs\Utils\Database; |
| 8 | 12 | |
| @@ -18,8 +22,48 @@ | ||
| 18 | 22 | public function init() { |
| 19 | 23 | add_action( 'init', [ $this, 'rules' ] ); |
| 20 | 24 | add_action( 'betterdocs::settings::saved', [ $this, 'save_permalink_structure' ], 2, 3 ); |
| 21 | 25 | add_action( 'template_redirect', [ $this, 'handle_pagination_redirect' ] ); |
| 26 | + add_filter( 'term_link', [ $this, 'taxonomy_term_link' ], 10, 3 ); | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Swap a BetterDocs taxonomy base slug for its WPML-translated value in term links. | |
| 31 | + * | |
| 32 | + * doc_tag / doc_category are registered with the default-language slug, so | |
| 33 | + * WordPress' core get_term_link() always builds `/docs-tag/{slug}/` etc. When | |
| 34 | + * WPML translates the "URL <taxonomy> tax slug" string, rewrite the base segment | |
| 35 | + * to the active language's slug so term links match the translated archive URL. | |
| 36 | + * No-op for other taxonomies, when the slug isn't translated, or when the base | |
| 37 | + * segment isn't present (e.g. MKB category permalinks that omit it). | |
| 38 | + * | |
| 39 | + * @param string $termlink | |
| 40 | + * @param \WP_Term $term | |
| 41 | + * @param string $taxonomy | |
| 42 | + * @return string | |
| 43 | + */ | |
| 44 | + public function taxonomy_term_link( $termlink, $term, $taxonomy ) { | |
| 45 | + if ( ! is_string( $termlink ) ) { | |
| 46 | + return $termlink; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $slug_map = [ | |
| 50 | + 'doc_tag' => trim( $this->settings->get( 'tag_slug', 'docs-tag' ), '/' ), | |
| 51 | + 'doc_category' => trim( $this->settings->get( 'category_slug', 'docs-category' ), '/' ), | |
| 52 | + ]; | |
| 53 | + | |
| 54 | + if ( ! isset( $slug_map[ $taxonomy ] ) || $slug_map[ $taxonomy ] === '' ) { | |
| 55 | + return $termlink; | |
| 56 | + } | |
| 57 | + | |
| 58 | + $default = $slug_map[ $taxonomy ]; | |
| 59 | + $translated = \WPDeveloper\BetterDocs\Utils\Helper::wpml_translated_tax_slug( $taxonomy, $default ); | |
| 60 | + | |
| 61 | + if ( $translated === $default ) { | |
| 62 | + return $termlink; | |
| 63 | + } | |
| 64 | + | |
| 65 | + return preg_replace( '#/' . preg_quote( $default, '#' ) . '/#', '/' . $translated . '/', $termlink, 1 ); | |
| 22 | 66 | } |
| 23 | 67 | |
| 24 | 68 | /** |
| 25 | 69 | * Handle pagination redirect |