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 ); diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 758d5019..ffe41c83 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -1077,7 +1077,7 @@ function wpsc_supercache_dir_for_url( $url ) { } 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; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase global $cache_enabled, $super_cache_enabled; static $saved_supercache_dir = array(); @@ -1090,14 +1090,18 @@ 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 ( ! is_string( $permalink ) ) { + $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 ) ) { /* - * 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 ); @@ -1112,10 +1116,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 { @@ -1139,7 +1142,7 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $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 ) && isset( $_SERVER['REQUEST_URI'] ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages, true ) ) { $dir = ABSPATH . $uri . '/'; } $dir = str_replace( '..', '', str_replace( '//', '/', $dir ) );