From d402e9a7e3373cb8a7c6363d804d31454032da51 Mon Sep 17 00:00:00 2001 From: asus Date: Thu, 25 Apr 2024 16:24:33 +0200 Subject: [PATCH] fixed error in fbpatch dates --- plugins/fbpatch/fbpatch.php | 2 +- plugins/fbpatch/js/dates.js | 69 ++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/plugins/fbpatch/fbpatch.php b/plugins/fbpatch/fbpatch.php index 6265a9c..5f5a23a 100644 --- a/plugins/fbpatch/fbpatch.php +++ b/plugins/fbpatch/fbpatch.php @@ -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: */ diff --git a/plugins/fbpatch/js/dates.js b/plugins/fbpatch/js/dates.js index 19d50e7..09d8e4e 100644 --- a/plugins/fbpatch/js/dates.js +++ b/plugins/fbpatch/js/dates.js @@ -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(``).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; +}; +