PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.5.0
Secure Custom Fields v6.5.0
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / forms / form-front.php
secure-custom-fields / includes / forms Last commit date
WC_Order.php 1 year ago form-attachment.php 1 year ago form-comment.php 1 year ago form-customizer.php 1 year ago form-front.php 1 year ago form-gutenberg.php 1 year ago form-nav-menu.php 1 year ago form-post.php 1 year ago form-taxonomy.php 1 year ago form-user.php 1 year ago form-widget.php 1 year ago index.php 1 year ago
form-front.php
601 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'acf_form_front' ) ) :
8 #[AllowDynamicProperties]
9 class acf_form_front {
10
11 /**
12 * An array of registered form settings.
13 *
14 * @var array
15 */
16 private $forms = array();
17
18 /**
19 * An array of default fields.
20 *
21 * @var array
22 */
23 public $fields = array();
24
25 /**
26 * Constructs the class.
27 *
28 * @since ACF 5.0.0
29 */
30 public function __construct() {
31 add_action( 'acf/validate_save_post', array( $this, 'validate_save_post' ), 1 );
32 add_filter( 'acf/pre_save_post', array( $this, 'pre_save_post' ), 5, 2 );
33 }
34
35 /**
36 * Returns fields used by frontend forms.
37 *
38 * @since SCF 6.5
39 *
40 * @return array
41 */
42 public function get_default_fields(): array {
43 $this->fields = array(
44 '_post_title' => array(
45 'prefix' => 'acf',
46 'name' => '_post_title',
47 'key' => '_post_title',
48 'label' => __( 'Title', 'secure-custom-fields' ),
49 'type' => 'text',
50 'required' => true,
51 ),
52
53 '_post_content' => array(
54 'prefix' => 'acf',
55 'name' => '_post_content',
56 'key' => '_post_content',
57 'label' => __( 'Content', 'secure-custom-fields' ),
58 'type' => 'wysiwyg',
59 ),
60
61 '_validate_email' => array(
62 'prefix' => 'acf',
63 'name' => '_validate_email',
64 'key' => '_validate_email',
65 'label' => __( 'Validate Email', 'secure-custom-fields' ),
66 'type' => 'text',
67 'value' => '',
68 'wrapper' => array( 'style' => 'display:none !important;' ),
69 ),
70 );
71
72 return $this->fields;
73 }
74
75 /**
76 * Validates form arguments and applies defaults.
77 *
78 * @type function
79 * @date 28/2/17
80 * @since ACF 5.5.8
81 *
82 * @param $post_id (int)
83 * @return $post_id (int)
84 */
85 function validate_form( $args ) {
86
87 // defaults
88 // Todo: Allow message and button text to be generated by CPT settings.
89 $args = wp_parse_args(
90 $args,
91 array(
92 'id' => 'acf-form',
93 'post_id' => false,
94 'new_post' => false,
95 'field_groups' => false,
96 'fields' => false,
97 'post_title' => false,
98 'post_content' => false,
99 'form' => true,
100 'form_attributes' => array(),
101 'return' => add_query_arg( 'updated', 'true', acf_get_current_url() ),
102 'html_before_fields' => '',
103 'html_after_fields' => '',
104 'submit_value' => __( 'Update', 'secure-custom-fields' ),
105 'updated_message' => __( 'Post updated', 'secure-custom-fields' ),
106 'label_placement' => 'top',
107 'instruction_placement' => 'label',
108 'field_el' => 'div',
109 'uploader' => 'wp',
110 'honeypot' => true,
111 'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>', // 5.5.10
112 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', // 5.5.10
113 'html_submit_spinner' => '<span class="acf-spinner"></span>', // 5.5.10
114 'kses' => true, // 5.6.5
115 )
116 );
117
118 $args['form_attributes'] = wp_parse_args(
119 $args['form_attributes'],
120 array(
121 'id' => $args['id'],
122 'class' => 'acf-form',
123 'action' => '',
124 'method' => 'post',
125 )
126 );
127
128 // filter post_id
129 $args['post_id'] = acf_get_valid_post_id( $args['post_id'] );
130
131 // new post?
132 if ( $args['post_id'] === 'new_post' ) {
133 $args['new_post'] = wp_parse_args(
134 $args['new_post'],
135 array(
136 'post_type' => 'post',
137 'post_status' => 'draft',
138 )
139 );
140 }
141
142 // filter
143 $args = apply_filters( 'acf/validate_form', $args );
144
145 // return
146 return $args;
147 }
148
149
150 /**
151 * description
152 *
153 * @type function
154 * @date 28/2/17
155 * @since ACF 5.5.8
156 *
157 * @param $post_id (int)
158 * @return $post_id (int)
159 */
160 function add_form( $args = array() ) {
161
162 // validate
163 $args = $this->validate_form( $args );
164
165 // append
166 $this->forms[ $args['id'] ] = $args;
167 }
168
169
170 /**
171 * description
172 *
173 * @type function
174 * @date 28/2/17
175 * @since ACF 5.5.8
176 *
177 * @param $post_id (int)
178 * @return $post_id (int)
179 */
180 function get_form( $id = '' ) {
181
182 // bail early if not set
183 if ( ! isset( $this->forms[ $id ] ) ) {
184 return false;
185 }
186
187 // return
188 return $this->forms[ $id ];
189 }
190
191 /**
192 * Returns all registered forms.
193 *
194 * @type function
195 * @date 28/2/17
196 * @since ACF 5.5.8
197 *
198 * @return forms (array)
199 */
200 function get_forms() {
201 return $this->forms;
202 }
203
204 /**
205 * This function will validate fields from the above array
206 *
207 * @type function
208 * @date 7/09/2016
209 * @since ACF 5.4.0
210 *
211 * @param $post_id (int)
212 * @return $post_id (int)
213 */
214 function validate_save_post() {
215
216 // register field if isset in $_POST
217 foreach ( $this->get_default_fields() as $k => $field ) {
218
219 // bail early if no in $_POST
220 if ( ! isset( $_POST['acf'][ $k ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
221 continue;
222 }
223
224 // register
225 acf_add_local_field( $field );
226 }
227
228 // honeypot
229 if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam.
230
231 acf_add_validation_error( '', __( 'Spam Detected', 'secure-custom-fields' ) );
232 }
233 }
234
235
236 /**
237 * description
238 *
239 * @type function
240 * @date 7/09/2016
241 * @since ACF 5.4.0
242 *
243 * @param $post_id (int)
244 * @return $post_id (int)
245 */
246 function pre_save_post( $post_id, $form ) {
247
248 // vars
249 $save = array(
250 'ID' => 0,
251 );
252
253 // determine save data
254 if ( is_numeric( $post_id ) ) {
255
256 // update post
257 $save['ID'] = $post_id;
258 } elseif ( $post_id == 'new_post' ) {
259
260 // merge in new post data
261 $save = array_merge( $save, $form['new_post'] );
262 } else {
263
264 // not post
265 return $post_id;
266 }
267
268 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in check_submit_form().
269 // save post_title
270 if ( isset( $_POST['acf']['_post_title'] ) ) {
271 $save['post_title'] = acf_extract_var( $_POST['acf'], '_post_title' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP when saved.
272 }
273
274 // save post_content
275 if ( isset( $_POST['acf']['_post_content'] ) ) {
276 $save['post_content'] = acf_extract_var( $_POST['acf'], '_post_content' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP when saved.
277 }
278 // phpcs:enable WordPress.Security.NonceVerification.Missing
279
280 // honeypot
281 if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam.
282 return false;
283 }
284
285 // validate
286 if ( count( $save ) == 1 ) {
287 return $post_id;
288 }
289
290 // save
291 if ( $save['ID'] ) {
292 wp_update_post( $save );
293 } else {
294 $post_id = wp_insert_post( $save );
295 }
296
297 // return
298 return $post_id;
299 }
300
301
302 /**
303 * This function will enqueue a form
304 *
305 * @type function
306 * @date 7/09/2016
307 * @since ACF 5.4.0
308 *
309 * @param $post_id (int)
310 * @return $post_id (int)
311 */
312 function enqueue_form() {
313
314 // check
315 $this->check_submit_form();
316
317 // load acf scripts
318 acf_enqueue_scripts();
319 }
320
321
322 /**
323 * This function will maybe submit form data
324 *
325 * @type function
326 * @date 3/3/17
327 * @since ACF 5.5.10
328 *
329 * @param n/a
330 * @return n/a
331 */
332 function check_submit_form() {
333
334 // Verify nonce.
335 if ( ! acf_verify_nonce( 'acf_form' ) ) {
336 return false;
337 }
338
339 // Confirm form was submit.
340 if ( ! isset( $_POST['_acf_form'] ) ) {
341 return false;
342 }
343
344 // Load registered form using id.
345 $form = $this->get_form( acf_sanitize_request_args( $_POST['_acf_form'] ) );
346
347 // Fallback to encrypted JSON.
348 if ( ! $form ) {
349 $form = json_decode( acf_decrypt( sanitize_text_field( $_POST['_acf_form'] ) ), true );
350 if ( ! $form ) {
351 return false;
352 }
353 }
354
355 // Run kses on all $_POST data.
356 if ( $form['kses'] && isset( $_POST['acf'] ) ) {
357 $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- False positive.
358 }
359
360 // Validate data and show errors.
361 // Todo: Return WP_Error and show above form, keeping input values.
362 acf_validate_save_post( true );
363
364 // Submit form.
365 $this->submit_form( $form );
366 }
367
368
369 /**
370 * This function will submit form data
371 *
372 * @type function
373 * @date 3/3/17
374 * @since ACF 5.5.10
375 *
376 * @param n/a
377 * @return n/a
378 */
379 function submit_form( $form ) {
380
381 // filter
382 $form = apply_filters( 'acf/pre_submit_form', $form );
383
384 // vars
385 $post_id = acf_maybe_get( $form, 'post_id', 0 );
386
387 // add global for backwards compatibility
388 $GLOBALS['acf_form'] = $form;
389
390 // allow for custom save
391 $post_id = apply_filters( 'acf/pre_save_post', $post_id, $form );
392
393 // save
394 acf_save_post( $post_id );
395
396 // restore form (potentially modified)
397 $form = $GLOBALS['acf_form'];
398
399 // action
400 do_action( 'acf/submit_form', $form, $post_id );
401
402 // vars
403 $return = acf_maybe_get( $form, 'return', '' );
404
405 // redirect
406 if ( $return ) {
407
408 // update %placeholders%
409 $return = str_replace( '%post_id%', $post_id, $return );
410 $return = str_replace( '%post_url%', get_permalink( $post_id ), $return );
411
412 // redirect
413 wp_redirect( $return ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- unsafe redirects allowed.
414 exit;
415 }
416 }
417
418
419 /**
420 * description
421 *
422 * @type function
423 * @date 7/09/2016
424 * @since ACF 5.4.0
425 *
426 * @param $post_id (int)
427 * @return $post_id (int)
428 */
429 function render_form( $args = array() ) {
430
431 // Vars.
432 $is_registered = false;
433 $field_groups = array();
434 $fields = array();
435
436 // Allow form settings to be directly provided.
437 if ( is_array( $args ) ) {
438 $args = $this->validate_form( $args );
439
440 // Otherwise, lookup registered form.
441 } else {
442 $is_registered = true;
443 $args = $this->get_form( $args );
444 if ( ! $args ) {
445 return false;
446 }
447 }
448
449 // Extract vars.
450 $post_id = $args['post_id'];
451
452 // Prevent ACF from loading values for "new_post".
453 if ( $post_id === 'new_post' ) {
454 $post_id = false;
455 }
456
457 // Set uploader type.
458 acf_update_setting( 'uploader', $args['uploader'] );
459
460 // Register local fields.
461 foreach ( $this->get_default_fields() as $k => $field ) {
462 acf_add_local_field( $field );
463 }
464
465 // Append post_title field.
466 if ( $args['post_title'] ) {
467 $_post_title = acf_get_field( '_post_title' );
468 $_post_title['value'] = $post_id ? get_post_field( 'post_title', $post_id ) : '';
469 $fields[] = $_post_title;
470 }
471
472 // Append post_content field.
473 if ( $args['post_content'] ) {
474 $_post_content = acf_get_field( '_post_content' );
475 $_post_content['value'] = $post_id ? get_post_field( 'post_content', $post_id ) : '';
476 $fields[] = $_post_content;
477 }
478
479 // Load specific fields.
480 if ( $args['fields'] ) {
481
482 // Lookup fields using $strict = false for better compatibility with field names.
483 foreach ( $args['fields'] as $selector ) {
484 $fields[] = acf_maybe_get_field( $selector, $post_id, false );
485 }
486
487 // Load specific field groups.
488 } elseif ( $args['field_groups'] ) {
489 foreach ( $args['field_groups'] as $selector ) {
490 $field_groups[] = acf_get_field_group( $selector );
491 }
492
493 // Load fields for the given "new_post" args.
494 } elseif ( $args['post_id'] == 'new_post' ) {
495 $field_groups = acf_get_field_groups( $args['new_post'] );
496
497 // Load fields for the given "post_id" arg.
498 } else {
499 $field_groups = acf_get_field_groups(
500 array(
501 'post_id' => $args['post_id'],
502 )
503 );
504 }
505
506 // load fields from the found field groups.
507 if ( $field_groups ) {
508 foreach ( $field_groups as $field_group ) {
509 $_fields = acf_get_fields( $field_group );
510 if ( $_fields ) {
511 foreach ( $_fields as $_field ) {
512 $fields[] = $_field;
513 }
514 }
515 }
516 }
517
518 // Add honeypot field.
519 if ( $args['honeypot'] ) {
520 $fields[] = acf_get_field( '_validate_email' );
521 }
522
523 // Display updated_message
524 if ( ! empty( $_GET['updated'] ) && $args['updated_message'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used as a flag; data not used.
525 printf( $args['html_updated_message'], $args['updated_message'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers.
526 }
527
528 // display form
529 if ( $args['form'] ) : ?>
530 <form <?php echo acf_esc_attrs( $args['form_attributes'] ); ?>>
531 <?php
532 endif;
533
534 // Render hidde form data.
535 acf_form_data(
536 array(
537 'screen' => 'acf_form',
538 'post_id' => $args['post_id'],
539 'form' => $is_registered ? $args['id'] : acf_encrypt( json_encode( $args ) ),
540 )
541 );
542
543 ?>
544 <div class="acf-fields acf-form-fields -<?php echo esc_attr( $args['label_placement'] ); ?>">
545 <?php echo $args['html_before_fields']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?>
546 <?php acf_render_fields( $fields, $post_id, $args['field_el'], $args['instruction_placement'] ); ?>
547 <?php echo $args['html_after_fields']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?>
548 </div>
549 <?php if ( $args['form'] ) : ?>
550 <div class="acf-form-submit">
551 <?php printf( $args['html_submit_button'], $args['submit_value'] ); ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?>
552 <?php echo $args['html_submit_spinner']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?>
553 </div>
554 </form>
555 <?php endif;
556 }
557 }
558
559 // initialize
560 acf()->form_front = new acf_form_front();
561 endif; // class_exists check
562
563
564 /**
565 * Functions
566 *
567 * alias of acf()->form->functions
568 *
569 * @type function
570 * @date 11/06/2014
571 * @since ACF 5.0.0
572 *
573 * @param n/a
574 * @return n/a
575 */
576 function acf_form_head() {
577
578 acf()->form_front->enqueue_form();
579 }
580
581 function acf_form( $args = array() ) {
582
583 acf()->form_front->render_form( $args );
584 }
585
586 function acf_get_form( $id = '' ) {
587
588 return acf()->form_front->get_form( $id );
589 }
590
591 function acf_get_forms() {
592 return acf()->form_front->get_forms();
593 }
594
595 function acf_register_form( $args ) {
596
597 acf()->form_front->add_form( $args );
598 }
599
600 ?>
601