update acf field handling to centerize every call

This commit is contained in:
asus
2024-04-08 21:43:08 +02:00
parent 1e291d16ba
commit 4c0e4c001f
12 changed files with 538 additions and 126 deletions

View File

@@ -40,7 +40,7 @@ include_once(Plgntls::root_path() . 'php/partners_register.php');
include_once(Plgntls::root_path() . 'php/partners_page.php');
include_once(Plgntls::root_path() . 'php/partners_form.php');
// utils
include_once(Plgntls::root_path() . 'php/_utils_states.php');
include_once(Plgntls::root_path() . 'php/_utils_acf_fields.php');
include_once(Plgntls::root_path() . 'php/_utils_redirections.php');
include_once(Plgntls::root_path() . 'php/_utils_display_css.php');
include_once(Plgntls::root_path() . 'php/_utils_checks_roles.php');
@@ -81,6 +81,7 @@ class Cipf {
const ACF_ACCOUNT_STATE = ['_name'=>'etat_compte', 'new'=>'nouveau prof', 'to_pay'=>'doit payer', 'valid'=>'carte valide', 'waiting_invalid'=>'en attente invalide', 'waiting_valid'=>'en attente valide', 'expired'=>'carte expiree'];
const ACF_TRANSFERT_STATE = ['_name'=>'etat_virement', 'success'=>'virement validé'];
const ACF_PAGE_STATE = ['_name'=>'etat_page_partenaire', 'publish'=>'Publiee', 'draft'=>'Brouillon'];
const ACF_CARD_ORDER_ID = ['_name'=>'order_id'];
// META
const META_PAYEMENT_STATUS = 'cipf_payement_status';
@@ -129,6 +130,15 @@ class Cipf {
// SHORTCODES
// SCHEDULED EVENTS
const DURATION_OLD_ORDER_ID = '3 days';
const DURATION_ACCOUNT_DELETE_AFTER_EXPIRE = '6 months';
const DURATION_REMINDER_BEFORE_ACCOUNT_EXPIRE_FIRST = '1 month';
const DURATION_REMINDER_BEFORE_ACCOUNT_EXPIRE_LAST = '7 days';
const DURATION_REMINDER_BEFORE_ACCOUNT_DELETE_FIRST = '1 month';
const DURATION_REMINDER_BEFORE_ACCOUNT_DELETE_LAST = '7 days';
const DURATION_REMINDER_BEFORE_OFFER_EXPIRE = '7 days';
// OPTIONS
const OPTION_PAYPAL = [
'_name'=>'cipf_paypal_credentials',

View File

@@ -33,33 +33,15 @@ if (!defined('ABSPATH')) {
/*
* events and emails :
*
* - payments : : suppress old order_ids
* - partners : email : offres temporaires -> gerer qu'elles disparaissent apres la date de validite -> la passer en masquer
* - partners : email : la gestion des offres à échéance
* - payments : email : schedule event pour supprimer le compte xx temps (6 mois ?) apres fin de validite de la carte
* - payments : email : schedule event pour supprimer les codes
* - payments : email : schedule event pour desactiver la carte
* - payments : email : faire rappels emails
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* 1. [/] payments : TIME_OLD_ORDER_ID - suppress old order_ids
* 2. [ ] prof (email) : TIME_ACCOUNT_DELETE_AFTER_EXPIRE - schedule event to suppress the account xx time (6 month ?) after end of validity of the card, and supress the codes
* 3. [ ] prof (email) : - schedule event to deactivate the card
* 4. [ ] prof (email) : TIME_REMINDER_BEFORE_ACCOUNT_EXPIRE_FIRST - send emails reminder before account deactivate
* 5. [ ] prof (email) : TIME_REMINDER_BEFORE_ACCOUNT_EXPIRE_LAST - send emails reminder before account deactivate
* 6. [ ] prof (email) : TIME_REMINDER_BEFORE_ACCOUNT_DELETE_FIRST - send emails reminder before account delete
* 7. [ ] prof (email) : TIME_REMINDER_BEFORE_ACCOUNT_DELETE_LAST - send emails reminder before account delete
* 8. [ ] partner (email) : TIME_REMINDER_BEFORE_OFFER_EXPIRE - send emails reminder before offer ends
* 9. [ ] partners (email) : - temp offers -> make them disappeay after end card validity -> put them in hidden
*
*/
@@ -67,7 +49,7 @@ if (!defined('ABSPATH')) {
/*
* add a schedule event to delete this order_id
* 1. prof, add a schedule event to delete this order_id
* after 3 days ?
*
*/
@@ -85,6 +67,135 @@ add_action('orderid_deletion_event_CIPF', 'delete_order_id_later_CIPF', 10, 2);
/*
* 2. prof, suppress account XX time after expiration, also email
*
*/
function schedule_prof_account_suppression_CIPF($user_id) {
Plgntls::debug_infos();
$duration_deletion = Cipf::DURATION_ACCOUNT_DELETE_AFTER_EXPIRE;
$date_limit = get_date_limit_CIPF($user_id);
$time_limit = $date_limit->getTimestamp();
//$date_interval = date_interval_create_from_date_string($duration_deletion);
//$date_deletion = $date_limit->add($date_interval);
$date_deletion = $date_limit->modify('+'.$duration_deletion);
$time_deletion = $date_deletion->getTimestamp();
//error_log("duration_deletion: " . json_encode($duration_deletion));
//error_log("date_limit : " . json_encode($date_limit));
//error_log("time_limit : " . json_encode($time_limit));
////error_log("date_interval: " . json_encode($date_interval));
//error_log("date_deletion: " . json_encode($date_deletion));
//error_log("time_deletion: " . json_encode($time_deletion));
/*
duration_deletion: "6 months"
date_limit : {"date":"2094-09-19 15:33:35.000000","timezone_type":3,"timezone":"UTC"}
time_limit : 3919851215
date_deletion: {"date":"2094-09-19 15:33:35.000000","timezone_type":3,"timezone":"UTC"}
time_deletion: 3935748815
duration_deletion: "6 months"
date_limit : {"date":"2094-09-19 15:32:03.000000","timezone_type":3,"timezone":"UTC"}
time_limit : 3919851123
date_deletion: {"date":"2094-09-19 15:32:03.000000","timezone_type":3,"timezone":"UTC"}
time_deletion: 3935748723
*/
//$delay = strtotime('+3 days');
// wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
//function event_CIPF($user_id) {
// Plgntls::debug_infos();
//}
//add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
/*
* 3. prof, deactivate card at expiration time, also email
*
function schedule__CIPF($user_id) {
Plgntls::debug_infos();
$delay = strtotime('+3 days');
wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
function event_CIPF($user_id) {
Plgntls::debug_infos();
}
add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
*/
/*
* 4. && 5. prof, send email reminder before account expire
*
function schedule__CIPF($user_id) {
Plgntls::debug_infos();
$delay = strtotime('+3 days');
wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
function event_CIPF($user_id) {
Plgntls::debug_infos();
}
add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
*/
/*
* 6. && 7. prof, send email reminder account deletion
*
function schedule__CIPF($user_id) {
Plgntls::debug_infos();
$delay = strtotime('+3 days');
wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
function event_CIPF($user_id) {
Plgntls::debug_infos();
}
add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
*/
/*
* 8. partner, send email before offer expire
*
function schedule__CIPF($user_id) {
Plgntls::debug_infos();
$delay = strtotime('+3 days');
wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
function event_CIPF($user_id) {
Plgntls::debug_infos();
}
add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
*/
/*
* 9. partner, handle offer expire, also sens email
*
function schedule__CIPF($user_id) {
Plgntls::debug_infos();
$delay = strtotime('+3 days');
wp_schedule_single_event($delay, 'scedule_event_CIPF', array($user_id));
}
function event_CIPF($user_id) {
Plgntls::debug_infos();
}
add_action('schedule_event_CIPF', 'event_CIPF', 10, 2);
*/

View File

@@ -42,6 +42,8 @@ if (!defined('ABSPATH')) {
*
* [/] cgv
*
* [/] order_id
*
*
*/
@@ -83,7 +85,7 @@ function get_field_init_CIPF($acf_field_name, $acf_id) {
* global 'setter' and 'izzer' for this file
*
*/
function is_acf_state_CIPF($acf_field, $state_name, $acf_id) {
function is_acf_field_CIPF($acf_field, $acf_value, $acf_id) {
Plgntls::debug_infos();
/*
@@ -101,14 +103,49 @@ function is_acf_state_CIPF($acf_field, $state_name, $acf_id) {
*/
$acf_state = get_field_init_CIPF($acf_field['_name'], $acf_id);;
if ($acf_state === $acf_field[$state_name]) {
if ($acf_state === $acf_field[$acf_value]) {
return true;
}
return false;
}
function set_acf_state_CIPF($acf_field, $state_name, $acf_id) {
/*
* some values are predefined in the plugin class for acf fields
*
*/
function is_predefined_acf_state($acf_field, $acf_value) {
Plgntls::debug_infos();
update_field($acf_field['_name'], $acf_field[$state_name], $acf_id);
if (empty($acf_value)) {
return false;
}
if (!is_string($acf_value)) {
return false;
}
if(isset($acf_field[$acf_value])) {
return true;
}
return false;
}
function set_acf_field_CIPF($acf_field, $acf_value, $acf_id) {
Plgntls::debug_infos();
/*
* if acf_value is the name of a key in const acf_field,
* then we want to get its value from there,
* otherwise we just use it's value directly
*
*/
if (is_predefined_acf_state($acf_field, $acf_value)) {
$value = $acf_field[$acf_value];
}
else {
$value = $acf_value;
}
error_log("acf_field: " . json_encode($acf_field));
update_field($acf_field['_name'], $value, $acf_id);
}
function get_acf_field_CIPF($acf_field, $state_name, $acf_id) {
Plgntls::debug_infos();
return get_field_init_CIPF($acf_field, $acf_id);
}
@@ -136,7 +173,7 @@ function is_account_state_CIPF($state_name, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return is_acf_state_CIPF($acf_account_state, $state_name, $acf_id);
return is_acf_field_CIPF($acf_account_state, $state_name, $acf_id);
}
function is_account_new_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -183,7 +220,7 @@ function set_account_state_CIPF($state_name, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
set_acf_state_CIPF($acf_account_state, $state_name, $acf_id);
set_acf_field_CIPF($acf_account_state, $state_name, $acf_id);
}
function set_account_new_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -228,7 +265,7 @@ function is_card_state_CIPF($state, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return is_acf_state_CIPF($acf_card_state, $state, $acf_id);
return is_acf_field_CIPF($acf_card_state, $state, $acf_id);
}
function is_card_new_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -248,7 +285,7 @@ function set_card_state_CIPF($state, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
set_acf_state_CIPF($acf_card_state, $state, $acf_id);
set_acf_field_CIPF($acf_card_state, $state, $acf_id);
}
function set_card_new_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -277,7 +314,7 @@ function is_page_state_CIPF($state, $post_id = null) {
$post_id = get_the_ID();
}
$acf_id = $post_id;
return is_acf_state_CIPF($acf_page_state, $state, $acf_id);
return is_acf_field_CIPF($acf_page_state, $state, $acf_id);
}
function is_page_publish_CIPF($post_id = null) {
Plgntls::debug_infos();
@@ -297,7 +334,7 @@ function set_page_state_CIPF($state, $post_id = null) {
$post_id = get_the_ID();
}
$acf_id = $post_id;
set_acf_state_CIPF($acf_page_state, $state, $acf_id);
set_acf_field_CIPF($acf_page_state, $state, $acf_id);
}
function set_page_publish_CIPF($post_id = null) {
Plgntls::debug_infos();
@@ -326,7 +363,7 @@ function is_card_method_CIPF($state, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return is_acf_state_CIPF($acf_card_payment_method, $state, $acf_id);
return is_acf_field_CIPF($acf_card_payment_method, $state, $acf_id);
}
function is_payment_method_paypal_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -359,7 +396,7 @@ function is_payment_state_CIPF($type, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return is_acf_state_CIPF($acf_card_payment_state, $type, $acf_id);
return is_acf_field_CIPF($acf_card_payment_state, $type, $acf_id);
}
function is_payment_started_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -387,7 +424,7 @@ function set_payment_state_CIPF($type, $user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
set_acf_state_CIPF($acf_card_payment_state, $type, $acf_id);
set_acf_field_CIPF($acf_card_payment_state, $type, $acf_id);
}
function set_payment_started_CIPF($user_id = null) {
Plgntls::debug_infos();
@@ -425,7 +462,7 @@ function set_card_number_CIPF($user_id = null) {
$date_now = date_create('today');
$card_id = $date_now->format('Ymd') . $user_id;
update_field($acf_card_number['_name'], $card_id, $acf_id);
set_acf_field_CIPF($acf_card_number, $card_id, $acf_id);
}
@@ -444,7 +481,46 @@ function reset_acf_cgv_CIPF($user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
update_field($acf_cgv['_name'], array(""), $acf_id);
set_acf_field_CIPF($acf_cgv, array(""), $acf_id);
}
/*
* order_id
*
*/
function reset_acf_order_id_CIPF($user_id = null) {
Plgntls::debug_infos();
$acf_order_id = Cipf::ACF_CARD_ORDER_ID;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
set_acf_field_CIPF($acf_order_id, "", $acf_id);
}
function get_acf_order_id_CIPF($user_id = null) {
Plgntls::debug_infos();
$acf_order_id = Cipf::ACF_CARD_ORDER_ID;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return get_acf_field_CIPF($acf_order_id, $acf_id);
}
function set_acf_order_id_CIPF($order_id, $user_id = null) {
Plgntls::debug_infos();
$acf_order_id = Cipf::ACF_CARD_ORDER_ID;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
set_acf_field_CIPF($acf_order_id, $order_id, $acf_id);
}
@@ -463,6 +539,7 @@ function is_transfert_success_CIPF($user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
// we dont use the utility function 'get_acf_field_CIPF()' because we want to know if it was never init. Do we ?
$transfert_state = get_field($acf_transfert_state['_name'], $acf_id);
@@ -489,7 +566,7 @@ function reset_acf_transfert_CIPF($user_id = null) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
update_field($acf_transfert_state['_name'], array(""), $acf_id);
set_acf_field_CIPF($acf_transfert_state, array(""), $acf_id);
}

View File

@@ -226,7 +226,7 @@ function toggle_partner_page_CIPF() {
}
add_action('template_redirect', 'toggle_partner_page_CIPF');
add_action('template_redirect', 'toggle_partner_page_CIPF', 5);

View File

@@ -38,20 +38,17 @@ if (!defined('ABSPATH')) {
function update_user_pre_order_CIPF($message) {
Plgntls::debug_infos();
$acf_payment_status = Cipf::ACF_CARD_PAYMENT_STATE;
$meta_order_id = Cipf::META_ORDER_ID;
$order_id = $message->id;
$user_id = get_current_user_id();
$acf_id = 'user_'.$user_id;
/*
* - delete previous order ids
* ->if we are here it's because a new order will try to be created
* - addind order_id to cipf_order_id meta field
* set new order_id
* -> it will delete the previous one
* -> if we are here it's because a new order will try to be created
*
*/
delete_user_meta($user_id, $meta_order_id);
add_user_meta($user_id, $meta_order_id, $order_id);
set_acf_order_id_CIPF($order_id, $user_id);
/*
* create a meta field to check states of payements on prof author page :
@@ -77,8 +74,9 @@ function update_user_post_capture_CIPF($message) {
$order_id = $message->id;
$user_id = get_current_user_id();
$status = null;
if (is_object($message) && isset($message->status))
if (is_object($message) && isset($message->status)) {
$status = $message->status;
}
/*
* capture status : https://developer.paypal.com/docs/api/orders/v2/#definition-order_status
@@ -96,18 +94,14 @@ function update_user_post_capture_CIPF($message) {
* -> it is at least necessary to find the user who did the purchase
*
*/
// find the user containing the order_id and delete this order_id
$user_id_to_update = find_user_with_order_id_CIPF($user_id, $order_id);
if ($user_id_to_update === false)
if ($user_id_to_update === false) {
throw new HttpException('cannot find user with this order_id', 502);
}
if ($status === 'COMPLETED') {
// proceed to validate payment for user
success_payment_for_user_CIPF($user_id_to_update, $order_id);
}
else if ($status === 'PENDING') {
// i don't know what to do yet, maybe make more checks and output a message to contact diego ?
// https://developer.paypal.com/docs/api/orders/v2/#definition-capture_status_details
}
else {
failure_payment_for_user_CIPF($user_id_to_update, $order_id, $status);
}
@@ -129,17 +123,10 @@ function update_user_post_capture_CIPF($message) {
*/
function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
Plgntls::debug_infos();
$meta_order_id = Cipf::META_ORDER_ID;
$acf_id = 'user_'.$user_id;
/*
* remove the order_id from user meta
*
*/
delete_user_meta($user_id, $meta_order_id, $order_id);
// schedule_delete_orderid_CIPF($user_id, $order_id)
set_payment_failure_CIPF($user_id);
set_account_to_pay_CIPF($user_id);
send_emails_CIPF('payment_echec', $user_id);
@@ -152,18 +139,11 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
*/
function success_payment_for_user_CIPF($user_id, $order_id) {
Plgntls::debug_infos();
$meta_order_id = Cipf::META_ORDER_ID;
$acf_id = 'user_'.$user_id;
/*
* remove the order_id from user meta
*
*/
delete_user_meta($user_id, $meta_order_id, $order_id);
// schedule_delete_orderid_CIPF($user_id, $order_id)
update_card_expiration_CIPF($user_id);
if (is_card_new_CIPF()) {
@@ -188,10 +168,10 @@ function success_payment_for_user_CIPF($user_id, $order_id) {
*/
function find_user_with_order_id_CIPF($current_user_id, $order_id) {
Plgntls::debug_infos();
$meta_order_id = Cipf::META_ORDER_ID;
$acf_order_id = Cipf::ACF_CARD_ORDER_ID;
$role_prof = Cipf::ROLE_PROF;
$user_metas = get_user_meta($current_user_id, $meta_order_id, false);
$user_metas = get_user_meta($current_user_id, $acf_order_id['_name'], false);
if (in_array($order_id, $user_metas)) {
return $current_user_id;
}
@@ -202,16 +182,18 @@ function find_user_with_order_id_CIPF($current_user_id, $order_id) {
* https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/
*
*/
$users = get_users(array(
$user = get_users(array(
'role' => $role_prof,
'meta_key' => $meta_order_id,
'meta_key' => $acf_order_id,
'meta_value' => $order_id,
'fields' => 'ID',
));
if (count($users) > 1)
if (count($user) > 1) {
throw new HttpException('multiple users with same order_id', 502);
else if (count($users) === 1)
return reset($users);
}
else if (count($user) === 1) {
return reset($user);
}
return false;
}

View File

@@ -54,6 +54,10 @@ function check_can_pay_CIPF() {
$user_id = get_current_user_id();
$acf_id = 'user_' . $user_id;
schedule_prof_account_suppression_CIPF($user_id);
/*
* check if payment is virement or immediat
*

View File

@@ -148,6 +148,7 @@ function card_date_exists_CIPF($user_id = null) {
*
*/
$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)) {
@@ -196,7 +197,7 @@ function update_card_expiration_CIPF($user_id = null) {
*
*/
$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);
set_acf_field_CIPF($acf_card_expiration, $date_plus_one_year->format($acf_date_format), $acf_id);
}