fixed pbm with field update prevention, but not sure if robust
This commit is contained in:
@@ -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();
|
||||
|
||||
if (!isset($field['wrapper'])) {
|
||||
error_log("the acf field should have property field['wrapper']: " . json_encode($field));
|
||||
return false;
|
||||
}
|
||||
if (!isset($field['wrapper']['class'])) {
|
||||
error_log("the acf field should have property field['wrapper']['class']: " . json_encode($field));
|
||||
return false;
|
||||
}
|
||||
$class = $field['wrapper']['class'];
|
||||
@@ -112,25 +114,13 @@ function is_acf_field_disabled($field) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 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 'client_side_disabled'
|
||||
* - then use other filters to complete the work : 'acf/field_wrapper_attributes' and 'acf/pre_update_value'
|
||||
* checks if the acf field can be disabled
|
||||
* by using a simple booleen 'disabled' property in the field object
|
||||
*
|
||||
*/
|
||||
function disable_acf_field_CIPF($field) {
|
||||
function acf_field_can_be_disabled_booleen_CIPF($field) {
|
||||
Plgntls::debug_infos();
|
||||
|
||||
if (!is_acf_field_disabled($field)) {
|
||||
return $field;
|
||||
}
|
||||
|
||||
|
||||
$type = $field['type'];
|
||||
if (in_array($type, array(
|
||||
'text',
|
||||
@@ -143,12 +133,67 @@ function disable_acf_field_CIPF($field) {
|
||||
'time_picker',
|
||||
'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',
|
||||
'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'])) {
|
||||
return $field;
|
||||
}
|
||||
@@ -158,9 +203,6 @@ function disable_acf_field_CIPF($field) {
|
||||
$to_disable = array_map(function($e){return (string)$e;}, $to_disable);
|
||||
$field['disabled'] = $to_disable;
|
||||
}
|
||||
else {
|
||||
$field['client_side_disabled'] = 1;
|
||||
}
|
||||
|
||||
return $field;
|
||||
}
|
||||
@@ -171,8 +213,7 @@ add_filter('acf/load_field', 'disable_acf_field_CIPF');
|
||||
|
||||
|
||||
/*
|
||||
* filter the wrapper of the acf field
|
||||
* if it has the property 'client_side_disabled',
|
||||
* if wrapper class contains 'inert',
|
||||
* -> add inert attribute : it will prevent any action from front,
|
||||
* -> but it will not prevent from being saved with form
|
||||
* -> 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
error_log("+field: " . json_encode($field));
|
||||
$wrapper["inert"] = "true";
|
||||
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
|
||||
* -> and _POST[acf] is set : it means it comes from front
|
||||
* -> dont update the values
|
||||
* -> should work for all acf fields
|
||||
* 200 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-value-functions.php
|
||||
* this is the filter that prevent acf fields from beeing
|
||||
*
|
||||
* if _POST[acf] not set, field is not updated from front, so don't prevent update
|
||||
* if field does not contains class 'disabled' (or whatever it is), don't prevent update
|
||||
* 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();
|
||||
|
||||
if (isset($field['client_side_disabled'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($_POST['acf'])) {
|
||||
return $null;
|
||||
}
|
||||
if (!contains_class_disabled_CIPF($field)) {
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user