/*
Theme Name: Theme Starter
Author: John Doe
*/
 
// Change this if you ever switch CDN providers/domains.
define( 'KIRA_CDN_URL', 'https://cdn.kira-restaurant.com' );
 
/**
 * 1. Rewrite the base uploads URL used by WordPress core.
 * This covers wp_get_attachment_url(), the media library,
 * and any new content going forward (images, PDFs, docs, everything
 * under /wp-content/uploads/).
 */
function kira_cdn_upload_dir( $upload ) {
    $site_uploads_url = home_url( '/wp-content/uploads' );
    $cdn_uploads_url   = KIRA_CDN_URL . '/wp-content/uploads';
 
    $upload['url']     = str_replace( $site_uploads_url, $cdn_uploads_url, $upload['url'] );
    $upload['baseurl'] = str_replace( $site_uploads_url, $cdn_uploads_url, $upload['baseurl'] );
 
    return $upload;
}
add_filter( 'upload_dir', 'kira_cdn_upload_dir' );
 
/**
 * 2. Catch-all: rewrite any lingering site-domain upload links
 * inside post content, widgets, etc. (e.g. old PDFs linked manually,
 * images pasted with full URLs before the CDN was set up).
 */
function kira_cdn_rewrite_content( $content ) {
    $site_uploads_url = home_url( '/wp-content/uploads' );
    $cdn_uploads_url   = KIRA_CDN_URL . '/wp-content/uploads';
 
    return str_replace( $site_uploads_url, $cdn_uploads_url, $content );
}
add_filter( 'the_content', 'kira_cdn_rewrite_content' );
add_filter( 'widget_text', 'kira_cdn_rewrite_content' );
add_filter( 'acf_the_content', 'kira_cdn_rewrite_content' ); // remove if you don't use ACF
 
/**
 * 3. Rewrite responsive image srcset URLs (otherwise WP will mix
 * CDN + non-CDN URLs in the same <img> tag).
 */
function kira_cdn_rewrite_srcset( $sources ) {
    $site_uploads_url = home_url( '/wp-content/uploads' );
    $cdn_uploads_url   = KIRA_CDN_URL . '/wp-content/uploads';
 
    foreach ( $sources as &$source ) {
        $source['url'] = str_replace( $site_uploads_url, $cdn_uploads_url, $source['url'] );
    }
 
    return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'kira_cdn_rewrite_srcset' );
 
/**
 * 4. Explicitly rewrite direct attachment URLs (covers PDFs and any
 * file fetched via wp_get_attachment_url(), e.g. "View Menu PDF" links).
 */
function kira_cdn_rewrite_attachment_url( $url ) {
    $site_uploads_url = home_url( '/wp-content/uploads' );
    $cdn_uploads_url   = KIRA_CDN_URL . '/wp-content/uploads';
 
    return str_replace( $site_uploads_url, $cdn_uploads_url, $url );
}
add_filter( 'wp_get_attachment_url', 'kira_cdn_rewrite_attachment_url' );
 
/**
 * 5. Redirect anyone hitting an upload file directly on the main
 * domain (e.g. yoursite.com/wp-content/uploads/menu.pdf) to the CDN
 * version instead. This catches old bookmarked links, links shared
 * before the CDN existed, search engine results not yet updated, etc.
 *
 * NOTE: For actual files (images/PDFs) this request usually never
 * reaches PHP — the web server serves the static file directly.
 * This PHP redirect is a safety net for cases where it *does* reach
 * WordPress (e.g. attachment pages, or servers that route everything
 * through index.php). For guaranteed coverage on ALL direct file
 * requests, add the .htaccess rule below as well.
 */
function kira_redirect_direct_uploads_to_cdn() {
    if ( is_admin() ) {
        return;
    }
 
    $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '';
 
    if ( strpos( $request_uri, '/wp-content/uploads/' ) === 0 ) {
        $redirect_url = KIRA_CDN_URL . $request_uri;
        wp_redirect( $redirect_url, 301 );
        exit;
    }
}
add_action( 'template_redirect', 'kira_redirect_direct_uploads_to_cdn' );
