Files
2024_WEBSITE_fipf/plugins/fbpatch/php/patches/dates.php
2024-04-24 00:28:25 +02:00

213 lines
5.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!');
}
/*
* 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
*
*/
function try_format_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;
}
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);
/*
* 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);
/*
* modify the submited post before inserting
* - 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);
/*
* 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);
}
/*
* utility functions
*
*/
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) {
if (strpos($key, $needle) === 0) {
$keys[] = $key;
}
}
return $keys;
}
?>