ACF_Form_User::get_allowed_field_keysprivateACF 6.8.7

Returns the top-level ACF field keys that are allowed to be saved for the given user in the current save context. On user_register the new user has just been created via either the public register form or the admin add-user form; on profile_update the user was rendered via the edit form (self-edit or admin-edit-other).

Method of the class: ACF_Form_User{}

Hooks from the method

Returns

array.

Usage

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

Changelog

Since 6.8.7 Introduced.

ACF_Form_User::get_allowed_field_keys() code ACF 6.8.8

private function get_allowed_field_keys( $user_id ) {
	if ( current_action() === 'user_register' ) {
		$queries = array(
			array(
				'user_id'   => 'new',
				'user_form' => 'register',
			),
			array(
				'user_id'   => 'new',
				'user_form' => 'add',
			),
		);
	} else {
		$queries = array(
			array(
				'user_id'   => $user_id,
				'user_form' => 'edit',
			),
		);
	}

	$keys = array();
	foreach ( $queries as $query ) {
		foreach ( acf_get_field_groups( $query ) 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 user save is allowed to persist.
	 *
	 * Use this to permit additional field keys when a developer dynamically
	 * injects fields into the register/add/edit user form via JavaScript
	 * that aren't part of a field group whose location rules target this user.
	 *
	 * @since 6.8.7
	 *
	 * @param array $keys    The allowed top-level $_POST['acf'] keys.
	 * @param int   $user_id The user being saved.
	 */
	$keys = apply_filters( 'acf/form/user/allowed_field_keys', $keys, $user_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 ) ) ) );
}