Skip to content

Commit 33dda2e

Browse files
Bug: Fixes issues for content replacement for the site and media URLs (#2014)
* Fix for #1986. Fixes various issues with content replacement callback and also updates tests to use WP 6.6 and 6.7 Fixes various issues with using content replacement for the site URL. * Fixed issues with PHPCS * Updated PR to replace srcset URL for different HTTP Protocols * Added changeset * Refactoring based off @whoami-pwd feedback. Reduced some of the nesting for cotent replacement. * Fixes for linting * Update plugins/faustwp/includes/replacement/callbacks.php Co-authored-by: Alex K. <[email protected]> * Added fixes as per code review from @whoami-pwd * Refactoring some of the image srcset replacement. * Small fix for PHPCS for a missing fullstop * Refactoring some of the media urls with @whoami-pwd * Fixes * Small refactor of parameters with @whoami-pwd --------- Co-authored-by: Alex K. <[email protected]>
1 parent d57b88a commit 33dda2e

File tree

7 files changed

+395
-62
lines changed

7 files changed

+395
-62
lines changed

.changeset/tidy-toys-bake.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@faustwp/wordpress-plugin': minor
3+
---
4+
5+
### Fixes
6+
7+
- Fixes various issues with content replacement callback functions and replacing the site_url and media_urls
8+
- Fixed an issue with content replacement when media replacement was disabled and rewrites enabled, it was overwriting and updating the media URL to the frontend URL rather than leaving it as the original site URL
9+
10+
11+
### Added
12+
13+
- Added 6.6 and 6.7 to Github Actions
14+
- Added 2 new filters for `faustwp_get_wp_site_urls` and `faustwp_get_wp_site_media_urls` to allow users add/remove/edit site and media URLS for the content replacement.

.github/workflows/unit-test-plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-22.04
1212
strategy:
1313
matrix:
14-
wordpress: [ '6.5', '6.4', '6.3', '6.2', '6.1' ]
14+
wordpress: [ '6.7', '6.6', '6.5', '6.4', '6.3', '6.2', '6.1' ]
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ process.yml
1111
!.env.local.sample
1212
build/
1313
faustjs.code-workspace
14+
plugins/faustwp/.docker/plugins/akismet/
15+
plugins/faustwp/.docker/plugins/hello.php
16+
plugins/faustwp/.docker/plugins/index.php
1417

1518
# Ignore the WordPress source where used by various development environments
1619
wordpress/

plugins/faustwp/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
links:
1212
- db
1313
environment:
14-
WP_VERSION: ${WP_VERSION:-6.5}
14+
WP_VERSION: ${WP_VERSION:-6.7}
1515
WORDPRESS_DB_HOST: db
1616
WORDPRESS_DB_NAME: wordpress
1717
WORDPRESS_DB_USER: root

plugins/faustwp/includes/replacement/callbacks.php

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,40 @@
3131
*
3232
* @return string The post content.
3333
*/
34-
function content_replacement( $content ) {
35-
$use_wp_domain_for_permalinks = ! domain_replacement_enabled();
36-
$use_wp_domain_for_media = use_wp_domain_for_media();
34+
function content_replacement( string $content ): string {
3735

38-
if ( $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
39-
return $content;
40-
}
41-
42-
$replacement = faustwp_get_setting( 'frontend_uri' );
43-
if ( ! $replacement ) {
44-
$replacement = '/';
36+
if ( ! $content ) {
37+
return '';
4538
}
4639

47-
$site_url = site_url();
48-
$media_dir = str_replace( $site_url, '', wp_upload_dir()['baseurl'] );
49-
$media_url = $site_url . $media_dir;
40+
$replace_content_urls = domain_replacement_enabled();
41+
$replace_media_urls = ! use_wp_domain_for_media();
5042

51-
if ( $use_wp_domain_for_permalinks && ! $use_wp_domain_for_media ) {
52-
$content = str_replace( $media_url, $replacement . $media_dir, $content );
43+
if ( ! $replace_content_urls && ! $replace_media_urls ) {
5344
return $content;
5445
}
5546

56-
if ( ! $use_wp_domain_for_permalinks && ! $use_wp_domain_for_media ) {
57-
$content = str_replace( $site_url, $replacement, $content );
47+
$wp_site_urls = faustwp_get_wp_site_urls( site_url() );
48+
if ( empty( $wp_site_urls ) ) {
5849
return $content;
5950
}
6051

61-
if ( ! $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
62-
$content = preg_replace( "#{$site_url}(?!{$media_dir})#", "{$replacement}", $content );
63-
return $content;
52+
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
53+
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
54+
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';
55+
56+
if ( $replace_content_urls && $replace_media_urls ) {
57+
return str_replace( $wp_site_urls, $frontend_uri, $content );
58+
}
59+
60+
if ( $replace_media_urls ) {
61+
return str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
6462
}
6563

66-
return $content;
64+
$site_urls_pattern = implode( '|', array_map( 'preg_quote', $wp_site_urls ) );
65+
$pattern = '#(' . $site_urls_pattern . ')(?!' . $relative_upload_url . '(\/|$))#';
66+
67+
return preg_replace( $pattern, $frontend_uri, $content );
6768
}
6869

6970
/**
@@ -86,49 +87,58 @@ function image_source_replacement( $content ) {
8687
"#src=\"{$frontend_uri}/#",
8788
'#src="/#',
8889
);
90+
8991
return preg_replace( $patterns, "src=\"{$site_url}/", $content );
9092
}
9193

9294
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_source_srcset_replacement' );
9395
/**
94-
* Callback for WordPress 'the_content' filter to replace paths to media.
96+
* Callback for WordPress 'wp_calculate_image_srcset' filter to replace paths when generating a srcset
9597
*
96-
* @param array $sources One or more arrays of source data to include in the 'srcset'.
98+
* @link https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/
9799
*
98-
* @return string One or more arrays of source data.
100+
* @param array<string> $sources One or more arrays of source data to include in the 'srcset'.
101+
*
102+
* @return array One or more arrays of source data.
99103
*/
100104
function image_source_srcset_replacement( $sources ) {
101-
$use_wp_domain_for_media = use_wp_domain_for_media();
102-
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
103-
$site_url = site_url();
104105

105-
/**
106-
* For urls with no domain or the frontend domain, replace with the WP site_url.
107-
* This was the default replacement pattern until Faust 1.2, at which point this
108-
* was adjusted to correct replacement bugs.
109-
*/
110-
$patterns = array(
111-
"#^{$site_url}/#",
106+
if ( ! is_array( $sources ) || empty( $sources ) ) {
107+
return $sources;
108+
}
109+
110+
$wp_site_urls = faustwp_get_wp_site_urls( site_url() );
111+
if ( empty( $wp_site_urls ) ) {
112+
return $sources;
113+
}
114+
115+
$replace_media_urls = ! use_wp_domain_for_media();
116+
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
117+
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
118+
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
119+
$site_url = site_url() . '/';
120+
121+
$wp_media_site_url = $frontend_uri . $relative_upload_url;
122+
$patterns = array(
123+
"#^{$frontend_uri}/#",
112124
'#^/#',
113125
);
114126

115-
$replacement = $frontend_uri;
116-
117127
/**
118-
* If using WP domain for media and a frontend URL is encountered, rewrite it to WP URL.
128+
* Update each source with the correct replacement URL
119129
*/
120-
if ( $use_wp_domain_for_media ) {
121-
$patterns = array(
122-
"#^{$frontend_uri}/#",
123-
'#^/#',
124-
);
125-
$replacement = $site_url;
126-
}
127-
128-
if ( is_array( $sources ) ) {
129-
foreach ( $sources as $width => $source ) {
130-
$sources[ $width ]['url'] = preg_replace( $patterns, "$replacement/", $source['url'] );
130+
foreach ( $sources as $width => $source ) {
131+
$url = $source['url'];
132+
133+
if ( $replace_media_urls ) {
134+
$sources[ $width ]['url'] = ( strpos( $url, $relative_upload_url ) === 0 )
135+
? $frontend_uri . $url
136+
: str_replace( $wp_media_urls, $wp_media_site_url, $source['url'] );
137+
continue;
131138
}
139+
140+
// We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url.
141+
$sources[ $width ]['url'] = preg_replace( $patterns, $site_url, $source['url'] );
132142
}
133143

134144
return $sources;
@@ -240,7 +250,15 @@ function post_preview_link( $link, $post ) {
240250
*/
241251
function post_link( $link ) {
242252
global $pagenow;
243-
$target_pages = array( 'admin-ajax.php', 'index.php', 'edit.php', 'post.php', 'post-new.php', 'upload.php', 'media-new.php' );
253+
$target_pages = array(
254+
'admin-ajax.php',
255+
'index.php',
256+
'edit.php',
257+
'post.php',
258+
'post-new.php',
259+
'upload.php',
260+
'media-new.php',
261+
);
244262

245263
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `is_ajax_generate_permalink_request()` and `is_wp_link_ajax_request()`.
246264
if ( empty( $_POST ) && 'post-new.php' === $pagenow ) {
@@ -251,7 +269,7 @@ function post_link( $link ) {
251269
if ( in_array( $pagenow, $target_pages, true )
252270
&& is_ajax_generate_permalink_request()
253271
) {
254-
return $link;
272+
return $link;
255273
}
256274

257275
if (

plugins/faustwp/includes/replacement/functions.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,70 @@ function is_ajax_generate_permalink_request(): bool {
126126
*/
127127
function is_wp_link_ajax_request(): bool {
128128
return ( wp_doing_ajax()
129-
&& ! empty( $_POST['_ajax_linking_nonce'] )
130-
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
131-
&& ! empty( $_POST['action'] )
132-
&& 'wp-link-ajax' === $_POST['action'] );
129+
&& ! empty( $_POST['_ajax_linking_nonce'] )
130+
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
131+
&& ! empty( $_POST['action'] )
132+
&& 'wp-link-ajax' === $_POST['action'] );
133+
}
134+
135+
136+
/**
137+
* Get all site URLs for each possible HTTP protocol
138+
*
139+
* @param string $site_url The site url.
140+
*
141+
* @return array<string> An array of site urls.
142+
*/
143+
function faustwp_get_wp_site_urls( string $site_url ): array {
144+
145+
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
146+
147+
$is_https = strpos( $site_url, 0, 6 ) === 'https:';
148+
149+
return apply_filters(
150+
'faustwp_get_wp_site_urls',
151+
array(
152+
$is_https ? "https://$host_url" : "http://$host_url",
153+
$is_https ? "http://$host_url" : "https://$host_url",
154+
"//$host_url",
155+
)
156+
);
157+
}
158+
159+
/**
160+
* Get all media urls based off the available site urls
161+
*
162+
* @param array<string> $wp_site_urls The array of potential site urls.
163+
* @param string $relative_upload_url The relative upload url.
164+
*
165+
* @return array<string> The array of media Urls
166+
*/
167+
function faustwp_get_wp_media_urls( array $wp_site_urls, string $relative_upload_url ) {
168+
169+
$media_urls = array();
170+
foreach ( $wp_site_urls as $site_url ) {
171+
$media_urls[] = $site_url . $relative_upload_url;
172+
}
173+
174+
return apply_filters( 'faustwp_get_wp_site_media_urls', $media_urls );
175+
}
176+
177+
178+
/**
179+
* Gets the relative wp-content upload URL.
180+
*
181+
* @param array<string> $site_urls An array of site URLs.
182+
* @param string $upload_url An array of site URLs.
183+
*
184+
* @return string The relative upload URL.
185+
*/
186+
function faustwp_get_relative_upload_url( array $site_urls, string $upload_url = '' ): string {
187+
188+
foreach ( $site_urls as $site_url ) {
189+
if ( strpos( $upload_url, $site_url ) === 0 ) {
190+
return (string) str_replace( $site_url, '', $upload_url );
191+
}
192+
}
193+
194+
return '';
133195
}

0 commit comments

Comments
 (0)