fbpatch better version of dates patch

This commit is contained in:
asus
2024-04-24 11:17:35 +02:00
parent 24afcffae4
commit 2397b24b57
2 changed files with 53 additions and 73 deletions

View File

@@ -10,6 +10,26 @@ if (!defined('ABSPATH')) {
/*
* 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
* -> except id accessed by divi
* - 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)
*
*/
@@ -35,48 +55,11 @@ function try_format_acf_date($value, $post_id, $field) {
return $value;
}
if (\FBPATCH\is_get_field_from_divi()) {
return $value;
}
return $acf_date;
}
add_filter('acf/load_value', __NAMESPACE__.'\try_format_acf_date', 10, 3);
/*
* 2. this function will prevent format date when output, if get field is from divi
*
* the acf field value is now the right format Ymd, but the metadata is still the date text
* if the field is called by divi, returns false and divi will use the metadata
*
* 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;
}
if (!\FBPATCH\is_get_field_from_divi()) {
return $null;
}
return false;
}
add_filter('acf/pre_format_value', __NAMESPACE__.'\prevent_format_for_acf_date_picker', 10, 5);
@@ -102,13 +85,8 @@ add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_dates_patch',
/*
* modify the submited post before inserting
* 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
@@ -137,8 +115,9 @@ add_action('df_before_insert_post', __NAMESPACE__.'\process_form_acf_dates', 10,
/*
* function acf_update_field( $field, $specific = array() )
* 980 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/advanced-custom-fields/includes/acf-field-functions.php
* 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) {
@@ -164,37 +143,10 @@ function add_date_to_acf_object_class($acf_id) {
/*
* utility functions
* here i use it to check if there are hidden field for acf dates
*
*/
function is_get_field_from_divi() {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$get_field_file = null;
foreach ($backtrace as $trace) {
if ($trace['function'] === "get_field" || $trace['function'] === "get_field_object") {
$get_field_file = $trace['file'];
break;
}
}
if (str_contains($get_field_file, '/Divi/')) {
return true;
}
else {
return false;
}
}
function array_has_keys_starting_with($needle, &$haystack) {
$keys = array();
foreach ($haystack as $key => $value) {