PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.1.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.1.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-form.php

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

358 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The Form Class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Form {
9
10 /**
11 * The form id
12 *
13 * @var integer
14 */
15 public $id = 0;
16
17 /**
18 * The form title
19 *
20 * @var string
21 */
22 public $name = '';
23
24 /**
25 * Holds the post data object
26 *
27 * @var null|WP_post
28 */
29 public $data = null;
30
31 /**
32 * Form fields
33 *
34 * @var array
35 */
36 public $form_fields = array();
37
38 /**
39 * The Constructor
40 *
41 * @param int|WP_Post $form
42 */
43 public function __construct( $form = null ) {
44
45 if ( is_numeric( $form ) ) {
46
47 $the_post = get_post( $form );
48
49 if ( $the_post ) {
50 $this->id = $the_post->ID;
51 $this->name = $the_post->post_title;
52 $this->data = $the_post;
53 }
54
55 } elseif ( is_a( $form, 'WP_Post' ) ) {
56 $this->id = $form->ID;
57 $this->name = $form->post_title;
58 $this->data = $form;
59 }
60 }
61
62 /**
63 * Get the form ID
64 *
65 * @return integer
66 */
67 public function get_id() {
68 return $this->id;
69 }
70
71 /**
72 * Get the form name
73 *
74 * @return string
75 */
76 public function get_name() {
77 return $this->name;
78 }
79
80 /**
81 * Get all form fields of this form
82 *
83 * @return array
84 */
85 public function get_fields() {
86
87 // return if already fetched
88 if ( $this->form_fields ) {
89 return $this->form_fields;
90 }
91
92 $fields = get_children(array(
93 'post_parent' => $this->id,
94 'post_status' => 'publish',
95 'post_type' => 'wpuf_input',
96 'numberposts' => '-1',
97 'orderby' => 'menu_order',
98 'order' => 'ASC',
99 ));
100
101 $form_fields = array();
102
103 foreach ( $fields as $key => $content ) {
104
105 $field = maybe_unserialize( $content->post_content );
106
107 $field['id'] = $content->ID;
108
109 // Add inline property for radio and checkbox fields
110 $inline_supported_fields = array( 'radio_field', 'checkbox_field' );
111 if ( in_array( $field['template'] , $inline_supported_fields ) ) {
112 if ( ! isset( $field['inline'] ) ) {
113 $field['inline'] = 'no';
114 }
115 }
116
117 // Add 'selected' property
118 $option_based_fields = array( 'dropdown_field', 'multiple_select', 'radio_field', 'checkbox_field' );
119 if ( in_array( $field['template'] , $option_based_fields ) ) {
120 if ( ! isset( $field['selected'] ) ) {
121
122 if ( 'dropdown_field' === $field['template'] || 'radio_field' === $field['template'] ) {
123 $field['selected'] = '';
124 } else {
125 $field['selected'] = array();
126 }
127
128 }
129 }
130
131 // Add 'multiple' key for template:repeat
132 if ( 'repeat_field' === $field['template'] && ! isset( $field['multiple'] ) ) {
133 $field['multiple'] = '';
134 }
135
136 if ( 'recaptcha' === $field['template'] ) {
137 $field['name'] = 'recaptcha';
138 $field['enable_no_captcha'] = isset( $field['enable_no_captcha'] ) ? $field['enable_no_captcha'] : '';
139
140 }
141
142 if ( 'custom_html' === $field['template'] ) {
143 $field['name'] = 'custom_html';
144 }
145
146 $form_fields[] = apply_filters( 'weforms-get-form-fields', $field );
147 }
148
149 $this->form_fields = $form_fields;
150
151 return $this->form_fields;
152 }
153
154 /**
155 * Check if any perticular field template is used in a form
156 *
157 * @return array
158 */
159 public function has_field( $field_template ) {
160
161 foreach ( $this->get_fields() as $key => $field) {
162 if ( isset( $field['template'] ) && $field['template'] == $field_template) {
163 return true;
164 }
165 }
166 }
167
168 /**
169 * Get formatted field name/values
170 *
171 * @return array
172 */
173 public function get_field_values() {
174 $values = array();
175 $fields = $this->get_fields();
176
177 if ( ! $fields ) {
178 return $values;
179 }
180
181 $ignore_fields = array( 'recaptcha', 'section_break' );
182 $options_fields = array( 'dropdown_field', 'radio_field', 'multiple_select', 'checkbox_field' );
183
184 foreach ($fields as $field) {
185
186 if ( in_array( $field['template'], $ignore_fields ) ) {
187 continue;
188 }
189
190 if ( ! isset( $field['name'] ) ) {
191 continue;
192 }
193
194 $value = array(
195 'label' => isset( $field['label'] ) ? $field['label'] : '',
196 'type' => $field['template'],
197 );
198
199 // put options if this is an option field
200 if ( in_array( $field['template'], $options_fields ) ) {
201 $value['options'] = $field['options'];
202 }
203
204 $values[ $field['name'] ] = $value;
205 }
206
207 return $values;
208 }
209
210 /**
211 * Get form settings
212 *
213 * @return array
214 */
215 public function get_settings() {
216 return get_post_meta( $this->id, 'wpuf_form_settings', true );
217 }
218
219 /**
220 * Check if the form submission is open
221 *
222 * @return boolean|WP_Error
223 */
224 public function is_submission_open() {
225 $settings = $this->get_settings();
226
227 $needs_login = ( isset( $settings['require_login'] ) && $settings['require_login'] == 'true' ) ? true : false;
228 $has_limit = ( isset( $settings['limit_entries'] ) && $settings['limit_entries'] == 'true' ) ? true : false;
229 $is_scheduled = ( isset( $settings['schedule_form'] ) && $settings['schedule_form'] == 'true' ) ? true : false;
230
231 if ( $this->data->post_status != 'publish' ) {
232 return new WP_Error( 'needs-publish', __( 'The form is not published yet.', 'weforms' ) );
233 }
234
235 if ( $needs_login && !is_user_logged_in() ) {
236 return new WP_Error( 'needs-login', $settings['req_login_message'] );
237 }
238
239 if ( $has_limit ) {
240 $limit = (int) $settings['limit_number'];
241 $form_entries = $this->num_form_entries();
242
243 if ( $limit < $form_entries ) {
244 return new WP_Error( 'entry-limit', $settings['limit_message'] );
245 }
246 }
247
248 if ( $is_scheduled ) {
249 $start_time = strtotime( $settings['schedule_start'] );
250 $end_time = strtotime( $settings['schedule_end'] );
251 $current_time = current_time( 'timestamp' );
252
253 // too early?
254 if ( $current_time < $start_time ) {
255 return new WP_Error( 'form-pending', $settings['sc_pending_message'] );
256 } elseif ( $current_time > $end_time ) {
257 return new WP_Error( 'form-expired', $settings['sc_expired_message'] );
258 }
259 }
260
261 return true;
262 }
263
264 /**
265 * Get form notifications
266 *
267 * @return array
268 */
269 public function get_notifications() {
270 $notifications = get_post_meta( $this->id, 'notifications', true );
271
272 if ( ! $notifications ) {
273 return array();
274 }
275
276 return $notifications;
277 }
278
279 /**
280 * Get all the integrations
281 *
282 * @return array
283 */
284 public function get_integrations() {
285 $integrations = get_post_meta( $this->id, 'integrations', true );
286
287 if ( ! $integrations ) {
288 return array();
289 }
290
291 return $integrations;
292 }
293
294 /**
295 * Get entries of this form
296 *
297 * @return \WeForms_Form_Entry_Manager
298 */
299 public function entries() {
300 return new WeForms_Form_Entry_Manager( $this->id, $this );
301 }
302
303 /**
304 * Get number of form entries
305 *
306 * @return integer
307 */
308 public function num_form_entries() {
309 return weforms_count_form_entries( $this->id );
310 }
311
312 /**
313 * Get the number of form views
314 *
315 * @return integer
316 */
317 public function num_form_views() {
318 return weforms_get_form_views( $this->id );
319 }
320
321
322 /**
323 * prepare_entries
324 *
325 * @return array
326 */
327 public function prepare_entries() {
328
329 $fields = weforms()->fields->get_fields();
330 $form_fields = $this->get_fields();
331
332 $entry_fields = array();
333
334 $ignore_list = apply_filters('wefroms_entry_ignore_list', array(
335 'section_break',
336 'custom_html',
337 'recaptcha'
338 ) );
339
340 foreach ($form_fields as $field) {
341
342 if ( in_array( $field['template'], $ignore_list ) ) {
343 continue;
344 }
345
346 if ( ! array_key_exists( $field['template'], $fields ) ) {
347 continue;
348 }
349
350 $field_class = $fields[ $field['template'] ];
351
352 $entry_fields[ $field['name'] ] = $field_class->prepare_entry( $field );
353 }
354
355 return $entry_fields;
356 }
357 }
358