added dates functions for prof card
This commit is contained in:
165
plugins/cipf_plugin/php/profs_dates.php
Normal file
165
plugins/cipf_plugin/php/profs_dates.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?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_class::debug_infos();
|
||||
$acf_card_expiration = PLGNTLS_class::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');
|
||||
$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_class::debug_infos();
|
||||
|
||||
/*
|
||||
* get date limit as DateTime object
|
||||
*
|
||||
*/
|
||||
$current_date_limit = get_date_limit_CIPF();
|
||||
if ($current_date_limit === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns diff in days
|
||||
*
|
||||
*/
|
||||
$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
|
||||
*
|
||||
*/
|
||||
function is_card_date_expired_CIPF($user_id = null) {
|
||||
PLGNTLS_class::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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function update_card_expiration_CIPF($user_id = null, $card_duration = '0') {
|
||||
PLGNTLS_class::debug_infos();
|
||||
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;
|
||||
|
||||
/*
|
||||
* 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();
|
||||
if ($current_date_limit === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* update date limit validity to add 1 year
|
||||
*
|
||||
*/
|
||||
$date_plus_one_year = $current_date_limit->add(date_interval_create_from_date_string('+'.$card_duration));
|
||||
update_field($acf_card_expiration['_name'], $date_plus_one_year->format($acf_date_format), $acf_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user