From 5f02665867f56209ae4fbd6169e7a625e4c10cf5 Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Sun, 13 Oct 2019 18:30:27 +0300 Subject: [PATCH 1/4] ensure reference_site_url is always without slash --- includes/Context.php | 2 +- tests/phpunit/integration/ContextTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/Context.php b/includes/Context.php index e0deb20504c..5ff58523b0a 100644 --- a/includes/Context.php +++ b/includes/Context.php @@ -156,7 +156,7 @@ public function get_reference_site_url() { $site_url = $orig_site_url; } - return $site_url; + return untrailingslashit( $site_url ); } /** diff --git a/tests/phpunit/integration/ContextTest.php b/tests/phpunit/integration/ContextTest.php index a034116a991..4a87dcf706b 100644 --- a/tests/phpunit/integration/ContextTest.php +++ b/tests/phpunit/integration/ContextTest.php @@ -108,6 +108,15 @@ public function test_get_reference_site_url() { $this->assertEquals( 'https://test.com', $context->get_reference_site_url() ); remove_filter( 'googlesitekit_site_url', $other_url_filter ); + $trailing_slash_url = function () { + return 'https://test.com/'; + }; + + // It always returns a URL without a trailing slash. + add_filter( 'googlesitekit_site_url', $trailing_slash_url ); + $this->assertEquals( 'https://test.com', $context->get_reference_site_url() ); + remove_filter( 'googlesitekit_site_url', $trailing_slash_url ); + // If the filtered value returns an empty value, it falls back to the home_url. add_filter( 'googlesitekit_site_url', '__return_empty_string' ); $this->assertEquals( $home_url, $context->get_reference_site_url() ); From baa451e197c58f58b5177623e691d77dc25058e4 Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Fri, 18 Oct 2019 10:22:33 +0300 Subject: [PATCH 2/4] remove redundant untrailingslashes home_url is untrailingslashed by core so both are now consistently without trailing slash --- includes/Context.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Context.php b/includes/Context.php index 5ff58523b0a..5066da0e73d 100644 --- a/includes/Context.php +++ b/includes/Context.php @@ -169,8 +169,8 @@ public function get_reference_site_url() { * @return string|false The reference permalink URL or false if post does not exist. */ public function get_reference_permalink( $post = 0 ) { - $reference_site_url = untrailingslashit( $this->get_reference_site_url() ); - $orig_site_url = untrailingslashit( home_url() ); + $reference_site_url = $this->get_reference_site_url(); + $orig_site_url = home_url(); // Gets post object. On front area we need to use get_queried_object to get the current post object. if ( ! $post ) { From b544439ccf6cdbcedcbe0d2777af5a946be21f4a Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Fri, 18 Oct 2019 10:23:42 +0300 Subject: [PATCH 3/4] remove unnecessary trailingslashes in str_replace both are now consistently without a trailing slash so no additional slash-handling is necessary here --- includes/Core/REST_API/REST_Routes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Core/REST_API/REST_Routes.php b/includes/Core/REST_API/REST_Routes.php index 9aaca087d64..c9d9e232ef1 100644 --- a/includes/Core/REST_API/REST_Routes.php +++ b/includes/Core/REST_API/REST_Routes.php @@ -556,8 +556,8 @@ function( $dataset ) use ( $module ) { if ( filter_var( $query, FILTER_VALIDATE_URL ) ) { // Translate public/alternate reference URLs to local if different. $query_url = str_replace( - trailingslashit( $this->context->get_reference_site_url() ), - trailingslashit( home_url() ), + $this->context->get_reference_site_url(), + home_url(), $query ); $post_id = url_to_postid( $query_url ); From a10b6f0f8d72f1ea8b2b4cd09d10b390af604e57 Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Fri, 18 Oct 2019 10:24:00 +0300 Subject: [PATCH 4/4] remove redundant untrailingslashit --- includes/Modules/Analytics.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Modules/Analytics.php b/includes/Modules/Analytics.php index 2c06b858eba..528c231ee24 100644 --- a/includes/Modules/Analytics.php +++ b/includes/Modules/Analytics.php @@ -1005,7 +1005,7 @@ function ( \Google_Service_Analytics_Account $account ) { } $found_property = new \Google_Service_Analytics_Webproperty(); - $current_url = untrailingslashit( $this->context->get_reference_site_url() ); + $current_url = $this->context->get_reference_site_url(); // If requested for a specific property, only match by property ID. if ( ! empty( $data['existingPropertyId'] ) ) {