acf_form_comment::get_allowed_field_keysprivateACF 6.8.7

Returns the top-level ACF field keys that are allowed to be saved for the given comment. Built from the field groups whose comment location rules match the comment's post type.

Method of the class: acf_form_comment{}

Hooks from the method

Returns

array.

Usage

// private - for code of main (parent) class only
$result = $this->get_allowed_field_keys( $comment_id );
$comment_id(int) (required)
The ID of comment being saved.

Changelog

Since 6.8.7 Introduced.

acf_form_comment::get_allowed_field_keys() code ACF 6.8.8

private function get_allowed_field_keys( $comment_id ) {
	$keys    = array();
	$comment = get_comment( $comment_id );

	if ( $comment ) {
		$field_groups = acf_get_field_groups(
			array(
				'comment' => get_post_type( $comment->comment_post_ID ),
			)
		);

		foreach ( $field_groups as $field_group ) {
			foreach ( acf_get_fields( $field_group ) 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];
				}
			}
		}
	}

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

	/**
	 * Filters the list of $_POST['acf'] keys a comment submission is allowed to save.
	 *
	 * Use this to permit additional field keys when a developer dynamically injects
	 * fields into the comment form via JavaScript that aren't part of a field group
	 * whose location rules target this comment.
	 *
	 * @since 6.8.7
	 *
	 * @param array $keys       The allowed top-level $_POST['acf'] keys.
	 * @param int   $comment_id The comment being saved.
	 */
	$keys = apply_filters( 'acf/form/comment/allowed_field_keys', $keys, $comment_id );

	// Re-normalize after the filter so a misbehaving callback can't break array_flip().
	$keys = array_filter( (array) $keys, 'is_scalar' );
	return array_values( array_unique( array_filter( array_map( 'strval', $keys ) ) ) );
}