PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.1
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.4.1, at includes/class-field-manager.php

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