Files
2024_WEBSITE_fipf/plugins/fbpatch/php/patches/dates.php

282 lines
7.0 KiB
PHP

<?php
namespace FBPATCH;
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
- when jquery choose the date format,
it also creates a hidden field with the value formated as 'yymmdd' (Ymd)
- before dfb saves the post queries,
saves the acf-date of the hidden field into a special property of the acf field
and removes it from the post-array
- jquery creates a hidden field with the value formated as 'yymmdd' (Ymd)
- before insert post in dfb :
- find the hidden field, and for which acf field it is
- save this data in the acf field
- also save the formated date in the acf field, in a class maybe ?
- add a filter for the acf field, when it displays the field, to show the formated value
../../../../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
*/
/*
* 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) {
$acf_date_class = Fbpatch::ACF_DATE_CLASS;
if ($field['type'] !== 'date_picker') {
return $null;
}
if (!str_contains($field['wrapper']['class'], $acf_date_class)) {
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) {
$keys = array();
foreach ($haystack as $key => $value) {
if (strpos($key, $needle) === 0) {
$keys[] = $key;
}
}
return $keys;
}
/*
* 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) {
$field = get_field_object($acf_id);
$field_class = $field['wrapper']['class'];
$field_classes = explode(' ', $field_class);
$class = Fbpatch::ACF_DATE_CLASS;
$field_classes = array_filter($field_classes, function($value) {
$class = Fbpatch::ACF_DATE_CLASS;
if (strpos($value, $class) !== 0) {
return $value;
}
});
$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"
*/
?>