Skip to content

Commit

Permalink
v 0.8.0
Browse files Browse the repository at this point in the history
- New module : Delete account.
- New helper for HTML of error messages.
- Hook for Email validation rule.
  • Loading branch information
Darklg committed Aug 8, 2024
1 parent 54d66cd commit 6b95725
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 89 deletions.
35 changes: 35 additions & 0 deletions inc/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
defined('ABSPATH') || die;

function wpuextranet_get_html_errors($errors = array(), $args = array()) {
/* Do not */
if (empty($errors)) {
return '';
}
$defaults = array(
'type' => 'error',
'form_id' => ''
);
$args = array_merge($defaults, $args);
$classname = 'extranet-message extranet-message--' . $args['type'] . ' form-' . $args['form_id'] . '-' . $args['type'] . '';
$html_return = '';
/* Display errors */
if (count($errors) > 1) {
$html_return .= '<ul class="' . $classname . '">';
foreach ($errors as $error) {
$html_return .= '<li>';
if ($args['type'] == 'error') {
$html_return .= '<strong class="error">' . __('Error:', 'wpu_extranet') . '</strong> ';
}
$html_return .= $error;
$html_return .= '</li>';
}
$html_return .= '</ul>';
} else {
$html_return .= '<p class="' . $classname . '">';
$html_return .= implode('<br />', $errors);
$html_return .= '</p>';
}

return $html_return;
}
16 changes: 9 additions & 7 deletions inc/modules/change-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function wpu_extranet_change_email__action() {
}

/* Invalid current email */
if($current_email == $new_email){
if ($current_email == $new_email) {
$errors[] = __('New email is the same as the current email.', 'wpu_extranet');
}

Expand All @@ -61,27 +61,29 @@ function wpu_extranet_change_email__action() {
$errors[] = __('Email is already in use.', 'wpu_extranet');
}

$errors = apply_filters('wpu_extranet__email__validation', $errors, $new_email);
$errors = apply_filters('wpu_extranet_change_email__action_errors', $errors, $current_email, $new_email, $confirm_new_email);

$html_return = '';
$return_type = 'error';
if (empty($errors)) {
// Change email
wp_update_user(array(
'ID' => $user_id,
'user_email' => $new_email
));
$html_return = '<p class="extranet-message extranet-message--success form-email-success">' . __('Email successfully updated!', 'wpu_extranet') . '</p>';
} else {
$html_return = '<p class="extranet-message extranet-message--error form-email-error">' . implode('<br />', $errors) . '</p>';
$return_type = 'success';
$errors[] = __('Email successfully updated!', 'wpu_extranet');
}

return $html_return;
return wpuextranet_get_html_errors($errors, array(
'form_id' => 'change_email',
'type' => $return_type
));
}

/* HTML Form
-------------------------- */


