WC_Order.php
2 weeks ago
form-attachment.php
1 year ago
form-comment.php
8 months ago
form-customizer.php
11 months ago
form-front.php
6 days ago
form-gutenberg.php
1 year ago
form-nav-menu.php
8 months ago
form-post.php
2 months ago
form-taxonomy.php
1 month ago
form-user.php
8 months ago
form-widget.php
11 months ago
index.php
1 year ago
form-front.php
1207 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'acf_form_front' ) ) : |
| 8 | class acf_form_front { |
| 9 | |
| 10 | /** |
| 11 | * An array of registered form settings. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | private $forms = array(); |
| 16 | |
| 17 | /** |
| 18 | * An array of default fields. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | public $fields = array(); |
| 23 | |
| 24 | /** |
| 25 | * Per-request render id, shared across every render_form() call in this request. |
| 26 | * |
| 27 | * @var string|null |
| 28 | */ |
| 29 | private $render_id = null; |
| 30 | |
| 31 | /** |
| 32 | * Grant verified for the next HTTP form submission. |
| 33 | * |
| 34 | * A null value keeps direct submit_form() calls on their existing path. |
| 35 | * |
| 36 | * @var array|null |
| 37 | */ |
| 38 | private $submission_grant = null; |
| 39 | |
| 40 | /** |
| 41 | * Constructs the class. |
| 42 | * |
| 43 | * @since ACF 5.0.0 |
| 44 | */ |
| 45 | public function __construct() { |
| 46 | add_action( |
| 47 | 'acf/validate_save_post', |
| 48 | function () { |
| 49 | $this->validate_save_post_authority(); |
| 50 | }, |
| 51 | 0 |
| 52 | ); |
| 53 | add_action( 'acf/validate_save_post', array( $this, 'validate_save_post' ), 1 ); |
| 54 | add_filter( 'acf/pre_save_post', array( $this, 'pre_save_post' ), 5, 2 ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns fields used by frontend forms. |
| 59 | * |
| 60 | * @since SCF 6.5 |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function get_default_fields(): array { |
| 65 | $this->fields = array( |
| 66 | '_post_title' => array( |
| 67 | 'prefix' => 'acf', |
| 68 | 'name' => '_post_title', |
| 69 | 'key' => '_post_title', |
| 70 | 'label' => __( 'Title', 'secure-custom-fields' ), |
| 71 | 'type' => 'text', |
| 72 | 'required' => true, |
| 73 | ), |
| 74 | |
| 75 | '_post_content' => array( |
| 76 | 'prefix' => 'acf', |
| 77 | 'name' => '_post_content', |
| 78 | 'key' => '_post_content', |
| 79 | 'label' => __( 'Content', 'secure-custom-fields' ), |
| 80 | 'type' => 'wysiwyg', |
| 81 | ), |
| 82 | |
| 83 | '_validate_email' => array( |
| 84 | 'prefix' => 'acf', |
| 85 | 'name' => '_validate_email', |
| 86 | 'key' => '_validate_email', |
| 87 | 'label' => __( 'Validate Email', 'secure-custom-fields' ), |
| 88 | 'type' => 'text', |
| 89 | 'value' => '', |
| 90 | 'wrapper' => array( 'style' => 'display:none !important;' ), |
| 91 | ), |
| 92 | ); |
| 93 | |
| 94 | return $this->fields; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Validates form arguments and applies defaults. |
| 99 | * |
| 100 | * @type function |
| 101 | * @date 28/2/17 |
| 102 | * @since ACF 5.5.8 |
| 103 | * |
| 104 | * @param $post_id (int) |
| 105 | * @return $post_id (int) |
| 106 | */ |
| 107 | function validate_form( $args ) { |
| 108 | |
| 109 | // defaults |
| 110 | // Todo: Allow message and button text to be generated by CPT settings. |
| 111 | $args = wp_parse_args( |
| 112 | $args, |
| 113 | array( |
| 114 | 'id' => 'acf-form', |
| 115 | 'post_id' => false, |
| 116 | 'new_post' => false, |
| 117 | 'field_groups' => false, |
| 118 | 'fields' => false, |
| 119 | 'post_title' => false, |
| 120 | 'post_content' => false, |
| 121 | 'form' => true, |
| 122 | 'form_attributes' => array(), |
| 123 | 'return' => add_query_arg( 'updated', 'true', acf_get_current_url() ), |
| 124 | 'html_before_fields' => '', |
| 125 | 'html_after_fields' => '', |
| 126 | 'submit_value' => __( 'Update', 'secure-custom-fields' ), |
| 127 | 'updated_message' => __( 'Post updated', 'secure-custom-fields' ), |
| 128 | 'label_placement' => 'top', |
| 129 | 'instruction_placement' => 'label', |
| 130 | 'field_el' => 'div', |
| 131 | 'uploader' => 'wp', |
| 132 | 'honeypot' => true, |
| 133 | 'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>', // 5.5.10 |
| 134 | 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', // 5.5.10 |
| 135 | 'html_submit_spinner' => '<span class="acf-spinner"></span>', // 5.5.10 |
| 136 | 'kses' => true, // 5.6.5 |
| 137 | ) |
| 138 | ); |
| 139 | |
| 140 | $args['form_attributes'] = wp_parse_args( |
| 141 | $args['form_attributes'], |
| 142 | array( |
| 143 | 'id' => $args['id'], |
| 144 | 'class' => 'acf-form', |
| 145 | 'action' => '', |
| 146 | 'method' => 'post', |
| 147 | ) |
| 148 | ); |
| 149 | |
| 150 | // filter post_id |
| 151 | $args['post_id'] = acf_get_valid_post_id( $args['post_id'] ); |
| 152 | |
| 153 | // new post? |
| 154 | if ( $args['post_id'] === 'new_post' ) { |
| 155 | $args['new_post'] = wp_parse_args( |
| 156 | $args['new_post'], |
| 157 | array( |
| 158 | 'post_type' => 'post', |
| 159 | 'post_status' => 'draft', |
| 160 | ) |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | // filter |
| 165 | $args = apply_filters( 'acf/validate_form', $args ); |
| 166 | |
| 167 | // return |
| 168 | return $args; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * description |
| 174 | * |
| 175 | * @type function |
| 176 | * @date 28/2/17 |
| 177 | * @since ACF 5.5.8 |
| 178 | * |
| 179 | * @param $post_id (int) |
| 180 | * @return $post_id (int) |
| 181 | */ |
| 182 | function add_form( $args = array() ) { |
| 183 | |
| 184 | // validate |
| 185 | $args = $this->validate_form( $args ); |
| 186 | |
| 187 | // append |
| 188 | $this->forms[ $args['id'] ] = $args; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * description |
| 194 | * |
| 195 | * @type function |
| 196 | * @date 28/2/17 |
| 197 | * @since ACF 5.5.8 |
| 198 | * |
| 199 | * @param $post_id (int) |
| 200 | * @return $post_id (int) |
| 201 | */ |
| 202 | function get_form( $id = '' ) { |
| 203 | |
| 204 | // bail early if not set |
| 205 | if ( ! isset( $this->forms[ $id ] ) ) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // return |
| 210 | return $this->forms[ $id ]; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns all registered forms. |
| 215 | * |
| 216 | * @type function |
| 217 | * @date 28/2/17 |
| 218 | * @since ACF 5.5.8 |
| 219 | * |
| 220 | * @return forms (array) |
| 221 | */ |
| 222 | function get_forms() { |
| 223 | return $this->forms; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Checks the rendered-form grant before AJAX field validation. |
| 228 | * |
| 229 | * @since SCF 6.9.4 |
| 230 | * |
| 231 | * @return void |
| 232 | */ |
| 233 | private function validate_save_post_authority(): void { |
| 234 | // AJAX uses a separate nonce path. Check the rendered form before |
| 235 | // field validators run. |
| 236 | if ( wp_doing_ajax() |
| 237 | && 'acf/validate_save_post' === acf_request_arg( 'action' ) |
| 238 | && ( |
| 239 | 'acf_form' === acf_request_arg( '_acf_screen' ) |
| 240 | || isset( $_POST['_acf_form'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The existing AJAX handler verifies its nonce before firing this action. |
| 241 | || isset( $_POST['_acf_form_meta'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The existing AJAX handler verifies its nonce before firing this action. |
| 242 | || isset( $_POST['_acf_render_id'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The existing AJAX handler verifies its nonce before firing this action. |
| 243 | ) |
| 244 | && false === $this->prepare_submitted_form( true ) |
| 245 | ) { |
| 246 | wp_send_json_success( |
| 247 | array( |
| 248 | 'valid' => 0, |
| 249 | 'errors' => array( |
| 250 | array( |
| 251 | 'input' => false, |
| 252 | 'message' => __( 'SCF could not validate this form because its security information is invalid or has expired.', 'secure-custom-fields' ), |
| 253 | ), |
| 254 | ), |
| 255 | ) |
| 256 | ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * This function will validate fields from the above array |
| 262 | * |
| 263 | * @type function |
| 264 | * @date 7/09/2016 |
| 265 | * @since ACF 5.4.0 |
| 266 | * |
| 267 | * @param $post_id (int) |
| 268 | * @return $post_id (int) |
| 269 | */ |
| 270 | function validate_save_post() { |
| 271 | |
| 272 | // register field if isset in $_POST |
| 273 | foreach ( $this->get_default_fields() as $k => $field ) { |
| 274 | |
| 275 | // bail early if no in $_POST |
| 276 | if ( ! isset( $_POST['acf'][ $k ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere. |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | // register |
| 281 | acf_add_local_field( $field ); |
| 282 | } |
| 283 | |
| 284 | // honeypot |
| 285 | if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam. |
| 286 | |
| 287 | acf_add_validation_error( '', __( 'Spam Detected', 'secure-custom-fields' ) ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * description |
| 294 | * |
| 295 | * @type function |
| 296 | * @date 7/09/2016 |
| 297 | * @since ACF 5.4.0 |
| 298 | * |
| 299 | * @param $post_id (int) |
| 300 | * @return $post_id (int) |
| 301 | */ |
| 302 | function pre_save_post( $post_id, $form ) { |
| 303 | |
| 304 | // vars |
| 305 | $save = array( |
| 306 | 'ID' => 0, |
| 307 | ); |
| 308 | |
| 309 | // determine save data |
| 310 | if ( is_numeric( $post_id ) ) { |
| 311 | |
| 312 | // update post |
| 313 | $save['ID'] = $post_id; |
| 314 | } elseif ( $post_id == 'new_post' ) { |
| 315 | |
| 316 | // merge in new post data |
| 317 | $save = array_merge( $save, $form['new_post'] ); |
| 318 | } else { |
| 319 | |
| 320 | // not post |
| 321 | return $post_id; |
| 322 | } |
| 323 | |
| 324 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in check_submit_form(). |
| 325 | // Always extract the special _post_title / _post_content fields from $_POST['acf'] so they |
| 326 | // cannot leak into acf_update_values() downstream, but only apply them to the post when the |
| 327 | // form was rendered with the corresponding option enabled (mirrors render_form()). |
| 328 | if ( isset( $_POST['acf']['_post_title'] ) ) { |
| 329 | $post_title = acf_extract_var( $_POST['acf'], '_post_title' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Sanitized by WP when saved; wp_insert_post / wp_update_post expect slashed input. |
| 330 | if ( ! empty( $form['post_title'] ) ) { |
| 331 | $save['post_title'] = $post_title; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if ( isset( $_POST['acf']['_post_content'] ) ) { |
| 336 | $post_content = acf_extract_var( $_POST['acf'], '_post_content' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Sanitized by WP when saved; wp_insert_post / wp_update_post expect slashed input. |
| 337 | if ( ! empty( $form['post_content'] ) ) { |
| 338 | $save['post_content'] = $post_content; |
| 339 | } |
| 340 | } |
| 341 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 342 | |
| 343 | // honeypot |
| 344 | if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam. |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | // validate |
| 349 | if ( count( $save ) == 1 ) { |
| 350 | return $post_id; |
| 351 | } |
| 352 | |
| 353 | // save |
| 354 | if ( $save['ID'] ) { |
| 355 | wp_update_post( $save ); |
| 356 | } else { |
| 357 | $post_id = wp_insert_post( $save ); |
| 358 | } |
| 359 | |
| 360 | // return |
| 361 | return $post_id; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /** |
| 366 | * This function will enqueue a form |
| 367 | * |
| 368 | * @type function |
| 369 | * @date 7/09/2016 |
| 370 | * @since ACF 5.4.0 |
| 371 | * |
| 372 | * @param $post_id (int) |
| 373 | * @return $post_id (int) |
| 374 | */ |
| 375 | function enqueue_form() { |
| 376 | |
| 377 | // check |
| 378 | $this->check_submit_form(); |
| 379 | |
| 380 | // load acf scripts |
| 381 | acf_enqueue_scripts(); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /** |
| 386 | * This function will maybe submit form data |
| 387 | * |
| 388 | * @type function |
| 389 | * @date 3/3/17 |
| 390 | * @since ACF 5.5.10 |
| 391 | * |
| 392 | * @param n/a |
| 393 | * @return n/a |
| 394 | */ |
| 395 | function check_submit_form() { |
| 396 | |
| 397 | // Verify nonce. |
| 398 | if ( ! acf_verify_nonce( 'acf_form' ) ) { |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | $form = $this->prepare_submitted_form(); |
| 403 | if ( false === $form ) { |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | // Run kses on all $_POST data. |
| 408 | if ( $form['kses'] && isset( $_POST['acf'] ) ) { |
| 409 | $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- False positive. |
| 410 | } |
| 411 | |
| 412 | // Validate data and show errors. |
| 413 | // Todo: Return WP_Error and show above form, keeping input values. |
| 414 | acf_validate_save_post( true ); |
| 415 | |
| 416 | // Submit form. |
| 417 | $this->submit_form( $form ); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /** |
| 422 | * This function will submit form data |
| 423 | * |
| 424 | * @type function |
| 425 | * @date 3/3/17 |
| 426 | * @since ACF 5.5.10 |
| 427 | * |
| 428 | * @param n/a |
| 429 | * @return n/a |
| 430 | */ |
| 431 | function submit_form( $form ) { |
| 432 | |
| 433 | $submission_grant = $this->submission_grant; |
| 434 | $this->submission_grant = null; |
| 435 | |
| 436 | // filter |
| 437 | $form = apply_filters( 'acf/pre_submit_form', $form ); |
| 438 | |
| 439 | // vars |
| 440 | $post_id = acf_maybe_get( $form, 'post_id', 0 ); |
| 441 | |
| 442 | // add global for backwards compatibility |
| 443 | $GLOBALS['acf_form'] = $form; |
| 444 | |
| 445 | // allow for custom save |
| 446 | $post_id = apply_filters( 'acf/pre_save_post', $post_id, $form ); |
| 447 | |
| 448 | // Direct calls have no HTTP grant, so keep the existing field-key lookup. |
| 449 | // HTTP submissions reuse the signed keys from the rendered grant. |
| 450 | if ( null === $submission_grant ) { |
| 451 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- Direct callers retain their existing nonce contract. |
| 452 | if ( isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) ) { |
| 453 | $allowed_field_keys = $this->get_allowed_field_keys( $form ); |
| 454 | $this->restrict_submitted_field_keys( $allowed_field_keys, false ); |
| 455 | } |
| 456 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 457 | } else { |
| 458 | // The grant already carries the render-time allowlist. Re-pruning before |
| 459 | // save keeps a callback from reintroducing an unsigned root. |
| 460 | $this->restrict_submitted_field_keys( $submission_grant['allowed_field_keys'] ); |
| 461 | } |
| 462 | |
| 463 | // save |
| 464 | acf_save_post( $post_id ); |
| 465 | |
| 466 | // restore form (potentially modified) |
| 467 | $form = $GLOBALS['acf_form']; |
| 468 | |
| 469 | // action |
| 470 | do_action( 'acf/submit_form', $form, $post_id ); |
| 471 | |
| 472 | // vars |
| 473 | $return = acf_maybe_get( $form, 'return', '' ); |
| 474 | |
| 475 | // redirect |
| 476 | if ( $return ) { |
| 477 | |
| 478 | // update %placeholders% |
| 479 | $return = str_replace( '%post_id%', $post_id, $return ); |
| 480 | $return = str_replace( '%post_url%', get_permalink( $post_id ), $return ); |
| 481 | |
| 482 | // redirect |
| 483 | wp_redirect( $return ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- unsafe redirects allowed. |
| 484 | exit; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Returns the per-request render ID, generating one if necessary. |
| 490 | * |
| 491 | * @since SCF 6.8.8 |
| 492 | * |
| 493 | * @return string |
| 494 | */ |
| 495 | protected function get_render_id(): string { |
| 496 | if ( null === $this->render_id ) { |
| 497 | $this->render_id = wp_generate_uuid4(); |
| 498 | } |
| 499 | return $this->render_id; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Loads the submitted form and applies its signed grant. |
| 504 | * |
| 505 | * Registered IDs are matched exactly. Encrypted inline JSON remains a |
| 506 | * fallback, and grant verification works without OpenSSL. |
| 507 | * |
| 508 | * @since SCF 6.9.4 |
| 509 | * |
| 510 | * @param bool $validation_only Whether AJAX validation may use a registered |
| 511 | * form that is unavailable in this request. |
| 512 | * @return array|false The verified form, or false for an invalid request. |
| 513 | */ |
| 514 | protected function prepare_submitted_form( bool $validation_only = false ) { |
| 515 | $this->submission_grant = null; |
| 516 | |
| 517 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- The standard submit path verifies its nonce before this method runs; the AJAX handler verifies its own nonce. |
| 518 | if ( ! isset( $_POST['_acf_form'] ) || ! is_scalar( $_POST['_acf_form'] ) ) { |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | $submitted_form_value = (string) wp_unslash( $_POST['_acf_form'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- The signed form anchor covers the exact bytes. |
| 523 | $form_is_resolved = false; |
| 524 | |
| 525 | if ( $validation_only ) { |
| 526 | if ( ! isset( $_POST['_acf_post_id'] ) || ! is_scalar( $_POST['_acf_post_id'] ) ) { |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | // The grant covers the raw form value and target. AJAX validation does |
| 531 | // not need the full configuration because it never saves. |
| 532 | $form = array( |
| 533 | 'post_id' => (string) wp_unslash( $_POST['_acf_post_id'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Checked against the signed metadata below. |
| 534 | ); |
| 535 | } else { |
| 536 | $form = $this->get_form( $submitted_form_value ); |
| 537 | $form_is_resolved = is_array( $form ); |
| 538 | |
| 539 | if ( ! $form_is_resolved ) { |
| 540 | $decrypted_form = acf_decrypt( $submitted_form_value ); |
| 541 | if ( is_string( $decrypted_form ) ) { |
| 542 | $form = json_decode( $decrypted_form, true ); |
| 543 | $form_is_resolved = is_array( $form ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if ( ! $form_is_resolved ) { |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | $form = $this->verify_form_meta( $form, $form_is_resolved ); |
| 553 | if ( false === $form ) { |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | $this->restrict_submitted_field_keys( $this->submission_grant['allowed_field_keys'] ); |
| 558 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 559 | |
| 560 | return $form; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Checks and merges `_acf_form_meta[]` grants for a submitted form. |
| 565 | * |
| 566 | * Every token must have a valid MAC and match the current render. A sibling |
| 567 | * grant adds field keys only when it has the same destination as the primary |
| 568 | * grant. |
| 569 | * |
| 570 | * @since SCF 6.9.4 |
| 571 | * |
| 572 | * @param array $form The primary form configuration loaded from `_acf_form`. |
| 573 | * @param bool $verify_form_configuration Whether the full primary form configuration is available. |
| 574 | * @return array|false The verified form, or false if its grant is missing or invalid. |
| 575 | */ |
| 576 | private function verify_form_meta( array $form, bool $verify_form_configuration = true ) { |
| 577 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified by the caller's normal or AJAX nonce path. |
| 578 | if ( empty( $_POST['_acf_form_meta'] ) || ! is_array( $_POST['_acf_form_meta'] ) ) { |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | if ( empty( $_POST['_acf_render_id'] ) || ! is_scalar( $_POST['_acf_render_id'] ) ) { |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | if ( ! isset( $_POST['_acf_form'] ) || ! is_scalar( $_POST['_acf_form'] ) ) { |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | if ( ! isset( $_POST['_acf_post_id'] ) || ! is_scalar( $_POST['_acf_post_id'] ) ) { |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | $expected_render_id = (string) wp_unslash( $_POST['_acf_render_id'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Compared only with authenticated metadata. |
| 595 | $primary_form_value = (string) wp_unslash( $_POST['_acf_form'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Must match the exact render-side bytes. |
| 596 | $submitted_post_id = (string) wp_unslash( $_POST['_acf_post_id'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Compared only with authenticated metadata. |
| 597 | $expected_anchor = hash( 'sha256', $primary_form_value ); |
| 598 | $primary_post_id = isset( $form['post_id'] ) ? (string) $form['post_id'] : ''; |
| 599 | |
| 600 | /** |
| 601 | * Filters how long a `_acf_form_meta[]` payload remains valid after the page that |
| 602 | * emitted it was rendered. |
| 603 | * |
| 604 | * @since SCF 6.8.8 |
| 605 | * |
| 606 | * @param int $ttl Allowed age of a meta payload, in seconds. |
| 607 | */ |
| 608 | $ttl = (int) apply_filters( 'acf/form/meta_ttl', DAY_IN_SECONDS ); |
| 609 | $now = time(); |
| 610 | if ( $ttl <= 0 ) { |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | $valid_metas = array(); |
| 615 | $primary = null; |
| 616 | |
| 617 | foreach ( $_POST['_acf_form_meta'] as $token ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Each token is authenticated before it is parsed. |
| 618 | if ( ! is_scalar( $token ) ) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | $decoded = $this->decode_form_meta_token( (string) wp_unslash( $token ) ); |
| 623 | if ( false === $decoded ) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | if ( get_current_blog_id() !== $decoded['blog_id'] |
| 628 | || ! hash_equals( $expected_render_id, $decoded['render_id'] ) |
| 629 | || $decoded['issued_at'] > $now + MINUTE_IN_SECONDS |
| 630 | || ( $now - $decoded['issued_at'] ) >= $ttl |
| 631 | ) { |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | $valid_metas[] = $decoded; |
| 636 | |
| 637 | if ( hash_equals( $expected_anchor, $decoded['form_anchor'] ) ) { |
| 638 | if ( ! hash_equals( $primary_post_id, $decoded['target_post_id'] ) |
| 639 | || ! hash_equals( $submitted_post_id, $decoded['target_post_id'] ) |
| 640 | ) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | if ( null !== $primary && ! $this->form_meta_destinations_match( $primary, $decoded ) ) { |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | $primary = $decoded; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if ( null === $primary ) { |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | if ( 'new_post' === $primary['target_post_id'] && $verify_form_configuration ) { |
| 657 | if ( ! isset( $form['new_post'] ) || ! is_array( $form['new_post'] ) ) { |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | $new_post_fingerprint = $this->get_new_post_fingerprint( $form['new_post'] ); |
| 662 | if ( false === $new_post_fingerprint |
| 663 | || ! hash_equals( $primary['new_post_fingerprint'], $new_post_fingerprint ) |
| 664 | ) { |
| 665 | return false; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | $allowed_field_keys = array(); |
| 670 | $post_title = false; |
| 671 | $post_content = false; |
| 672 | |
| 673 | foreach ( $valid_metas as $decoded ) { |
| 674 | if ( ! $this->form_meta_destinations_match( $primary, $decoded ) ) { |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | $allowed_field_keys = array_merge( $allowed_field_keys, $decoded['allowed_field_keys'] ); |
| 679 | $post_title = $post_title || $decoded['post_title']; |
| 680 | $post_content = $post_content || $decoded['post_content']; |
| 681 | } |
| 682 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 683 | |
| 684 | $form['post_title'] = $post_title; |
| 685 | $form['post_content'] = $post_content; |
| 686 | |
| 687 | $this->submission_grant = array( |
| 688 | 'allowed_field_keys' => $this->normalize_allowed_field_keys( $allowed_field_keys ), |
| 689 | ); |
| 690 | |
| 691 | return $form; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Verifies a form metadata token before decoding its JSON. |
| 696 | * |
| 697 | * @since SCF 6.9.4 |
| 698 | * |
| 699 | * @param string $token Encoded metadata token. |
| 700 | * @return array|false The authenticated metadata, or false for an invalid token. |
| 701 | */ |
| 702 | protected function decode_form_meta_token( string $token ) { |
| 703 | $parts = explode( '.', $token, 2 ); |
| 704 | if ( 2 !== count( $parts ) ) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | $payload = base64_decode( $parts[0], true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Base64 transports the JSON; the HMAC authenticates it. |
| 709 | if ( false === $payload ) { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | $expected_mac = hash_hmac( 'sha256', $payload, wp_salt( 'nonce' ) ); |
| 714 | if ( ! hash_equals( $expected_mac, $parts[1] ) ) { |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | $meta = json_decode( $payload, true ); |
| 719 | if ( ! is_array( $meta ) ) { |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | $expected_keys = array( |
| 724 | 'blog_id', |
| 725 | 'render_id', |
| 726 | 'issued_at', |
| 727 | 'form_anchor', |
| 728 | 'target_post_id', |
| 729 | ); |
| 730 | if ( isset( $meta['target_post_id'] ) && 'new_post' === $meta['target_post_id'] ) { |
| 731 | $expected_keys[] = 'new_post_fingerprint'; |
| 732 | } |
| 733 | $expected_keys = array_merge( |
| 734 | $expected_keys, |
| 735 | array( |
| 736 | 'allowed_field_keys', |
| 737 | 'post_title', |
| 738 | 'post_content', |
| 739 | ) |
| 740 | ); |
| 741 | |
| 742 | if ( array_keys( $meta ) !== $expected_keys |
| 743 | || ! is_int( $meta['blog_id'] ) |
| 744 | || ! is_string( $meta['render_id'] ) |
| 745 | || '' === $meta['render_id'] |
| 746 | || ! is_int( $meta['issued_at'] ) |
| 747 | || ! is_string( $meta['form_anchor'] ) |
| 748 | || 1 !== preg_match( '/^[a-f0-9]{64}$/', $meta['form_anchor'] ) |
| 749 | || ! is_string( $meta['target_post_id'] ) |
| 750 | || ! is_array( $meta['allowed_field_keys'] ) |
| 751 | || ! is_bool( $meta['post_title'] ) |
| 752 | || ! is_bool( $meta['post_content'] ) |
| 753 | || ( 'new_post' === $meta['target_post_id'] |
| 754 | && ( |
| 755 | ! is_string( $meta['new_post_fingerprint'] ) |
| 756 | || 1 !== preg_match( '/^[a-f0-9]{64}$/', $meta['new_post_fingerprint'] ) |
| 757 | ) |
| 758 | ) |
| 759 | || $meta['allowed_field_keys'] !== $this->normalize_allowed_field_keys( $meta['allowed_field_keys'] ) |
| 760 | ) { |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | return $meta; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Checks whether two grants point to the same save destination. |
| 769 | * |
| 770 | * @since SCF 6.9.4 |
| 771 | * |
| 772 | * @param array $left First grant. |
| 773 | * @param array $right Second grant. |
| 774 | * @return bool |
| 775 | */ |
| 776 | protected function form_meta_destinations_match( array $left, array $right ): bool { |
| 777 | if ( ! hash_equals( $left['target_post_id'], $right['target_post_id'] ) ) { |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | if ( 'new_post' !== $left['target_post_id'] ) { |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | return hash_equals( $left['new_post_fingerprint'], $right['new_post_fingerprint'] ); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Builds a stable, keyed fingerprint for new-post settings. |
| 790 | * |
| 791 | * Associative arrays are sorted recursively, while list order stays |
| 792 | * unchanged. The HMAC keeps low-entropy values such as post passwords from |
| 793 | * becoming targets for offline guessing. |
| 794 | * |
| 795 | * @since SCF 6.9.4 |
| 796 | * |
| 797 | * @param array $new_post New-post settings. |
| 798 | * @return string|false Fingerprint, or false when settings cannot be encoded. |
| 799 | */ |
| 800 | protected function get_new_post_fingerprint( array $new_post ) { |
| 801 | $serialized = wp_json_encode( $new_post ); |
| 802 | if ( ! is_string( $serialized ) ) { |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | $normalized = json_decode( $serialized, true ); |
| 807 | if ( ! is_array( $normalized ) ) { |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | $canonical = wp_json_encode( $this->canonicalize_new_post_value( $normalized ) ); |
| 812 | if ( ! is_string( $canonical ) ) { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | return hash_hmac( 'sha256', 'acf_form:new_post|' . $canonical, wp_salt( 'nonce' ) ); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Sorts associative arrays without changing list order. |
| 821 | * |
| 822 | * @since SCF 6.9.4 |
| 823 | * |
| 824 | * @param mixed $value Value to canonicalize. |
| 825 | * @return mixed The value with associative keys sorted. |
| 826 | */ |
| 827 | protected function canonicalize_new_post_value( $value ) { |
| 828 | if ( ! is_array( $value ) ) { |
| 829 | return $value; |
| 830 | } |
| 831 | |
| 832 | foreach ( $value as $key => $item ) { |
| 833 | $value[ $key ] = $this->canonicalize_new_post_value( $item ); |
| 834 | } |
| 835 | |
| 836 | $is_list = array() === $value || array_keys( $value ) === range( 0, count( $value ) - 1 ); |
| 837 | if ( ! $is_list ) { |
| 838 | ksort( $value, SORT_STRING ); |
| 839 | } |
| 840 | |
| 841 | return $value; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Cleans the list of allowed top-level field keys. |
| 846 | * |
| 847 | * @since SCF 6.9.4 |
| 848 | * |
| 849 | * @param mixed $keys Candidate field keys. |
| 850 | * @return array |
| 851 | */ |
| 852 | protected function normalize_allowed_field_keys( $keys ): array { |
| 853 | $keys = array_filter( (array) $keys, 'is_string' ); |
| 854 | $keys = array_filter( $keys ); |
| 855 | |
| 856 | return array_values( array_unique( $keys ) ); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Removes submitted ACF and upload roots that the grant did not allow. |
| 861 | * |
| 862 | * @since SCF 6.9.4 |
| 863 | * |
| 864 | * @param array $allowed_field_keys Allowed top-level field keys. |
| 865 | * @param bool $restrict_files Whether to filter uploaded file branches. |
| 866 | * @return void |
| 867 | */ |
| 868 | protected function restrict_submitted_field_keys( array $allowed_field_keys, bool $restrict_files = true ): void { |
| 869 | $allowed_field_keys = array_flip( $this->normalize_allowed_field_keys( $allowed_field_keys ) ); |
| 870 | |
| 871 | // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Keys are checked here. Values stay slashed and follow the existing sanitization steps. |
| 872 | if ( isset( $_POST['acf'] ) ) { |
| 873 | $_POST['acf'] = is_array( $_POST['acf'] ) |
| 874 | ? array_intersect_key( $_POST['acf'], $allowed_field_keys ) |
| 875 | : array(); |
| 876 | } |
| 877 | |
| 878 | if ( $restrict_files && isset( $_FILES['acf'] ) ) { |
| 879 | if ( ! is_array( $_FILES['acf'] ) ) { |
| 880 | $_FILES['acf'] = array(); |
| 881 | } else { |
| 882 | foreach ( $_FILES['acf'] as $attribute => $branches ) { |
| 883 | $_FILES['acf'][ $attribute ] = is_array( $branches ) |
| 884 | ? array_intersect_key( $branches, $allowed_field_keys ) |
| 885 | : array(); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Returns the fields selected by a form configuration. |
| 894 | * |
| 895 | * Rendering and saving share this lookup so they agree on the allowed roots. |
| 896 | * |
| 897 | * @since SCF 6.8.5 |
| 898 | * |
| 899 | * @param array $args The validated form configuration. |
| 900 | * @return array |
| 901 | */ |
| 902 | protected function get_form_fields( array $args ): array { |
| 903 | $fields = array(); |
| 904 | $field_groups = array(); |
| 905 | $post_id = $args['post_id']; |
| 906 | |
| 907 | // Prevent ACF from loading values for "new_post". |
| 908 | if ( 'new_post' === $post_id ) { |
| 909 | $post_id = false; |
| 910 | } |
| 911 | |
| 912 | // Register the default fields so acf_get_field() can resolve their keys. |
| 913 | foreach ( $this->get_default_fields() as $field ) { |
| 914 | acf_add_local_field( $field ); |
| 915 | } |
| 916 | |
| 917 | // Append post_title field. |
| 918 | if ( $args['post_title'] ) { |
| 919 | $fields[] = acf_get_field( '_post_title' ); |
| 920 | } |
| 921 | |
| 922 | // Append post_content field. |
| 923 | if ( $args['post_content'] ) { |
| 924 | $fields[] = acf_get_field( '_post_content' ); |
| 925 | } |
| 926 | |
| 927 | // Load specific fields. |
| 928 | if ( $args['fields'] ) { |
| 929 | foreach ( $args['fields'] as $selector ) { |
| 930 | if ( $post_id ) { |
| 931 | // Lookup fields using $strict = false for better compatibility with field names. |
| 932 | $fields[] = acf_maybe_get_field( $selector, $post_id, false ); |
| 933 | } else { |
| 934 | // There is no post for resolving meta references. Avoid |
| 935 | // acf_maybe_get_field(), which can call get_queried_object() |
| 936 | // before WordPress has built the main query. |
| 937 | $fields[] = acf_get_field( $selector ); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Load specific field groups. |
| 942 | } elseif ( $args['field_groups'] ) { |
| 943 | foreach ( $args['field_groups'] as $selector ) { |
| 944 | $field_groups[] = acf_get_field_group( $selector ); |
| 945 | } |
| 946 | |
| 947 | // Load fields for the given "new_post" args. |
| 948 | } elseif ( 'new_post' === $args['post_id'] ) { |
| 949 | $field_groups = acf_get_field_groups( $args['new_post'] ); |
| 950 | |
| 951 | // Load fields for the given "post_id" arg. |
| 952 | } else { |
| 953 | $field_groups = acf_get_field_groups( |
| 954 | array( |
| 955 | 'post_id' => $args['post_id'], |
| 956 | ) |
| 957 | ); |
| 958 | } |
| 959 | |
| 960 | // Load fields from the found field groups. |
| 961 | if ( $field_groups ) { |
| 962 | foreach ( $field_groups as $field_group ) { |
| 963 | $_fields = acf_get_fields( $field_group ); |
| 964 | if ( $_fields ) { |
| 965 | foreach ( $_fields as $_field ) { |
| 966 | $fields[] = $_field; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Add honeypot field. |
| 973 | if ( $args['honeypot'] ) { |
| 974 | $fields[] = acf_get_field( '_validate_email' ); |
| 975 | } |
| 976 | |
| 977 | return array_filter( $fields ); |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Returns the top-level ACF keys rendered by a form. |
| 982 | * |
| 983 | * Seamless clone subfields may use the parent input name, for example |
| 984 | * acf[clone_key][subkey]. In that case this method returns the parent key. |
| 985 | * |
| 986 | * @since SCF 6.8.5 |
| 987 | * |
| 988 | * @param array $form The validated form configuration. |
| 989 | * @param array $fields Optional pre-discovered fields for this form to avoid a |
| 990 | * redundant get_form_fields() call when the caller already has them. |
| 991 | * @return array |
| 992 | */ |
| 993 | public function get_allowed_field_keys( array $form, array $fields = array() ): array { |
| 994 | $keys = array(); |
| 995 | $fields = ! empty( $fields ) ? $fields : $this->get_form_fields( $form ); |
| 996 | |
| 997 | foreach ( $fields as $field ) { |
| 998 | $prefix = $field['prefix'] ?? 'acf'; |
| 999 | |
| 1000 | if ( 'acf' === $prefix ) { |
| 1001 | if ( ! empty( $field['key'] ) ) { |
| 1002 | $keys[] = $field['key']; |
| 1003 | } |
| 1004 | } elseif ( preg_match( '/^acf\[([^]]+)]$/', $prefix, $matches ) ) { |
| 1005 | $keys[] = $matches[1]; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | $keys = array_values( array_unique( array_filter( $keys ) ) ); |
| 1010 | |
| 1011 | /** |
| 1012 | * Filters the list of $_POST['acf'] keys a front-end form submission is allowed to save. |
| 1013 | * |
| 1014 | * Add keys here for fields injected while the form renders. The result is |
| 1015 | * signed into the form's grant, so keys added here are accepted on save. |
| 1016 | * This filter does not run again during submission. |
| 1017 | * |
| 1018 | * @since SCF 6.8.5 |
| 1019 | * |
| 1020 | * @param array $keys The allowed top-level $_POST['acf'] keys. |
| 1021 | * @param array $form The validated form configuration. |
| 1022 | */ |
| 1023 | $keys = apply_filters( 'acf/form/allowed_field_keys', $keys, $form ); |
| 1024 | |
| 1025 | // Clean the result again so invalid callback values cannot break |
| 1026 | // array_flip() in submit_form(). |
| 1027 | $keys = array_filter( (array) $keys, 'is_scalar' ); |
| 1028 | return array_values( array_unique( array_filter( array_map( 'strval', $keys ) ) ) ); |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * Renders a front-end ACF form. |
| 1033 | * |
| 1034 | * Accepts either an array of form configuration (validated via validate_form()) or the |
| 1035 | * string id of a form previously registered with acf_register_form(). Outputs the form |
| 1036 | * HTML directly. |
| 1037 | * |
| 1038 | * @since ACF 5.4.0 |
| 1039 | * |
| 1040 | * @param array|string $args Form configuration array, or the id of a registered form. |
| 1041 | * @return false|void False if a registered form id was passed and no matching form exists; |
| 1042 | * otherwise outputs the form and returns no value. |
| 1043 | */ |
| 1044 | public function render_form( $args = array() ) { |
| 1045 | |
| 1046 | // Vars. |
| 1047 | $is_registered = false; |
| 1048 | |
| 1049 | // Allow form settings to be directly provided. |
| 1050 | if ( is_array( $args ) ) { |
| 1051 | $args = $this->validate_form( $args ); |
| 1052 | |
| 1053 | // Otherwise, lookup registered form. |
| 1054 | } else { |
| 1055 | $is_registered = true; |
| 1056 | $args = $this->get_form( $args ); |
| 1057 | if ( ! $args ) { |
| 1058 | return false; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | // Extract vars. |
| 1063 | $post_id = $args['post_id']; |
| 1064 | |
| 1065 | // Prevent ACF from loading values for "new_post". |
| 1066 | if ( 'new_post' === $post_id ) { |
| 1067 | $post_id = false; |
| 1068 | } |
| 1069 | |
| 1070 | // Set uploader type. |
| 1071 | acf_update_setting( 'uploader', $args['uploader'] ); |
| 1072 | |
| 1073 | // Load the fields once for both rendering and the grant. |
| 1074 | $fields = $this->get_form_fields( $args ); |
| 1075 | |
| 1076 | // Pre-fill the title and content fields from the current post. |
| 1077 | foreach ( $fields as &$field ) { |
| 1078 | if ( ! isset( $field['key'] ) ) { |
| 1079 | continue; |
| 1080 | } |
| 1081 | if ( '_post_title' === $field['key'] ) { |
| 1082 | $field['value'] = $post_id ? get_post_field( 'post_title', $post_id ) : ''; |
| 1083 | } elseif ( '_post_content' === $field['key'] ) { |
| 1084 | $field['value'] = $post_id ? get_post_field( 'post_content', $post_id ) : ''; |
| 1085 | } |
| 1086 | } |
| 1087 | unset( $field ); |
| 1088 | |
| 1089 | // Display updated_message |
| 1090 | if ( ! empty( $_GET['updated'] ) && $args['updated_message'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used as a flag; data not used. |
| 1091 | printf( $args['html_updated_message'], $args['updated_message'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. |
| 1092 | } |
| 1093 | |
| 1094 | // display form |
| 1095 | if ( $args['form'] ) : ?> |
| 1096 | <form <?php echo acf_esc_attrs( $args['form_attributes'] ); ?>> |
| 1097 | <?php |
| 1098 | endif; |
| 1099 | |
| 1100 | // Render hidden form data. |
| 1101 | $render_id = $this->get_render_id(); |
| 1102 | $acf_form_value = $is_registered ? $args['id'] : acf_encrypt( wp_json_encode( $args ) ); |
| 1103 | acf_form_data( |
| 1104 | array( |
| 1105 | 'screen' => 'acf_form', |
| 1106 | 'post_id' => $args['post_id'], |
| 1107 | 'form' => $acf_form_value, |
| 1108 | 'render_id' => $render_id, |
| 1109 | ) |
| 1110 | ); |
| 1111 | |
| 1112 | /** |
| 1113 | * Add one grant for each acf_form() call. When several calls share an |
| 1114 | * outer <form>, the browser submits only the last `_acf_form` value. |
| 1115 | * The `_acf_form_meta[]` array keeps every form's allowed roots. |
| 1116 | */ |
| 1117 | $meta = array( |
| 1118 | 'blog_id' => get_current_blog_id(), |
| 1119 | 'render_id' => $render_id, |
| 1120 | 'issued_at' => time(), |
| 1121 | 'form_anchor' => hash( 'sha256', (string) $acf_form_value ), |
| 1122 | 'target_post_id' => (string) $args['post_id'], |
| 1123 | ); |
| 1124 | if ( 'new_post' === $args['post_id'] ) { |
| 1125 | $meta['new_post_fingerprint'] = $this->get_new_post_fingerprint( $args['new_post'] ); |
| 1126 | } |
| 1127 | $meta['allowed_field_keys'] = $this->get_allowed_field_keys( $args, $fields ); |
| 1128 | $meta['post_title'] = (bool) $args['post_title']; |
| 1129 | $meta['post_content'] = (bool) $args['post_content']; |
| 1130 | |
| 1131 | $meta_payload = ( |
| 1132 | 'new_post' === $args['post_id'] |
| 1133 | && false === $meta['new_post_fingerprint'] |
| 1134 | ) ? false : wp_json_encode( $meta ); |
| 1135 | $meta_token = ''; |
| 1136 | if ( is_string( $meta_payload ) ) { |
| 1137 | $meta_token = base64_encode( $meta_payload ) // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Base64 transports the JSON; the HMAC authenticates it. |
| 1138 | . '.' |
| 1139 | . hash_hmac( 'sha256', $meta_payload, wp_salt( 'nonce' ) ); |
| 1140 | } |
| 1141 | |
| 1142 | acf_hidden_input( |
| 1143 | array( |
| 1144 | 'name' => '_acf_form_meta[]', |
| 1145 | 'value' => $meta_token, |
| 1146 | ) |
| 1147 | ); |
| 1148 | |
| 1149 | ?> |
| 1150 | <div class="acf-fields acf-form-fields -<?php echo esc_attr( $args['label_placement'] ); ?>"> |
| 1151 | <?php echo $args['html_before_fields']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?> |
| 1152 | <?php acf_render_fields( $fields, $post_id, $args['field_el'], $args['instruction_placement'] ); ?> |
| 1153 | <?php echo $args['html_after_fields']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?> |
| 1154 | </div> |
| 1155 | <?php if ( $args['form'] ) : ?> |
| 1156 | <div class="acf-form-submit"> |
| 1157 | <?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. ?> |
| 1158 | <?php echo $args['html_submit_spinner']; ?><?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- designed to contain potentially unsafe HTML, set by developers. ?> |
| 1159 | </div> |
| 1160 | </form> |
| 1161 | <?php endif; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | // initialize |
| 1166 | acf()->form_front = new acf_form_front(); |
| 1167 | endif; // class_exists check |
| 1168 | |
| 1169 | |
| 1170 | /** |
| 1171 | * Functions |
| 1172 | * |
| 1173 | * alias of acf()->form->functions |
| 1174 | * |
| 1175 | * @type function |
| 1176 | * @date 11/06/2014 |
| 1177 | * @since ACF 5.0.0 |
| 1178 | * |
| 1179 | * @param n/a |
| 1180 | * @return n/a |
| 1181 | */ |
| 1182 | function acf_form_head() { |
| 1183 | |
| 1184 | acf()->form_front->enqueue_form(); |
| 1185 | } |
| 1186 | |
| 1187 | function acf_form( $args = array() ) { |
| 1188 | |
| 1189 | acf()->form_front->render_form( $args ); |
| 1190 | } |
| 1191 | |
| 1192 | function acf_get_form( $id = '' ) { |
| 1193 | |
| 1194 | return acf()->form_front->get_form( $id ); |
| 1195 | } |
| 1196 | |
| 1197 | function acf_get_forms() { |
| 1198 | return acf()->form_front->get_forms(); |
| 1199 | } |
| 1200 | |
| 1201 | function acf_register_form( $args ) { |
| 1202 | |
| 1203 | acf()->form_front->add_form( $args ); |
| 1204 | } |
| 1205 | |
| 1206 | ?> |
| 1207 |