new update for readonly_acf field with better solution

This commit is contained in:
asus
2024-04-20 14:57:10 +02:00
parent f2bcc51ec0
commit 6bbedf52cb
3 changed files with 72 additions and 207 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_cipf
Plugin URI:
Description:
Author: hugogogo
Version: 0.5.10
Version: 0.5.10.1
Author URI:
*/
@@ -97,6 +97,7 @@ class Cipf {
const ACF_PARTNER_OFFER_1_OUTPUT = ['_name'=>'afficher_offre_1', 'show'=>'Afficher', 'hide'=>'Masquer'];
const ACF_PARTNER_OFFER_2_OUTPUT = ['_name'=>'afficher_offre_2', 'show'=>'Afficher', 'hide'=>'Masquer'];
const ACF_PARTNER_OFFER_3_OUTPUT = ['_name'=>'afficher_offre_3', 'show'=>'Afficher', 'hide'=>'Masquer'];
const ACF_READONLY_CLASS = 'readonly_acf';
// META
const META_PAYEMENT_STATUS = 'cipf_payement_status';

View File

@@ -35,7 +35,7 @@ if (!defined('ABSPATH')) {
{"ID":43901,"key":"field_66201b335f91a","label":"user","name":"user","aria-label":"","prefix":"acf", "type":"user", "value":null, "menu_order":22,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"role":"","return_format":"array","multiple":0,"allow_null":0,"bidirectional":0,"bidirectional_target":[],"_name":"user","_valid":1}
{"ID":43902,"key":"field_66201b3f5f91b","label":"gmap","name":"gmap","aria-label":"","prefix":"acf", "type":"google_map", "value":null, "menu_order":23,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"center_lat":-37,"center_lng":144,"zoom":"","height":"","_name":"gmap","_valid":1}
{"ID":43903,"key":"field_66201b4c5f91c","label":"date picker","name":"date_picker","aria-label":"","prefix":"acf", "type":"date_picker", "value":null, "menu_order":24,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"display_format":"d\/m\/Y","return_format":"d\/m\/Y","first_day":1,"_name":"date_picker","_valid":1}
{"ID":43904,"key":"field_66201b565f91d","label":"ate time picker","name":"ate_time_picker","aria-label":"","prefix":"acf", "type":"date_picker", "value":null,"menu_order":25,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"display_format":"d\/m\/Y","return_format":"d\/m\/Y","first_day":1,"_name":"ate_time_picker","_valid":1}
{"ID":43904,"key":"field_66201b565f91d","label":"date time picker","name":"date_time_picker","aria-label":"","prefix":"acf", "type":"date_picker", "value":null, "menu_order":25,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"display_format":"d\/m\/Y","return_format":"d\/m\/Y","first_day":1,"_name":"date_time_picker","_valid":1}
{"ID":43905,"key":"field_66201b5e5f91e","label":"time picker","name":"time_picker","aria-label":"","prefix":"acf", "type":"time_picker", "value":null, "menu_order":26,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"display_format":"g:i a","return_format":"g:i a","_name":"time_picker","_valid":1}
{"ID":43906,"key":"field_66201b685f91f","label":"color picker","name":"color_picker","aria-label":"","prefix":"acf", "type":"color_picker","value":null, "menu_order":27,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"default_value":"","enable_opacity":0,"return_format":"string","_name":"color_picker","_valid":1}
{"ID":43907,"key":"field_66201b745f920","label":"message","name":"","aria-label":"","prefix":"acf", "type":"message", "value":false,"menu_order":28,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":43775,"wrapper":{"width":"","class":"read_only_cipf","id":""},"message":"message","new_lines":"wpautop","esc_html":0,"_name":"","_valid":1}
@@ -91,12 +91,53 @@ if (!defined('ABSPATH')) {
/*
* there is one action hook suitable to close the fieldset element :
* -> ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-field-functions.php
* line 807 : do_action( 'acf/render_field', $field );
*
* acf uses priority 9 for its 'acf/render_field' hook
* so we hook before and after
*
*
*
*/
function add_fieldset_start_to_acf_disabled_CIPF($field) {
Plgntls::debug_infos();
if (contains_class_disabled_CIPF($field)) {
echo "<fieldset disabled class='acf_disabled_fieldset_cipf' style='border:none; margin:0px; padding:0px;'>\n";
}
return $field;
}
function add_fieldset_end_to_acf_disabled_CIPF($field) {
Plgntls::debug_infos();
if (!contains_class_disabled_CIPF($field)) {
return;
}
echo "</fieldset>\n";
}
add_action('acf/render_field', 'add_fieldset_start_to_acf_disabled_CIPF', 8);
add_action('acf/render_field', 'add_fieldset_end_to_acf_disabled_CIPF', 10);
/*
* checks if the acf field wrapper contains the class 'read_only_cipf'
*
*/
function contains_class_disabled_CIPF($field) {
Plgntls::debug_infos();
$readonly_class = Cipf::ACF_READONLY_CLASS;
if (!isset($field['wrapper'])) {
error_log("the acf field should have property field['wrapper']: " . json_encode($field));
@@ -107,166 +148,13 @@ function contains_class_disabled_CIPF($field) {
return false;
}
$class = $field['wrapper']['class'];
if (!str_contains($class, 'read_only_cipf')) {
if (!str_contains($class, $readonly_class)) {
return false;
}
return true;
}
/*
* checks if the acf field can be disabled
* by using a simple booleen 'disabled' property in the field object
*
*/
function acf_field_can_be_disabled_booleen_CIPF($field) {
Plgntls::debug_infos();
$type = $field['type'];
if (in_array($type, array(
'text',
'textarea',
'number',
'email',
'url',
'password',
'date_picker',
'time_picker',
'select',
))) {
return true;
}
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;
}
$choices = $field['choices'];
$to_disable = array_keys($choices);
// acf cast the strings (line 128 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-radio.php)
$to_disable = array_map(function($e){return (string)$e;}, $to_disable);
$field['disabled'] = $to_disable;
}
return $field;
}
add_filter('acf/load_field', 'disable_acf_field_CIPF');
/*
* 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
* 737 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-field-functions.php
*
*/
function change_acf_field_wrapper_CIPF($wrapper, $field) {
Plgntls::debug_infos();
if (!contains_class_disabled_CIPF($field)) {
return $wrapper;
}
if (acf_field_can_be_disabled_CIPF($field)) {
return $wrapper;
}
$wrapper["inert"] = "true";
return $wrapper;
}
add_filter('acf/field_wrapper_attributes', 'change_acf_field_wrapper_CIPF', 10, 2);
/*
* 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 prevent_acf_field_from_updating_CIPF($null, $value, $post_id, $field) {
Plgntls::debug_infos();
if (!isset($_POST['acf'])) {
return $null;
}
if (!contains_class_disabled_CIPF($field)) {
return $null;
}
return false;
}
add_filter('acf/pre_update_value', 'prevent_acf_field_from_updating_CIPF', 10, 4);

View File

@@ -105,29 +105,5 @@ add_action('template_redirect', 'partner_form_creation_page_CIPF');
/*
* actions after partner form is validated
*
*/
//function partner_after_form_CIPF($form_id, $post_array, $form_type) {
// Plgntls::debug_infos(2);
// $form_partner_id = Cipf::FORM_PARTNER_PAGE_ID;
//
// if ($form_partner_id !== $form_id) {
// return;
// }
// Plgntls::debug_infos();
//
//// error_log("form_id: " . json_encode($form_id));
//// error_log("post_array: " . json_encode($post_array));
//// error_log("form_type: " . json_encode($form_type));
//
// // should protect : if post creation there will not be an ID
// $post_id = $post_array['ID'];
//}
//add_action('df_after_process', 'partner_after_form_CIPF', 10, 3);
?>