| @@ -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 | |
| @@ -205,9 +201,9 @@ | ||
| 205 | 201 | function ( $carry, $item ) { |
| 206 | 202 | $carry .= ( strpos( $item, '%' ) !== false ? '([^/]+)' : $item ) . '/'; |
| 207 | 203 | return $carry; |
| 208 | 204 | }, |
| 209 | - '^' | |
| 205 | + '' | |
| 210 | 206 | ) . '?$'; |
| 211 | 207 | } |
| 212 | 208 | |
| 213 | 209 | public function make_query( $segments ) { |
| @@ -245,18 +241,28 @@ | ||
| 245 | 241 | $hierarchy_slug = $this->hierarchy_based_slug_switch(); |
| 246 | 242 | |
| 247 | 243 | // Add rule to handle pagination attempts |
| 248 | 244 | $this->add_rewrite_rule( |
| 249 | - '^' . $base . '/page/([0-9]+)/?$', | |
| 245 | + $base . '/page/([0-9]+)/?$', | |
| 250 | 246 | 'index.php?post_type=docs', |
| 251 | 247 | 'top' |
| 252 | 248 | ); |
| 253 | 249 | |
| 254 | 250 | $base = $this->get_base_slug(); |
| 255 | - $this->add_rewrite_rule( '^' . $base . '/(feed|rdf|rss|rss2|atom)/?$', 'index.php?post_type=docs&feed=$matches[1]' ); | |
| 256 | - $this->add_rewrite_rule( '^' . $base . '/authors/([0-9]+)/?$', 'index.php?post_type=docs&author=$matches[1]' ); | |
| 257 | - $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]' ); | |
| 258 | 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 | + | |
| 259 | 265 | /** |
| 260 | 266 | * This code of blocks used to determine single docs permalink. |
| 261 | 267 | */ |
| 262 | 268 | $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' ); |
| @@ -261,20 +267,8 @@ | ||
| 261 | 267 | */ |
| 262 | 268 | $_docs_perma_struct = betterdocs()->settings->get( 'permalink_structure', 'docs' ); |
| 263 | 269 | $_normalized_structure = $this->normalzie_doc_perma_structure( $_docs_perma_struct ); |
| 264 | 270 | |
| 265 | - if( $hierarchy_slug ) { // register hierarchy based slug rewrite rule, for single doc permalink | |
| 266 | - // When MKB or WPML is used, the actual base for single docs might differ from the generic $base | |
| 267 | - $perma_base = isset( $_normalized_structure['raw'][0] ) && strpos( $_normalized_structure['raw'][0], '%' ) === false | |
| 268 | - ? $_normalized_structure['raw'][0] | |
| 269 | - : $base; | |
| 270 | - | |
| 271 | - $this->add_rewrite_rule( '^' . $perma_base . '/(.+?)/([^/]+)(?:/([0-9]+))?/?$', 'index.php?doc_category=$matches[1]&docs=$matches[2]&post_type=docs', 'top' ); | |
| 272 | - | |
| 273 | - // Removed flush_rewrite_rules() from here. Calling flush_rewrite_rules() on the 'init' hook causes | |
| 274 | - // infinite database updates and destroys custom rules when WPML or MKB dynamically filters keys. | |
| 275 | - } | |
| 276 | - | |
| 277 | 271 | $_feed_regex = $_normalized_structure['raw']; |
| 278 | 272 | $_feed_regex[] = '(feed|rdf|rss|rss2|atom)'; |
| 279 | 273 | $_feed_group = $_normalized_structure['group']; |
| 280 | 274 | $_feed_group[] = '%feed%'; |
| @@ -285,9 +279,9 @@ | ||
| 285 | 279 | $this->make_regex( $_normalized_structure['raw'] ), |
| 286 | 280 | $this->make_query( $_normalized_structure['group'] ) |
| 287 | 281 | ); |
| 288 | 282 | |
| 289 | - $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]' ); | |
| 290 | 284 | } |
| 291 | 285 | |
| 292 | 286 | public function add_rewrite_rule( $regex, $query, $after = 'top' ) { |
| 293 | 287 | add_rewrite_rule( $regex, $query, $after ); |