Skip to content

Commit

Permalink
Merge pull request #652 from google/fix/651-reference-url-trailing-slash
Browse files Browse the repository at this point in the history
Ensure reference_site_url is always without a trailing slash
  • Loading branch information
felixarntz authored Oct 18, 2019
2 parents 510250c + a10b6f0 commit 423ff84
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions includes/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function get_reference_site_url() {
$site_url = $orig_site_url;
}

return $site_url;
return untrailingslashit( $site_url );
}

/**
Expand All @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/Core/REST_API/REST_Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/Modules/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/integration/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down

0 comments on commit 423ff84

Please sign in to comment.