208 lines
3.9 KiB
PHP
208 lines
3.9 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!');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
* 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 is_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;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
* 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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|