From 58593e110fa85df0a740be992be26590a011653f Mon Sep 17 00:00:00 2001 From: Giovanni Cascione <43221199+spleen1981@users.noreply.github.com> Date: Sun, 15 May 2022 17:43:43 +0200 Subject: [PATCH 1/4] QTS: fix conflict pages/posts in filter_request --- .../slugs/includes/class-qtranslate-slug.php | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index b65128b7..28821f27 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -353,8 +353,10 @@ function filter_request( $query ) { global $q_config; global $wp; - if ( isset( $query['error'] ) && isset( $wp->matched_query ) ) { - unset( $query['error'] ); + if ( isset( $wp->matched_query ) ) { + if ( isset( $query['error'] ) ){ + unset( $query['error'] ); + } $query = array_merge( wp_parse_args( $wp->matched_query ), $query ); } @@ -370,6 +372,9 @@ function filter_request( $query ) { // -> page elseif ( isset( $query['pagename'] ) || isset( $query['page_id'] ) ): + if ( isset( $query['name'] ) ) { + unset($query['name']); + } $page = wp_cache_get( 'qts_page_request' ); if ( ! $page ) { $page = isset( $query['page_id'] ) ? get_post( $query['page_id'] ) : $this->get_page_by_path( $query['pagename'] ); @@ -384,18 +389,6 @@ function filter_request( $query ) { $query['pagename'] = get_page_uri( $page ); $function = 'get_page_link'; - // -> post - elseif ( isset( $query['name'] ) || isset( $query['p'] ) ): - $post = isset( $query['p'] ) ? get_post( $query['p'] ) : $this->get_page_by_path( $query['name'], OBJECT, 'post' ); - if ( ! $post ) { - return $query; - } - $query['name'] = $post->post_name; - $id = $post->ID; - $cache_array = array( $post ); - update_post_caches( $cache_array ); - $function = 'get_permalink'; - // -> category elseif ( ( isset( $query['category_name'] ) || isset( $query['cat'] ) ) ): if ( isset( $query['category_name'] ) ) { @@ -427,6 +420,7 @@ function filter_request( $query ) { $function = 'get_tag_link'; else: + // -> custom post type foreach ( $this->get_public_post_types() as $post_type ) { if ( array_key_exists( $post_type->name, $query ) && ! in_array( $post_type->name, array( @@ -437,7 +431,6 @@ function filter_request( $query ) { break; } } - if ( isset( $query['post_type'] ) ) { if ( count( $query ) == 1 ) { $function = 'get_post_type_archive_link'; @@ -471,6 +464,19 @@ function filter_request( $query ) { $function = 'get_term_link'; } endforeach; + + // -> post + if ( ! $function && ( isset($query['name']) || isset($query['p'] ) ) ) { + $post = isset($query['p']) ? get_post($query['p']) : $this->get_page_by_path($query['name'], OBJECT, 'post'); + if (!$post) { + return $query; + } + $query['name'] = $post->post_name; + $id = $post->ID; + $cache_array = array($post); + update_post_caches($cache_array); + $function = 'get_permalink'; + } endif; if ( isset( $function ) ) { From 3e954f6f42cec947ad8dd31f84e5038777dd50e3 Mon Sep 17 00:00:00 2001 From: Giovanni Cascione Date: Mon, 16 May 2022 10:44:24 +0200 Subject: [PATCH 2/4] QTS: add comments for filter_request --- modules/slugs/includes/class-qtranslate-slug.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index 28821f27..3a16a910 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -353,6 +353,11 @@ function filter_request( $query ) { global $q_config; global $wp; + /* As $wp->matched_query filters the query including slugs mudule custom rewrite rules, it provides all necessary data to reach the intended resource. + * However discarding completely $query arg and using only $wp->matched_query would result in losing possible custom query vars. + * Hence $query and $wp->matched_query are merged and unneeded/conflictual keys (e.g. 'error') from $query are removed. + */ + if ( isset( $wp->matched_query ) ) { if ( isset( $query['error'] ) ){ unset( $query['error'] ); @@ -372,6 +377,9 @@ function filter_request( $query ) { // -> page elseif ( isset( $query['pagename'] ) || isset( $query['page_id'] ) ): + /* 'name' key from passed $query var is removed if 'pagename' key is present in $wp->matched_query. + * Both keys causes conflict between posts and pages as 'name' is used for posts, resulting in 404 error. + */ if ( isset( $query['name'] ) ) { unset($query['name']); } @@ -421,6 +429,8 @@ function filter_request( $query ) { else: + // If none of the conditions above are matched, specific tests to identify custom post types and taxonomies are performed here. + // -> custom post type foreach ( $this->get_public_post_types() as $post_type ) { if ( array_key_exists( $post_type->name, $query ) && ! in_array( $post_type->name, array( @@ -465,6 +475,12 @@ function filter_request( $query ) { } endforeach; + /* As 'name' key is present also at least both for pages and custom post types, this condition alone cannot be used to identify uniquely the posts. + * For pages and custom post types specific tests can be and are performed earlier but no additional specific condition seems to be applicable for posts. + * Given the above, the following test needs to be placed after the custom post types one, and is it positive if 'name' key is there and no other match is found earlier. + * If a specific condition was found to uniquely identify posts, this block should be placed in the main if block. + */ + // -> post if ( ! $function && ( isset($query['name']) || isset($query['p'] ) ) ) { $post = isset($query['p']) ? get_post($query['p']) : $this->get_page_by_path($query['name'], OBJECT, 'post'); From e747c3d94389d8ec88a70a38fbd8a020c65ce552 Mon Sep 17 00:00:00 2001 From: Giovanni Cascione Date: Mon, 16 May 2022 22:38:57 +0200 Subject: [PATCH 3/4] QTS: fix /%category%/%postname%/ filter_request queries --- modules/slugs/includes/class-qtranslate-slug.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index 3a16a910..60d15d17 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -398,7 +398,8 @@ function filter_request( $query ) { $function = 'get_page_link'; // -> category - elseif ( ( isset( $query['category_name'] ) || isset( $query['cat'] ) ) ): + // If 'name' key is defined, query is relevant to a post with a /%category%/%postname%/ permalink structure and will be captured later. + elseif ( ( ( isset( $query['category_name'] ) || isset( $query['cat'] ) ) && ! isset($query['name'] ) ) ): if ( isset( $query['category_name'] ) ) { $term_slug = $this->get_last_slash( empty( $query['category_name'] ) ? $wp->request : $query['category_name'] ); $term = $this->get_term_by( 'slug', $term_slug, 'category' ); From 192f2d369d34ad227d0bacc2575f6eb2892bbe66 Mon Sep 17 00:00:00 2001 From: Giovanni Cascione Date: Tue, 17 May 2022 01:02:28 +0200 Subject: [PATCH 4/4] QTS: fix extra rewrite rules to handle utf8 chars --- modules/slugs/includes/class-qtranslate-slug.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index 60d15d17..5b0541b8 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -942,12 +942,12 @@ private function generate_extra_rules( $name = false ) { $struct = $wp_rewrite->extra_permastructs[ $name ]; if ( is_array( $struct ) ) { if ( count( $struct ) == 2 ) { - $rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%", $struct[1] ); + $rules = $wp_rewrite->generate_rewrite_rules( "/".rawurldecode($base)."/%".rawurldecode($name)."%", $struct[1] ); } else { - $rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%", $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); + $rules = $wp_rewrite->generate_rewrite_rules( "/".rawurldecode($base)."/%".rawurldecode($name)."%", $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); } } else { - $rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%" ); + $rules = $wp_rewrite->generate_rewrite_rules( "/".rawurldecode($base)."/%".rawurldecode($name)."%" ); } $wp_rewrite->rules = array_merge( $rules, $wp_rewrite->rules ); endif;