diff --git a/.changeset/plenty-lamps-confess.md b/.changeset/plenty-lamps-confess.md new file mode 100644 index 000000000..31cc83f27 --- /dev/null +++ b/.changeset/plenty-lamps-confess.md @@ -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. diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php index 82a271baa..2574382f9 100644 --- a/plugins/faustwp/includes/replacement/callbacks.php +++ b/plugins/faustwp/includes/replacement/callbacks.php @@ -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. * diff --git a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php index 15cbb5161..c8b97cbe3 100644 --- a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php +++ b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php @@ -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( 'moo', content_replacement( 'moo' ) ); + $this->assertSame( + 'moo', + content_replacement( 'moo' ) + ); + + $this->assertSame( + 'Hi', + content_replacement( 'Hi' ) + ); + + $this->assertSame( + 'Hello World', + content_replacement( 'Hello World' ) + ); + + 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 = 'Download. 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' ); } @@ -98,6 +124,9 @@ public function test_image_source_replacement_filters_content_when_image_replace # Do replace exact domain match $this->assertSame( '', image_source_replacement( '' ) ); $this->assertSame( '', image_source_replacement( '' ) ); + + // Ensure unrelated domains are left alone. + $this->assertSame( '', image_source_replacement( '' ) ); faustwp_update_setting( 'frontend_uri', null ); faustwp_update_setting( 'enable_image_source', '0' ); }