diff --git a/plugins/fbpatch/fbpatch.php b/plugins/fbpatch/fbpatch.php index 6f6f8b2..6265a9c 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.1 +Version: 0.2.2 Author URI: */ diff --git a/plugins/fbpatch/php/fbpatch_class.php b/plugins/fbpatch/php/fbpatch_class.php index 75d2e4a..50d07c5 100644 --- a/plugins/fbpatch/php/fbpatch_class.php +++ b/plugins/fbpatch/php/fbpatch_class.php @@ -45,7 +45,7 @@ class Fbpatch { private static $_patches = [ '_name'=>'fbpatch_list_of_patches', - 'dates' => ['checked'=>true, 'title'=>'dates', 'description'=>"gerer des dates pour acf dans n'importe quels formats"], + 'dates' => ['checked'=>true, 'title'=>'dates', 'description'=>"gerer des dates pour acf dans n'importe quels formats (fonctionne bien pour divi, pas certain pour d'autres plugins)"], 'calculations'=> ['checked'=>true, 'title'=>'calculations', 'description'=>"afficher le total des calculs dès l'ouverture des formulaires"], 'hide_show' => ['checked'=>true, 'title'=>'masquer les offres', 'description'=>"permettre de masquer les offres en editant un formulaire, sans les supprimer"], 'modals' => ['checked'=>false, 'title'=>'modals', 'description'=>"permettre plusieurs modals sur une meme page"], @@ -208,6 +208,104 @@ class Fbpatch { } + + + + + + + + /* + * --------------------------------------------------------------------------- + * DATES + * stores the real dates in the database + * + */ + private static $dates_option = 'dfb_acf_dates_option'; + + /* + * stores acf dates by post_id + * + */ + public static function update_acf_date($post_id, $acf_key, $acf_date) { + if (empty($acf_date)) { + return; + } + $dates = get_option(self::$dates_option); + + /* + * if option does not exists, add it with its first value + * + */ + if ($dates === false) { + $dates[$post_id] = array($acf_key => $acf_date); + add_option(self::$dates_option, $dates, '', false); + return; + } + + /* + * if the option does not contains key for this post_id, add it + * + */ + if (!isset($dates[$post_id])) { + $dates[$post_id] = array($acf_key => $acf_date); + update_option(self::$dates_option, $dates, false); + return; + } + + /* + * if the post_id dont contains this acf field yet just add it + * + */ + if (!isset($dates[$post_id][$acf_key])) { + $dates[$post_id][$acf_key] = $acf_date; + update_option(self::$dates_option, $dates, false); + return; + } + + /* + * if the acf_key already exists, + * only update option if it is different + * + */ + if ($dates[$post_id][$acf_key] !== $acf_date) { + $dates[$post_id][$acf_key] = $acf_date; + update_option(self::$dates_option, $dates, false); + return; + } + } + + /* + * if acf_date exists for this acf field, use it instead of the value + * return the date in acf format 'Ymd' or false if doesnt exists + * + */ + public static function get_acf_date($post_id, $acf_key) { + $dates = get_option(self::$dates_option); + + if ($dates === false) { + return false; + } + if (!isset($dates[$post_id])) { + return false; + } + if (!isset($dates[$post_id][$acf_key])) { + return false; + } + + /* + * returns the date, or false if is empty + * + */ + $date = $dates[$post_id][$acf_key]; + if (empty($date)) { + return false; + } + return $date; + } + + + } diff --git a/plugins/fbpatch/php/patches/dates.php b/plugins/fbpatch/php/patches/dates.php index 727f46d..51710e9 100644 --- a/plugins/fbpatch/php/patches/dates.php +++ b/plugins/fbpatch/php/patches/dates.php @@ -10,34 +10,46 @@ if (!defined('ABSPATH')) { + + + + /* - -- when jquery choose the date format, - it also creates a hidden field with the value formated as 'yymmdd' (Ymd) -- before dfb saves the post queries, - saves the acf-date of the hidden field into a special property of the acf field - and removes it from the post-array - -- jquery creates a hidden field with the value formated as 'yymmdd' (Ymd) -- before insert post in dfb : - - find the hidden field, and for which acf field it is - - save this data in the acf field - - also save the formated date in the acf field, in a class maybe ? - - add a filter for the acf field, when it displays the field, to show the formated value - -../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/scripts/builder-bundle.min.js - -jquery date picker : -7509 : ../../../../wordpress_docker/volumes/wp-volumes/wp-content/plugins/divi-form-builder/includes/modules/FormField/FormField.php - +* 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); /* -* prevent format date when output -* otherwise, a date as text 'monday 3 october 2024' will output as '03/10/2024' +* 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 * @@ -50,6 +62,11 @@ function prevent_format_for_acf_date_picker($null, $value, $post_id, $field, $es 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); @@ -59,8 +76,16 @@ add_filter('acf/pre_format_value', __NAMESPACE__.'\prevent_format_for_acf_date_p + + + + + + /* * 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() { @@ -77,19 +102,42 @@ add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_dates_patch', -function array_has_keys_starting_with($needle, &$haystack) { - $keys = array(); - foreach ($haystack as $key => $value) { - if (strpos($key, $needle) === 0) { - $keys[] = $key; - } - } - return $keys; -} + + + /* -* function acf_update_field( $field, $specific = array() ) {} +* 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 * */ @@ -115,166 +163,49 @@ function add_date_to_acf_object_class($acf_id) { -function update_acf_date($acf_date, $acf_key, $post_id, $new_date_text) { -// error_log("get_field_object: " . json_encode(get_field_object($acf_key, $post_id))); -// error_log("--get_field : " . json_encode(get_field($acf_key, $post_id))); -// error_log("get_post_meta : " . json_encode(get_post_meta($post_id, 'date_1'))); - \FBPATCH\add_date_to_acf_object_class($acf_key, $acf_date); -} -/* -acf_date_object: { - "ID":43981, - "key":"field_66212132b126b", - "label":"date_test", - "name":"date_test", - "aria-label":"", - "prefix":"acf", - "type":"date_picker", - "value":null, - "menu_order":0, - "instructions":"", - "required":0, - "id":"", - "class":"", - "conditional_logic":0, - "parent":43980, - "wrapper":{"width":"","class":"","id":""}, - "display_format":"d\/m\/Y", - "return_format":"d\/m\/Y", - "first_day":1, - "_name":"date_test", - "_valid":1 -} -*/ + + + + /* -* modify the post to -* -* -> 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 +* utility functions * */ -function process_form_acf_dates($form_id, $post_array) { -//error_log('post_array: ' . json_encode($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; + +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; + } } - /* - * - * - */ - 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\update_acf_date($acf_date, $acf_key, $post_array['ID'], $new_date_text); + if (str_contains($get_field_file, '/Divi/')) { + return true; + } + else { + return false; } } -//add_action('df_before_process', __NAMESPACE__.'\process_form_acf_dates', 10, 3); -//add_action('df_after_process', __NAMESPACE__.'\process_form_acf_dates', 10, 3); -add_action('df_before_insert_post', __NAMESPACE__.'\process_form_acf_dates', 10, 2); -/* - -- before inser : -post_array: { - "post_status":"publish", - "field_title":["title","date 1","date 2"], - "post_title":"post_date_dfb", - "meta_input":{ - "date_1":"mercredi 10 avril 2024", - "_date_1":"field_66212132b126b", - "date_2":"04\/04\/2024", - "_date_2":"field_662619cfff3a1" - }, - "acf_date_hidden_for_date_1":"20240410", - "acf_date_hidden_for_date_2":"", - "ID":"44121", - "form_type_confirm":"", - "post_type":"post", - "tax_input":[], - "post_date":"2024-04-22 13:39:13" +function array_has_keys_starting_with($needle, &$haystack) { + $keys = array(); + foreach ($haystack as $key => $value) { + if (strpos($key, $needle) === 0) { + $keys[] = $key; + } + } + return $keys; } -- before process : -form_id: "" -post_array: { - "post_status":"publish", - "field_title":["title","date"], - "post_title":"edit_date_dfb", - "meta_input":["de_fb_date_test"], - "date_test":"dimanche 02 avril 2023", - "ID":"43978", - "form_type_confirm":"" -} -form_type: "post" - - -after: - -form_id: "" -post_array: { - "post_status":"publish", - "field_title":["title","date"], - "post_title":"edit_date_dfb", - "meta_input":{"date_test":"dimanche 02 avril 2023","_date_test":"field_66212132b126b"}, - "ID":"43978", - "form_type_confirm":"", - "post_type":"post", - "tax_input":[], - "post_date":"2024-04-18 15:32:43" -} -form_type: "post" - ---------------- - -before : - -form_id: "" -post_array: { - "post_status":"publish", - "field_title":["title","date"], - "post_title":"edit_date_dfb", - "meta_input":["de_fb_date_test"], - "date_test":"dimanche 16 avril 2023", - "acf_date_hidden_for_date_test":"20230416", - "ID":"43978", - "form_type_confirm":"" -} -form_type: "post" - - -after : - -form_id: "" -post_array: { - "post_status":"publish", - "field_title":["title","date"], - "post_title":"edit_date_dfb", - "meta_input":{ - "date_test":"dimanche 16 avril 2023", - "_date_test":"field_66212132b126b" - }, - "acf_date_hidden_for_date_test":"20230416", - "ID":"43978", - "form_type_confirm":"", - "post_type":"post", - "tax_input":[], - "post_date":"2024-04-18 15:32:43" -} -form_type: "post" -*/ - - -