| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Submit helper |
| 4 | 4 | * |
| 5 | 5 | * @since 6.9 |
| 6 | + * | |
| 6 | 7 | * @package Formidable |
| 7 | 8 | */ |
| 8 | 9 | |
| 9 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -29,11 +30,19 @@ | ||
| 29 | 30 | */ |
| 30 | 31 | const DEFAULT_ORDER = 9999; |
| 31 | 32 | |
| 32 | 33 | /** |
| 34 | + * Track the new order of last row fields after a new field is added. | |
| 35 | + * | |
| 36 | + * @var array Keys are field IDs, values are the order. | |
| 37 | + */ | |
| 38 | + private static $last_row_fields_order = array(); | |
| 39 | + | |
| 40 | + /** | |
| 33 | 41 | * Gets submit field object. |
| 34 | 42 | * |
| 35 | 43 | * @param int $form_id Form ID. |
| 44 | + * | |
| 36 | 45 | * @return object |
| 37 | 46 | */ |
| 38 | 47 | public static function get_submit_field( $form_id ) { |
| 39 | 48 | return FrmField::get_all_types_in_form( $form_id, self::FIELD_TYPE, 1 ); |
| @@ -42,8 +51,9 @@ | ||
| 42 | 51 | /** |
| 43 | 52 | * Checks if there is submit button field on the current page. |
| 44 | 53 | * |
| 45 | 54 | * @param array $values Prepared form values. |
| 55 | + * | |
| 46 | 56 | * @return bool |
| 47 | 57 | */ |
| 48 | 58 | public static function has_submit_field_on_current_page( $values ) { |
| 49 | 59 | if ( empty( $values['fields'] ) ) { |
| @@ -56,8 +66,9 @@ | ||
| 56 | 66 | /** |
| 57 | 67 | * Checks if the given fields list contains a submit field. |
| 58 | 68 | * |
| 59 | 69 | * @param array $fields Array of fields. |
| 70 | + * | |
| 60 | 71 | * @return bool |
| 61 | 72 | */ |
| 62 | 73 | private static function has_submit_field_in_list( $fields ) { |
| 63 | 74 | foreach ( $fields as $field ) { |
| @@ -72,13 +83,13 @@ | ||
| 72 | 83 | /** |
| 73 | 84 | * Gets current action (create or update) from the global variable. |
| 74 | 85 | * |
| 75 | 86 | * @param int $form_id Form ID. |
| 87 | + * | |
| 76 | 88 | * @return string |
| 77 | 89 | */ |
| 78 | 90 | public static function get_current_action_from_global_var( $form_id ) { |
| 79 | 91 | global $frm_vars; |
| 80 | - | |
| 81 | 92 | return $frm_vars['form_params'][ $form_id ]['action'] ?? 'create'; |
| 82 | 93 | } |
| 83 | 94 | |
| 84 | 95 | /** |
| @@ -84,8 +95,9 @@ | ||
| 84 | 95 | /** |
| 85 | 96 | * Gets submit button settings from form option. |
| 86 | 97 | * |
| 87 | 98 | * @param object $form Form object. |
| 99 | + * | |
| 88 | 100 | * @return array |
| 89 | 101 | */ |
| 90 | 102 | private static function get_submit_settings_from_form( $form ) { |
| 91 | 103 | return array( |
| @@ -119,12 +131,14 @@ | ||
| 119 | 131 | /** |
| 120 | 132 | * Copies submit field settings to form options. |
| 121 | 133 | * |
| 122 | 134 | * @param object $form Form object. |
| 135 | + * | |
| 123 | 136 | * @return object |
| 124 | 137 | */ |
| 125 | 138 | public static function copy_submit_field_settings_to_form( $form ) { |
| 126 | 139 | $submit_field = self::get_submit_field( $form->id ); |
| 140 | + | |
| 127 | 141 | if ( ! $submit_field ) { |
| 128 | 142 | return $form; |
| 129 | 143 | } |
| 130 | 144 | |
| @@ -138,8 +152,10 @@ | ||
| 138 | 152 | * |
| 139 | 153 | * @param object $form Form object. |
| 140 | 154 | * @param array $fields Array of fields. |
| 141 | 155 | * @param bool $reset_fields Flag to refresh fields after one is created or updated. |
| 156 | + * | |
| 157 | + * @return void | |
| 142 | 158 | */ |
| 143 | 159 | public static function maybe_create_submit_field( $form, $fields, &$reset_fields ) { |
| 144 | 160 | if ( self::has_submit_field_in_list( $fields ) ) { |
| 145 | 161 | return; |
| @@ -144,10 +160,9 @@ | ||
| 144 | 160 | if ( self::has_submit_field_in_list( $fields ) ) { |
| 145 | 161 | return; |
| 146 | 162 | } |
| 147 | 163 | |
| 148 | - $field_data = FrmFieldsHelper::setup_new_vars( self::FIELD_TYPE, $form->id ); | |
| 149 | - | |
| 164 | + $field_data = FrmFieldsHelper::setup_new_vars( self::FIELD_TYPE, $form->id ); | |
| 150 | 165 | $submit_settings = self::get_submit_settings_from_form( $form ); |
| 151 | 166 | $field_data['field_options'] = $submit_settings + $field_data['field_options']; |
| 152 | 167 | $field_data['name'] = FrmForm::get_option( |
| 153 | 168 | array( |
| @@ -165,8 +180,10 @@ | ||
| 165 | 180 | /** |
| 166 | 181 | * Removes submit field from the list of fields. |
| 167 | 182 | * |
| 168 | 183 | * @param array $fields Array of fields. |
| 184 | + * | |
| 185 | + * @return void | |
| 169 | 186 | */ |
| 170 | 187 | public static function remove_submit_field_from_list( &$fields ) { |
| 171 | 188 | foreach ( $fields as $key => $field ) { |
| 172 | 189 | if ( self::FIELD_TYPE === FrmField::get_field_type( $field ) ) { |
| @@ -179,17 +196,76 @@ | ||
| 179 | 196 | /** |
| 180 | 197 | * Checks if the given fields array only contains the submit field. |
| 181 | 198 | * |
| 182 | 199 | * @param array $fields Array of fields. |
| 200 | + * | |
| 183 | 201 | * @return false|object Return the last found submit field, or `false` if there is at least another field. |
| 184 | 202 | */ |
| 185 | 203 | public static function only_contains_submit_field( $fields ) { |
| 186 | 204 | $submit_field = false; |
| 205 | + | |
| 187 | 206 | foreach ( $fields as $field ) { |
| 188 | 207 | if ( self::FIELD_TYPE !== FrmField::get_field_type( $field ) ) { |
| 189 | 208 | return false; |
| 190 | 209 | } |
| 210 | + | |
| 191 | 211 | $submit_field = $field; |
| 192 | 212 | } |
| 213 | + | |
| 193 | 214 | return $submit_field; |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Updates fields in the last row when new field is added. | |
| 219 | + * | |
| 220 | + * @since 6.25.1 | |
| 221 | + * | |
| 222 | + * @param int $field_count The current field count. | |
| 223 | + * | |
| 224 | + * @return void | |
| 225 | + */ | |
| 226 | + public static function update_last_row_fields_order_when_adding_field( $field_count ) { | |
| 227 | + $last_row_field_ids = FrmAppHelper::get_post_param( 'last_row_field_ids', array() ); | |
| 228 | + | |
| 229 | + if ( ! is_array( $last_row_field_ids ) || ! $last_row_field_ids ) { | |
| 230 | + return; | |
| 231 | + } | |
| 232 | + | |
| 233 | + foreach ( $last_row_field_ids as $index => $last_row_field_id ) { | |
| 234 | + $last_row_field_id = absint( $last_row_field_id ); | |
| 235 | + | |
| 236 | + if ( ! $last_row_field_id ) { | |
| 237 | + continue; | |
| 238 | + } | |
| 239 | + // Plus 2 here because the new field has that plus 1. | |
| 240 | + $new_order = $field_count + $index + 2; | |
| 241 | + $updated = FrmField::update( | |
| 242 | + $last_row_field_id, | |
| 243 | + array( 'field_order' => $new_order ) | |
| 244 | + ); | |
| 245 | + | |
| 246 | + if ( false !== $updated ) { | |
| 247 | + self::$last_row_fields_order[ $last_row_field_id ] = $new_order; | |
| 248 | + } | |
| 249 | + } | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Prints the hidden input that contains the last row fields order to be processed in JS after adding new field. | |
| 254 | + * | |
| 255 | + * @since 6.25.1 | |
| 256 | + * | |
| 257 | + * @return void | |
| 258 | + */ | |
| 259 | + public static function print_last_row_fields_order_input() { | |
| 260 | + if ( ! self::$last_row_fields_order ) { | |
| 261 | + return; | |
| 262 | + } | |
| 263 | + | |
| 264 | + printf( | |
| 265 | + '<input id="frm-last-row-fields-order" type="hidden" value="%s" />', | |
| 266 | + esc_attr( wp_json_encode( self::$last_row_fields_order ) ) | |
| 267 | + ); | |
| 268 | + | |
| 269 | + self::$last_row_fields_order = array(); | |
| 194 | 270 | } |
| 195 | 271 | } |