From 77daf355ca87fabfeca2670dce076bb2df47caa8 Mon Sep 17 00:00:00 2001 From: John Parris Date: Wed, 11 Oct 2023 08:15:24 -0400 Subject: [PATCH 1/4] fix: do not rewrite links to files in post content when image replacement is disabled --- plugins/faustwp/includes/replacement/callbacks.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php index 82a271baa..f2abe190e 100644 --- a/plugins/faustwp/includes/replacement/callbacks.php +++ b/plugins/faustwp/includes/replacement/callbacks.php @@ -41,12 +41,17 @@ 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' ); +// add_filter( 'the_content', __NAMESPACE__ . '\\image_source_replacement' ); /** * Callback for WordPress 'the_content' filter to replace paths to media. * From 866d057ec9feefce350f8efb4b04864973806e87 Mon Sep 17 00:00:00 2001 From: John Parris Date: Wed, 11 Oct 2023 08:15:54 -0400 Subject: [PATCH 2/4] test: confirm behavior of image and URL replacement --- .../integration/ReplacementCallbacksTests.php | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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' ); } From fd7b42c50271457c8345df191997bbdad6237b28 Mon Sep 17 00:00:00 2001 From: John Parris Date: Wed, 11 Oct 2023 08:20:59 -0400 Subject: [PATCH 3/4] chore: add changeset --- .changeset/plenty-lamps-confess.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/plenty-lamps-confess.md 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. From 729f6eeb9ff7c25f2d975db14f609c58484660d7 Mon Sep 17 00:00:00 2001 From: John Parris Date: Thu, 12 Oct 2023 17:01:48 -0400 Subject: [PATCH 4/4] chore: coding standards --- plugins/faustwp/includes/replacement/callbacks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php index f2abe190e..2574382f9 100644 --- a/plugins/faustwp/includes/replacement/callbacks.php +++ b/plugins/faustwp/includes/replacement/callbacks.php @@ -51,7 +51,6 @@ function content_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. *