Skip to content

Commit

Permalink
Refactoring some of the image srcset replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmurphy committed Jan 10, 2025
1 parent 8e9a232 commit 9770673
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
42 changes: 18 additions & 24 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ function content_replacement( string $content ): string {
}

if ( $replace_media_urls ) {
return faustwp_replace_media_url( $content, $wp_media_urls, $frontend_uri . $relative_upload_url );
return str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
}

$site_urls_pattern = implode( '|', array_map( 'preg_quote', $wp_site_urls ) );
$pattern = '#(' . $site_urls_pattern . ')(?!' . $relative_upload_url . '(\/|$))#';

return preg_replace( $pattern, $frontend_uri, $content );
}

Expand Down Expand Up @@ -109,47 +110,40 @@ function image_source_srcset_replacement( $sources ) {
return $sources;
}

$replace_media_urls = ! use_wp_domain_for_media();
$wp_site_urls = faustwp_get_wp_site_urls();
$wp_site_urls = faustwp_get_wp_site_urls();
if ( empty( $wp_site_urls ) ) {
return $sources;
}

$replace_media_urls = ! use_wp_domain_for_media();
$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' );
$site_url = site_url() . '/';

$wp_media_site_url = $frontend_uri . $relative_upload_url;
$patterns = array(
"#^{$frontend_uri}/#",
'#^/#',
);

/**
* Update each source with the correct replacement URL
*/
foreach ( $sources as $width => $source ) {
if ( ! $replace_media_urls ) {
$i = 0;
$url = preg_replace_callback(
$patterns,
function () use ( &$wp_site_urls, &$i ) {
$replacement = $wp_site_urls[ $i ] . '/';
$i++;

return $replacement;
},
$source['url']
);

$sources[ $width ]['url'] = $url;

continue;
}
$url = $source['url'];

if ( $replace_media_urls ) {
$url = ( strpos( $url, $relative_upload_url ) === 0 )
? $frontend_uri . $url
: str_replace( $wp_media_urls, $wp_media_site_url, $url );
} else {

if ( strpos( $source['url'], $relative_upload_url ) === 0 ) {
$sources[ $width ]['url'] = $frontend_uri . $source['url'];
continue;
// We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url
$url = preg_replace( $patterns, $site_url, $url );
}

$sources[ $width ]['url'] = faustwp_replace_media_url( $source['url'], $wp_media_urls, $wp_media_site_url );
$sources[ $width ]['url'] = $url;
}

return $sources;
Expand Down
13 changes: 0 additions & 13 deletions plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,3 @@ function faustwp_get_relative_upload_url( $site_urls ) {

return '';
}

/***
* Replaces the media URL for various media urls
*
* @param string $content The content to be updated with the new media URL.
* @param array $wp_media_urls An array of media URLS.
* @param string $replace_url The media URL to be updated to.
*
* @return string The replaced string
*/
function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) {
return str_replace( $wp_media_urls, $replace_url, $content );
}

0 comments on commit 9770673

Please sign in to comment.