Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hooks for Member Edit tabs. #3048

Open
ipokkel opened this issue Jul 4, 2024 · 1 comment
Open

Add Hooks for Member Edit tabs. #3048

ipokkel opened this issue Jul 4, 2024 · 1 comment

Comments

@ipokkel
Copy link
Member

ipokkel commented Jul 4, 2024

Is your feature request related to a problem? Please describe.
When updating a member's User Fields using the Member Edit tabs there are no hooks available to trigger additional functionality.

Describe the solution you'd like
The ability to hook into an action or filter when updating user data or user metadata to trigger additional functionality.

Describe alternatives you've considered
Attempted to hook into pmpro_personal_options_update, personal_options_update, and edit_user_profile_update without success.

Additional context
Moderators: #578663

@RomualdYT
Copy link

RomualdYT commented Oct 13, 2024

Same issue. I'm struggling to apply a hook that triggers when updating information in the member area, which includes additional custom fields. Despite my efforts, I can't get any action to work properly when users modify their details. I've tried using various hooks, but none seem to function as expected. How can I make sure that the hook is applied correctly and performs the desired actions when the user updates their information?

My action :

add_action('pmpro_personal_options_update', 'update_or_add_to_directory', 10, 1);
function update_or_add_to_directory($user_id) {
    // Récupérer les données mises à jour
    $user = get_userdata($user_id);
    $first_name = $user->first_name;
    $last_name = $user->last_name;
    $email = $user->user_email;
    $user_login = $user->user_login; // Pseudo de l'utilisateur
    $current_datetime = current_time('mysql'); // Date et heure actuelles au format MySQL

    // Autres champs personnalisés
    $appear_in_directory = get_user_meta($user_id, 'voulez_vous_appara_tre_dans_l_annuaire', true);
    $promotion = get_user_meta($user_id, 'promotion', true); // Correspond à cursus
    $year_of_promotion = get_user_meta($user_id, 'si_oui_quelle_ann_e_de_promotion', true); // Correspond à promotion

    // Si l'utilisateur souhaite apparaître dans l'annuaire
    if ($appear_in_directory === 'yes') {
        global $wpdb;

        // Vérifier si l'utilisateur est déjà dans la table alumnii par email ou par nom + prénom
        $existing_entry = $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM alumnii WHERE email = %s OR (nom = %s AND prenom = %s)", 
            $email, $last_name, $first_name
        ));

        if (!$existing_entry) {
            // Si l'entrée n'existe pas, ajouter une nouvelle ligne
            $wpdb->insert(
                'alumnii',
                array(
                    'wdt_created_by' => $user_login,
                    'wdt_created_at' => $current_datetime,
                    'wdt_last_edited_by' => $user_login,
                    'wdt_last_edited_at' => $current_datetime,
                    'nom' => $last_name,
                    'prenom' => $first_name,
                    'email' => $email,
                    'statut' => 'diplomé',
                    'cursus' => $promotion,
                    'promotion' => $year_of_promotion,
                    'etat' => 'activé'
                ),
                array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s')
            );

            // Envoyer un email de confirmation
            wp_mail($email, "Confirmation de l'annuaire", "Vous avez été ajouté à l'annuaire avec succès.");
        } else {
            // Si l'entrée existe, vérifier s'il y a des changements
            $changes = array();
            if ($existing_entry->email != $email) {
                $changes['email'] = $email;
            }
            if ($existing_entry->promotion != $year_of_promotion) {
                $changes['promotion'] = $year_of_promotion;
            }
            if ($existing_entry->cursus != $promotion) {
                $changes['cursus'] = $promotion;
            }

            // Si des changements ont été détectés, mettre à jour les colonnes modifiées
            if (!empty($changes)) {
                $changes['wdt_last_edited_by'] = $user_login;
                $changes['wdt_last_edited_at'] = $current_datetime;

                $wpdb->update(
                    'alumnii',
                    $changes,
                    array('email' => $existing_entry->email), // Critère de recherche par email
                    array('%s', '%s', '%s', '%s', '%s'),
                    array('%s')
                );

                // Envoyer un email de confirmation de mise à jour
                wp_mail($email, "Mise à jour de l'annuaire", "Vos informations dans l'annuaire ont été mises à jour.");
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants