Files
2024_WEBSITE_fipf/plugins/cipf_plugin/php/_actions_emails.php

204 lines
5.3 KiB
PHP

<?php
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
* events and emails :
*
* 1. [/] profs : payment_success : paypal payment success
* 2. [/] profs : payment_echec : paypal payment echec
* 3. [/] profs : transfert_success : validation transfert prof reussi, send email
* 4. [ ] partners : offer_expired : offres temporaires -> gerer qu'elles disparaissent apres la date de validite -> la passer en masquer
* 5. [ ] partners : offer_will_expire : la gestion des offres à échéance
* 6. [ ] prof : account_deleted : schedule event pour supprimer le compte xx temps (6 mois ?) apres fin de validite de la carte
* 7. [ ] prof : account_will_expire : faire rappels emails avant expiration
* 8. [ ] prof : account_expired : desactiver carte expiree
*
*/
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;
}
$header_content_type = 'Content-Type: text/html; charset=UTF-8';
$emails_option = Plgntls::get_option_safe($emails_option_object);
if (!$emails_option) {
return false;
}
if (!isset($emails_option[$email_name])) {
return false;
}
$email = $emails_option[$email_name];
$emails = array();
if (isset($email['notification_send']) && $email['notification_send'] === 'on') {
$tmp_email = array();
$tmp_email['to'] = $email['notification_to'];
$tmp_email['subject'] = $email['notification_subject'];
$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;
}
$emails[] = $tmp_email;
}
if (isset($email['confirmation_send']) && $email['confirmation_send'] === 'on') {
$tmp_email = array();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
if ($user_id === 0) {
return false;
}
$user = get_user_by('id', $user_id);
$user_email = $user->user_email;
$tmp_email['to'] = $user_email;
$tmp_email['subject'] = $email['confirmation_subject'];
$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;
}
$emails[] = $tmp_email;
}
return $emails;
}
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($email_name));
return;
}
foreach ($emails as $email) {
$sent = wp_mail($email['to'], $email['subject'], $email['message'], $email['headers']);
if (!$sent) {
error_log('Email sending failed!: ' . json_encode($email));
}
}
}
/*
* get the acf reminders,
* - continue the ones not for the reminder_type
* - continue if date is not yet soon enough
* - continue if email was already sent
*
*/
function handle_send_reminders_CIPF($acf_id, $user_id, $date_limit, $reminder_type, $email_type) {
$is_before_reminders = true;
$reminders_choices = get_emails_reminders_choices_CIPF($acf_id);
foreach ($reminders_choices as $reminder_key => $value) {
$duration = get_duration_CIPF($reminder_key, $reminder_type);
if (false === $duration) {
continue;
}
$diff = compare_diff_with_today_CIPF($date_limit, $duration);
if ($diff > 0) {
continue;
}
$is_before_reminders = false;
if (is_email_reminder_choice_CIPF($reminder_key, $acf_id)) {
continue;
}
set_email_reminder_choice_CIPF($reminder_key, $acf_id);
send_emails_CIPF($email_type, $user_id);
return; // don't send multiple emails
}
/*
* if didnt return, it either means that :
* - today is before any reminder
* - today is after all reminders
* use 'is_before_reminders' to know
*
*/
if ($is_before_reminders) {
reset_emails_reminders_box_CIPF($reminder_type, $acf_id);
}
}
/*
* extract the duration
* returns false if not for the right type
*
*/
function get_duration_CIPF($reminder, $type) {
Plgntls::debug_infos();
$explode_reminder = explode('/', $reminder);
$explode_reminder = array_map('trim', $explode_reminder);
if ($explode_reminder[0] !== $type) {
return false;
}
$duration = $explode_reminder[1];
return $duration;
}
/*
* find the diffs :
* - the intended diff between the deletion date and the reminder
* - the diff between the deletion date and today
* and return the difference
*
*/
function compare_diff_with_today_CIPF($date, $duration) {
Plgntls::debug_infos();
$date_reminder = $date->modify($duration);
$date_diff = date_diff($date, $date_reminder);
$date_diff = (int)$date_diff->format('%R%a');
$date_now = date_create('now');
$current_date_diff = date_diff($date, $date_now);
$current_date_diff = (int)$current_date_diff->format('%R%a');
return ($date_diff - $current_date_diff);
}
?>