PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.6
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.6
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
← All changes | includes/Core/Rewrite.php +17 -63 4.8.24.2.6 View file →
@@ -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() {
@@ -245,9 +201,9 @@
245 201 function ( $carry, $item ) {
246 202 $carry .= ( strpos( $item, '%' ) !== false ? '([^/]+)' : $item ) . '/';
247 203 return $carry;
248 204 },
249 - '^'
205 + ''
250 206 ) . '?$';
251 207 }
252 208
253 209 public function make_query( $segments ) {
@@ -285,18 +241,28 @@
285 241 $hierarchy_slug = $this->hierarchy_based_slug_switch();
286 242
287 243 // Add rule to handle pagination attempts
288 244 $this->add_rewrite_rule(
289 - '^' . $base . '/page/([0-9]+)/?$',
245 + $base . '/page/([0-9]+)/?$',
290 246 'index.php?post_type=docs',
291 247 'top'
292 248 );
293 249
294 250 $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]' );
251 + $this->add_rewrite_rule( $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' );
252 + $this->add_rewrite_rule( $base . '/authors/([0-9]+)/?$', 'index.php?post_type=docs&author=$matches[1]' );
253 + $this->add_rewrite_rule( $base . '/authors/([0-9]+)/page/([0-9]+)/?$', 'index.php?post_type=docs&author=$matches[1]&page=$matches[2]' );
298 254
255 + if( $hierarchy_slug ) { // reigster hierarchy based slug rewrite rule, for single doc permalink
256 + $this->add_rewrite_rule( $base . '/(.+?)/([^/]+)(?:/([0-9]+))?/?$', 'index.php?doc_category=$matches[1]&docs=$matches[2]&post_type=docs' );
257 +
258 + $rewrite_rules = get_option('rewrite_rules');
259 +
260 + if( ! isset( $rewrite_rules[$base . '/(.+?)/([^/]+)(?:/([0-9]+))?/?$'] ) ) { // if this nested rule is not set then flush the rules
261 + flush_rewrite_rules();
262 + }
263 + }
264 +
299 265 /**
300 266 * This code of blocks used to determine single docs permalink.
301 267 */
302 268 $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' );
@@ -301,20 +267,8 @@
301 267 */
302 268 $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' );
303 269 $_normalized_structure = $this->normalzie_doc_perma_structure( $_docs_perma_struct );
304 270
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 271 $_feed_regex = $_normalized_structure['raw'];
318 272 $_feed_regex[] = '(feed|rdf|rss|rss2|atom)';
319 273 $_feed_group = $_normalized_structure['group'];
320 274 $_feed_group[] = '%feed%';
@@ -325,9 +279,9 @@
325 279 $this->make_regex( $_normalized_structure['raw'] ),
326 280 $this->make_query( $_normalized_structure['group'] )
327 281 );
328 282
329 - $this->add_rewrite_rule( '^' . $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' );
283 + $this->add_rewrite_rule( $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' );
330 284 }
331 285
332 286 public function add_rewrite_rule( $regex, $query, $after = 'top' ) {
333 287 add_rewrite_rule( $regex, $query, $after );