updated acf find non init field value with new technique to init with temp value first

This commit is contained in:
asus
2024-03-19 20:15:26 +01:00
parent cbfbfabd18
commit 58d02708b9
8 changed files with 154 additions and 36 deletions

View File

@@ -42,6 +42,32 @@ if (!defined('ABSPATH')) {
function get_field_init_CIPF($acf_field, $acf_id) {
$acf_state = get_field($acf_field['_name'], $acf_id);
if ($acf_state !== null) {
return $acf_state;
}
/*
* 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'], 'temps', $acf_id);
$acf_object = get_field_object($acf_field['_name'], $acf_id);
$default = $acf_object['default_value'];
if (empty($default)) {
$choices = $acf_object['choices'];
$default = reset($choices);
}
update_field($acf_field['_name'], $default, $acf_id);
$acf_state = get_field($acf_field['_name'], $acf_id);
return $acf_state;
}
/*
@@ -58,10 +84,18 @@ function is_acf_state_CIPF($user_id = null, $acf_field, $state_name) {
/*
* when acf fields have not been initated a first time, you can't find them by name
* so use key instead
* - one solution is to use key instead
* but it means knowing the key, in my case it prevents fabien to create a field himself
* - another solution would be to :
* try if is null,
* if yes it means it was not initalize,
* then initalize with any value,
* find the default value,
* and assign it
*
*/
$acf_state = get_field($acf_field['_key'], $acf_id);
*/
$acf_state = get_field_init_CIPF($acf_field, $acf_id);;
if ($acf_state === $acf_field[$state_name]) {
return true;