Skip to content

Commit 37a4555

Browse files
committed
simplify the code
1 parent fd98d88 commit 37a4555

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

plugins/faustwp/includes/replacement/callbacks.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,31 @@ function content_replacement( ?string $content ) {
5656
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
5757
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';
5858

59-
/* If the setting IS enabled, use front-end URL for internal URLs, but not media */
59+
/* If "Enable Post and Category URL" is enabled, use front-end URL for internal URLs, but not for media links */
60+
6061
if ( $replace_content_urls ) {
61-
if ( $replace_content_urls ) {
62-
$pattern = '#href="(' . implode( '|', array_map( 'preg_quote', $wp_site_urls ) ) . ')([^"]*)"#';
63-
64-
/* Check for media urls and skip them */
65-
$content = preg_replace_callback(
66-
$pattern,
67-
function ( $matches ) use ( $wp_media_urls, $frontend_uri ) {
68-
$full_url = $matches[1] . $matches[2];
69-
70-
// Skip if the full URL matches a known media URL - could be csv, pdf as well
71-
foreach ( $wp_media_urls as $media_url ) {
72-
if ( strpos( $full_url, $media_url ) === 0 ) {
73-
return $matches[0]; // Return original href
74-
}
75-
}
62+
63+
//Look for href links
64+
preg_match_all( '#href="([^"]+)"#i', $content, $href_links );
7665

77-
// Normalize the URL path
78-
$relative_path = ltrim( $matches[2], '/' );
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 ) continue;
70+
71+
$is_wp_url = array_filter( $wp_site_urls, fn( $base ) => strpos( $url, $base ) === 0 );
72+
if ( ! $is_wp_url ) continue;
7973

80-
return 'href="' . trailingslashit( $frontend_uri ) . $relative_path . '"';
81-
},
82-
$content
83-
);
74+
//get relative link
75+
$relative = ltrim( str_replace( reset( $is_wp_url ), '', $url ), '/' );
76+
$updated = 'href="' . $frontend_uri .'/'. $relative . '"';
77+
78+
$original = $href_links[0][$i];
79+
$content = str_replace( $original, $updated, $content );
8480
}
8581
}
8682

87-
/* If the setting is NOT enabled, use front-end URL for media URLs */
83+
/* If "Use the WordPress domain for media URLs in post content" is NOT enabled, use front-end URL for media URLs */
8884

8985
if ( $replace_media_urls ) {
9086
$content = str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );

plugins/faustwp/includes/replacement/functions.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,11 @@ function faustwp_get_wp_media_urls( array $wp_site_urls, string $relative_upload
189189
*
190190
* @return string The relative upload URL.
191191
*/
192-
function faustwp_get_relative_upload_url( array|string $site_urls, string $upload_url = '' ): string {
193-
if ( is_array( $site_urls ) ) {
194-
foreach ( $site_urls as $site_url ) {
195-
if ( strpos( $upload_url, $site_url ) === 0 ) {
196-
return (string) str_replace( $site_url, '', $upload_url );
197-
}
198-
}
199-
} else {
200-
if ( $site_urls ) {
201-
$upload_url = str_replace( $site_urls, '', $upload_url );
192+
function faustwp_get_relative_upload_url( array $site_urls, string $upload_url = '' ): string {
193+
foreach ( $site_urls as $site_url ) {
194+
if ( strpos( $upload_url, $site_url ) === 0 ) {
195+
return (string) str_replace( $site_url, '', $upload_url );
202196
}
203197
}
204-
205198
return '';
206-
}
199+
}

0 commit comments

Comments
 (0)