events actions and email for profs are now well supported, without repeatitions

This commit is contained in:
asus
2024-04-11 14:36:44 +02:00
parent f2ca863dcb
commit 6aa3915ef9
4 changed files with 52 additions and 14 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_cipf
Plugin URI:
Description:
Author: hugogogo
Version: 0.5.6.1
Version: 0.5.7
Author URI:
*/

View File

@@ -28,7 +28,7 @@ if (!defined('ABSPATH')) {
function prepare_emails_CIPF($email_name, $user_id) {
Plgntls::debug_infos();
$emails_option_object = Cipf::OPTION_EMAILS;
if (!isset($emails_option_object['_default'][$email_name])) {
return false;
}
@@ -59,12 +59,13 @@ function prepare_emails_CIPF($email_name, $user_id) {
$tmp_email['message'] = $email['notification_message'];
$tmp_email['headers'] = array();
$tmp_email['headers'][] = $header_content_type;
$tmp_email['headers'][] = 'User_id: '.$user_id;
$from = $email['notification_from'];
if (empty($from)) {
$from = get_option('admin_email');
}
if (!empty($from)) {
$tmp_email['headers'][] = 'From:'.$from;
$tmp_email['headers'][] = 'From: '.$from;
}
$emails[] = $tmp_email;
}
@@ -75,12 +76,13 @@ function prepare_emails_CIPF($email_name, $user_id) {
$tmp_email['message'] = $email['confirmation_message'];
$tmp_email['headers'] = array();
$tmp_email['headers'][] = $header_content_type;
$tmp_email['headers'][] = 'User_id: '.$user_id;
$from = $email['confirmation_from'];
if (empty($from)) {
$from = get_option('admin_email');
}
if (!empty($from)) {
$tmp_email['headers'][] = 'From:'.$from;
$tmp_email['headers'][] = 'From: '.$from;
}
$emails[] = $tmp_email;
}
@@ -93,12 +95,11 @@ function send_emails_CIPF($email_name, $user_id = null) {
Plgntls::debug_infos();
$emails = prepare_emails_CIPF($email_name, $user_id);
if (false === $emails) {
error_log('Email preparing failed!: ' . json_encode($emails));
error_log('Email preparing failed!: ' . json_encode($email_name));
return;
}
foreach ($emails as $email) {
error_log('Email: ' . json_encode($email));
$sent = wp_mail($email['to'], $email['subject'], $email['message'], $email['headers']);
if (!$sent) {
error_log('Email sending failed!: ' . json_encode($email));

View File

@@ -71,7 +71,10 @@ function handle_card_expire_CIPF($user_id) {
else {
set_account_expired_CIPF($user_id);
}
send_emails_CIPF('account_expired', $user_id);
if (!is_email_reminder_choice_CIPF('card_expired', $user_id)) {
set_email_reminder_choice_CIPF('card_expired', $user_id);
send_emails_CIPF('card_expired', $user_id);
}
}
else {
if (is_account_waiting_transfert_CIPF($user_id)) {
@@ -81,6 +84,7 @@ function handle_card_expire_CIPF($user_id) {
set_account_valid_CIPF($user_id);
}
reset_emails_reminders_deletion_account_CIPF($user_id);
unset_email_reminder_choice_CIPF('card_expired', $user_id);
}
}
@@ -228,9 +232,6 @@ function handle_reminders_before_account_deleted_CIPF($user_id) {
if (is_email_reminder_choice_CIPF($reminder_key, $user_id)) {
continue;
}
/*
unset_email_reminder_choice_CIPF($reminder_key, $user_id);
*/
set_email_reminder_choice_CIPF($reminder_key, $user_id);
send_emails_CIPF('account_will_be_deleted', $user_id);
return; // don't send multiple emails

View File

@@ -21,8 +21,8 @@ function replace_words($matches, $user_id = null) {
$current_user = get_user_by('id', $user_id);
}
else if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
}
else {
return "";
@@ -90,16 +90,24 @@ function filter_email_wp($args) {
// pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/';
$user_id = \CUSTER\find_user_id_from_headers($args);
$old_to = $args['to'];
$new_to = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_to);
$new_to = preg_replace_callback($pattern, function($matches) use ($user_id) {
return \CUSTER\replace_words($matches, $user_id);
}, $old_to);
$args['to'] = $new_to;
$old_subject = $args['subject'];
$new_subject = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_subject);
$new_subject = preg_replace_callback($pattern, function($matches) use ($user_id) {
return \CUSTER\replace_words($matches, $user_id);
}, $old_subject);
$args['subject'] = $new_subject;
$old_message = $args['message'];
$new_message = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_message);
$new_message = preg_replace_callback($pattern, function($matches) use ($user_id) {
return \CUSTER\replace_words($matches, $user_id);
}, $old_message);
$args['message'] = $new_message;
return $args;
@@ -127,4 +135,32 @@ add_filter('wp_new_user_notification_email', __NAMESPACE__.'\filter_regitration_
function find_user_id_from_headers(&$args) {
if (empty($args)) {
return null;
}
if (!isset($args['headers'])) {
return null;
}
$user_id = null;
$headers = $args['headers'];
foreach ($headers as $key => $header) {
$explode_header = explode(':', $header);
$explode_header = array_map('trim', $explode_header);
if ($explode_header[0] === 'User_id') {
$user_id = $explode_header[1];
unset($headers[$key]);
$headers = array_values($headers);
}
}
$args['headers'] = $headers;
return $user_id;
}
?>