Skip to content

Commit 6a79fe0

Browse files
authored
Fix conflict with page rewrite rule. (#34)
* Fix coflict with page rewrite rule.
1 parent 6e75e36 commit 6a79fe0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

includes/class-wp-rewrite-rules.php

+22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function rewrite_rules_array_filter( $rules ) {
6262

6363
foreach ( $rules as $key => $rule ) {
6464
if ( preg_match_all( '/' . self::FIELD_REGEXP . '/', $key, $key_matches ) ) {
65+
if ( $this->conflicts_with_page_permalink( $key, $key_matches ) ) {
66+
continue;
67+
}
68+
6569
$key_new = preg_replace(
6670
'/' . self::FIELD_REGEXP . '/',
6771
'([^/]+)',
@@ -140,4 +144,22 @@ private function build_rule_on_field_match( $key_matches, $i ) {
140144

141145
return http_build_query( $rule_rewrite );
142146
}
147+
148+
/**
149+
* Determines if rewrite rule contains only custom field tag and conflicts with pages permalink.
150+
*
151+
* @param string $key Rewrite rule.
152+
* @param array $key_matches All found matches.
153+
*
154+
* @access private
155+
* @return boolean Whether rewrite rule conflicts with page rule.
156+
*/
157+
private function conflicts_with_page_permalink( $key, $key_matches ) {
158+
if ( count( $key_matches[ self::FIELD_REGEXP_MAIN_GROUP ] ) == 1 ) {
159+
// If the rewrite rule is only %field_some%/?$ it will be in conflict with pages rewrite rule.
160+
return preg_match( '/^' . self::FIELD_REGEXP . '\/\?\$$/', $key );
161+
}
162+
163+
return false;
164+
}
143165
}

test/integration/suites/MetaKeyPermalinkStructure/PageWithMetaKey.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function test_generates_permalink_to_page_not_using_meta_key() {
4141
/**
4242
* Test case.
4343
*/
44-
function IGNORE_test_go_to_page_not_using_meta_key_permalink_structure() {
44+
function test_go_to_page_not_using_meta_key_permalink_structure() {
4545
// given.
4646
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
4747

0 commit comments

Comments
 (0)