@@ -30,6 +30,15 @@ function __construct( $form_id, $field_ids = array(), $global = false ) {
30
30
31
31
}
32
32
33
+ /**
34
+ * Initializes the plugin by adding the necessary Gravity Forms filters.
35
+ *
36
+ * This method first verifies that Gravity Forms is loaded. It then conditionally registers
37
+ * a field validation filter for decimal quantities on either a global or a form-specific basis.
38
+ * For Gravity Forms versions before 2.8 with HTML5 disabled, only the validation filter is added.
39
+ * Otherwise, additional filters are registered to stash the current form, modify the quantity
40
+ * input tag to allow decimal values, and adjust input content to ensure the 'step' attribute is set to 'any'.
41
+ */
33
42
function init () {
34
43
35
44
// make sure Gravity Forms is loaded
@@ -56,6 +65,19 @@ function init() {
56
65
add_filter ( 'gform_field_content ' , array ( $ this , 'fix_content ' ), 10 , 5 );
57
66
}
58
67
68
+ /**
69
+ * Validates and allows decimal input for quantity and product fields.
70
+ *
71
+ * This callback checks if the field is enabled for decimals and if its user-submitted value is a valid decimal number
72
+ * (accepting both dot and comma as separators). If a valid decimal is detected, it updates the validation result accordingly.
73
+ *
74
+ * @param array $result The current validation result array, which includes an 'is_valid' flag.
75
+ * @param mixed $value The submitted value for the field.
76
+ * @param object $form The form object being processed.
77
+ * @param object $field The field object including type and validation configuration.
78
+ *
79
+ * @return array The modified validation result.
80
+ */
59
81
function allow_quantity_float ( $ result , $ value , $ form , $ field ) {
60
82
if (
61
83
$ this ->is_enabled_field ( $ field ) &&
@@ -75,6 +97,23 @@ function stash_current_form( $form ) {
75
97
return $ form ;
76
98
}
77
99
100
+ /**
101
+ * Modifies the HTML markup for a Gravity Forms quantity field to enable decimal input.
102
+ *
103
+ * If the form ID matches the configured form (or if the plugin applies globally), the currently
104
+ * processed form is stored, and the field is enabled for decimals, this function updates the
105
+ * input field markup by adding a "step='any'" attribute to the number input element. Otherwise,
106
+ * it returns the unmodified markup.
107
+ *
108
+ * @param string $markup The original HTML markup for the field input.
109
+ * @param array $field The configuration array for the form field.
110
+ * @param mixed $value The current value of the field.
111
+ * @param mixed $lead_id The identifier for the current lead/entry.
112
+ * @param int $form_id The identifier of the form being processed.
113
+ *
114
+ * @return string The modified HTML markup with an added "step='any'" attribute if applicable,
115
+ * or the original markup if the conditions are not met.
116
+ */
78
117
function modify_quantity_input_tag ( $ markup , $ field , $ value , $ lead_id , $ form_id ) {
79
118
80
119
$ is_correct_form = $ this ->form_id == $ form_id || $ this ->global ;
@@ -93,6 +132,21 @@ function modify_quantity_input_tag( $markup, $field, $value, $lead_id, $form_id
93
132
return $ markup ;
94
133
}
95
134
135
+ /**
136
+ * Modifies HTML content for quantity fields to support decimal values.
137
+ *
138
+ * This function scans the provided HTML content for input elements with the "ginput_quantity" class.
139
+ * If an input field includes a decimal value and lacks a step attribute set to "any", the function
140
+ * updates the input tag to include step="any", ensuring that decimal quantities are handled correctly.
141
+ *
142
+ * @param string $content The HTML content containing the input fields.
143
+ * @param mixed $field Field data provided by Gravity Forms.
144
+ * @param mixed $value Current value of the field.
145
+ * @param int|string $lead_id The entry identifier.
146
+ * @param int|string $form_id The form identifier.
147
+ *
148
+ * @return string The modified HTML content with updated input tags.
149
+ */
96
150
function fix_content ( $ content , $ field , $ value , $ lead_id , $ form_id ) {
97
151
// ensure the step is 'any' for any fields that have a decimal value.
98
152
return preg_replace_callback (
@@ -113,6 +167,17 @@ function ( $matches ) {
113
167
);
114
168
}
115
169
170
+ /**
171
+ * Retrieves the field input markup while avoiding recursive modification.
172
+ *
173
+ * Temporarily disables the quantity input tag modification filter to prevent recursion,
174
+ * retrieves the field input HTML via GFCommon, and then re-applies the filter.
175
+ *
176
+ * @param array $field The configuration array for the Gravity Forms field.
177
+ * @param mixed $value The current value of the field.
178
+ * @param array $form The configuration array for the Gravity Forms form.
179
+ * @return string The generated HTML markup for the field input.
180
+ */
116
181
function get_field_input ( $ field , $ value , $ form ) {
117
182
118
183
remove_filter ( 'gform_field_input ' , array ( $ this , 'modify_quantity_input_tag ' ), 10 , 5 );
0 commit comments