From 65fb755faf27d5bddcaeb34a7448c074b75b81e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20Todorovi=C4=87?= <15647032+stodorovic@users.noreply.github.com> Date: Tue, 7 Apr 2026 17:10:20 -0500 Subject: [PATCH 1/7] fix: use home_url() instead of site_url() in get_current_url_supercache_dir --- wp-cache-phase2.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index d3c5ce3d..751f7d86 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -822,7 +822,7 @@ function get_supercache_dir( $blog_id = 0 ) { return trailingslashit( apply_filters( 'wp_super_cache_supercachedir', $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', str_replace( 'https://', '', $home ) ) ) ) ) ) ); } function get_current_url_supercache_dir( $post_id = 0 ) { - global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST, $wp_cache_home_path; + global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST; static $saved_supercache_dir = array(); if ( isset( $saved_supercache_dir[ $post_id ] ) ) { @@ -831,14 +831,14 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $DONOTREMEMBER = 0; if ( $post_id != 0 ) { - $site_url = site_url(); + $home_url = home_url(); $permalink = get_permalink( $post_id ); - if ( ! str_contains( $permalink, $site_url ) ) { + if ( ! str_contains( $permalink, $home_url ) ) { /* - * Sometimes site_url doesn't return the siteurl. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made + * Sometimes home_url doesn't return the home url. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made */ $DONOTREMEMBER = 1; - wp_cache_debug( "get_current_url_supercache_dir: WARNING! site_url ($site_url) not found in permalink ($permalink).", 1 ); + wp_cache_debug( "get_current_url_supercache_dir: WARNING! home_url ($home_url) not found in permalink ($permalink).", 1 ); if ( preg_match( '`^(https?:)?//([^/]+)(/.*)?$`i', $permalink, $matches ) ) { if ( $WPSC_HTTP_HOST != $matches[2] ) { wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink).", 1 ); @@ -853,10 +853,9 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $uri = ''; } } else { - $uri = str_replace( $site_url, '', $permalink ); - $home_path = $wp_cache_home_path ?? ''; - if ( $home_path !== '' && ! str_starts_with( $uri, $home_path ) ) { - $uri = rtrim( $home_path, '/' ) . $uri; + $uri = wp_parse_url( $permalink, PHP_URL_PATH ); + if ( ! is_string( $uri ) ) { + $uri = ''; } } } else { @@ -882,7 +881,7 @@ function ( $matches ) { $dir = do_cacheaction( 'supercache_dir', $dir ); } $dir = $cache_path . 'supercache/' . $dir . '/'; - if ( is_array( $cached_direct_pages ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages ) ) { + if ( is_array( $cached_direct_pages ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages, true ) ) { $dir = ABSPATH . $uri . '/'; } $dir = str_replace( '..', '', str_replace( '//', '/', $dir ) ); From a4eed5ec964d225bd28951f8996fc904d87197e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20Todorovi=C4=87?= <15647032+stodorovic@users.noreply.github.com> Date: Tue, 7 Apr 2026 17:13:17 -0500 Subject: [PATCH 2/7] fix: split isset() to single argument per call --- plugins/multisite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/multisite.php b/plugins/multisite.php index 11071752..86943268 100644 --- a/plugins/multisite.php +++ b/plugins/multisite.php @@ -22,7 +22,7 @@ function wp_super_cache_blogs_field( $name, $blog_id ) { $blog_id = (int) $blog_id; - if ( isset( $_GET['id'], $_GET['action'], $_GET['_wpnonce'] ) + if ( isset( $_GET['id'] ) && isset( $_GET['action'] ) && isset( $_GET['_wpnonce'] ) && $blog_id === filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT ) && wp_verify_nonce( $_GET['_wpnonce'], 'wp-cache' . $blog_id ) ) { From 0934babd4e71bfdd6a6ff3cb9aeb601d3af7c34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20Todorovi=C4=87?= <15647032+stodorovic@users.noreply.github.com> Date: Tue, 7 Apr 2026 17:14:37 -0500 Subject: [PATCH 3/7] fix: address PHPCS errors in get_current_url_supercache_dir --- wp-cache-phase2.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 751f7d86..29cf1b0a 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -822,7 +822,7 @@ function get_supercache_dir( $blog_id = 0 ) { return trailingslashit( apply_filters( 'wp_super_cache_supercachedir', $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', str_replace( 'https://', '', $home ) ) ) ) ) ) ); } function get_current_url_supercache_dir( $post_id = 0 ) { - global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST; + global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase static $saved_supercache_dir = array(); if ( isset( $saved_supercache_dir[ $post_id ] ) ) { @@ -881,7 +881,7 @@ function ( $matches ) { $dir = do_cacheaction( 'supercache_dir', $dir ); } $dir = $cache_path . 'supercache/' . $dir . '/'; - if ( is_array( $cached_direct_pages ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages, true ) ) { + if ( is_array( $cached_direct_pages ) && isset( $_SERVER['REQUEST_URI'] ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages, true ) ) { $dir = ABSPATH . $uri . '/'; } $dir = str_replace( '..', '', str_replace( '//', '/', $dir ) ); From 52fa56ccf09d831ed06813c8404b24ab085bfaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20Todorovi=C4=87?= <15647032+stodorovic@users.noreply.github.com> Date: Tue, 7 Apr 2026 17:15:36 -0500 Subject: [PATCH 4/7] fix: guard against get_permalink() returning false --- wp-cache-phase2.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 29cf1b0a..e3007420 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -833,7 +833,11 @@ function get_current_url_supercache_dir( $post_id = 0 ) { if ( $post_id != 0 ) { $home_url = home_url(); $permalink = get_permalink( $post_id ); - if ( ! str_contains( $permalink, $home_url ) ) { + if ( ! is_string( $permalink ) ) { + $DONOTREMEMBER = 1; + wp_cache_debug( "get_current_url_supercache_dir: WARNING! get_permalink($post_id) did not return a valid string. Using front page.", 1 ); + $uri = ''; + } elseif ( ! str_contains( $permalink, $home_url ) ) { /* * Sometimes home_url doesn't return the home url. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made */ From fe6d0b2c582413bb63020c7671eea0dc665ffee2 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Tue, 7 Apr 2026 17:19:52 -0500 Subject: [PATCH 5/7] fix: add phpcs ignore for pre-existing DONOTREMEMBER variable --- wp-cache-phase2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index e3007420..a0bbe103 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -834,7 +834,7 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $home_url = home_url(); $permalink = get_permalink( $post_id ); if ( ! is_string( $permalink ) ) { - $DONOTREMEMBER = 1; + $DONOTREMEMBER = 1; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase wp_cache_debug( "get_current_url_supercache_dir: WARNING! get_permalink($post_id) did not return a valid string. Using front page.", 1 ); $uri = ''; } elseif ( ! str_contains( $permalink, $home_url ) ) { From e38f9e7ece497bec4b919e88d7501e0325bd2d4a Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 11 Nov 2026 15:15:15 -0600 Subject: [PATCH 6/7] Leave multisite.php unchanged in this PR The chained isset() rewrite behaved the same as the original and overlaps the multisite refactor in PR 1022. --- plugins/multisite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/multisite.php b/plugins/multisite.php index 86943268..11071752 100644 --- a/plugins/multisite.php +++ b/plugins/multisite.php @@ -22,7 +22,7 @@ function wp_super_cache_blogs_field( $name, $blog_id ) { $blog_id = (int) $blog_id; - if ( isset( $_GET['id'] ) && isset( $_GET['action'] ) && isset( $_GET['_wpnonce'] ) + if ( isset( $_GET['id'], $_GET['action'], $_GET['_wpnonce'] ) && $blog_id === filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT ) && wp_verify_nonce( $_GET['_wpnonce'], 'wp-cache' . $blog_id ) ) { From aee8521399097c3f0b6edbe6c3f6c2fe20e6eeea Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 11 Nov 2026 15:15:57 -0600 Subject: [PATCH 7/7] Drive supercache dir tests through the home URL get_current_url_supercache_dir() now takes the path from the permalink, so the two tests that set $wp_cache_home_path no longer exercise it. Filter home and siteurl instead, which also covers a subsite whose path differs from the main site's. --- .../php/integration/SupercacheDirCaseTest.php | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/tests/php/integration/SupercacheDirCaseTest.php b/tests/php/integration/SupercacheDirCaseTest.php index 5977f591..ccc7a46e 100644 --- a/tests/php/integration/SupercacheDirCaseTest.php +++ b/tests/php/integration/SupercacheDirCaseTest.php @@ -110,6 +110,32 @@ private function publish( $title ) { return array( $post_id, (string) wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH ) ); } + /** + * Return get_current_url_supercache_dir() for a new post while home and siteurl are $url. + * + * Filtered at priority 20 because WP_HOME and WP_SITEURL are defined in the test + * config, and core puts those back on the options at priority 10. + * + * @param string $url Value for both the home and siteurl options. + * @return string Supercache directory for the post. + */ + private function dir_for_new_post_with_home( $url ) { + $filter = static function () use ( $url ) { + return $url; + }; + add_filter( 'option_home', $filter, 20 ); + add_filter( 'option_siteurl', $filter, 20 ); + + try { + list( $post_id ) = $this->publish( 'Hello There' ); + + return get_current_url_supercache_dir( $post_id ); + } finally { + remove_filter( 'option_home', $filter, 20 ); + remove_filter( 'option_siteurl', $filter, 20 ); + } + } + /** * An overlong path switches caching off for the request, so nothing is ever * written to the placeholder directory and no two long URLs can share it. @@ -194,18 +220,15 @@ public function test_percent_escapes_are_uppercased_on_the_post_id_branch() { * the function returns a short absolute path inside the cache directory that * nothing writes to, having switched caching off for the request. * - * Driven through $wp_cache_home_path rather than a long slug, because + * Driven through a long home path rather than a long slug, because * post_name is capped well below the limit. Uses a fresh post ID, so it does * not compete for the memoised key 0. */ public function test_a_path_too_long_for_the_filesystem_is_not_returned() { - $GLOBALS['wp_cache_home_path'] = '/' . str_repeat( 'a', PHP_MAXPATHLEN ) . '/'; $GLOBALS['cache_enabled'] = true; $GLOBALS['super_cache_enabled'] = true; - list( $post_id ) = $this->publish( 'Hello There' ); - - $dir = get_current_url_supercache_dir( $post_id ); + $dir = $this->dir_for_new_post_with_home( 'http://' . WP_TESTS_DOMAIN . '/' . str_repeat( 'a', PHP_MAXPATHLEN ) ); $this->assertTrue( $GLOBALS['cache_enabled'], 'The deletion branch must not switch caching off for the admin request that is saving the post.' ); $this->assertTrue( $GLOBALS['super_cache_enabled'] ); @@ -229,9 +252,7 @@ public function test_a_path_too_long_for_the_filesystem_is_not_returned() { * normally uses, so an install whose home option carries uppercase had those * requests name a different directory than its web requests did. * - * Nothing here memoises: a home option that site_url() does not appear in - * sends the function down its DONOTREMEMBER branch, so key 0 stays free for - * the test above. + * Uses a fresh post ID, so key 0 stays free for the test above. */ public function test_host_from_the_home_option_is_lowercased() { $GLOBALS['WPSC_HTTP_HOST'] = ''; @@ -257,17 +278,12 @@ public function test_host_from_the_home_option_is_lowercased() { } /** - * The rest of the path is lowercased on the post ID branch too. An install in - * a subdirectory with a capital in it used to produce /Blog/... here while the - * directory on disk was /blog/..., because inc/htaccess.php lowercases the - * home root for the same convention. + * The home path is kept, and lowercased, on the post ID branch. A multisite + * subsite at /Blog/ shares $wp_cache_home_path with the main site, so the path + * has to come from the permalink, and inc/htaccess.php lowercases the home root. */ public function test_path_is_lowercased_on_the_post_id_branch() { - $GLOBALS['wp_cache_home_path'] = '/Blog/'; - - list( $post_id ) = $this->publish( 'Hello There' ); - - $dir = get_current_url_supercache_dir( $post_id ); + $dir = $this->dir_for_new_post_with_home( 'http://' . WP_TESTS_DOMAIN . '/Blog' ); $this->assertStringContainsString( '/blog/hello-there/', $dir ); $this->assertStringNotContainsString( '/Blog/', $dir );