$template_data['title'], 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', )); // If the page is created successfully, set it as the default home page if ($page_id && !is_wp_error($page_id)) { // Set Elementor template data update_post_meta($page_id, '_elementor_edit_mode', 'builder'); update_post_meta($page_id, '_elementor_template_type', 'page'); // Process images and update content $template_data['content'] = process_images($template_data['content']); update_post_meta($page_id, '_elementor_data', $template_data['content']); // Set the page as the home page update_option('page_on_front', $page_id); update_option('show_on_front', 'page'); } } // Redirect to the home page after import $home_url = home_url('/'); wp_redirect($home_url); exit; } else { // Handle the case when no JSON files found // Redirect to an error page or display an error message wp_die('No Elementor template JSON files found in the folder.'); exit; } } function process_images(&$content) { // Directory where the images will be saved $uploads_directory = wp_upload_dir()['path']; // Traverse through each element in the content array foreach ($content as &$element) { // Iterate through the nested elements foreach ($element['elements'] as &$nested_element) { // Check if the nested element has further nested elements foreach ($nested_element['elements'] as &$further_nested_element) { // Check if the further nested element contains the image URL if (isset($further_nested_element['settings']['image']['url'])) { // Get the image URL $image_url = $further_nested_element['settings']['image']['url']; // Get the image filename $image_filename = basename($image_url); // Check if the image exists in the uploads directory if (!file_exists($uploads_directory . '/' . $image_filename)) { // If the image doesn't exist, download it from the template directory and save it to the uploads directory $image_destination = $uploads_directory . '/' . $image_filename; // Download and save the image $result = save_image_from_url($image_url, $image_destination); if ($result !== false) { // If the image is successfully saved, update the image URL in the Elementor content $further_nested_element['settings']['image']['url'] = wp_upload_dir()['url'] . '/' . $image_filename; } else { // If downloading fails, output an error message echo "Failed to download and save image: $image_url"; } } else { // If the image already exists in the uploads directory, update the image URL in the Elementor content $further_nested_element['settings']['image']['url'] = wp_upload_dir()['url'] . '/' . $image_filename; } } } } } return $content; } // Function to download and save image from URL function save_image_from_url($image_url, $destination_path) { // Initialize cURL session $ch = curl_init($image_url); // Set options for cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Skip SSL verification (optional) // Execute cURL session $image_data = curl_exec($ch); // Check if cURL request was successful if ($image_data === false) { // Handle cURL error return false; } // Close cURL session curl_close($ch); // Save the image to the destination directory $result = file_put_contents($destination_path, $image_data); // Check if image was saved successfully if ($result === false) { // Handle file writing error return false; } return $destination_path; // Return the path to the saved image }