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

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