/**
 * MONTAUTO: Create Volunteer Profile from Form Submission
 */
add_action('wpcf7_mail_sent', 'montauto_sync_volunteer_profile');

function montauto_sync_volunteer_profile($contact_form) {
    // 1. Get the current submission
    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return;

    $data = $submission->get_posted_data();
    $files = $submission->uploaded_files();

    // 2. Create the Volunteer Post
    // We combine the name and surname for the title
    $name = isset($data['first-name']) ? sanitize_text_field($data['first-name']) : '';
    $surname = isset($data['surname']) ? sanitize_text_field($data['surname']) : '';
    
    $volunteer_data = array(
        'post_title'    => $name . ' ' . $surname,
        'post_type'     => 'volunteers', // Matches your Simple CPT slug
        'post_status'   => 'draft',     // Stays hidden until you approve
        'post_author'   => 1,
    );

    // 3. Insert the Post
    $post_id = wp_insert_post($volunteer_post);

    if ($post_id && !is_wp_error($post_id)) {
        // 4. Update the Custom Fields (LinkedIn & Offer)
        update_post_meta($post_id, 'linkedin_url', sanitize_url($data['linkedin']));
        update_post_meta($post_id, 'expertise', sanitize_textarea_field($data['volunteer_offer']));

        // 5. Handle the Profile Image
        if (!empty($files['profile_pic'])) {
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/media.php');

            $file_path = $files['profile_pic'][0];
            $file_name = basename($file_path);

            // Sideload the image into the Media Library
            $file_array = array(
                'name'     => $file_name,
                'tmp_name' => $file_path
            );

            $attachment_id = media_handle_sideload($file_array, $post_id);

            if (!is_wp_error($attachment_id)) {
                set_post_thumbnail($post_id, $attachment_id);
            }
        }
    }
}<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://montauto.earth/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://montauto.earth/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-posts-footer-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-posts-header-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-posts-megamenu-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://montauto.earth/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
