fixed pbm with field update prevention, but not sure if robust

This commit is contained in:
asus
2024-04-18 14:18:19 +02:00
parent 782d45f115
commit f2bcc51ec0

View File

@@ -92,16 +92,18 @@ if (!defined('ABSPATH')) {
/* /*
* checks if the acf field contains the class 'read_only_cipf' * checks if the acf field wrapper contains the class 'read_only_cipf'
* *
*/ */
function is_acf_field_disabled($field) { function contains_class_disabled_CIPF($field) {
Plgntls::debug_infos(); Plgntls::debug_infos();
if (!isset($field['wrapper'])) { if (!isset($field['wrapper'])) {
error_log("the acf field should have property field['wrapper']: " . json_encode($field));
return false; return false;
} }
if (!isset($field['wrapper']['class'])) { if (!isset($field['wrapper']['class'])) {
error_log("the acf field should have property field['wrapper']['class']: " . json_encode($field));
return false; return false;
} }
$class = $field['wrapper']['class']; $class = $field['wrapper']['class'];
@@ -112,25 +114,13 @@ function is_acf_field_disabled($field) {
return true; return true;
} }
/* /*
* if acf field has class to be disabled, * checks if the acf field can be disabled
* -> add a disabled value to object after it is loaded * by using a simple booleen 'disabled' property in the field object
* -> the way to do it depends on the field type
* -> for the fields that dont have a default mechanisme :
* - add 'client_side_disabled'
* - then use other filters to complete the work : 'acf/field_wrapper_attributes' and 'acf/pre_update_value'
* *
*/ */
function disable_acf_field_CIPF($field) { function acf_field_can_be_disabled_booleen_CIPF($field) {
Plgntls::debug_infos(); Plgntls::debug_infos();
if (!is_acf_field_disabled($field)) {
return $field;
}
$type = $field['type']; $type = $field['type'];
if (in_array($type, array( if (in_array($type, array(
'text', 'text',
@@ -143,12 +133,67 @@ function disable_acf_field_CIPF($field) {
'time_picker', 'time_picker',
'select', 'select',
))) { ))) {
$field['disabled'] = 1; return true;
} }
else if (in_array($type, array( return false;
}
/*
* checks if the acf field can be disabled
* by using an array as 'disabled' property in the field object
*
*/
function acf_field_can_be_disabled_array_CIPF($field) {
Plgntls::debug_infos();
$type = $field['type'];
if (in_array($type, array(
'checkbox', 'checkbox',
'radio', 'radio',
))) { ))) {
return true;
}
return false;
}
/*
* check if acf field can be disabled
* whatever the method
*
*/
function acf_field_can_be_disabled_CIPF($field) {
Plgntls::debug_infos();
if (acf_field_can_be_disabled_booleen_CIPF($field)) {
return true;
}
if (acf_field_can_be_disabled_array_CIPF($field)) {
return true;
}
return false;
}
/*
* if acf field has class to be disabled,
* -> add a disabled value to object after it is loaded
* -> the way to do it depends on the field type
* -> for the fields that dont have a default mechanisme :
* - add 'inert' class to wrapper if not alreadt there
* - then use other filters to complete the work : 'acf/field_wrapper_attributes' and 'acf/pre_update_value'
*
*/
function disable_acf_field_CIPF($field) {
Plgntls::debug_infos();
if (!contains_class_disabled_CIPF($field)) {
return $field;
}
if (acf_field_can_be_disabled_booleen_CIPF($field)) {
$field['disabled'] = 1;
}
else if (acf_field_can_be_disabled_array_CIPF($field)) {
if (!isset($field['choices'])) { if (!isset($field['choices'])) {
return $field; return $field;
} }
@@ -158,9 +203,6 @@ function disable_acf_field_CIPF($field) {
$to_disable = array_map(function($e){return (string)$e;}, $to_disable); $to_disable = array_map(function($e){return (string)$e;}, $to_disable);
$field['disabled'] = $to_disable; $field['disabled'] = $to_disable;
} }
else {
$field['client_side_disabled'] = 1;
}
return $field; return $field;
} }
@@ -171,8 +213,7 @@ add_filter('acf/load_field', 'disable_acf_field_CIPF');
/* /*
* filter the wrapper of the acf field * if wrapper class contains 'inert',
* if it has the property 'client_side_disabled',
* -> add inert attribute : it will prevent any action from front, * -> add inert attribute : it will prevent any action from front,
* -> but it will not prevent from being saved with form * -> but it will not prevent from being saved with form
* -> see filter 'acf/pre_update_value' for that * -> see filter 'acf/pre_update_value' for that
@@ -182,11 +223,13 @@ add_filter('acf/load_field', 'disable_acf_field_CIPF');
function change_acf_field_wrapper_CIPF($wrapper, $field) { function change_acf_field_wrapper_CIPF($wrapper, $field) {
Plgntls::debug_infos(); Plgntls::debug_infos();
if (!isset($field['client_side_disabled'])) { if (!contains_class_disabled_CIPF($field)) {
return $wrapper;
}
if (acf_field_can_be_disabled_CIPF($field)) {
return $wrapper; return $wrapper;
} }
error_log("+field: " . json_encode($field));
$wrapper["inert"] = "true"; $wrapper["inert"] = "true";
return $wrapper; return $wrapper;
} }
@@ -196,24 +239,28 @@ add_filter('acf/field_wrapper_attributes', 'change_acf_field_wrapper_CIPF', 10,
/* /*
* if acf field has class to be disabled * this is the filter that prevent acf fields from beeing
* -> and _POST[acf] is set : it means it comes from front *
* -> dont update the values * if _POST[acf] not set, field is not updated from front, so don't prevent update
* -> should work for all acf fields * if field does not contains class 'disabled' (or whatever it is), don't prevent update
* 200 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-value-functions.php * if field can use a native acf way to be disabled, don't prevent update
*
* -> 200 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-value-functions.php
* *
*/ */
function disable_all_acf_field_CIPF($null, $value, $post_id, $field) { function prevent_acf_field_from_updating_CIPF($null, $value, $post_id, $field) {
Plgntls::debug_infos(); Plgntls::debug_infos();
if (isset($field['client_side_disabled'])) { if (!isset($_POST['acf'])) {
return false; return $null;
} }
if (!contains_class_disabled_CIPF($field)) {
return $null; return $null;
} }
add_filter('acf/pre_update_value', 'disable_all_acf_field_CIPF', 10, 4);
return false;
}
add_filter('acf/pre_update_value', 'prevent_acf_field_from_updating_CIPF', 10, 4);