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'; $dependencies = array('jquery-ui-datepicker'); $version = null; $defer = false; wp_enqueue_script($handle, $url, $dependencies, $version, $defer); } add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_dates_patch', 22); 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; } 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; } /* * function acf_update_field( $field, $specific = array() ) {} * 980 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-field-functions.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 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_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" */ ?>