Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MERL-1228: Fix issue with rewriting links to front end #1603

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-lamps-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Fixed issue where file links were rewritten to the front-end domain when they should not have been.
8 changes: 6 additions & 2 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ function content_replacement( $content ) {
$replacement = '/';
}

$content = str_replace( "href=\"{$site_url}", "href=\"{$replacement}", $content );
if ( ! is_image_source_replacement_enabled() ) {
$upload_dir = str_replace( $site_url, '', wp_upload_dir()['baseurl'] );
$content = preg_replace( "#href=\"{$site_url}(?!{$upload_dir})#", "href=\"{$replacement}", $content );
} else {
$content = str_replace( "href=\"{$site_url}", "href=\"{$replacement}", $content );
}

return str_replace( 'href="//', 'href="/', $content );
}

add_filter( 'the_content', __NAMESPACE__ . '\\image_source_replacement' );
/**
* Callback for WordPress 'the_content' filter to replace paths to media.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,33 @@ public function test_content_replacement_does_not_filter_content_when_content_re
public function test_content_replacement_filters_content_when_content_replacement_enabled() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
add_filter( 'faustwp_domain_replacement_enabled', '__return_true' );
$this->assertSame( '<a href="http://moo">moo</a>', content_replacement( '<a href="http://example.org">moo</a>' ) );
$this->assertSame(
'<a href="http://moo">moo</a>',
content_replacement( '<a href="http://example.org">moo</a>' )
);

$this->assertSame(
'<a href="http://moo/?p=1">Hi</a>',
content_replacement( '<a href="http://example.org/?p=1">Hi</a>' )
);

$this->assertSame(
'<a href="http://moo/posts/hello-world">Hello World</a>',
content_replacement( '<a href="http://example.org/posts/hello-world">Hello World</a>' )
);

faustwp_update_setting( 'frontend_uri', null );
remove_filter( 'faustwp_domain_replacement_enabled', '__return_true' );
}

public function test_content_replacement_does_not_filter_wp_upload_dir_link() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
add_filter( 'faustwp_domain_replacement_enabled', '__return_true' );
$content = '<a href="http://example.org/wp-content/uploads/file.pdf">Download</a>. http://example.org/wp-content/uploads/file.pdf';
$this->assertSame(
$content,
content_replacement( $content )
);
faustwp_update_setting( 'frontend_uri', null );
remove_filter( 'faustwp_domain_replacement_enabled', '__return_true' );
}
Expand All @@ -98,6 +124,9 @@ public function test_image_source_replacement_filters_content_when_image_replace
# Do replace exact domain match
$this->assertSame( '<img src="http://example.org/image.png">', image_source_replacement( '<img src="http://foo.co/image.png">' ) );
$this->assertSame( '<img src="http://example.org/image.png">', image_source_replacement( '<img src="/image.png">' ) );

// Ensure unrelated domains are left alone.
$this->assertSame( '<img src="http://fake/image.png">', image_source_replacement( '<img src="http://fake/image.png">' ) );
faustwp_update_setting( 'frontend_uri', null );
faustwp_update_setting( 'enable_image_source', '0' );
}
Expand Down
Loading