function wpu_extranet_change_email__form($args = array()) {
if (!is_array($args)) {
$args = array();
Expand Down
17 changes: 7 additions & 10 deletions inc/modules/change-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ function wpu_extranet_change_password__action() {
$errors[] = __('Password is too short, minimum of 6 characters.', 'wpu_extranet');
}

$html_return = '';
$return_type = 'error';
if (empty($errors)) {
// Change password
wp_set_password($new_password, $current_user->ID);
// Log-in again.
wpu_extranet_log_user($current_user);

$html_return .= '<p class="extranet-message extranet-message--success form-password-success">' . __('Password successfully changed!', 'wpu_extranet') . '</p>';
} else {
$html_return .= '<ul class="extranet-message extranet-message--error form-password-error">';
foreach ($errors as $error) {
$html_return .= '<li><strong class="error">' . __('Error:', 'wpu_extranet') . '</strong> ' . $error . '</li>';
}
$html_return .= '</ul>';
$return_type = 'success';
$errors[] = __('Password successfully changed!', 'wpu_extranet');
}

return $html_return;
return wpuextranet_get_html_errors($errors, array(
'form_id' => 'change_password',
'type' => $return_type
));
}

/* HTML Form
Expand Down
138 changes: 138 additions & 0 deletions inc/modules/delete-account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
defined('ABSPATH') || die;

/* ----------------------------------------------------------
Delete account
---------------------------------------------------------- */

/* Test
-------------------------- */

function wpu_extranet_user_can_delete_account($errors, $user_id) {
if (current_user_can('edit_users', $user_id)) {
$errors[] = __('You can not delete this type of account.', 'wpu_extranet');
}

$errors = apply_filters('wpu_extranet_user_can_delete_account', $errors, $user_id);

return $errors;
}

/* Form action
-------------------------- */

function wpu_extranet_delete_account__action() {
if (empty($_POST)) {
return '';
}
if (!is_user_logged_in()) {
return '';
}
if (!isset($_POST['current_password'])) {
return '';
}

if (!isset($_POST['wpuextranet_delete_account']) || !wp_verify_nonce($_POST['wpuextranet_delete_account'], 'wpuextranet_delete_account_action')) {
return '';
}

$password = trim($_POST['current_password']);

$user_id = get_current_user_id();
$current_user = get_user_by('id', $user_id);
if (!$current_user) {
return;
}

/* Check errors */
$errors = array();

/* Empty fields */
if (empty($password)) {
$errors[] = __('All fields are required.', 'wpu_extranet');
}

/* Invalid password */
if (!wp_check_password($password, $current_user->data->user_pass, $current_user->ID)) {
$errors[] = __('Invalid password.', 'wpu_extranet');
}

/* User can edit other users : prevent deletion */
$errors = wpu_extranet_user_can_delete_account($errors, $user_id);

if (empty($errors)) {
/* Delete user and redirect to home */
require_once ABSPATH . 'wp-admin/includes/user.php';
wp_delete_user($user_id);
wp_redirect(home_url() . '#');
die;
}

/* Display errors */
return wpuextranet_get_html_errors($errors, array(
'form_id' => 'delete_account',
'type' => 'error'
));

}

/* HTML Form
-------------------------- */

function wpu_extranet_delete_account__form($args = array()) {
if (!is_array($args)) {
$args = array();
}
if (!isset($args['before_fields'])) {
$args['before_fields'] = '';
}
$html = '';

$user_id = get_current_user_id();
$userdata = get_userdata($user_id);

$errors = wpu_extranet_user_can_delete_account(array(), $user_id);
$user_can_delete_account = empty($errors);
if (!empty($errors)) {
$args['before_fields'] .= wpuextranet_get_html_errors($errors, array(
'form_id' => 'delete_account',
'type' => 'error'
));
}

$settings = wpu_extranet_get_skin_settings();
$html .= '<div class="' . $settings['form_wrapper_classname'] . ' form-delete_account-wrapper">';
$html .= '<h3>' . __('Delete your account', 'wpu_extranet') . '</h3>';
$html .= '<form name="delete_accountform" id="delete_accountform" action="' . get_permalink() . '#delete_accountform" method="post">';
$html .= $args['before_fields'];
if ($user_can_delete_account) {
$html .= '<ul class="' . $settings['form_items_classname'] . '">';
$html .= wpu_extranet__display_field('current_password', array(
'type' => 'password',
'attributes' => 'minlength="6" autocomplete="off" required="required"',
'label' => __('Enter your current password', 'wpu_extranet')
));
$html .= '<li class="' . $settings['form_box_submit_classname'] . '">';
$html .= wp_nonce_field('wpuextranet_delete_account_action', 'wpuextranet_delete_account', true, false);
$html .= '<button class="' . $settings['form_submit_button_classname'] . '" type="submit"><span>' . __('Delete your account', 'wpu_extranet') . '</span></button>';
}
$html .= '</li>';
$html .= '</ul>';
$html .= '</form>';
$html .= '</div>';
return $html;
}

/* ----------------------------------------------------------
Example code
---------------------------------------------------------- */

/*
$html_return_delete_account = wpu_extranet_delete_account__action();
get_header();
echo '<h1>' . get_the_title() . '</h1>';
echo wpu_extranet_delete_account__form(array(
'before_fields' => $html_return_delete_account
));
get_footer();
*/
12 changes: 8 additions & 4 deletions inc/modules/edit-metas.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ function wpu_extranet_update_metas__action() {

$upload_avatar = wpu_extranet_update_metas__action__avatar($user_id);

$html_return = '';
if ($has_update || $upload_avatar) {
$html_return = '<p class="extranet-message extranet-message--success form-password-success">' . __('Profile successfully updated!', 'wpu_extranet') . '</p>';
return wpuextranet_get_html_errors(array(
__('Profile successfully updated!', 'wpu_extranet')
), array(
'form_id' => 'editmetas',
'type' => 'success'
));
}

return $html_return;
return '';
}

function wpu_extranet_update_metas__action__avatar($user_id) {
Expand Down Expand Up @@ -143,7 +147,7 @@ function wpu_extranet_update_metas__form($args = array()) {
}
$html .= wpu_extranet__display_field('wpuextranet_avatar', array(
'label' => __('Avatar', 'wpu_extranet'),
'before_content' => $avatar_script.'<div class="avatar-grid"><div>' . $avatar_img . '</div><div>',
'before_content' => $avatar_script . '<div class="avatar-grid"><div>' . $avatar_img . '</div><div>',
'after_content' => '<small id="wpu-extranet-avatar-message">' . $avatar_message . '</small></div></div>',
'attributes' => 'accept="image/png, image/jpg, image/jpeg"',
'type' => 'file',
Expand Down
18 changes: 11 additions & 7 deletions inc/modules/lost-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ function wpu_extranet_lostpassword__action() {
wp_redirect(wpu_extranet__get_dashboard_page());
die;
}
$messages = array();
$return_type = 'error';

$html_return = '';
if (isset($_GET['lostpassword']) && $_GET['lostpassword'] == 'success') {
$html_return .= '<p class="extranet-message extranet-message--success form-lostpassword-success">' . __('Check your email for the confirmation link.', 'wpu_extranet') . '</p>';
$return_type = 'success';
$messages[] = __('Check your email for the confirmation link.', 'wpu_extranet');
}
if (isset($_GET['lostpassworderror'])) {
$html_return .= '<p class="extranet-message extranet-message--error form-lostpassword-error"><strong class="error">' . __('Error:', 'wpu_extranet') . '</strong> ';
switch ($_GET['lostpassworderror']) {
case '1':
$html_return .= __('Your account could not be found.', 'wpu_extranet');
$messages[] = __('Your account could not be found.', 'wpu_extranet');
break;
default:
$html_return .= __('Reset password failed.', 'wpu_extranet');
$messages[] = __('Reset password failed.', 'wpu_extranet');
}
$html_return .= '</p>';
}

return $html_return;
return wpuextranet_get_html_errors($messages, array(
'form_id' => 'reset_password',
'type' => $return_type
));

}

/* Form HTML
Expand Down
27 changes: 18 additions & 9 deletions inc/modules/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ function wpu_extranet_register__action() {
return '';
}

$messages = array();

/* Checked honeypot */
$honeypot_id = wpu_extranet_register_get_honeypot_id();
if (isset($_POST[$honeypot_id]) && $_POST[$honeypot_id] == 1) {
return '';
}

if(isset($_POST['user_email'])){
$messages = apply_filters('wpu_extranet__email__validation', $messages, $_POST['user_email']);
}

$register_success_user_id = false;
if (!empty($_POST) && isset($_POST['user_login'], $_POST['user_email'], $_POST['user_password'])) {
$user_login = sanitize_text_field($_POST['user_login']);
Expand All @@ -84,30 +90,33 @@ function wpu_extranet_register__action() {
}
}

$html_return = '';
$return_type = 'error';

if (isset($_GET['register']) && $_GET['register'] == 'success') {
$html_return .= '<p class="extranet-message extranet-message--success form-register-success">' . __('Registration confirmation will be emailed to you.', 'wpu_extranet') . '</p>';
$return_type = 'success';
$messages[] = __('Registration confirmation will be emailed to you.', 'wpu_extranet');
}

if (isset($_GET['registererror'])) {
$html_return .= '<p class="extranet-message extranet-message--error form-register-error"><strong class="error">' . __('Error:', 'wpu_extranet') . '</strong> ';
switch ($_GET['registererror']) {
case '1':
$html_return .= __('This username already exists.', 'wpu_extranet');
$messages[] = __('This username already exists.', 'wpu_extranet');
break;
case '2':
$html_return .= __('This email is already registered.', 'wpu_extranet');
$messages[] = __('This email is already registered.', 'wpu_extranet');
break;
case '3':
$html_return .= __('This username contains invalid characters.', 'wpu_extranet');
$messages[] = __('This username contains invalid characters.', 'wpu_extranet');
break;
default:
$html_return .= __('Registration failed.', 'wpu_extranet');
$messages[] = __('Registration failed.', 'wpu_extranet');
}
$html_return .= '</p>';
}

return $html_return;
return wpuextranet_get_html_errors($messages, array(
'form_id' => 'register',
'type' => $return_type
));
}

/* Form HTML
Expand Down
2 changes: 1 addition & 1 deletion lang/wpu_extranet-fr_FR.l10n.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
return ['domain'=>NULL,'plural-forms'=>NULL,'messages'=>['All fields are required.'=>'Tous les champs sont requis.','Invalid email.'=>'E-mail invalide.','New email is the same as the current email.'=>'Le nouvel e-mail est le même que l’e-mail actuel.','Emails do not match.'=>'Les e-mails ne sont pas identiques.','Email is already in use.'=>'L’e-mail est déjà utilisé.','Email successfully updated!'=>'L’e-mail a été mis à jour avec succès !','Change email'=>'Changer d\'adresse e-mail','Your current email'=>'Votre adresse e-mail actuelle','New email'=>'Nouvel e-mail','Confirm new email'=>'Confirmez votre nouvel e-mail','Current password is incorrect.'=>'Le mot de passe actuel est incorrect.','Passwords do not match.'=>'Les mots de passe ne correspondent pas.','Password is too short, minimum of 6 characters.'=>'Le mot de passe est trop court, minimum de 6 caractères.','Password successfully changed!'=>'Mot de passe changé avec succès !','Error:'=>'Erreur :','Change password'=>'Changer le mot de passe','Enter your current password'=>'Veuillez saisir votre mot de passe actuel','New password'=>'Nouveau mot de passe','Confirm new password'=>'Confirmer le nouveau mot de passe','Profile successfully updated!'=>'Profil mis à jour avec succès!','Infos'=>'Infos','The current avatar is generated by %s.'=>'L’avatar actuel est généré par %s.','Delete this avatar'=>'Supprimer cet avatar','Avatar'=>'Avatar','Username'=>'Nom d\'utilisateur','Email'=>'E-mail','Edit my infos'=>'Modifier mes infos','Check your email for the confirmation link.'=>'Vérifiez vos courriels pour y trouver le lien de confirmation.','Your account could not be found.'=>'Votre compte est introuvable.','Reset password failed.'=>'Échec de la réinitialisation du mot de passe.','Username or Email Address'=>'Identifiant ou adresse email','Get New Password'=>'Obtenir un nouveau mot de passe','Registration confirmation will be emailed to you.'=>'La confirmation d’inscription vous sera envoyée par courriel.','This username already exists.'=>'Ce nom d’utilisateur existe déjà.','This email is already registered.'=>'Cet e-mail est déjà enregistré.','This username contains invalid characters.'=>'Ce nom d’utilisateur contient des caractères non valides.','Registration failed.'=>'Échec de l’inscription.','Password'=>'Mot de passe','Register'=>'S\'inscrire','Log out'=>'Se déconnecter','First name'=>'Prénom','Last name'=>'Nom'],'language'=>'fr_FR','x-generator'=>'Poedit 3.4.4'];
return ['domain'=>NULL,'plural-forms'=>NULL,'messages'=>['Error:'=>'Erreur :','All fields are required.'=>'Tous les champs sont requis.','Invalid email.'=>'E-mail invalide.','New email is the same as the current email.'=>'Le nouvel e-mail est le même que l’e-mail actuel.','Emails do not match.'=>'Les e-mails ne sont pas identiques.','Email is already in use.'=>'L’e-mail est déjà utilisé.','Email successfully updated!'=>'L’e-mail a été mis à jour avec succès !','Change email'=>'Changer d\'adresse e-mail','Your current email'=>'Votre adresse e-mail actuelle','New email'=>'Nouvel e-mail','Confirm new email'=>'Confirmez votre nouvel e-mail','Current password is incorrect.'=>'Le mot de passe actuel est incorrect.','Passwords do not match.'=>'Les mots de passe ne correspondent pas.','Password is too short, minimum of 6 characters.'=>'Le mot de passe est trop court, minimum de 6 caractères.','Password successfully changed!'=>'Mot de passe changé avec succès !','Change password'=>'Changer le mot de passe','Enter your current password'=>'Veuillez saisir votre mot de passe actuel','New password'=>'Nouveau mot de passe','Confirm new password'=>'Confirmer le nouveau mot de passe','You can not delete this type of account.'=>'Vous ne pouvez pas supprimer ce type de compte.','Invalid password.'=>'Mot de passe invalide.','Delete your account'=>'Supprimer votre compte','Profile successfully updated!'=>'Profil mis à jour avec succès!','Infos'=>'Infos','The current avatar is generated by %s.'=>'L’avatar actuel est généré par %s.','Delete this avatar'=>'Supprimer cet avatar','Avatar'=>'Avatar','Username'=>'Nom d\'utilisateur','Email'=>'E-mail','Edit my infos'=>'Modifier mes infos','Check your email for the confirmation link.'=>'Vérifiez vos courriels pour y trouver le lien de confirmation.','Your account could not be found.'=>'Votre compte est introuvable.','Reset password failed.'=>'Échec de la réinitialisation du mot de passe.','Username or Email Address'=>'Identifiant ou adresse email','Get New Password'=>'Obtenir un nouveau mot de passe','Registration confirmation will be emailed to you.'=>'La confirmation d’inscription vous sera envoyée par courriel.','This username already exists.'=>'Ce nom d’utilisateur existe déjà.','This email is already registered.'=>'Cet e-mail est déjà enregistré.','This username contains invalid characters.'=>'Ce nom d’utilisateur contient des caractères non valides.','Registration failed.'=>'Échec de l’inscription.','Password'=>'Mot de passe','Register'=>'S\'inscrire','Log out'=>'Se déconnecter','First name'=>'Prénom','Last name'=>'Nom','Simple toolbox to create an extranet or a customer account'=>'Boîte à outils simple pour créer un extranet ou un compte client'],'language'=>'fr_FR','x-generator'=>'Poedit 3.4.4'];
Binary file modified lang/wpu_extranet-fr_FR.mo
Binary file not shown.
Loading

0 comments on commit 6b95725

Please sign in to comment.