
/**
 * Sends a verification email to new freelancers and sets a custom sender name/email.
 *
 * Add this code using the "Code Snippets" plugin.
 */

// --------------------------------------------------------------------
// FUNCTION 1: Sends the verification email when a new user registers.
// --------------------------------------------------------------------
add_action( 'user_register', 'send_freelancer_verification_email', 10, 1 );
function send_freelancer_verification_email( $user_id ) {
    
    // Get the user's data
    $user = get_userdata( $user_id );
    $user_email = $user->user_email;
    
    // Check if the new user has the 'freelancer' role
    if ( in_array( 'wp_freeio_freelancer', (array) $user->roles ) ) {

        // Create a unique verification token
        $token = sha1( $user_id . $user_email . time() );

        // Store the token and verification status in the user's profile
        update_user_meta( $user_id, '_email_verification_token', $token );
        update_user_meta( $user_id, '_is_email_verified', 'no' );

        // Create the verification link
        $verification_link = add_query_arg(
            array(
                'action' => 'verify_email',
                'user_id' => $user_id,
                'token' => $token
            ),
            home_url('/')
        );

        // --- EMAIL CONTENT ---
        $subject = 'Veuillez vérifier votre adresse e-mail'; // Email Subject
        $message = "Bonjour, <br><br>";
        $message .= "Merci de vous être inscrit ! Veuillez cliquer sur le lien ci-dessous pour vérifier votre adresse e-mail :<br><br>";
        $message .= '<a href="' . esc_url( $verification_link ) . '">Vérifier mon e-mail</a><br><br>';
        $message .= "Une fois votre e-mail vérifié, votre compte sera examiné par un administrateur avant d'être approuvé.<br><br>";
        $message .= "Merci,<br>L'équipe de " . get_bloginfo('name');

        $headers = array('Content-Type: text/html; charset=UTF-8');

        // Send the email
        wp_mail( $user_email, $subject, $message, $headers );
    }
}


// --------------------------------------------------------------------
// FUNCTION 2: Handles the verification link when the user clicks it.
// --------------------------------------------------------------------
add_action( 'template_redirect', 'handle_freelancer_email_verification' );
function handle_freelancer_email_verification() {
    
    // Check if our action, user_id, and token are in the URL
    if ( isset( $_GET['action'] ) && $_GET['action'] == 'verify_email' && isset( $_GET['user_id'] ) && isset( $_GET['token'] ) ) {

        $user_id = absint( $_GET['user_id'] );
        $token = sanitize_text_field( $_GET['token'] );

        // Get the token we stored in the user's profile
        $stored_token = get_user_meta( $user_id, '_email_verification_token', true );

        // If the tokens match, verify the user
        if ( $stored_token === $token ) {
            // Mark the user's email as verified
            update_user_meta( $user_id, '_is_email_verified', 'yes' );
            
            // Optional: You can delete the token now that it's been used
            delete_user_meta( $user_id, '_email_verification_token' );

            // Display a success message and stop the page from loading further
            wp_die( 'Merci ! Votre adresse e-mail a été vérifiée avec succès. Votre compte est maintenant en attente d\'approbation par un administrateur.', 'E-mail vérifié' );
        } else {
            // Display an error message if the token is invalid
            wp_die( 'Désolé, ce lien de vérification n\'est pas valide ou a expiré.', 'Lien invalide' );
        }
    }
}


// --------------------------------------------------------------------
// FUNCTION 3a: Changes the default "From" name for all outgoing emails.
// --------------------------------------------------------------------
add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
function custom_wp_mail_from_name( $original_email_from ) {
    return 'Holistic Babe'; // Sender name
}

// --------------------------------------------------------------------
// FUNCTION 3b: Changes the default "From" email for all outgoing emails.
// --------------------------------------------------------------------
add_filter( 'wp_mail_from', 'custom_wp_mail_from' );
function custom_wp_mail_from( $original_email_address ) {
    return 'admin@holistic-babe.fr'; // Sender email
}
