From 1447fdbc8e382d67737d07674a65d358343d3107 Mon Sep 17 00:00:00 2001 From: asus Date: Tue, 23 Apr 2024 13:11:10 +0200 Subject: [PATCH] wip adding date to class was a mistake since it is global --- plugins/fbpatch/js/dates.js | 34 ++-- plugins/fbpatch/php/fbpatch_class.php | 3 +- plugins/fbpatch/php/patches/dates.php | 280 ++++++++++++++++++++++---- 3 files changed, 260 insertions(+), 57 deletions(-) diff --git a/plugins/fbpatch/js/dates.js b/plugins/fbpatch/js/dates.js index 639d6cb..3d4a958 100644 --- a/plugins/fbpatch/js/dates.js +++ b/plugins/fbpatch/js/dates.js @@ -3,30 +3,36 @@ * -> https://stackoverflow.com/questions/26667720/how-to-get-the-selected-date-from-jquery-datepicker * then create an hidden input with the date in acf format 'yymmdd' * +* another solution would be to find the elements with the datepicker, and add the onSelect here +* */ // store the original jQuery UI datepicker function const original_datepicker = jQuery.fn.datepicker; // override the datepicker function jQuery.fn.datepicker = function(options) { + /* + * first get the target element and create the hidden input + * + */ + const hidden_id = 'acf_date_hidden_' + this.attr('id'); + const hidden_name = 'acf_date_hidden_for_' + this.attr('name'); + if (jQuery('#' + hidden_id).length === 0) { + jQuery(``).insertAfter(this); + } + + /* + * then override the options to add action on select date : + * -> store the date in acf format in the hidden field value + * + */ options.onSelect = function(date_text, inst) { const acf_date_format = "yymmdd"; const selected_date = jQuery(this).datepicker('getDate'); + console.log("-- selected_date:", selected_date); const formated_date = jQuery.datepicker.formatDate(acf_date_format, selected_date); - const instance_element = document.getElementById(inst.id); - console.log("instance_element:", instance_element); - const hidden_field_id = inst.id + '_acf_date_hidden'; - if (document.getElementById(hidden_field_id) === null) { - var acf_date = document.createElement("input"); - acf_date.setAttribute('type', 'hidden'); - acf_date.setAttribute('id', hidden_field_id); - acf_date.setAttribute('name', 'acf_date_hidden_for_' + instance_element.name); - instance_element.insertAdjacentElement('afterend', acf_date, acf_date); - } - else { - var acf_date = document.getElementById(hidden_field_id); - } - acf_date.value = formated_date; + const hidden_id = 'acf_date_hidden_' + inst.id; + jQuery('#' + hidden_id).val(formated_date); } // call the original datepicker function with the updated option diff --git a/plugins/fbpatch/php/fbpatch_class.php b/plugins/fbpatch/php/fbpatch_class.php index 150c45f..178ac4d 100644 --- a/plugins/fbpatch/php/fbpatch_class.php +++ b/plugins/fbpatch/php/fbpatch_class.php @@ -20,6 +20,7 @@ class Fbpatch { const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_fbpatch', 'show'=>'show', 'hide'=>'hide']; const NONCE = ['_name'=>'nonce_name', '_action'=>'action_name']; const ADMIN_POST_PATCH_CHOICE = 'add_patches'; + const ACF_DATE_PREFIX = 'dfb_date_'; /* * get path an url from plugin root @@ -44,7 +45,7 @@ class Fbpatch { private static $_patches = [ '_name'=>'fbpatch_list_of_patches', - 'dates' => ['checked'=>true, 'title'=>'dates', 'description'=>"gerer des dates dans n'importe quels formats"], + 'dates' => ['checked'=>true, 'title'=>'dates', 'description'=>"gerer des dates pour acf dans n'importe quels formats"], 'calculations'=> ['checked'=>true, 'title'=>'calculations', 'description'=>"afficher le total des calculs dès l'ouverture des formulaires"], 'hide_show' => ['checked'=>true, 'title'=>'masquer les offres', 'description'=>"permettre de masquer les offres en editant un formulaire, sans les supprimer"], 'modals' => ['checked'=>false, 'title'=>'modals', 'description'=>"permettre plusieurs modals sur une meme page"], diff --git a/plugins/fbpatch/php/patches/dates.php b/plugins/fbpatch/php/patches/dates.php index 79b51f6..218d7e3 100644 --- a/plugins/fbpatch/php/patches/dates.php +++ b/plugins/fbpatch/php/patches/dates.php @@ -9,6 +9,7 @@ if (!defined('ABSPATH')) { } + /* - when jquery choose the date format, @@ -27,13 +28,41 @@ if (!defined('ABSPATH')) { ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/scripts/builder-bundle.min.js jquery date picker : -7509 : wordpress_docker/volumes/wp-volumes/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php +7509 : ../../../../wordpress_docker/volumes/wp-volumes/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php */ +/* +* prevent format date when output +* otherwise, a date as text 'monday 3 october 2024' will output as '03/10/2024' +* 147 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-value-functions.php +* -> the 'dfb_date' class is automatically added when submiting diviFormBuilder datepicker form +* +*/ +function prevent_format_for_acf_date_picker($null, $value, $post_id, $field, $escape_html) { + $prefix = Fbpatch::ACF_DATE_PREFIX; + if ($field['type'] !== 'date_picker') { + return $null; + } + if (!str_contains($field['wrapper']['class'], $prefix)) { + return $null; + } + return false; +} +add_filter('acf/pre_format_value', __NAMESPACE__.'\prevent_format_for_acf_date_picker', 10, 5); + + + + + + +/* +* load a jquery script to create a hidden field with the acf 'Ymd' date format +* +*/ function add_form_builder_dates_patch() { $handle = 'form_builder_dates_patch'; $url = Fbpatch::root_url() . '/js/dates.js'; @@ -46,55 +75,222 @@ add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_dates_patch', -/* -function add_form_builder_calculations_patch() { - $handle = 'form_builder_calculations_patch'; - $url = Fbpatch::root_url() . '/js/calculations.js'; - $dependencies = array('de_fb_calc'); - $version = null; - $defer = true; - wp_enqueue_script($handle, $url, $dependencies, $version, $defer); + + +function array_has_keys_starting_with($needle, &$haystack, $remove = false) { + $keys = array(); + foreach ($haystack as $key => $value) { + if (strpos($key, $needle) === 0) { + $keys[] = $key; + if ($remove) { + unset($haystack[$key]); + } + } + } + return $keys; } -add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_calculations_patch', 22); -*/ + +function array_has_values_starting_with($needle, &$haystack, $remove = false) { + $keys = array(); + foreach ($haystack as $key => $value) { + if (strpos($value, $needle) === 0) { + $values[] = $value; + if ($remove) { + unset($haystack[$key]); + } + } + } + return $values; +} + /* -* actions after partner form is validated +* function acf_update_field( $field, $specific = array() ) {} +* 980 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-field-functions.php * -* -> the date is output in edit form - 7505 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php +*/ +function add_date_to_acf_object_class($acf_id, $date) { + $prefix = Fbpatch::ACF_DATE_PREFIX; + $field = get_field_object($acf_id); + $field_class = $field['wrapper']['class']; + $field_classes = explode(' ', $field_class); + $class = $prefix . $date; + + $dates_class = array_has_values_starting_with($prefix, $field_classes, true); + $previous_date = reset($dates_class); + if (empty($date) && !empty($previous_date)) { + $class = $previous_date; + } + + $field_classes[] = $class; + $implode_class = implode(' ', $field_classes); + $field['wrapper']['class'] = $implode_class; + acf_update_field($field); +} + + + + +function update_acf_date($acf_date, $acf_key, $post_id, $new_date_text) { +// error_log("get_field_object: " . json_encode(get_field_object($acf_key, $post_id))); +// error_log("--get_field : " . json_encode(get_field($acf_key, $post_id))); +// error_log("get_post_meta : " . json_encode(get_post_meta($post_id, 'date_1'))); + + \FBPATCH\add_date_to_acf_object_class($acf_key, $acf_date); +} +/* +acf_date_object: { + "ID":43981, + "key":"field_66212132b126b", + "label":"date_test", + "name":"date_test", + "aria-label":"", + "prefix":"acf", + "type":"date_picker", + "value":null, + "menu_order":0, + "instructions":"", + "required":0, + "id":"", + "class":"", + "conditional_logic":0, + "parent":43980, + "wrapper":{"width":"","class":"","id":""}, + "display_format":"d\/m\/Y", + "return_format":"d\/m\/Y", + "first_day":1, + "_name":"date_test", + "_valid":1 +} +*/ + + + +/* +* modify the post to +* +* -> the date is output in edit form - 7505 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php * -> 845 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php -*/ -function partner_after_form_CIPF($form_id, $post_array, $form_type) { -// error_log("form_id: " . json_encode($form_id)); -// error_log("post_array: " . json_encode($post_array)); -// error_log("form_type: " . json_encode($form_type)); -} -//add_action('df_after_process', 'partner_after_form_CIPF', 10, 3); - - - -/* -* -> 506 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php -*/ -function partner_before_insert_post_CIPF($form_id, $post_array) { -// error_log("form_id: " . json_encode($form_id)); -// error_log("post_array: " . json_encode($post_array)); -// error_log("form_type: " . json_encode($form_type)); -} -//add_action('df_before_insert_post', 'partner_before_insert_post_CIPF', 10, 2); - - -/* -* -> 558 : ../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php -* post_array: {"post_status":"publish","meta_input":{"text":"testimon","_text":"field_662131b2b0e68","date_test":"20240427","_date_test":"field_66212132b126b"},"field_title":["text","date"],"ID":"43978","form_type_confirm":"","post_type":"post","tax_input":[],"post_date":"2024-04-18 15:32:43"} * */ -function partner_after_insert_post_CIPF($form_id, $post_id, $post_array) { -// error_log("form_id : " . json_encode($form_id)); -// error_log("post_id : " . json_encode($post_id)); -// error_log("post_array: " . json_encode($post_array)); +function process_form_acf_dates($form_id, $post_array) { +//error_log('post_array: ' . json_encode($post_array)); + $acf_field_start = 'acf_date_hidden_for_'; + $acf_date_fields = \FBPATCH\array_has_keys_starting_with($acf_field_start, $post_array); + if (empty($acf_date_fields)) { + return; + } + + /* + * + * + */ + foreach ($acf_date_fields as $field) { + $acf_date = $post_array[$field]; + $acf_field = substr($field, strlen($acf_field_start)); + $new_date_text = $post_array['meta_input'][$acf_field]; + $acf_key = $post_array['meta_input']['_'.$acf_field]; + \FBPATCH\update_acf_date($acf_date, $acf_key, $post_array['ID'], $new_date_text); + } } -//add_action('df_after_insert_post', 'partner_after_insert_post_CIPF', 10, 3); +//add_action('df_before_process', __NAMESPACE__.'\process_form_acf_dates', 10, 3); +//add_action('df_after_process', __NAMESPACE__.'\process_form_acf_dates', 10, 3); +add_action('df_before_insert_post', __NAMESPACE__.'\process_form_acf_dates', 10, 2); + +/* + +- before inser : +post_array: { + "post_status":"publish", + "field_title":["title","date 1","date 2"], + "post_title":"post_date_dfb", + "meta_input":{ + "date_1":"mercredi 10 avril 2024", + "_date_1":"field_66212132b126b", + "date_2":"04\/04\/2024", + "_date_2":"field_662619cfff3a1" + }, + "acf_date_hidden_for_date_1":"20240410", + "acf_date_hidden_for_date_2":"", + "ID":"44121", + "form_type_confirm":"", + "post_type":"post", + "tax_input":[], + "post_date":"2024-04-22 13:39:13" +} + + +- before process : +form_id: "" +post_array: { + "post_status":"publish", + "field_title":["title","date"], + "post_title":"edit_date_dfb", + "meta_input":["de_fb_date_test"], + "date_test":"dimanche 02 avril 2023", + "ID":"43978", + "form_type_confirm":"" +} +form_type: "post" + + +after: + +form_id: "" +post_array: { + "post_status":"publish", + "field_title":["title","date"], + "post_title":"edit_date_dfb", + "meta_input":{"date_test":"dimanche 02 avril 2023","_date_test":"field_66212132b126b"}, + "ID":"43978", + "form_type_confirm":"", + "post_type":"post", + "tax_input":[], + "post_date":"2024-04-18 15:32:43" +} +form_type: "post" + +--------------- + +before : + +form_id: "" +post_array: { + "post_status":"publish", + "field_title":["title","date"], + "post_title":"edit_date_dfb", + "meta_input":["de_fb_date_test"], + "date_test":"dimanche 16 avril 2023", + "acf_date_hidden_for_date_test":"20230416", + "ID":"43978", + "form_type_confirm":"" +} +form_type: "post" + + +after : + +form_id: "" +post_array: { + "post_status":"publish", + "field_title":["title","date"], + "post_title":"edit_date_dfb", + "meta_input":{ + "date_test":"dimanche 16 avril 2023", + "_date_test":"field_66212132b126b" + }, + "acf_date_hidden_for_date_test":"20230416", + "ID":"43978", + "form_type_confirm":"", + "post_type":"post", + "tax_input":[], + "post_date":"2024-04-18 15:32:43" +} +form_type: "post" +*/ + + +