// Auto-enqueued from original HTML (order preserved) add_action('wp_enqueue_scripts', function(){ wp_enqueue_script('ccf-main-tsx-1', get_template_directory_uri() . '/src/main.tsx', array(), null, true); }); /** * Demo content importer on theme activation. * - Creates pages from /demo-content/*.html * - Sets the front page * - Builds the Primary menu based on original nav order * - Rewrites asset URLs to absolute theme URIs and *.html links to actual permalinks */ add_action('after_switch_theme', function(){ if ( get_option('ccf_demo_import_done') ) { return; } $theme_dir = get_template_directory(); $theme_uri = get_template_directory_uri(); $demo_dir = $theme_dir . '/demo-content'; $pages_json = $demo_dir . '/pages.json'; if ( ! file_exists($pages_json) ) { return; } $pages_meta = json_decode(file_get_contents($pages_json), true); if ( ! is_array($pages_meta) ) { return; } // Pass 1: create pages to get IDs $page_ids = array(); foreach ($pages_meta as $slug => $meta) { $title = isset($meta['title']) ? $meta['title'] : ucfirst(str_replace('-', ' ', $slug)); $existing = get_page_by_path($slug); if ( $existing ) { $page_ids[$slug] = $existing->ID; continue; } $pid = wp_insert_post(array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => $title, 'post_name' => $slug, 'post_content'=> '', )); if ( $pid && ! is_wp_error($pid) ) { $page_ids[$slug] = $pid; } } if ( empty($page_ids) ) { return; } // Helper: rewrite relative asset URLs to absolute theme URIs $rewrite_assets = function($html) use ($theme_uri){ return preg_replace_callback( '/\b(?:src|href|poster)\s*=\s*([\'"])([^\'"#]+)\1/i', function($m) use ($theme_uri){ $q = $m[1]; $url = $m[2]; // skip absolutes and non-file schemes if ( preg_match('#^(?:https?:)?//#i', $url) || preg_match('#^(?:mailto:|tel:|data:)#i', $url) ) { return $m[0]; } if ( substr($url, -4) === '.php' ) { return $m[0]; } // normalize $url = ltrim(preg_replace('#^\./#', '', $url), '/'); return 'src='.$q.$theme_uri.'/'.$url.$q; }, $html ); }; // Helper: replace .html links with permalinks $replace_html_links = function($html) use ($page_ids){ foreach ($page_ids as $slug => $pid) { $permalink = get_permalink($pid); $pattern1 = '#href\s*=\s*([\'"])'.preg_quote($slug, '#').'\.html\1#i'; $html = preg_replace($pattern1, 'href="$permalink"', $html); } return $html; }; // Pass 2: fill content foreach ($pages_meta as $slug => $meta) { $pid = isset($page_ids[$slug]) ? $page_ids[$slug] : 0; if ( ! $pid ) { continue; } $file = $demo_dir . '/' . $slug . '.html'; if ( ! file_exists($file) ) { continue; } $html = file_get_contents($file); if ( ! $html ) { continue; } // Rewrite assets, then replace html links to permalinks $html = $rewrite_assets($html); // Replace specific .html filenames first for robustness foreach ($pages_meta as $slug2 => $meta2) { $permalink2 = isset($page_ids[$slug2]) ? get_permalink($page_ids[$slug2]) : ''; if ( $permalink2 ) { $html = preg_replace('#href\s*=\s*([\'"])'.preg_quote($slug2, '#').'\.html\1#i', 'href="'.$permalink2.'"', $html); } } // Update content wp_update_post(array( 'ID' => $pid, 'post_content' => $html, )); // Assign matching page template if exists $tpl = 'template-' . $slug . '.php'; if ( file_exists($theme_dir . '/' . $tpl) ) { update_post_meta($pid, '_wp_page_template', $tpl); } } // Set front page if index/home exists $front_slug = file_exists($demo_dir . '/index.html') ? 'index' : ( file_exists($demo_dir . '/home.html') ? 'home' : null ); if ( $front_slug && isset($page_ids[$front_slug]) ) { update_option('show_on_front', 'page'); update_option('page_on_front', $page_ids[$front_slug]); } // Build Primary menu based on original nav order if available $nav_json = $demo_dir . '/nav.json'; $menu_id = 0; $menu = wp_get_nav_menu_object('Primary Menu'); if ( ! $menu ) { $menu_id = wp_create_nav_menu('Primary Menu'); } else { $menu_id = $menu->term_id; } // Assign location $locations = get_theme_mod('nav_menu_locations'); if ( ! is_array($locations) ) { $locations = array(); } $locations['primary'] = $menu_id; set_theme_mod('nav_menu_locations', $locations); // Fill menu if ( file_exists($nav_json) ) { $nav_items = json_decode(file_get_contents($nav_json), true); if ( is_array($nav_items) ) { foreach ($nav_items as $item) { $href = isset($item['href']) ? $item['href'] : ''; $text = isset($item['text']) ? $item['text'] : 'Item'; if ( preg_match('#\.html#i', $href) ) { $slug = strtolower(pathinfo($href, PATHINFO_FILENAME)); if ( isset($page_ids[$slug]) ) { wp_update_nav_menu_item($menu_id, 0, array( 'menu-item-title' => $text, 'menu-item-object-id' => $page_ids[$slug], 'menu-item-object' => 'page', 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish', )); continue; } } // Anchor links / external if ( strpos($href, '#') === 0 ) { // anchor to front page $front_id = isset($page_ids[$front_slug]) ? $page_ids[$front_slug] : 0; $front_url = $front_id ? get_permalink($front_id) : home_url('/'); $url = $front_url . $href; wp_update_nav_menu_item($menu_id, 0, array( 'menu-item-title' => $text, 'menu-item-url' => $url, 'menu-item-type' => 'custom', 'menu-item-status'=> 'publish', )); } else { wp_update_nav_menu_item($menu_id, 0, array( 'menu-item-title' => $text, 'menu-item-url' => $href, 'menu-item-type' => 'custom', 'menu-item-status'=> 'publish', )); } } } } else { // If no nav.json, just add all pages foreach ($page_ids as $slug => $pid) { wp_update_nav_menu_item($menu_id, 0, array( 'menu-item-title' => get_the_title($pid), 'menu-item-object-id' => $pid, 'menu-item-object' => 'page', 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish', )); } } update_option('ccf_demo_import_done', 1); });