Skip to content

Commit 8684b83

Browse files
Fix the_content link replacements (#2104)
* Fix the_content link replacements * simplify the code * fix formatting * add fullstops * fix host_url port * fix empty space * fix slash issue * Updated e2e tests. Delete directories only if they exist for e2e tests. * Updated e2e tests for example tutorial to use npm degit As per issue #2089 there is a cache issue with using npx create app for the example tutorial on Github's side. Not sure why it is only failing for this branch though. * Fixed npm degit issue * Fixing npm degit. * Install e2e dependencies * Fixing npm ci issue for e2e directory * Updated e2e example test to only run when there is a change within the example directory * minor improvements * fix linting * adding port tests * Create long-laws-doubt.md --------- Co-authored-by: Colin Murphy <[email protected]>
1 parent 32e502d commit 8684b83

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

.changeset/long-laws-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@faustwp/wordpress-plugin": patch
3+
---
4+
5+
Fixed issue in content_replacement when site_url() contains port

.github/workflows/e2e-next-faustwp-example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: E2E Test Packages
22

33
on:
44
pull_request:
5-
paths-ignore:
6-
- '**/*.md'
5+
paths:
6+
- 'examples/next/**'
77

88
jobs:
99
e2e-test-faustwp-getting-started-example:

plugins/faustwp/includes/replacement/callbacks.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
add_filter( 'the_content', __NAMESPACE__ . '\\content_replacement' );
2828
add_filter( 'wpgraphql_content_blocks_resolver_content', __NAMESPACE__ . '\\content_replacement' );
2929
/**
30-
* Callback for WordPress 'the_content' filter.
30+
* Replace the URLs in the post content basesd on two settings if enabled:
31+
* 1. Enable Post and Category URL rewrites - rewrite the WordPress internal URL in the content with the Front-end URL
32+
* 1.1 If NOT enabled - use the WordPress URL
33+
* 2. Use the WordPress domain for media URLs in post content - use WordPress URL for media files (images,csv,pdf)
34+
* 2.1 If NOT enabled - use front-end url
3135
*
3236
* @param ?string $content The post content.
3337
*
@@ -40,6 +44,7 @@ function content_replacement( ?string $content ) {
4044
}
4145
$replace_content_urls = domain_replacement_enabled();
4246
$replace_media_urls = ! use_wp_domain_for_media();
47+
4348
if ( ! $replace_content_urls && ! $replace_media_urls ) {
4449
return $content;
4550
}
@@ -50,16 +55,46 @@ function content_replacement( ?string $content ) {
5055
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
5156
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
5257
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';
53-
if ( $replace_content_urls && $replace_media_urls ) {
54-
return str_replace( $wp_site_urls, $frontend_uri, $content );
58+
59+
/* If "Enable Post and Category URL" is enabled, use front-end URL for internal URLs, but not for media links */
60+
61+
if ( $replace_content_urls ) {
62+
63+
// Look for href links.
64+
preg_match_all( '#href="([^"]+)"#i', $content, $href_links );
65+
if ( is_array( $href_links ) && ! empty( $href_links[1] ) ) {
66+
foreach ( $href_links[1] as $i => $url ) {
67+
// skip media links.
68+
$is_media = array_filter( $wp_media_urls, fn( $media ) => strpos( $url, $media ) === 0 );
69+
if ( $is_media ) {
70+
continue;
71+
}
72+
73+
$is_wp_url = array_filter( $wp_site_urls, fn( $base ) => strpos( $url, $base ) === 0 );
74+
if ( ! $is_wp_url ) {
75+
continue;
76+
}
77+
78+
// get relative link.
79+
$relative = str_replace( reset( $is_wp_url ), '', $url );
80+
$updated = 'href="' . $frontend_uri . $relative . '"';
81+
82+
$original = $href_links[0][ $i ];
83+
84+
if ( $original ) {
85+
$content = str_replace( $original, $updated, $content );
86+
}
87+
}
88+
}
5589
}
90+
91+
/* If "Use the WordPress domain for media URLs in post content" is NOT enabled, use front-end URL for media URLs */
92+
5693
if ( $replace_media_urls ) {
57-
return str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
94+
$content = str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
5895
}
59-
$site_urls_pattern = implode( '|', array_map( 'preg_quote', $wp_site_urls ) );
60-
$pattern = '#(' . $site_urls_pattern . ')(?!' . $relative_upload_url . '(\/|$))#';
6196

62-
return preg_replace( $pattern, $frontend_uri, $content );
97+
return $content;
6398
}
6499

