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

200 lines 4.4 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 * Gets submit field object.
34 *
35 * @param int $form_id Form ID.
36 * @return object
37 */
38 public static function get_submit_field( $form_id ) {
39 return FrmField::get_all_types_in_form( $form_id, self::FIELD_TYPE, 1 );
40 }
41
42 /**
43 * Checks if there is submit button field on the current page.
44 *
45 * @param array $values Prepared form values.
46 * @return bool
47 */
48 public static function has_submit_field_on_current_page( $values ) {
49 if ( empty( $values['fields'] ) ) {
50 return false;
51 }
52
53 return self::has_submit_field_in_list( $values['fields'] );
54 }
55
56 /**
57 * Checks if the given fields list contains a submit field.
58 *
59 * @param array $fields Array of fields.
60 * @return bool
61 */
62 private static function has_submit_field_in_list( $fields ) {
63 foreach ( $fields as $field ) {
64 if ( self::FIELD_TYPE === FrmField::get_field_type( $field ) ) {
65 return true;
66 }
67 }
68
69 return false;
70 }
71
72 /**
73 * Gets current action (create or update) from the global variable.
74 *
75 * @param int $form_id Form ID.
76 * @return string
77 */
78 public static function get_current_action_from_global_var( $form_id ) {
79 global $frm_vars;
80
81 if ( isset( $frm_vars['form_params'][ $form_id ]['action'] ) ) {
82 return $frm_vars['form_params'][ $form_id ]['action'];
83 }
84
85 return 'create';
86 }
87
88 /**
89 * Gets submit button settings from form option.
90 *
91 * @param object $form Form object.
92 * @return array
93 */
94 private static function get_submit_settings_from_form( $form ) {
95 return array(
96 'edit_text' => FrmForm::get_option(
97 array(
98 'form' => $form,
99 'option' => 'edit_value',
100 )
101 ),
102 'align' => FrmForm::get_option(
103 array(
104 'form' => $form,
105 'option' => 'submit_align',
106 )
107 ),
108 'start_over' => FrmForm::get_option(
109 array(
110 'form' => $form,
111 'option' => 'start_over',
112 )
113 ),
114 'start_over_label' => FrmForm::get_option(
115 array(
116 'form' => $form,
117 'option' => 'start_over_label',
118 )
119 ),
120 );
121 }
122
123 /**
124 * Copies submit field settings to form options.
125 *
126 * @param object $form Form object.
127 * @return object
128 */
129 public static function copy_submit_field_settings_to_form( $form ) {
130 $submit_field = self::get_submit_field( $form->id );
131 if ( ! $submit_field ) {
132 return $form;
133 }
134
135 $form->options['submit_value'] = $submit_field->name;
136
137 return $form;
138 }
139
140 /**
141 * Maybe create a submit field for a form.
142 *
143 * @param object $form Form object.
144 * @param array $fields Array of fields.
145 * @param bool $reset_fields Flag to refresh fields after one is created or updated.
146 */
147 public static function maybe_create_submit_field( $form, $fields, &$reset_fields ) {
148 if ( self::has_submit_field_in_list( $fields ) ) {
149 return;
150 }
151
152 $field_data = FrmFieldsHelper::setup_new_vars( self::FIELD_TYPE, $form->id );
153
154 $submit_settings = self::get_submit_settings_from_form( $form );
155 $field_data['field_options'] = $submit_settings + $field_data['field_options'];
156 $field_data['name'] = FrmForm::get_option(
157 array(
158 'form' => $form,
159 'option' => 'submit_value',
160 'default' => __( 'Submit', 'formidable' ),
161 )
162 );
163
164 if ( FrmField::create( $field_data ) ) {
165 $reset_fields = true;
166 }
167 }
168
169 /**
170 * Removes submit field from the list of fields.
171 *
172 * @param array $fields Array of fields.
173 */
174 public static function remove_submit_field_from_list( &$fields ) {
175 foreach ( $fields as $key => $field ) {
176 if ( self::FIELD_TYPE === FrmField::get_field_type( $field ) ) {
177 unset( $fields[ $key ] );
178 return;
179 }
180 }
181 }
182
183 /**
184 * Checks if the given fields array only contains the submit field.
185 *
186 * @param array $fields Array of fields.
187 * @return false|object Return the last found submit field, or `false` if there is at least another field.
188 */
189 public static function only_contains_submit_field( $fields ) {
190 $submit_field = false;
191 foreach ( $fields as $field ) {
192 if ( self::FIELD_TYPE !== FrmField::get_field_type( $field ) ) {
193 return false;
194 }
195 $submit_field = $field;
196 }
197 return $submit_field;
198 }
199 }
200