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

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