From 567a7460715c7a00072a7a0ff33ca386c8059c48 Mon Sep 17 00:00:00 2001 From: Thibaut Vermeulen Date: Thu, 9 May 2024 01:20:39 +0200 Subject: [PATCH] Fixed issue where the hidden input field would result in double saving of user profile This even happened if no aspect-ratio field was being used on the user profile field. --- ...x-acf-field-image-aspect-ratio-crop-v5.php | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php b/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php index bb508b5..5e5f954 100644 --- a/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php +++ b/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php @@ -82,15 +82,32 @@ function __construct($settings) // Also options pages, taxonomies etc have ACF generated special post id that we don't know before save hook $this->temp_post_id = wp_generate_uuid4(); - // Store temporary post id in a hidden field + // Store temporary post id in a hidden field, only add the hidden field if we are using a field of type 'image_aspect_ratio_crop', this to prevent double saving of user profile add_action( - 'acf/input/form_data', - function () { - echo ""; - }, - 10, - 1 - ); + 'acf/input/form_data', + function () { + // Get all ACF fields on the current form + $fields = acf_get_fields(acf_get_form_data('screen')); + + // Flag to check if the specific field type is found + $has_special_field = false; + + // Check if any field is of the type 'image_aspect_ratio_crop' + foreach ($fields as $field) { + if (isset($field['type']) && $field['type'] === 'image_aspect_ratio_crop') { + $has_special_field = true; + break; + } + } + + // Only add the hidden input if the specific field type is present + if ($has_special_field) { + echo ""; + } + }, + 10, + 1 + ); // filters add_filter('get_media_item_args', [$this, 'get_media_item_args']);