Bug: Fixes issues for content replacement for the site and media URLs#2014
Bug: Fixes issues for content replacement for the site and media URLs#2014colinmurphy merged 14 commits intocanaryfrom
Conversation
… and also updates tests to use WP 6.6 and 6.7 Fixes various issues with using content replacement for the site URL.
🦋 Changeset detectedLatest commit: 11f1db2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📦 Next.js Bundle Analysis for @faustwp/getting-started-exampleThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
|
@josephfusco This is the draft PR for the site URL issues. This fixes the various issues as per our call last week but I also want to update the image srcset replacement with the same functionality so that we replace all HTTP protocols e.g. HTTP, HTTPS or // |
|
Just noting I am working on also updating the image_source_srcset_replacement function callback aswell to use all HTTP protocols so I will have an updated PR next week after the holidays. |
|
Awesome! |
Thanks @theodesp I have also now updated the PR with details on how to replicate the issue and test the issue. |
…ng for cotent replacement.
|
Can you give this another review please. Thanks @whoami-pwd for the suggestions around optimizing some the loops on our call earlier. I have tested locally again the different config for URLs and it is all working as expected but if you could test aswell that would be great please👍 |
whoami-pwd
left a comment
There was a problem hiding this comment.
Great work on addressing these issues! Please check the comments for a few suggestions.
| * | ||
| * @return string The replaced string | ||
| */ | ||
| function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) { |
There was a problem hiding this comment.
I am not sure if this function is needed. It looks like a wrapper to the str_replace with an alternative parameters order.
Please remember that the str_replace can also accept the array as a search param, so no need to loop through each one.
There was a problem hiding this comment.
We use this function in 2 places.Happy to extract the functionality out if you prefer?
There was a problem hiding this comment.
Yeah, my point is that the function itself is just a wrapper for the str_replace. We don't actually do anything except calling the str_replace here.
There was a problem hiding this comment.
Ah sorry I see your point now. I will remove it as a function should really be doing more that one operation
| * | ||
| * @return mixed The replaced content | ||
| */ | ||
| function faustwp_replace_urls( array $patterns, mixed $wp_site_urls, mixed $content ) { |
There was a problem hiding this comment.
Maybe it is better for us to keep things more simple here. Let's try to avoid using that function.
There was a problem hiding this comment.
Thanks @whoami-pwd
I was hoping to re-use that function when I built it, but it is only used once so I will remove it 👍
| /** | ||
| * Get all site URLs for each possible HTTP protocol | ||
| */ | ||
| function faustwp_get_wp_site_urls() { |
There was a problem hiding this comment.
Could you please add the return type here and to the phpdoc?
| return apply_filters( 'faustwp_get_wp_site_urls', array( $site_url ) ); | ||
| } | ||
|
|
||
| $is_https = substr( $site_url, 0, 6 ) === 'https:'; |
There was a problem hiding this comment.
strpos can be faster for same purpose.
There was a problem hiding this comment.
Thanks good to know 😄
| $host_url = wp_parse_url( $site_url, PHP_URL_HOST ); | ||
|
|
||
| if ( ! is_string( $host_url ) ) { | ||
| return apply_filters( 'faustwp_get_wp_site_urls', array( $site_url ) ); |
There was a problem hiding this comment.
Maybe we can reduce the function length and get rid off one return?
There was a problem hiding this comment.
This probably isn't needed as the site url should be a string anyway.
| $content = preg_replace( "#{$site_url}(?!{$media_dir})#", "{$replacement}", $content ); | ||
| return $content; | ||
| foreach ( $wp_site_urls as $site_url ) { | ||
| $pattern_exclude_media_urls = '#' . preg_quote( $site_url, '#' ) . "(?!{$relative_upload_url}(\/|$))#"; |
There was a problem hiding this comment.
I believe that we can implode the urls into the pattern itself instead of preg_replace it multiple times inside foreach. I guess if the urls array is less then ~50 it is faster and more compact.
There was a problem hiding this comment.
I tried to use implode but the only way I was able to do this is with array map. I don't know if the performance is worth it compared to readibility as I find the array map part not very readable.
$pattern_exclude_media_urls = '#(' . implode( '|', array_map( function( $url ) {
return preg_quote( $url, '#' );
}, $wp_site_urls ) ) . ")(?!{$relative_upload_url}(\/|$))#";
return preg_replace( $pattern_exclude_media_urls, $frontend_uri, $content );Happy for a better suggesting on how to do the implode part 👍
There was a problem hiding this comment.
I was actually able to tidy up the array_map based on your suggestion above so thanks 😄
| $upload_dir = wp_upload_dir()['baseurl']; | ||
|
|
||
| foreach ( $site_urls as $site_url ) { | ||
| if ( false !== strpos( $upload_dir, $site_url ) ) { |
There was a problem hiding this comment.
Are we checking if it starts with or contains?
There was a problem hiding this comment.
We cannot use str_starts_with as we need to support PHP 7. I will look at updating this to check if it starts with the site URL
There was a problem hiding this comment.
Sorry, I mean that strpos($haystack, $needle) === 0 verifies if it starts with and strpos($haystack, $needle) !== false that it contains.
Co-authored-by: Alex K. <33621842+whoami-pwd@users.noreply.github.com>
|
Thanks @whoami-pwd for the great code review 👍 Can you review the changes in the latest commit please. |
whoami-pwd
left a comment
There was a problem hiding this comment.
Nice work! I appreciate the updates you've made. There are still a couple of areas where we could improve the code quality/performance. Feel free to reach out if you have any questions.
| * @return array<string> The array of media Urls | ||
| */ | ||
| function faustwp_get_wp_media_urls( array $wp_site_urls ) { | ||
| $upload_url = faustwp_get_relative_upload_url( $wp_site_urls ); |
There was a problem hiding this comment.
I think we can just pass the $upload_url here as a parameter, then we won't need to call it once again inside the function.
| * | ||
| * @return string The replaced string | ||
| */ | ||
| function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) { |
There was a problem hiding this comment.
Yeah, my point is that the function itself is just a wrapper for the str_replace. We don't actually do anything except calling the str_replace here.
| function faustwp_get_wp_media_urls( array $wp_site_urls ) { | ||
| $upload_url = faustwp_get_relative_upload_url( $wp_site_urls ); | ||
|
|
||
| if ( ! is_string( $upload_url ) ) { |
There was a problem hiding this comment.
And then we will not need the extra verification like this, because we will force parameter with string type.
|
|
||
| $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls ); | ||
| $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls ); | ||
| $frontend_uri = faustwp_get_setting( 'frontend_uri' ); |
There was a problem hiding this comment.
Please typecast here to make sure it is string with some default value.
There was a problem hiding this comment.
This is not allowed by our phpcs settings 😅
There was a problem hiding this comment.
Sorry I was wrong here.

See #1956
This fixes various issues for content replacement callback functions for both the site and media URLs. There is 2 main fixes in this branch.
The branch also adds 2 new filters to allow developers to modify the URLS used for content replacement
faustwp_get_wp_site_urls- An array of site URLsfaustwp_get_wp_site_media_urls- An array of media site URLsThis branch also updated Github actions to also use WordPress 6.6. and 6.7
Tasks
Description
See above.
Related Issue(s):
#1956
Testing
Setup
As per issue #1956 I setup a WordPress Multisite (sub-domains) with the latest version of 6.7.1 using Local.
I setup 3 sub-domains and installed FaustWP, WPGraphQL & ACF and one of the sites was setup as
http://alpha.multisite.local/I setup a field group for pages and I setup a WYSIWG editor field with the field name
summaryI then updated the sample page to have an image for the WYSIWG field and also a link e.g.
I then used the following GraphQL query in the WPGraphQL IDE for debugging and fixing the issues
I then access the site on HTTPS rather than HTTP (site_url)
Tests
Note
http://localhost:3000is the frontend URL set in Faust WP and I am accessing the site on HTTPS e.g.https://alpha.multisite.local/wp-admin/Test 1: URL Rewrites Enabled
Query
Output
{ "data": { "pages": { "nodes": [ { "pageFields": { "summary": "<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-10\" src=\"http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp\" alt=\"\" width=\"300\" height=\"155\" srcset=\"http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp 300w, http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-768x397.webp 768w, http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp 888w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" /></p>\n<p><a href=\"http://localhost:3000/index.php/hello-world/\">Hello World</a></p>\n" } } ] } } }All 3 URLs (image src, srcset and link) were updated correctly to the frontend URL
http://localhost:3000/Test 2: URL Rewrites Enabled and Media Rewrites Disabled
Query
Output
{ "data": { "pages": { "nodes": [ { "pageFields": { "summary": "<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-10\" src=\"http://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp\" alt=\"\" width=\"300\" height=\"155\" srcset=\"https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp 300w, https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-768x397.webp 768w, https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp 888w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" /></p>\n<p><a href=\"http://localhost:3000/index.php/hello-world/\">Hello World</a></p>\n" } } ] } } }The image URL wasn't updated and the link was updated to the frontend URL.
Test 3: URL Rewrites Disabled and Media Rewrites Disabled
Query
Output
{ "data": { "pages": { "nodes": [ { "pageFields": { "summary": "<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-10\" src=\"http://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp\" alt=\"\" width=\"300\" height=\"155\" srcset=\"https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp 300w, https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-768x397.webp 768w, https://alpha.multisite.local/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp 888w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" /></p>\n<p><a href=\"https://alpha.multisite.local/index.php/hello-world/\">Hello World</a></p>\n" } } ] } } }Neither the image src, srcset or link was updated to use the frontend URL
Test 4: URL Rewrites Disabled and Media Rewrites Enabled
Query
Output
{ "data": { "pages": { "nodes": [ { "pageFields": { "summary": "<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-10\" src=\"http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp\" alt=\"\" width=\"300\" height=\"155\" srcset=\"http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp 300w, http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-768x397.webp 768w, http://localhost:3000/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp 888w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" /></p>\n<p><a href=\"https://alpha.multisite.local/index.php/hello-world/\">Hello World</a></p>\n" } } ] } } }Both the image src and srcset URLs were updated with the frontend URL where the link stayed the same
Screenshots
Documentation Changes
There was 2 new filters added
faustwp_get_wp_site_urlsfor allowing developers to add or modify an array of site URLS used for content replacementfaustwp_get_wp_site_media_urlsfor allowign developers to add or modify an array of media site URLS for content replacemntDependant PRs