acf_form_front::get_allowed_field_keyspublicACF 6.8.2

Returns the top-level $_POST['acf'] keys a given form configuration will accept on save.

Derived from the same field discovery render_form() uses, so the set of save-acceptable keys matches the set of keys the form actually rendered. For seamless clone fields whose subfield input names nest under the parent clone's key (e.g. acf[clone_key][subkey]), the parent's top-level key is what gets returned.

Метод класса: acf_form_front{}

Хуки из метода

Возвращает

array.

Использование

$acf_form_front = new acf_form_front();
$acf_form_front->get_allowed_field_keys( $form, $fields ): array;
$form(массив) (обязательный)
The validated form configuration.
$fields(массив)
Optional pre-discovered fields for this form to avoid a redundant get_form_fields() call when the caller already has them.
По умолчанию: array()

Список изменений

С версии 6.8.2 Введена.

Код acf_form_front::get_allowed_field_keys() ACF 6.8.8

public function get_allowed_field_keys( array $form, array $fields = array() ): array {
	$keys   = array();
	$fields = $fields ?: $this->get_form_fields( $form );

	foreach ( $fields as $field ) {
		$prefix = $field['prefix'] ?? 'acf';

		if ( $prefix === 'acf' ) {
			if ( ! empty( $field['key'] ) ) {
				$keys[] = $field['key'];
			}
		} elseif ( preg_match( '/^acf\[([^]]+)]$/', $prefix, $matches ) ) {
			$keys[] = $matches[1];
		}
	}

	// Include keys folded in from sibling acf_form() calls on the same page.
	if ( ! empty( $form['_additional_allowed_field_keys'] ) && is_array( $form['_additional_allowed_field_keys'] ) ) {
		$keys = array_merge( $keys, $form['_additional_allowed_field_keys'] );
	}

	$keys = array_values( array_unique( array_filter( $keys ) ) );

	/**
	 * Filters the list of $_POST['acf'] keys a front-end form submission is allowed to save.
	 *
	 * Use this to permit additional field keys when a developer dynamically injects fields
	 * into a form via JavaScript that aren't part of the form's declared field configuration.
	 *
	 * @since 6.8.2
	 *
	 * @param array $keys The allowed top-level $_POST['acf'] keys.
	 * @param array $form The validated form configuration.
	 */
	$keys = apply_filters( 'acf/form/allowed_field_keys', $keys, $form );

	// Re-normalize after the filter so a misbehaving callback can't break array_flip()
	// downstream in submit_form() with non-scalar or empty values.
	$keys = array_filter( (array) $keys, 'is_scalar' );
	return array_values( array_unique( array_filter( array_map( 'strval', $keys ) ) ) );
}