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

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