@@ -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 );
0 commit comments