PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.3
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.3
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-field-manager.php

class-field-manager.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.3, at includes/class-field-manager.php

278 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Form field manager class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Field_Manager {
9
10 /**
11 * The fields
12 *
13 * @var array
14 */
15 private $fields = [];
16
17 /**
18 * Going to store WeForms_Notification instance for tmp. Will refactor
19 *
20 * @var array
21 */
22 private $notification = null;
23
24 /**
25 * Get all the registered fields
26 *
27 * @return array
28 */
29 public function get_fields() {
30 if ( !empty( $this->fields ) ) {
31 return $this->fields;
32 }
33
34 $this->register_field_types();
35
36 return $this->fields;
37 }
38
39 /**
40 * Register the field types
41 *
42 * @return void
43 */
44 private function register_field_types() {
45 require_once __DIR__ . '/fields/class-abstract-fields.php';
46 require_once __DIR__ . '/fields/class-field-text.php';
47 require_once __DIR__ . '/fields/class-field-name.php';
48 require_once __DIR__ . '/fields/class-field-email.php';
49 require_once __DIR__ . '/fields/class-field-textarea.php';
50 require_once __DIR__ . '/fields/class-field-column.php';
51 require_once __DIR__ . '/fields/class-field-checkbox.php';
52 require_once __DIR__ . '/fields/class-field-radio.php';
53 require_once __DIR__ . '/fields/class-field-dropdown.php';
54 require_once __DIR__ . '/fields/class-field-multidropdown.php';
55 require_once __DIR__ . '/fields/class-field-url.php';
56 require_once __DIR__ . '/fields/class-field-sectionbreak.php';
57 require_once __DIR__ . '/fields/class-field-html.php';
58 require_once __DIR__ . '/fields/class-field-hidden.php';
59 require_once __DIR__ . '/fields/class-field-image.php';
60 require_once __DIR__ . '/fields/class-field-recaptcha.php';
61 require_once __DIR__ . '/fields/class-field-date.php';
62
63 $fields = [
64 'text_field' => new WeForms_Form_Field_Text(),
65 'name_field' => new WeForms_Form_Field_Name(),
66 'date_field' => new WeForms_Form_Field_Date_Free(),
67 'email_address' => new WeForms_Form_Field_Email(),
68 'textarea_field' => new WeForms_Form_Field_Textarea(),
69 'radio_field' => new WeForms_Form_Field_Radio(),
70 'checkbox_field' => new WeForms_Form_Field_Checkbox(),
71 'column_field' => new WeForms_Form_Field_Column(),
72 'dropdown_field' => new WeForms_Form_Field_Dropdown(),
73 'multiple_select' => new WeForms_Form_Field_MultiDropdown(),
74 'website_url' => new WeForms_Form_Field_URL(),
75 'section_break' => new WeForms_Form_Field_SectionBreak(),
76 'custom_html' => new WeForms_Form_Field_HTML(),
77 'custom_hidden_field' => new WeForms_Form_Field_Hidden(),
78 'image_upload' => new WeForms_Form_Field_Image(),
79 'recaptcha' => new WeForms_Form_Field_reCaptcha(),
80 ];
81
82 $this->fields = apply_filters( 'weforms_form_fields', $fields );
83 }
84
85 /**
86 * Get field groups
87 *
88 * @return array
89 */
90 public function get_field_groups() {
91 $groups = [
92 [
93 'title' => __( 'Custom Fields', 'weforms' ),
94 'id' => 'custom-fields',
95 'fields' => apply_filters( 'weforms_field_groups_custom',
96 [
97 'name_field',
98 'text_field',
99 'textarea_field',
100 'dropdown_field',
101 'multiple_select',
102 'radio_field',
103 'checkbox_field',
104 'website_url',
105 'email_address',
106 'custom_hidden_field',
107 'image_upload',
108 ]
109 ),
110 ],
111
112 [
113 'title' => __( 'Others', 'weforms' ),
114 'id' => 'others',
115 'fields' => apply_filters( 'weforms_field_groups_others',
116 [
117 'column_field',
118 'section_break',
119 'custom_html',
120 'recaptcha',
121 ]
122 ),
123 ],
124 ];
125
126 return apply_filters( 'weforms_field_groups', $groups );
127 }
128
129 /**
130 * Get fields JS setting for the form builder
131 *
132 * @return array
133 */
134 public function get_js_settings() {
135 $fields = $this->get_fields();
136 $js_array = [];
137
138 if ( $fields ) {
139 foreach ( $fields as $type => $object ) {
140 $js_array[ $type ] = $object->get_js_settings();
141 }
142 }
143
144 return $js_array;
145 }
146
147 /**
148 * Render the form fields
149 *
150 * @param int $form_id
151 * @param array $fields
152 * @param array $atts
153 *
154 * @return void
155 */
156 public function render_fields( $fields, $form_id, $atts = [] ) {
157 if ( !$fields ) {
158 return;
159 }
160
161 $this->notification = new WeForms_Notification();
162 $this->notification->set_merge_tags();
163
164 $fields = apply_filters( 'weforms_render_fields', $fields, $form_id );
165
166 foreach ( $fields as $field ) {
167 if ( !$field_object = $this->field_exists( $field['template'] ) ) {
168 if ( defined( 'WP_DEBUG' && WP_DEBUG ) ) {
169 echo '<h4 style="color: red;"><em>' . esc_attr( $field['template'] ). '</em> field not found.</h4>';
170 }
171
172 continue;
173 }
174
175 $field = $this->dynamic_fields( $field, $form_id, $atts );
176 $field = $this->replace_tags( $field, $form_id, $atts );
177 $field_object->render( $field, $form_id );
178 $field_object->conditional_logic( $field, $form_id );
179 }
180 }
181
182 /**
183 * Filter dynamic fields
184 *
185 * @param array $form_field
186 * @param int $form_id
187 *
188 * @return void
189 */
190 public function dynamic_fields( $form_field, $form_id, $atts = [] ) {
191 if ( !isset( $form_field['dynamic'] ) || empty( $form_field['dynamic']['status'] ) || empty( $form_field['dynamic']['param_name'] ) ) {
192 return $form_field;
193 }
194
195
196 $param_name = $form_field['dynamic']['param_name'];
197
198 if ( isset( $form_field['options'] ) ) {
199 $form_field['options'] = apply_filters( 'weforms_field_options_' . $param_name, $form_field['options'], $form_id );
200
201 if ( isset( $_GET[ $param_name ] ) && is_array( $_GET[ $param_name ] ) ) {
202 $form_field['default'] = array_merge( $form_field['options'], sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) );
203 }
204 }
205
206 foreach ( [ 'default', 'selected' ] as $key => $default_key ) {
207 if ( isset( $form_field[ $default_key ] ) ) {
208 $form_field[ $default_key ] = apply_filters( 'weforms_field_default_value_' . $param_name, $form_field[ $default_key ], $form_id );
209
210 if ( isset( $atts[ $param_name ] ) ) {
211 $form_field[ $default_key ] = $atts[ $param_name ];
212 }
213
214 if ( isset( $_GET[ $param_name ] ) ) {
215 if ( is_array( $form_field[ $default_key ] ) == is_array( sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) ) ) {
216 $form_field[ $default_key ] = sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) );
217 }
218
219 if ( ! is_array( $form_field[ $default_key ] ) == ! is_array( sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) ) ) {
220 $form_field[ $default_key ] = sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) );
221 }
222 }
223 }
224 }
225
226 return $form_field;
227 }
228
229 /**
230 * Replace merge tags
231 *
232 * @param array $form_field
233 * @param int $form_id
234 *
235 * @return void
236 */
237 public function replace_tags( $form_field, $form_id, $atts = [] ) {
238 $fields = [
239 'default',
240 'placeholder',
241 'first_name' => [ 'placeholder', 'default' ],
242 'middle_name' => [ 'placeholder', 'default' ],
243 'last_name' => [ 'placeholder', 'default' ],
244 ];
245
246 foreach ( $fields as $key => $field ) {
247 if ( is_array( $field ) ) {
248 foreach ( $field as $k => $sub_field ) {
249 if ( !empty( $form_field[ $key ] ) && !empty( $form_field[ $key ][ $sub_field ] ) ) {
250 $form_field[ $key ][ $sub_field ] = $this->notification->replace_tags( $form_field[ $key ][ $sub_field ] );
251 }
252 }
253 } else {
254 if ( !empty( $form_field[ $field ] ) && is_string( $form_field[ $field ] ) ) {
255 $form_field[ $field ] = $this->notification->replace_tags( $form_field[ $field ] );
256 }
257 }
258 }
259
260 return $form_field;
261 }
262
263 /**
264 * Check if a field exists
265 *
266 * @param string $field_type
267 *
268 * @return bool
269 */
270 public function field_exists( $field_type ) {
271 if ( array_key_exists( $field_type, $this->get_fields() ) ) {
272 return $this->fields[ $field_type ];
273 }
274
275 return false;
276 }
277 }
278