PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.33
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.33
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.33, at classes/helpers/FrmSubmitHelper.php

272 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 return $frm_vars['form_params'][ $form_id ]['action'] ?? 'create';
93 }
94
95 /**
96 * Gets submit button settings from form option.
97 *
98 * @param object $form Form object.
99 *
100 * @return array
101 */
102 private static function get_submit_settings_from_form( $form ) {
103 return array(
104 'edit_text' => FrmForm::get_option(
105 array(
106 'form' => $form,
107 'option' => 'edit_value',
108 )
109 ),
110 'align' => FrmForm::get_option(
111 array(
112 'form' => $form,
113 'option' => 'submit_align',
114 )
115 ),
116 'start_over' => FrmForm::get_option(
117 array(
118 'form' => $form,
119 'option' => 'start_over',
120 )
121 ),
122 'start_over_label' => FrmForm::get_option(
123 array(
124 'form' => $form,
125 'option' => 'start_over_label',
126 )
127 ),
128 );
129 }
130
131 /**
132 * Copies submit field settings to form options.
133 *
134 * @param object $form Form object.
135 *
136 * @return object
137 */
138 public static function copy_submit_field_settings_to_form( $form ) {
139 $submit_field = self::get_submit_field( $form->id );
140
141 if ( ! $submit_field ) {
142 return $form;
143 }
144
145 $form->options['submit_value'] = $submit_field->name;
146
147 return $form;
148 }
149
150 /**
151 * Maybe create a submit field for a form.
152 *
153 * @param object $form Form object.
154 * @param array $fields Array of fields.
155 * @param bool $reset_fields Flag to refresh fields after one is created or updated.
156 *
157 * @return void
158 */
159 public static function maybe_create_submit_field( $form, $fields, &$reset_fields ) {
160 if ( self::has_submit_field_in_list( $fields ) ) {
161 return;
162 }
163
164 $field_data = FrmFieldsHelper::setup_new_vars( self::FIELD_TYPE, $form->id );
165 $submit_settings = self::get_submit_settings_from_form( $form );
166 $field_data['field_options'] = $submit_settings + $field_data['field_options'];
167 $field_data['name'] = FrmForm::get_option(
168 array(
169 'form' => $form,
170 'option' => 'submit_value',
171 'default' => __( 'Submit', 'formidable' ),
172 )
173 );
174
175 if ( FrmField::create( $field_data ) ) {
176 $reset_fields = true;
177 }
178 }
179
180 /**
181 * Removes submit field from the list of fields.
182 *
183 * @param array $fields Array of fields.
184 *
185 * @return void
186 */
187 public static function remove_submit_field_from_list( &$fields ) {
188 foreach ( $fields as $key => $field ) {
189 if ( self::FIELD_TYPE === FrmField::get_field_type( $field ) ) {
190 unset( $fields[ $key ] );
191 return;
192 }
193 }
194 }
195
196 /**
197 * Checks if the given fields array only contains the submit field.
198 *
199 * @param array $fields Array of fields.
200 *
201 * @return false|object Return the last found submit field, or `false` if there is at least another field.
202 */
203 public static function only_contains_submit_field( $fields ) {
204 $submit_field = false;
205
206 foreach ( $fields as $field ) {
207 if ( self::FIELD_TYPE !== FrmField::get_field_type( $field ) ) {
208 return false;
209 }
210
211 $submit_field = $field;
212 }
213
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();
270 }
271 }
272