Files
2024_WORDPRESS_PLUGIN_fbpatch/php/patches/dates.php
2024-07-17 15:30:32 +02:00

185 lines
5.7 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!');
}
/*
* what I did :
* - in front, keep the acf date input with text date,
* but add a second hidden field with acf format date 'Ymd'
* - in back, saving the text date in the acf field,
* but keeping the acf date 'Ymd' in an option
* - in the same time, adding a class to the field group
* (available to all instances of this field)
* to maek it as using a text field
* - this way, if accessed the date with metadata,
* we get the text date
* - then, filter the value when loading,
* to use the acf format date 'Ymd' instead of the text date
* - and finally filter the format step
* (which is not triggered by admin output)
* to prevent formating if accessed by divi
* (so it will print the metadata instead)
*
*/
/*
* 1. this function will replace the date value by the one in acf format
* for dates created by divi form builder
*
* 118 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-value-functions.php
*
* we can either use filter 'acf/load_value' or 'acf/pre_load_value'
* - 'acf/pre_load_value' :
* it fires first, and will short-circuit the function to get the value
* it is useful to force the acf_date value to be always the same,
* otherwise acf uses some kind of cach system,
* so the value would be evaluated once then it would always be the same on the page : maybe it's alright ?
* - 'acf/load_value' :
* using this filter, the value is cached, as explained above
* - note : the first argument '$value' is actually a little misleading :
* - in case of the second filter, 'acf/load_value',
* it is indeed tha value that has already been retrieve and that we can override
* - but for the first filter, 'acf/pre_load_value', it is actually 'null',
* and if returned it will let the function go on to try to retrieve the value
*
*/
//function try_get_acf_date($value, $post_id, $field) {
// $acf_date_class = Fbpatch::ACF_DATE_CLASS;
// if ($field['type'] !== 'date_picker') {
// return $value;
// }
// if (!str_contains($field['wrapper']['class'], $acf_date_class)) {
// return $value;
// }
//
// $acf_date = Fbpatch::get_acf_date($post_id, $field['key']);
// if ($acf_date === false) {
// return $value;
// }
//
// return $acf_date;
//}
//add_filter('acf/load_value', __NAMESPACE__.'\try_get_acf_date', 10, 3);
////add_filter('acf/pre_load_value', __NAMESPACE__.'\try_get_acf_date', 10, 3);
/*
* load a jquery script to create a hidden field with the acf 'Ymd' date format
* jquery date picker :
* 7509 : ../../../../wordpress_docker/volumes/wp-volumes/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php
*
*/
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 view_date_post($form_id, $post_array) {
error_log("post_array: " . json_encode($post_array));
}
add_action('df_before_insert_post', __NAMESPACE__.'\view_date_post', 10, 2);
/*
* action about the submited post before inserting (we cannot modify it unfortunately)
* - find any datepicker
* - add class to their object to identify they are used by form builder
* - save the date in acf format 'Ymd' in the options
*
* -> 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) {
// $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\add_date_to_acf_object_class($acf_key, $acf_date);
// Fbpatch::update_acf_date($post_array['ID'], $acf_key, $acf_date);
// }
//}
//add_action('df_before_insert_post', __NAMESPACE__.'\process_form_acf_dates', 10, 2);
//
//
///*
//* to update a field object (so, globally)
//* using `acf_update_field`
//* -> 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);
//}
//
//
//
//
//
///*
//* here i use it to check if there are hidden field for acf dates
//*
//*/
//function array_has_keys_starting_with($needle, &$haystack) {
// $keys = array();
// foreach ($haystack as $key => $value) {
// if (strpos($key, $needle) === 0) {
// $keys[] = $key;
// }
// }
// return $keys;
//}
?>