Skip to content

Commit

Permalink
Replace slugs qts_page_request cache with transient (#1182)
Browse files Browse the repository at this point in the history
The WP cache is not appropriated for very short term storage.
Moreover a wrong value could have been stored between different requests.
Replace the cache call with a transient expiring after 30s.
Delete the transient right after reading it.
Rename it to `qtranslate_slugs_matched_page`.
  • Loading branch information
herrvigg authored Jun 6, 2022
1 parent ee9b964 commit 9fea88a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions modules/slugs/src/slugs-class-slugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ public function query_vars( $query_vars ) {
if ( preg_match( "#^$match#", $request_match, $matches ) || preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[(\d+)\]/', $query, $varmatch ) ) {
// this is a verbose page match, lets check to be sure about it
if ( ! $page_foundid = $this->get_page_by_path( $matches[ $varmatch[1] ] ) ) {
if ( ! $page_found = $this->get_page_by_path( $matches[ $varmatch[1] ] ) ) {
continue;
} else {
wp_cache_set( 'qts_page_request', $page_foundid ); // caching query :)
set_transient( 'qtranslate_slugs_matched_page', $page_found, 30 ); // Store the matched page for `filter_request`.
}
}
// Got a match.
Expand Down Expand Up @@ -374,17 +374,17 @@ function filter_request( $query ) {

// -> page
elseif ( isset( $query['pagename'] ) || isset( $query['page_id'] ) ):
$page = wp_cache_get( 'qts_page_request' );
if ( ! $page ) {
$page = get_transient( 'qtranslate_slugs_matched_page' );
if ( $page === false ) {
$page = isset( $query['page_id'] ) ? get_post( $query['page_id'] ) : $this->get_page_by_path( $query['pagename'] );
}
delete_transient( 'qtranslate_slugs_matched_page' );
if ( ! $page ) {
return $query;
}
$id = $page->ID;
$cache_array = array( $page );
update_post_caches( $cache_array, 'page' ); // caching query :)
wp_cache_delete( 'qts_page_request' );
update_post_caches( $cache_array, 'page' );
$query['pagename'] = get_page_uri( $page );
$function = 'get_page_link';

Expand Down

0 comments on commit 9fea88a

Please sign in to comment.