acf_filter_block_meta_values()
Restricts a post meta block's values to the fields of the field groups located on the block.
Block meta values come from post content, which anyone who can edit the post controls, rather than from a rendered form, so they are matched by location only. Values arrive keyed by field key or, as loaded by acf_add_block_meta_values(), by field name with _{name} key references. Names are mapped to the block's own fields so they can't resolve to a field with the same name elsewhere. Values for seamless clone sub fields are nested under the clone's key, as the clone field saves them (as in $_POST['acf']).
Хуков нет.
Возвращает
array. The allowed values, keyed by top-level field key.
Использование
acf_filter_block_meta_values( $block_name, $values, $post_id );
- $block_name(строка) (обязательный)
- The block type name.
- $values(разное) (обязательный)
- The block's values.
- $post_id(int|строка) (обязательный)
- The post being saved.
Список изменений
| С версии 6.8.11 | Введена. |
Код acf_filter_block_meta_values() acf filter block meta values ACF 6.8.11
function acf_filter_block_meta_values( $block_name, $values, $post_id ) {
if ( ! is_array( $values ) ) {
return array();
}
$fields_by_key = array();
$fields_by_name = array();
foreach ( acf_get_block_fields( array( 'name' => $block_name ) ) as $field ) {
$fields_by_key[ $field['key'] ] = $field;
if ( ! empty( $field['name'] ) ) {
$fields_by_name[ $field['name'] ] = $field;
}
}
$keyed_values = array();
foreach ( $values as $key => $value ) {
$key = (string) $key;
$field = $fields_by_key[ $key ] ?? $fields_by_name[ $key ] ?? null;
// A `_{name}` key reference for a named value, which is now keyed by field key.
$is_reference = '_' === substr( $key, 0, 1 ) && isset( $fields_by_name[ substr( $key, 1 ) ] );
if ( $field ) {
_acf_set_block_meta_value( $keyed_values, $field, $value );
} elseif ( ! $is_reference && ! isset( $keyed_values[ $key ] ) ) {
// Other keys are passed on, to be dropped and logged.
$keyed_values[ $key ] = $value;
}
}
return acf_filter_save_values(
$keyed_values,
array(
'context' => 'block_meta',
'post_id' => $post_id,
// There is no rendered form to sign (so no render IDs), so location matching is the only check.
'screen' => array( 'block' => $block_name ),
)
);
}