fixed error in fbpatch dates

This commit is contained in:
asus
2024-04-25 16:24:33 +02:00
parent 101d27be55
commit d402e9a7e3
2 changed files with 65 additions and 6 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_fbpatch
Plugin URI:
Description: some patchs for the form_builder plugin's bugs
Author: hugogogo
Version: 0.2.2
Version: 0.2.2.1
Author URI:
*/

View File

@@ -1,3 +1,4 @@
/*
* overriding the datepicker function to intercept the arguments
* -> https://stackoverflow.com/questions/26667720/how-to-get-the-selected-date-from-jquery-datepicker
@@ -39,22 +40,25 @@ jQuery.fn.datepicker = function(options) {
};
/*
* creates the false element
* place it above the real one to hide it
*
*/
function create_fake_date_input(options, element) {
async function create_fake_date_input(options, element) {
const fake_id = 'acf_date_fake_' + element.attr('id');
// false element already exists
if (jQuery('#' + fake_id).length !== 0) {
return;
}
const fake_name = 'acf_date_fake_for_' + element.attr('name');
const original_date_string = element.val();
const original_date_string = await get_acf_date_from_rest(element);
const fake_value = original_date_string;
/*
* we position the hidden element right in top of the real datepicker one
* it might nor work in some situation, but so far it's good
@@ -62,10 +66,9 @@ function create_fake_date_input(options, element) {
*
*/
let fake_style = `
border: 1px solid blue;
position: absolute;
top: 0px;
left: 100px;
left: 0px;
pointer-events: none;
`;
const font_weight = element.css('font-weight');
@@ -73,6 +76,7 @@ function create_fake_date_input(options, element) {
fake_style += `font-weight: ${font_weight};`;
}
/*
* gives the parent element a defined position if needed
* then create the false input above the real one
@@ -84,3 +88,58 @@ function create_fake_date_input(options, element) {
jQuery(`<input type="text" id="${fake_id}" name="${fake_name}" style="${fake_style}" value="${fake_value}">`).insertAfter(element);
}
/*
* use the rest api to get the formated value for the date field
*
*/
async function get_acf_date_from_rest(element) {
const pid = getUrlParameter('pid');
const prefix = "de_fb_";
const name = element.attr('name')
let acf_name = name.substring(prefix.length);
let date = element.val(); // default date, not formated, if fetch call fails
let fetch_data = null;
try {
const response = await fetch(`/wp-json/wp/v2/posts/${pid}?_fields=acf.${acf_name}&acf_format=standard`);
fetch_data = await response.json();
tmp_date = fetch_data.acf[acf_name];
if (tmp_date) {
date = tmp_date;
}
}
catch (error) {
console.error('Error fetching ACF field:', error);
}
return date;
}
/*
* https://stackoverflow.com/a/21903119/9497573
*
*/
function getUrlParameter(sParam) {
let sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
};