new date handling
This commit is contained in:
@@ -85,7 +85,7 @@ function schedule_prof_account_deletion_CIPF($user_id) {
|
||||
Plgntls::debug_infos();
|
||||
// $duration_deletion = Cipf::DURATION_ACCOUNT_DELETE_AFTER_EXPIRE;
|
||||
//
|
||||
// $date_limit = get_date_limit_CIPF($user_id);
|
||||
// $date_limit = get_card_date_expiration_CIPF($user_id);
|
||||
// if (false === $date_limit) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
223
plugins/cipf_plugin/php/_utils_acf_dates.php
Normal file
223
plugins/cipf_plugin/php/_utils_acf_dates.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* returns the date limit as a DateTime object
|
||||
* or false if not set
|
||||
*
|
||||
*/
|
||||
function get_date_CIPF($acf_date, $user_id) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
$acf_id = 'user_'.$user_id;
|
||||
|
||||
/*
|
||||
* get acf date field
|
||||
*
|
||||
*/
|
||||
$current_date_object = get_field_object($acf_date['_name'], $acf_id);
|
||||
if ($current_date_object === false) {
|
||||
return false;
|
||||
}
|
||||
else if (empty($current_date_object['value'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* create date object from acf date
|
||||
*
|
||||
*/
|
||||
$current_date_string = $current_date_object['value'];
|
||||
$current_format_field = $current_date_object['return_format'];
|
||||
// compare 2 dates : https://stackoverflow.com/q/8722806/9497573
|
||||
// also I dont use strtotime to compare 2 ints,
|
||||
// because i don't know if it will fail one day (2000y bug style)
|
||||
$current_date = date_create_from_format($current_format_field, $current_date_string);
|
||||
if ($current_date === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns the date object
|
||||
*
|
||||
*/
|
||||
return $current_date;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* returns true if date is in paste or is empty
|
||||
*
|
||||
*/
|
||||
function is_date_expired_CIPF($date) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
if (empty($date)) {
|
||||
return true;
|
||||
}
|
||||
if (!$date instanceof DateTimeInterface) {
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* check if expired
|
||||
*
|
||||
*/
|
||||
$date_now = date_create('today');
|
||||
$date_diff = date_diff($date_now, $date);
|
||||
$date_diff = (int)$date_diff->format('%R%a');
|
||||
|
||||
if ($date_diff < 0) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* add duration to date object
|
||||
* duration is expected in a string format : https://www.php.net/manual/en/class.dateinterval.php
|
||||
*
|
||||
*/
|
||||
function get_new_date_CIPF($date, $duration) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
$current_date = $date;
|
||||
// empty return true for false, null, empty string and array, and 0
|
||||
if (empty($date)) {
|
||||
$current_date = date_create('today');
|
||||
}
|
||||
else if (!$current_date instanceof DateTimeInterface) {
|
||||
$current_date = date_create('today');
|
||||
}
|
||||
|
||||
/*
|
||||
* update date limit validity to add 1 year
|
||||
*
|
||||
*/
|
||||
return $new_date = $current_date->add(date_interval_create_from_date_string($duration));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* update acf field with new date
|
||||
*
|
||||
*/
|
||||
function set_acf_date_CIPF($acf_date_field_to_update, $new_date, $user_id) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
$acf_id = 'user_'.$user_id;
|
||||
$acf_date_format = 'Y-m-d H:i:s';
|
||||
|
||||
set_acf_field_CIPF($acf_date_field_to_update, $new_date->format($acf_date_format), $acf_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* card date expiration
|
||||
*
|
||||
*/
|
||||
function get_card_date_expiration_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
return get_date_CIPF($acf_card_expiration, $user_id);
|
||||
}
|
||||
function is_card_date_expired_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
$card_date = get_card_date_expiration_CIPF($user_id);
|
||||
return is_date_expired_CIPF($card_date);
|
||||
}
|
||||
function isset_acf_card_expiration_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$acf_id = 'user_'.$user_id;
|
||||
return isset_acf_field_CIPF($acf_card_expiration, $acf_id);
|
||||
}
|
||||
function update_card_date_expiration_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_field = Cipf::ACF_CARD_EXPIRATION;
|
||||
$card_duration = Cipf::DURATION_CARD_VALIDITY;
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
$date = get_card_date_expiration_CIPF($user_id);
|
||||
$new_date = get_new_date_CIPF($date, $card_duration);
|
||||
set_acf_date_CIPF($acf_card_field, $new_date, $user_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* prof account deletion
|
||||
|
||||
- get prof deletion date
|
||||
- is prof deletion date expired
|
||||
|
||||
*
|
||||
*/
|
||||
function get_prof_accound_deletion_date_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$delete_duration = Cipf::DURATION_ACCOUNT_DELETE_AFTER_EXPIRE;
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$card_expiration_date = get_card_date_expiration_CIPF($user_id);
|
||||
return get_new_date_CIPF($card_expiration_date, $delete_duration);
|
||||
}
|
||||
function is_prof_account_deletion_expired_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
$account_date = get_prof_accound_deletion_date_CIPF($user_id);
|
||||
return is_date_expired_CIPF($account_date);
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
@@ -44,10 +44,6 @@ if (!defined('ABSPATH')) {
|
||||
*
|
||||
* [/] order id ('order_id')
|
||||
*
|
||||
* [/] card expiration date ('fin_de_validite')
|
||||
*
|
||||
* [/] prof deletion date ('date_suppression_compte')
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -545,25 +541,6 @@ function reset_acf_cgv_CIPF($user_id = null) {
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* fin_de_validite
|
||||
*
|
||||
*/
|
||||
function isset_acf_card_expiration_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$acf_id = 'user_'.$user_id;
|
||||
return isset_acf_field_CIPF($acf_card_expiration, $acf_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* date_suppression_compte
|
||||
|
||||
@@ -141,7 +141,7 @@ function success_payment_for_user_CIPF($user_id, $order_id) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
schedule_delete_orderid_CIPF($order_id, $user_id);
|
||||
update_card_expiration_CIPF($user_id);
|
||||
update_card_date_expiration_CIPF($user_id);
|
||||
|
||||
if (is_card_new_CIPF()) {
|
||||
set_card_number_CIPF($user_id);
|
||||
|
||||
@@ -1,208 +0,0 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* returns the date limit as a DateTime object
|
||||
* or false if not set
|
||||
*
|
||||
*/
|
||||
function get_date_limit_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
|
||||
/*
|
||||
* define ids
|
||||
*
|
||||
*/
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$acf_id = 'user_'.$user_id;
|
||||
|
||||
/*
|
||||
* get acf date field
|
||||
*
|
||||
*/
|
||||
$current_date_limit_object = get_field_object($acf_card_expiration['_name'], $acf_id);
|
||||
if ($current_date_limit_object === false) {
|
||||
return false;
|
||||
}
|
||||
else if (empty($current_date_limit_object['value'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* create date object from acf date
|
||||
*
|
||||
*/
|
||||
$current_date_limit_string = $current_date_limit_object['value'];
|
||||
$current_format_field = $current_date_limit_object['return_format'];
|
||||
// compare 2 dates : https://stackoverflow.com/q/8722806/9497573
|
||||
// also I dont use strtotime to compare 2 ints,
|
||||
// because i don't know if it will fail one day (2000y bug style)
|
||||
$current_date_limit = date_create_from_format($current_format_field, $current_date_limit_string);
|
||||
if ($current_date_limit === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns the date object
|
||||
*
|
||||
*/
|
||||
return $current_date_limit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* returns the diff in days between today and the expiration date of the card
|
||||
* returns false if the expiration date field is empty
|
||||
*
|
||||
*/
|
||||
function card_date_diff_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
/*
|
||||
* get date limit as DateTime object
|
||||
*
|
||||
*/
|
||||
$current_date_limit = get_date_limit_CIPF($user_id);
|
||||
if ($current_date_limit === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns diff in days
|
||||
*
|
||||
*/
|
||||
$date_now = date_create('today');
|
||||
$date_diff = date_diff($date_now, $current_date_limit);
|
||||
return (int)$date_diff->format('%R%a');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* returns true if date is in paste or is empty
|
||||
* use card_date_exists_CIPF to check if the field is empty
|
||||
*
|
||||
*/
|
||||
function is_card_date_expired_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
/*
|
||||
* define user_id
|
||||
*
|
||||
*/
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
/*
|
||||
* check if expired
|
||||
*
|
||||
*/
|
||||
$date_diff = card_date_diff_CIPF($user_id);
|
||||
if ($date_diff === false) {
|
||||
return true;
|
||||
}
|
||||
else if ($date_diff < 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* returns true if date is empty
|
||||
*
|
||||
*/
|
||||
function card_date_exists_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
|
||||
/*
|
||||
* define ids
|
||||
*
|
||||
*/
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$acf_id = 'user_'.$user_id;
|
||||
|
||||
/*
|
||||
* get acf date field
|
||||
*
|
||||
*/
|
||||
$date_now = date_create('today');
|
||||
// // we dont use utility fonction 'get_acf_field_CIPF' because we want to know if it was not init. Do we ?
|
||||
// $current_date = get_field($acf_card_expiration['_name'], $acf_id);
|
||||
//
|
||||
// if (empty($current_date)) {
|
||||
// return false;
|
||||
// }
|
||||
// if (is_null($current_date)) {
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
return isset_acf_card_expiration_CIPF($user_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* will add card_duration to card
|
||||
* card_duration is expected in a string format : https://www.php.net/manual/en/class.dateinterval.php
|
||||
*
|
||||
*/
|
||||
function update_card_expiration_CIPF($user_id = null) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_card_expiration = Cipf::ACF_CARD_EXPIRATION;
|
||||
$card_duration = Cipf::CARD_VALIDITY_TIME;
|
||||
|
||||
/*
|
||||
* define acf id and acf date format
|
||||
*
|
||||
*/
|
||||
if (is_null($user_id)) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
$acf_id = 'user_'.$user_id;
|
||||
$acf_date_format = 'Y-m-d H:i:s';
|
||||
|
||||
/*
|
||||
* get date limit as DateTime object
|
||||
*
|
||||
*/
|
||||
$current_date_limit = get_date_limit_CIPF($user_id);
|
||||
if ($current_date_limit === false) {
|
||||
$current_date_limit = date_create('today');
|
||||
}
|
||||
|
||||
/*
|
||||
* update date limit validity to add 1 year
|
||||
*
|
||||
*/
|
||||
$date_plus_one_year = $current_date_limit->add(date_interval_create_from_date_string('+'.$card_duration));
|
||||
set_acf_field_CIPF($acf_card_expiration, $date_plus_one_year->format($acf_date_format), $acf_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -44,7 +44,7 @@ function handle_transfert_validation_CIPF($user_id) {
|
||||
*
|
||||
*/
|
||||
set_account_valid_CIPF($user_id);
|
||||
update_card_expiration_CIPF($user_id);
|
||||
update_card_date_expiration_CIPF($user_id);
|
||||
set_card_number_CIPF($user_id);
|
||||
set_card_renew_CIPF($user_id);
|
||||
send_emails_CIPF('transfert_success', $user_id);
|
||||
@@ -60,7 +60,7 @@ function handle_transfert_validation_CIPF($user_id) {
|
||||
*/
|
||||
function handle_card_expire_CIPF($user_id) {
|
||||
Plgntls::debug_infos();
|
||||
if (false === card_date_exists_CIPF($user_id)) {
|
||||
if (false === isset_acf_card_expiration_CIPF($user_id)) {
|
||||
return;
|
||||
}
|
||||
if (is_card_date_expired_CIPF($user_id)) {
|
||||
|
||||
Reference in New Issue
Block a user