| @@ -1,12 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace WPDeveloper\BetterDocs\Core; |
| 3 | 4 | |
| 4 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | - exit; | |
| 6 | -} | |
| 7 | - | |
| 8 | - | |
| 9 | 5 | use WP_Post; |
| 10 | 6 | use WPDeveloper\BetterDocs\Utils\Base; |
| 11 | 7 | use WPDeveloper\BetterDocs\Utils\Database; |
| 12 | 8 | |
| @@ -22,51 +18,11 @@ | ||
| 22 | 18 | public function init() { |
| 23 | 19 | add_action( 'init', [ $this, 'rules' ] ); |
| 24 | 20 | add_action( 'betterdocs::settings::saved', [ $this, 'save_permalink_structure' ], 2, 3 ); |
| 25 | 21 | add_action( 'template_redirect', [ $this, 'handle_pagination_redirect' ] ); |
| 26 | - add_filter( 'term_link', [ $this, 'taxonomy_term_link' ], 10, 3 ); | |
| 27 | 22 | } |
| 28 | 23 | |
| 29 | 24 | /** |
| 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 ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | 25 | * Handle pagination redirect |
| 70 | 26 | * Redirects pagination attempts to main archive |
| 71 | 27 | */ |
| 72 | 28 | public function handle_pagination_redirect() { |
| @@ -185,13 +141,8 @@ | ||
| 185 | 141 | |
| 186 | 142 | return $structure; |
| 187 | 143 | } |
| 188 | 144 | |
| 189 | - public function hierarchy_based_slug_switch() { | |
| 190 | - $hierarchy_slug = $this->settings->get( 'enable_category_hierarchy_slugs', false ); | |
| 191 | - return $hierarchy_slug; | |
| 192 | - } | |
| 193 | - | |
| 194 | 145 | public function get_base_slug() { |
| 195 | 146 | $docs_slug = $this->settings->get( 'docs_slug', 'docs' ); |
| 196 | 147 | $docs_page = $this->settings->get( 'docs_page', 0 ); |
| 197 | 148 | $builtin_doc_page = $this->settings->get( 'builtin_doc_page', true ); |
| @@ -245,9 +196,9 @@ | ||
| 245 | 196 | function ( $carry, $item ) { |
| 246 | 197 | $carry .= ( strpos( $item, '%' ) !== false ? '([^/]+)' : $item ) . '/'; |
| 247 | 198 | return $carry; |
| 248 | 199 | }, |
| 249 | - '^' | |
| 200 | + '' | |
| 250 | 201 | ) . '?$'; |
| 251 | 202 | } |
| 252 | 203 | |
| 253 | 204 | public function make_query( $segments ) { |
| @@ -280,23 +231,17 @@ | ||
| 280 | 231 | * https://url/docs/(kb-slug)/(cat-slug)/(docs-slug) |
| 281 | 232 | * https://url/docs/(cat-slug)/(kb-slug)/(docs-slug) |
| 282 | 233 | */ |
| 283 | 234 | |
| 284 | - $base = $this->get_base_slug(); | |
| 285 | - $hierarchy_slug = $this->hierarchy_based_slug_switch(); | |
| 235 | + $base = $this->get_base_slug(); | |
| 286 | 236 | |
| 287 | 237 | // Add rule to handle pagination attempts |
| 288 | 238 | $this->add_rewrite_rule( |
| 289 | - '^' . $base . '/page/([0-9]+)/?$', | |
| 239 | + $base . '/page/([0-9]+)/?$', | |
| 290 | 240 | 'index.php?post_type=docs', |
| 291 | 241 | 'top' |
| 292 | 242 | ); |
| 293 | 243 | |
| 294 | - $base = $this->get_base_slug(); | |
| 295 | - $this->add_rewrite_rule( '^' . $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' ); | |
| 296 | - $this->add_rewrite_rule( '^' . $base . '/authors/([0-9]+)/?$', 'index.php?post_type=docs&author=$matches[1]' ); | |
| 297 | - $this->add_rewrite_rule( '^' . $base . '/authors/([0-9]+)/page/([0-9]+)/?$', 'index.php?post_type=docs&author=$matches[1]&page=$matches[2]' ); | |
| 298 | - | |
| 299 | 244 | /** |
| 300 | 245 | * This code of blocks used to determine single docs permalink. |
| 301 | 246 | */ |
| 302 | 247 | $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' ); |
| @@ -301,20 +246,8 @@ | ||
| 301 | 246 | */ |
| 302 | 247 | $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' ); |
| 303 | 248 | $_normalized_structure = $this->normalzie_doc_perma_structure( $_docs_perma_struct ); |
| 304 | 249 | |
| 305 | - if( $hierarchy_slug ) { // register hierarchy based slug rewrite rule, for single doc permalink | |
| 306 | - // When MKB or WPML is used, the actual base for single docs might differ from the generic $base | |
| 307 | - $perma_base = isset( $_normalized_structure['raw'][0] ) && strpos( $_normalized_structure['raw'][0], '%' ) === false | |
| 308 | - ? $_normalized_structure['raw'][0] | |
| 309 | - : $base; | |
| 310 | - | |
| 311 | - $this->add_rewrite_rule( '^' . $perma_base . '/(.+?)/([^/]+)(?:/([0-9]+))?/?$', 'index.php?doc_category=$matches[1]&docs=$matches[2]&post_type=docs', 'top' ); | |
| 312 | - | |
| 313 | - // Removed flush_rewrite_rules() from here. Calling flush_rewrite_rules() on the 'init' hook causes | |
| 314 | - // infinite database updates and destroys custom rules when WPML or MKB dynamically filters keys. | |
| 315 | - } | |
| 316 | - | |
| 317 | 250 | $_feed_regex = $_normalized_structure['raw']; |
| 318 | 251 | $_feed_regex[] = '(feed|rdf|rss|rss2|atom)'; |
| 319 | 252 | $_feed_group = $_normalized_structure['group']; |
| 320 | 253 | $_feed_group[] = '%feed%'; |
| @@ -325,9 +258,9 @@ | ||
| 325 | 258 | $this->make_regex( $_normalized_structure['raw'] ), |
| 326 | 259 | $this->make_query( $_normalized_structure['group'] ) |
| 327 | 260 | ); |
| 328 | 261 | |
| 329 | - $this->add_rewrite_rule( '^' . $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' ); | |
| 262 | + $this->add_rewrite_rule( $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' ); | |
| 330 | 263 | } |
| 331 | 264 | |
| 332 | 265 | public function add_rewrite_rule( $regex, $query, $after = 'top' ) { |
| 333 | 266 | add_rewrite_rule( $regex, $query, $after ); |