65100
/**

plugins/faustwp/includes/replacement/functions.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,19 @@ function is_wp_link_ajax_request(): bool {
136136

137137

138138
/**
139-
* Get all site URLs for each possible HTTP protocol
139+
* Get all site URLs for each possible HTTP protocol.
140140
*
141141
* @param string $site_url The site url.
142142
*
143143
* @return array<string> An array of site urls.
144144
*/
145145
function faustwp_get_wp_site_urls( string $site_url ): array {
146146

147-
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
148-
147+
$host_url_parse = wp_parse_url( $site_url );
148+
$host_url = $host_url_parse['host'] ?? '';
149+
if ( ! empty( $host_url_parse['port'] ) ) {
150+
$host_url .= ':' . $host_url_parse['port'];
151+
}
149152
$is_https = strpos( $site_url, 'https://' ) === 0;
150153

151154
return apply_filters(
@@ -158,6 +161,7 @@ function faustwp_get_wp_site_urls( string $site_url ): array {
158161
);
159162
}
160163

164+
161165
/**
162166
* Get all media urls based off the available site urls
163167
*
@@ -180,18 +184,16 @@ function faustwp_get_wp_media_urls( array $wp_site_urls, string $relative_upload
180184
/**
181185
* Gets the relative wp-content upload URL.
182186
*
183-
* @param array<string> $site_urls An array of site URLs.
184-
* @param string $upload_url An array of site URLs.
187+
* @param array<string>|string $site_urls An array of site URLs.
188+
* @param string $upload_url An array of site URLs.
185189
*
186190
* @return string The relative upload URL.
187191
*/
188192
function faustwp_get_relative_upload_url( array $site_urls, string $upload_url = '' ): string {
189-
190193
foreach ( $site_urls as $site_url ) {
191194
if ( strpos( $upload_url, $site_url ) === 0 ) {
192195
return (string) str_replace( $site_url, '', $upload_url );
193196
}
194197
}
195-
196198
return '';
197199
}

plugins/faustwp/tests/integration/ReplacementCallbacksTests.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,29 @@ public function test_image_sourceset_replacement_for_different_http_protocols_wi
642642
$this->assertSame( $expected, image_source_srcset_replacement( $sources ) );
643643
}
644644

645+
public function test_for_media_when_replacement_is_disabled_and_url_contains_port() {
646+
647+
$frontend_uri = 'http://localhost:3000';
648+
649+
update_option('siteurl', 'http://localhost:8881');
650+
$site_url = site_url();
651+
652+
faustwp_update_setting( 'frontend_uri', $frontend_uri );
653+
faustwp_update_setting( 'enable_image_source', '0' );
654+
655+
$this->assertSame( $frontend_uri . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp', content_replacement( $site_url . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp' ) );
656+
}
657+
public function test_URL_replacement_is_disabled_and_url_contains_port() {
658+
659+
$frontend_uri = 'http://localhost:3000';
660+
661+
update_option('siteurl', 'http://localhost:8881');
662+
$site_url = site_url();
663+
664+
faustwp_update_setting( 'frontend_uri', $frontend_uri );
665+
faustwp_update_setting( 'enable_rewrites', '0' );
666+
667+
$this->assertSame( $frontend_uri . '/sample-page', content_replacement( $site_url . '/sample-page' ) );
668+
}
669+
645670
}

0 commit comments

Comments
 (0)