PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.6.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.6.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / class-evf-form-handler.php
everest-forms / includes Last commit date
abstracts 6 years ago admin 6 years ago export 6 years ago fields 6 years ago interfaces 8 years ago libraries 7 years ago log-handlers 6 years ago shortcodes 6 years ago templates 6 years ago class-everest-forms.php 6 years ago class-evf-ajax.php 6 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 7 years ago class-evf-emails.php 6 years ago class-evf-fields.php 6 years ago class-evf-form-block.php 6 years ago class-evf-form-handler.php 6 years ago class-evf-form-task.php 6 years ago class-evf-forms-features.php 6 years ago class-evf-frontend-scripts.php 6 years ago class-evf-install.php 6 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 6 years ago class-evf-post-types.php 6 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 7 years ago class-evf-smart-tags.php 6 years ago class-evf-template-loader.php 6 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 6 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 6 years ago evf-formatting-functions.php 6 years ago evf-notice-functions.php 6 years ago evf-template-functions.php 6 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 6 years ago
class-evf-form-handler.php
459 lines
1 <?php
2 /**
3 * Form handler.
4 *
5 * Contains a bunch of helper methods as well.
6 *
7 * @package EverestForms
8 * @since 1.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Form Handler class.
15 */
16 class EVF_Form_Handler {
17
18 /**
19 * Fetches forms
20 *
21 * @since 1.0.0
22 * @param mixed $id Form ID.
23 * @param array $args Form Arguments.
24 * @return array|bool|null|WP_Post Form object.
25 */
26 public function get( $id = '', $args = array() ) {
27 $forms = array();
28 $args = apply_filters( 'everest_forms_get_form_args', $args );
29
30 if ( false === $id ) {
31 return false;
32 }
33
34 if ( ! empty( $id ) ) {
35 $the_post = get_post( absint( $id ) );
36
37 if ( $the_post && 'everest_form' === $the_post->post_type ) {
38 $forms = empty( $args['content_only'] ) ? $the_post : evf_decode( $the_post->post_content );
39 }
40 } else {
41 // No ID provided, get multiple forms.
42 $defaults = array(
43 'post_type' => 'everest_form',
44 'orderby' => 'id',
45 'order' => 'DESC',
46 'no_found_rows' => true,
47 'nopaging' => true,
48 );
49
50 $args = wp_parse_args( $args, $defaults );
51
52 $args['post_type'] = 'everest_form';
53
54 $forms = get_posts( $args );
55 }
56
57 if ( empty( $forms ) ) {
58 return false;
59 }
60
61 return $forms;
62 }
63
64 /**
65 * Delete forms.
66 *
67 * @since 1.0.0
68 * @param array $ids Form IDs.
69 * @return boolean
70 */
71 public function delete( $ids = array() ) {
72 // Check for permissions.
73 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
74 return false;
75 }
76
77 if ( ! is_array( $ids ) ) {
78 $ids = array( $ids );
79 }
80
81 $ids = array_map( 'absint', $ids );
82
83 foreach ( $ids as $id ) {
84 $form = wp_delete_post( $id, true );
85
86 if ( ! $form ) {
87 return false;
88 }
89 }
90
91 return true;
92 }
93
94 /**
95 * Create new form.
96 *
97 * @since 1.0.0
98 * @param string $title Form title.
99 * @param string $template Form template.
100 * @param array $args Form Arguments.
101 * @param array $data Additional data.
102 * @return int|bool Form ID on successful creation else false.
103 */
104 public function create( $title = '', $template = 'blank', $args = array(), $data = array() ) {
105 if ( empty( $title ) || ! current_user_can( 'manage_everest_forms' ) ) {
106 return false;
107 }
108
109 $args = apply_filters( 'everest_forms_create_form_args', $args, $data );
110 $form_style = array();
111 $style_needed = false;
112 $form_content = array(
113 'form_field_id' => '1',
114 'settings' => array(
115 'form_title' => sanitize_text_field( $title ),
116 'form_desc' => '',
117 ),
118 );
119
120 // Prevent content filters from corrupting JSON in post_content.
121 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
122 if ( $has_kses ) {
123 kses_remove_filters();
124 }
125 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
126 if ( $has_targeted_link_rel_filters ) {
127 wp_remove_targeted_link_rel_filters();
128 }
129
130 // Create a form.
131 $form_id = wp_insert_post(
132 array(
133 'post_title' => esc_html( $title ),
134 'post_status' => 'publish',
135 'post_type' => 'everest_form',
136 'post_content' => '{}',
137 )
138 );
139
140 $raw_templates = wp_safe_remote_get( 'https://raw.githubusercontent.com/wpeverest/extensions-json/master/everest-forms/templates/all_templates.json' );
141 $templates = json_decode( wp_remote_retrieve_body( $raw_templates ) );
142
143 if ( ! empty( $templates ) ) {
144 foreach ( $templates->templates as $template_data ) {
145 if ( $template_data->slug === $template && 'blank' !== $template_data->slug ) {
146 $form_content = json_decode( base64_decode( $template_data->settings ), true );
147
148 if ( isset( $template_data->styles ) ) {
149 $style_needed = true;
150 $form_style[ $form_id ] = json_decode( base64_decode( $template_data->styles ), true );
151 }
152 }
153 }
154 }
155
156 if ( $form_id ) {
157 $form_content['id'] = $form_id;
158 $form_content['settings']['form_title'] = $title;
159
160 $form_data = wp_parse_args(
161 $args,
162 array(
163 'ID' => $form_id,
164 'post_title' => esc_html( $title ),
165 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_content ) ),
166 )
167 );
168
169 wp_update_post( $form_data );
170
171 if ( ! empty( $form_style ) ) {
172 update_option( 'everest_forms_styles', $form_style );
173 }
174 }
175
176 // Restore removed content filters.
177 if ( $has_kses ) {
178 kses_init_filters();
179 }
180 if ( $has_targeted_link_rel_filters ) {
181 wp_init_targeted_link_rel_filters();
182 }
183
184 do_action( 'everest_forms_create_form', $form_id, $form_data, $data, $style_needed );
185
186 return $form_id;
187 }
188
189 /**
190 * Updates form
191 *
192 * @since 1.0.0
193 *
194 * @param string|int $form_id Form ID.
195 * @param array $data Data retrieved from $_POST and processed.
196 * @param array $args Empty by default, may have custom data not intended to be saved.
197 *
198 * @return mixed
199 * @internal param string $title
200 */
201 public function update( $form_id = '', $data = array(), $args = array() ) {
202 // Check for permissions.
203 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
204 return false;
205 }
206
207 if ( empty( $data ) ) {
208 return false;
209 }
210
211 if ( empty( $form_id ) ) {
212 $form_id = $data['form_id'];
213 }
214
215 $data = wp_unslash( $data );
216
217 if ( ! empty( $data['settings']['form_title'] ) ) {
218 $title = $data['settings']['form_title'];
219 } else {
220 $title = get_the_title( $form_id );
221 }
222
223 if ( ! empty( $data['settings']['form_desc'] ) ) {
224 $desc = $data['settings']['form_desc'];
225 } else {
226 $desc = '';
227 }
228
229 $data['form_field_id'] = ! empty( $data['form_field_id'] ) ? absint( $data['form_field_id'] ) : '0';
230
231 // This filter can destroy the JSON when messing with HTML.
232 remove_filter( 'content_save_pre', 'balanceTags', 50 );
233
234 // Don't allow tags for users who do not have appropriate cap.
235 if ( ! current_user_can( 'unfiltered_html' ) ) {
236 $data = map_deep( $data, 'wp_strip_all_tags' );
237 }
238
239 // Prevent content filters from corrupting JSON in post_content.
240 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
241 if ( $has_kses ) {
242 kses_remove_filters();
243 }
244 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
245 if ( $has_targeted_link_rel_filters ) {
246 wp_remove_targeted_link_rel_filters();
247 }
248
249 $form = array(
250 'ID' => $form_id,
251 'post_title' => esc_html( $title ),
252 'post_excerpt' => $desc,
253 'post_content' => evf_encode( $data ),
254 );
255 $form = apply_filters( 'everest_forms_save_form_args', $form, $data, $args );
256 $form_id = wp_update_post( $form );
257
258 // Restore removed content filters.
259 if ( $has_kses ) {
260 kses_init_filters();
261 }
262 if ( $has_targeted_link_rel_filters ) {
263 wp_init_targeted_link_rel_filters();
264 }
265
266 do_action( 'everest_forms_save_form', $form_id, $form );
267
268 return $form_id;
269 }
270
271 /**
272 * Duplicate forms.
273 *
274 * @since 1.0.0
275 *
276 * @param array $ids Form IDs to duplicate.
277 *
278 * @return boolean
279 */
280 public function duplicate( $ids = array() ) {
281 // Check for permissions.
282 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
283 return false;
284 }
285
286 if ( ! is_array( $ids ) ) {
287 $ids = array( $ids );
288 }
289
290 $ids = array_map( 'absint', $ids );
291
292 foreach ( $ids as $id ) {
293
294 // Get original entry.
295 $form = get_post( $id );
296
297 // Confirm form exists.
298 if ( ! $form || empty( $form ) ) {
299 return false;
300 }
301
302 // Get the form data.
303 $new_form_data = evf_decode( $form->post_content );
304
305 // Remove form ID from title if present.
306 $new_form_data['settings']['form_title'] = str_replace( '(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] );
307
308 // Create the duplicate form.
309 $new_form = array(
310 'post_author' => $form->post_author,
311 'post_content' => evf_encode( $new_form_data ),
312 'post_excerpt' => $form->post_excerpt,
313 'post_status' => $form->post_status,
314 'post_title' => $new_form_data['settings']['form_title'],
315 'post_type' => $form->post_type,
316 );
317 $new_form_id = wp_insert_post( $new_form );
318
319 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
320 return false;
321 }
322
323 // Set new form name.
324 $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')';
325
326 // Set new form ID.
327 $new_form_data['id'] = absint( $new_form_id );
328
329 // Update new duplicate form.
330 $new_form_id = $this->update( $new_form_id, $new_form_data );
331
332 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
333 return false;
334 }
335
336 return $new_form_id;
337 }
338
339 return true;
340 }
341
342 /**
343 * Get private meta information for a form.
344 *
345 * @since 1.1.0
346 *
347 * @param int $form_id Form ID.
348 * @param string $field Field.
349 *
350 * @return false|array
351 */
352 public function get_meta( $form_id, $field = '' ) {
353 if ( empty( $form_id ) ) {
354 return false;
355 }
356
357 $data = $this->get(
358 $form_id,
359 array(
360 'content_only' => true,
361 )
362 );
363
364 if ( isset( $data['meta'] ) ) {
365 if ( empty( $field ) ) {
366 return $data['meta'];
367 } elseif ( isset( $data['meta'][ $field ] ) ) {
368 return $data['meta'][ $field ];
369 }
370 }
371
372 return false;
373 }
374
375 /**
376 * Get the next available field ID and increment by one.
377 *
378 * @since 1.0.0
379 * @param int $form_id Form ID.
380 * @return mixed int or false
381 */
382 public function field_unique_key( $form_id ) {
383 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
384 return false;
385 }
386
387 if ( empty( $form_id ) ) {
388 return false;
389 }
390
391 $form = $this->get(
392 $form_id,
393 array(
394 'content_only' => true,
395 )
396 );
397
398 if ( ! empty( $form['form_field_id'] ) ) {
399 $form_field_id = absint( $form['form_field_id'] );
400 $form['form_field_id'] ++;
401 } else {
402 $form_field_id = '0';
403 $form['form_field_id'] = '1';
404 }
405
406 $this->update( $form_id, $form );
407
408 $field_id = evf_get_random_string() . '-' . $form_field_id;
409
410 return $field_id;
411 }
412
413 /**
414 * Get private meta information for a form field.
415 *
416 * @since 1.0.0
417 *
418 * @param int $form_id Form ID.
419 * @param string $field_id Field ID.
420 *
421 * @return array|bool
422 */
423 public function get_field( $form_id, $field_id = '' ) {
424
425 if ( empty( $form_id ) ) {
426 return false;
427 }
428
429 $data = $this->get(
430 $form_id,
431 array(
432 'content_only' => true,
433 )
434 );
435
436 return isset( $data['form_fields'][ $field_id ] ) ? $data['form_fields'][ $field_id ] : false;
437 }
438
439 /**
440 * Get private meta information for a form field.
441 *
442 * @since 1.0.0
443 *
444 * @param int $form_id Form ID.
445 * @param string $field Field.
446 *
447 * @return bool
448 */
449 public function get_field_meta( $form_id, $field = '' ) {
450
451 $field = $this->get_field( $form_id, $field );
452 if ( ! $field ) {
453 return false;
454 }
455
456 return isset( $field['meta'] ) ? $field['meta'] : false;
457 }
458 }
459