better handling of init acf field

This commit is contained in:
asus
2024-04-10 21:04:52 +02:00
parent 114c6bfe38
commit 265664537e
4 changed files with 86 additions and 33 deletions

View File

@@ -52,39 +52,89 @@ if (!defined('ABSPATH')) {
*/
/*
i* try to init acf value with
* - default value
* - first value
* also, if there is choices but default value is not set, init with first choice
* but if there is choices and default value is set, prefer default value
* if none of them are set, dont init
*
*/
function init_field_CIPF($acf_field_name, $acf_id) {
Plgntls::debug_infos();
/*
* check if need to init
*
*/
$acf_value = get_field($acf_field_name, $acf_id);
if ($acf_value !== null) {
return;
}
/*
* if need to init, check if exists
*
*/
$acf_groups = acf_get_field_groups();
$acf_object = null;
foreach ($acf_groups as $group) {
error_log("group: " . json_encode($group));
foreach (acf_get_fields($group['key']) as $field_key => $field) {
if ($field['_name'] === $acf_field_name) {
$acf_object = $field;
}
}
}
if (empty($acf_object)) {
return;
}
//error_log("acf_object: " . json_encode($acf_object));
/*
* init with best value
*
*/
$value = '';
$default = '';
$choice = '';
$should_init = false;
if (isset($acf_object['default_value'])) {
$default = $acf_object['default_value'];
$should_init = true;
}
if (isset($acf_object['choices'])) {
$choices = $acf_object['choices'];
if (!empty($choices)) {
$choice = reset($choices);
$should_init = true;
}
}
if (false === $should_init) {
return;
}
// empty returns true for 0 and '0'
if ($default == 0 || !empty($default)) {
$value = $default;
}
else if ($choice == 0 || !empty($choice)) {
$value = $choice;
}
update_field($acf_field_name, $value, $acf_id);
}
function get_field_init_CIPF($acf_field_name, $acf_id) {
Plgntls::debug_infos();
$acf_state = get_field($acf_field_name, $acf_id);
if ($acf_state !== null) {
return $acf_state;
}
init_field_CIPF($acf_field_name, $acf_id);
/*
* if get_field returns null, it means it is not initialized
* - initialize it with 'temp' value
* - then find it's default value, and update with it
* - if no default value, update with first value
*
*/
update_field($acf_field_name, '', $acf_id);
$acf_object = get_field_object($acf_field_name, $acf_id);
$default = '';
if (isset($acf_object['default_value'])) {
$default = $acf_object['default_value'];
}
else if (isset($acf_object['choices'])) {
$choices = $acf_object['choices'];
if (!empty($choices)) {
$default = reset($choices);
}
}
update_field($acf_field_name, $default, $acf_id);
$acf_state = get_field($acf_field_name, $acf_id);
return $acf_state;

View File

@@ -56,6 +56,7 @@ function partner_page_init_CIPF() {
}
Plgntls::debug_infos();
// https://developer.wordpress.org/reference/functions/get_query_var/#more-information
global $wp;
$wp->add_query_var('action');

View File

@@ -128,6 +128,7 @@ function handle_prof_account_deletion_CIPF($user_id) {
function handle_reminders_before_card_expire_CIPF($user_id) {
Plgntls::debug_infos();
$card_reminders = Cipf::DURATION_REMINDERS_BEFORE_ACCOUNT_EXPIRE;
$emails_reminders = Cipf:: ACF_EMAILS_REMINDERS;
if (!isset_acf_card_expiration_CIPF($user_id)) {
return;
@@ -151,9 +152,9 @@ function handle_reminders_before_card_expire_CIPF($user_id) {
$diff = abs($current_date_diff - $date_diff);
if ($diff === 0)) {
send_emails_CIPF('card_will_expire', $user_id);
return; // don't send multiple emails
if ($diff === 0) {
// send_emails_CIPF('card_will_expire', $user_id);
// return; // don't send multiple emails
}
}
}
@@ -196,8 +197,8 @@ function handle_reminders_before_account_deleted_CIPF($user_id) {
// i add +3 in case there is a difference of maximum 3 days between a 31 days month and a 28 days february
if (abs($current_date_diff) <= (abs($date_diff) + 3)) {
send_emails_CIPF('account_will_be_deleted', $user_id);
return; // don't send multiple emails
// send_emails_CIPF('account_will_be_deleted', $user_id);
// return; // don't send multiple emails
}
}
}