@@ -30,6 +30,15 @@ function __construct( $form_id, $field_ids = array(), $global = false ) {
3030
3131 }
3232
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+ */
3342 function init () {
3443
3544 // make sure Gravity Forms is loaded
@@ -56,6 +65,19 @@ function init() {
5665 add_filter ( 'gform_field_content ' , array ( $ this , 'fix_content ' ), 10 , 5 );
5766 }
5867
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+ */
5981 function allow_quantity_float ( $ result , $ value , $ form , $ field ) {
6082 if (
6183 $ this ->is_enabled_field ( $ field ) &&
@@ -75,6 +97,23 @@ function stash_current_form( $form ) {
7597 return $ form ;
7698 }
7799
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+ */
78117 function modify_quantity_input_tag ( $ markup , $ field , $ value , $ lead_id , $ form_id ) {
79118
80119 $ 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
93132 return $ markup ;
94133 }
95134
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+ */
96150 function fix_content ( $ content , $ field , $ value , $ lead_id , $ form_id ) {
97151 // ensure the step is 'any' for any fields that have a decimal value.
98152 return preg_replace_callback (
@@ -113,6 +167,17 @@ function ( $matches ) {
113167 );
114168 }
115169
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+ */
116181 function get_field_input ( $ field , $ value , $ form ) {
117182
118183 remove_filter ( 'gform_field_input ' , array ( $ this , 'modify_quantity_input_tag ' ), 10 , 5 );
0 commit comments