$template_path) { $existing_page = author_portfolio_new_get_page_by_title($page_title); if ($existing_page) { $imported_pages[] = $existing_page->ID; continue; // Skip if page exists } // Insert new page $page_id = wp_insert_post([ 'post_title' => $page_title, 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => '', 'page_template' => 'blankpage.php', ]); if ($page_id && file_exists($template_path)) { $start_time = microtime(true); // Read and validate JSON $json_data = file_get_contents($template_path); $template_data = json_decode($json_data, true); if (json_last_error() !== JSON_ERROR_NONE) { error_log('Invalid JSON for page ' . $page_title . ': ' . json_last_error_msg()); continue; } if (!empty($template_data['content'])) { // Process the content (e.g., replace image IDs, if needed) $processed_content = author_portfolio_process_elementor_content($template_data['content']); if ($processed_content) { update_post_meta($page_id, '_elementor_data', wp_slash(json_encode($processed_content))); update_post_meta($page_id, '_elementor_edit_mode', 'builder'); // Mark the page as needing Elementor CSS regeneration update_post_meta($page_id, '_elementor_css', ''); } else { error_log('Failed to process Elementor content for page ' . $page_title); } } $end_time = microtime(true); error_log('Imported Elementor template for ' . $page_title . ' in ' . ($end_time - $start_time) . ' seconds'); $imported_pages[] = $page_id; } } // Insert "Books" page with Gutenberg blocks (No Elementor) $books_page = author_portfolio_new_get_page_by_title('Books'); if (!$books_page) { $books_content = ''; $books_page_id = wp_insert_post([ 'post_title' => 'Books', 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => $books_content, 'page_template' => 'fullwidth.php', ]); if ($books_page_id) { $imported_pages[] = $books_page_id; } } // Insert "Blog" page and set it as the posts page $blog_page = author_portfolio_new_get_page_by_title('Blog'); if (!$blog_page) { $blog_page_id = wp_insert_post([ 'post_title' => 'Blog', 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => '', 'page_template' => 'fullwidth.php', ]); if ($blog_page_id) { $imported_pages[] = $blog_page_id; update_option('page_for_posts', $blog_page_id); } } $home_page = author_portfolio_new_get_page_by_title('Home 1'); if ($home_page) { update_option('page_on_front', $home_page->ID); update_option('show_on_front', 'page'); } author_portfolio_setup_demo_menu($imported_pages); } // Helper function to process Elementor content (e.g., replace image IDs) function author_portfolio_process_elementor_content($content) { if (!is_array($content)) { return false; } // Recursively process the content to handle images or other dependencies foreach ($content as &$element) { if (!empty($element['elements'])) { $element['elements'] = author_portfolio_process_elementor_content($element['elements']); } // Example: Replace image IDs (if your JSON uses placeholder IDs) if (!empty($element['settings']['image']['id'])) { $image_id = $element['settings']['image']['id']; if (!author_portfolio_attachment_id_exists($image_id)) { // If the image ID doesn't exist, try to import the image from the URL if (!empty($element['settings']['image']['url'])) { $new_image_id = author_portfolio_import_image_from_url($element['settings']['image']['url']); if ($new_image_id) { $element['settings']['image']['id'] = $new_image_id; } else { // Remove the image reference if import fails unset($element['settings']['image']['id']); } } else { unset($element['settings']['image']['id']); } } } } return $content; }