PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.5.4
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.5.4
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 8 years ago shortcodes 6 years ago templates 7 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 8 years ago class-evf-deprecated-action-hooks.php 7 years ago class-evf-deprecated-filter-hooks.php 7 years ago class-evf-emails.php 7 years ago class-evf-fields.php 7 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 7 years ago class-evf-frontend-scripts.php 7 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 8 years ago class-evf-post-types.php 7 years ago class-evf-privacy.php 7 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 7 years ago class-evf-smart-tags.php 7 years ago class-evf-template-loader.php 7 years ago class-evf-validation.php 8 years ago evf-conditional-functions.php 7 years ago evf-core-functions.php 6 years ago evf-deprecated-functions.php 7 years ago evf-entry-functions.php 6 years ago evf-formatting-functions.php 7 years ago evf-notice-functions.php 6 years ago evf-template-functions.php 7 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 6 years ago
class-evf-form-handler.php
434 lines
1 <?php
2
3 /**
4 * form handler.
5 *
6 * Contains a bunch of helper methods as well.
7 *
8 * @package EverestForms
9 * @author WPEverest
10 * @since 1.0.0
11 */
12 class EVF_Form_Handler {
13
14 /**
15 * Fetches forms
16 *
17 * @since 1.0.0
18 * @param mixed $id
19 * @param array $args
20 * @return array|bool|null|WP_Post
21 */
22 public function get( $id = '', $args = array() ) {
23 $forms = array();
24 $args = apply_filters( 'everest_forms_get_form_args', $args );
25
26 if ( false === $id ) {
27 return false;
28 }
29
30 if ( ! empty( $id ) ) {
31 $the_post = get_post( absint( $id ) );
32
33 if ( $the_post && 'everest_form' === $the_post->post_type ) {
34 $forms = empty( $args['content_only'] ) ? $the_post : evf_decode( $the_post->post_content );
35 }
36 } else {
37 // No ID provided, get multiple forms.
38 $defaults = array(
39 'post_type' => 'everest_form',
40 'orderby' => 'id',
41 'order' => 'DESC',
42 'no_found_rows' => true,
43 'nopaging' => true,
44 );
45
46 $args = wp_parse_args( $args, $defaults );
47
48 $args['post_type'] = 'everest_form';
49
50 $forms = get_posts( $args );
51 }
52
53 if ( empty( $forms ) ) {
54 return false;
55 }
56
57 return $forms;
58 }
59
60 /**
61 * Delete forms.
62 *
63 * @since 1.0.0
64 * @param array $ids
65 * @return boolean
66 */
67 public function delete( $ids = array() ) {
68 // Check for permissions.
69 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
70 return false;
71 }
72
73 if ( ! is_array( $ids ) ) {
74 $ids = array( $ids );
75 }
76
77 $ids = array_map( 'absint', $ids );
78
79 foreach ( $ids as $id ) {
80 $form = wp_delete_post( $id, true );
81
82 if ( class_exists( 'EVF_Entry_Handler' ) ) {
83 // Delete entry if exists.
84 }
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
99 * @param array $args
100 * @param array $data
101 * @return mixed
102 */
103 public static function create( $title = '', $template = 'blank', $args = array(), $data = array() ) {
104 if ( empty( $title ) || ! current_user_can( 'manage_everest_forms' ) ) {
105 return false;
106 }
107
108 $args = apply_filters( 'everest_forms_create_form_args', $args, $data );
109 $form_content = array(
110 'form_field_id' => '1',
111 'settings' => array(
112 'form_title' => sanitize_text_field( $title ),
113 'form_desc' => '',
114 ),
115 );
116
117 // Check for template and format the form content.
118 if ( in_array( $template, array( 'contact' ), true ) ) {
119 include_once dirname( __FILE__ ) . "/templates/{$template}.php";
120 $form_content = $form_template[ $template ];
121 }
122
123 // Prevent content filters from corrupting JSON in post_content.
124 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
125 if ( $has_kses ) {
126 kses_remove_filters();
127 }
128 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
129 if ( $has_targeted_link_rel_filters ) {
130 wp_remove_targeted_link_rel_filters();
131 }
132
133 // Create a form.
134 $form_id = wp_insert_post(
135 array(
136 'post_title' => esc_html( $title ),
137 'post_status' => 'publish',
138 'post_type' => 'everest_form',
139 'post_content' => '{}',
140 )
141 );
142
143 if ( $form_id ) {
144 $form_data = wp_parse_args(
145 $args,
146 array(
147 'ID' => $form_id,
148 'post_title' => esc_html( $title ),
149 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_content ) ),
150 )
151 );
152
153 wp_update_post( $form_data );
154 }
155
156 // Restore removed content filters.
157 if ( $has_kses ) {
158 kses_init_filters();
159 }
160 if ( $has_targeted_link_rel_filters ) {
161 wp_init_targeted_link_rel_filters();
162 }
163
164 do_action( 'everest_forms_create_form', $form_id, $form_data, $data );
165
166 return $form_id;
167 }
168
169 /**
170 * Updates form
171 *
172 * @since 1.0.0
173 * @param string $form_id
174 * @param array $data
175 * @param array $args
176 * @return mixed
177 * @internal param string $title
178 */
179 public function update( $form_id = '', $data = array(), $args = array() ) {
180 // Check for permissions.
181 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
182 return false;
183 }
184
185 if ( empty( $data ) ) {
186 return false;
187 }
188
189 if ( empty( $form_id ) ) {
190 $form_id = $data['form_id'];
191 }
192
193 $data = wp_unslash( $data );
194
195 if ( ! empty( $data['settings']['form_title'] ) ) {
196 $title = $data['settings']['form_title'];
197 } else {
198 $title = get_the_title( $form_id );
199 }
200
201 if ( ! empty( $data['settings']['form_desc'] ) ) {
202 $desc = $data['settings']['form_desc'];
203 } else {
204 $desc = '';
205 }
206
207 $data['form_field_id'] = ! empty( $data['form_field_id'] ) ? absint( $data['form_field_id'] ) : '0';
208
209 // This filter can destroy the JSON when messing with HTML.
210 remove_filter( 'content_save_pre', 'balanceTags', 50 );
211
212 // Don't allow tags for users who do not have appropriate cap.
213 if ( ! current_user_can( 'unfiltered_html' ) ) {
214 $data = map_deep( $data, 'wp_strip_all_tags' );
215 }
216
217 // Prevent content filters from corrupting JSON in post_content.
218 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
219 if ( $has_kses ) {
220 kses_remove_filters();
221 }
222 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
223 if ( $has_targeted_link_rel_filters ) {
224 wp_remove_targeted_link_rel_filters();
225 }
226
227 $form = array(
228 'ID' => $form_id,
229 'post_title' => esc_html( $title ),
230 'post_excerpt' => $desc,
231 'post_content' => evf_encode( $data ),
232 );
233 $form = apply_filters( 'everest_forms_save_form_args', $form, $data, $args );
234 $form_id = wp_update_post( $form );
235
236 // Restore removed content filters.
237 if ( $has_kses ) {
238 kses_init_filters();
239 }
240 if ( $has_targeted_link_rel_filters ) {
241 wp_init_targeted_link_rel_filters();
242 }
243
244 do_action( 'everest_forms_save_form', $form_id, $form );
245
246 return $form_id;
247 }
248
249 /**
250 * Duplicate forms.
251 *
252 * @since 1.0.0
253 * @param array $ids
254 * @return boolean
255 */
256 public function duplicate( $ids = array() ) {
257 // Check for permissions.
258 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
259 return false;
260 }
261
262 if ( ! is_array( $ids ) ) {
263 $ids = array( $ids );
264 }
265
266 $ids = array_map( 'absint', $ids );
267
268 foreach ( $ids as $id ) {
269
270 // Get original entry.
271 $form = get_post( $id );
272
273 // Confirm form exists.
274 if ( ! $form || empty( $form ) ) {
275 return false;
276 }
277
278 // Get the form data.
279 $new_form_data = evf_decode( $form->post_content );
280
281 // Remove form ID from title if present.
282 $new_form_data['settings']['form_title'] = str_replace( '(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] );
283
284 // Create the duplicate form.
285 $new_form = array(
286 'post_author' => $form->post_author,
287 'post_content' => evf_encode( $new_form_data ),
288 'post_excerpt' => $form->post_excerpt,
289 'post_status' => $form->post_status,
290 'post_title' => $new_form_data['settings']['form_title'],
291 'post_type' => $form->post_type,
292 );
293 $new_form_id = wp_insert_post( $new_form );
294
295 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
296 return false;
297 }
298
299 // Set new form name.
300 $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')';
301
302 // Set new form ID.
303 $new_form_data['id'] = absint( $new_form_id );
304
305 // Update new duplicate form.
306 $new_form_id = $this->update( $new_form_id, $new_form_data );
307
308 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
309 return false;
310 }
311
312 return $new_form_id;
313 }
314
315 return true;
316 }
317
318 /**
319 * Get private meta information for a form.
320 *
321 * @since 1.1.0
322 *
323 * @param string $form_id
324 * @param string $field
325 *
326 * @return bool
327 */
328 public function get_meta( $form_id, $field = '' ) {
329 if ( empty( $form_id ) ) {
330 return false;
331 }
332
333 $data = $this->get(
334 $form_id,
335 array(
336 'content_only' => true,
337 )
338 );
339
340 if ( isset( $data['meta'] ) ) {
341 if ( empty( $field ) ) {
342 return $data['meta'];
343 } elseif ( isset( $data['meta'][ $field ] ) ) {
344 return $data['meta'][ $field ];
345 }
346 }
347
348 return false;
349 }
350
351 /**
352 * Get the next available field ID and increment by one.
353 *
354 * @since 1.0.0
355 * @param int $form_id
356 * @return mixed int or false
357 */
358 public function field_unique_key( $form_id ) {
359 if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) {
360 return false;
361 }
362
363 if ( empty( $form_id ) ) {
364 return false;
365 }
366
367 $form = $this->get(
368 $form_id,
369 array(
370 'content_only' => true,
371 )
372 );
373
374 if ( ! empty( $form['form_field_id'] ) ) {
375 $form_field_id = absint( $form['form_field_id'] );
376 $form['form_field_id'] ++;
377 } else {
378 $form_field_id = '0';
379 $form['form_field_id'] = '1';
380 }
381
382 $this->update( $form_id, $form );
383
384 $field_id = evf_get_random_string() . '-' . $form_field_id;
385
386 return $field_id;
387 }
388
389
390 /**
391 * Get private meta information for a form field.
392 *
393 * @since 1.0.0
394 * @param string $form_id
395 * @param string $field_id
396 * @return bool
397 */
398 public function get_field( $form_id, $field_id = '' ) {
399
400 if ( empty( $form_id ) ) {
401 return false;
402 }
403
404 $data = $this->get(
405 $form_id,
406 array(
407 'content_only' => true,
408 )
409 );
410
411 return isset( $data['form_fields'][ $field_id ] ) ? $data['form_fields'][ $field_id ] : false;
412 }
413
414 /**
415 * Get private meta information for a form field.
416 *
417 * @since 1.0.0
418 *
419 * @param string $form_id
420 * @param string $field
421 *
422 * @return bool
423 */
424 public function get_field_meta( $form_id, $field = '' ) {
425
426 $field = $this->get_field( $form_id, $field );
427 if ( ! $field ) {
428 return false;
429 }
430
431 return isset( $field['meta'] ) ? $field['meta'] : false;
432 }
433 }
434