PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmSubmitHelper.php

FrmSubmitHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26.1, at classes/helpers/FrmSubmitHelper.php

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