plugin updates
This commit is contained in:
@@ -67,6 +67,7 @@ function acf_handle_json_block_registration( $settings, $metadata ) {
|
||||
'acf_block_version' => 2,
|
||||
'api_version' => 2,
|
||||
'validate' => true,
|
||||
'validate_on_load' => true,
|
||||
'use_post_meta' => false,
|
||||
)
|
||||
);
|
||||
@@ -110,6 +111,7 @@ function acf_handle_json_block_registration( $settings, $metadata ) {
|
||||
'blockVersion' => 'acf_block_version',
|
||||
'postTypes' => 'post_types',
|
||||
'validate' => 'validate',
|
||||
'validateOnLoad' => 'validate_on_load',
|
||||
'usePostMeta' => 'use_post_meta',
|
||||
);
|
||||
$textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : 'acf';
|
||||
@@ -590,7 +592,7 @@ function acf_rendered_block( $attributes, $content = '', $is_preview = false, $p
|
||||
acf_setup_meta( $block['data'], $block['id'], true );
|
||||
|
||||
if ( ! empty( $block['validate'] ) ) {
|
||||
$validation = acf_get_block_validation_state( $block );
|
||||
$validation = acf_get_block_validation_state( $block, false, false, true );
|
||||
}
|
||||
|
||||
$fields = acf_get_block_fields( $block );
|
||||
@@ -616,7 +618,7 @@ function acf_rendered_block( $attributes, $content = '', $is_preview = false, $p
|
||||
$block = acf_add_block_meta_values( $block, $post_id );
|
||||
acf_setup_meta( $block['data'], $block['id'], true );
|
||||
if ( ! empty( $block['validate'] ) ) {
|
||||
$validation = acf_get_block_validation_state( $block );
|
||||
$validation = acf_get_block_validation_state( $block, false, false, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1216,26 +1218,35 @@ function acf_serialize_block_attributes( $block_attributes ) {
|
||||
/**
|
||||
* Handle validating a block's fields and return the validity, and any errors.
|
||||
*
|
||||
* This function uses the values loaded into Local Meta, which means they have to be
|
||||
* converted back to the data format because they can be validated.
|
||||
* This function can use values loaded into Local Meta, which means they have to be
|
||||
* converted back to the data format before they can be validated.
|
||||
*
|
||||
* @since 6.3
|
||||
*
|
||||
* @param array $block An array of the block's data attribute.
|
||||
* @param boolean $using_defaults True if the block is currently being generated with default values. Default false.
|
||||
* @param boolean $use_post_data True if we should validate the POSTed data rather than local meta values. Default false.
|
||||
* @param boolean $on_load True if we're validating as part of a render. This is essentially the same as a first load. Default false.
|
||||
* @return array An array containing a valid boolean, and an errors array.
|
||||
*/
|
||||
function acf_get_block_validation_state( $block, $using_defaults = false, $use_post_data = false ) {
|
||||
function acf_get_block_validation_state( $block, $using_defaults = false, $use_post_data = false, $on_load = false ) {
|
||||
$block_id = $block['id'];
|
||||
|
||||
if ( $use_post_data ) {
|
||||
if ( $on_load && empty( $block['validate_on_load'] ) ) {
|
||||
// If we're in a page load render, and validate on load is false, skip validation.
|
||||
$errors = false;
|
||||
} elseif ( $use_post_data ) {
|
||||
$errors = acf_validate_block_from_post_data( $block );
|
||||
} elseif ( $using_defaults || empty( $block['data'] ) ) {
|
||||
// If data is empty or it's first preview, load the default fields for this block so we can get a required validation state from the current field set.
|
||||
$errors = acf_validate_block_from_local_meta( $block_id, acf_get_block_fields( $block ) );
|
||||
// Treat as "on load" if it's the first render of a block.
|
||||
if ( empty( $block['validate_on_load'] ) ) {
|
||||
$errors = false;
|
||||
} else {
|
||||
$errors = acf_validate_block_from_local_meta( $block_id, acf_get_block_fields( $block ), true );
|
||||
}
|
||||
} else {
|
||||
$errors = acf_validate_block_from_local_meta( $block_id, get_field_objects( $block_id, false ) );
|
||||
$errors = acf_validate_block_from_local_meta( $block_id, get_field_objects( $block_id, false ), false );
|
||||
}
|
||||
|
||||
return array(
|
||||
@@ -1267,35 +1278,46 @@ function acf_validate_block_from_post_data( $block ) {
|
||||
*
|
||||
* @since 6.3.1
|
||||
*
|
||||
* @param string $block_id The block ID
|
||||
* @param array $field_objects The field objects in local meta to be validated.
|
||||
* @param string $block_id The block ID.
|
||||
* @param array $field_objects The field objects in local meta to be validated.
|
||||
* @param boolean $using_defaults True if this is the first load of the block, when special validation may apply.
|
||||
* @return array|boolean An array containing the validation errors, or false if there are no errors.
|
||||
*/
|
||||
function acf_validate_block_from_local_meta( $block_id, $field_objects ) {
|
||||
function acf_validate_block_from_local_meta( $block_id, $field_objects, $using_defaults = false ) {
|
||||
if ( empty( $field_objects ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$skip_conditional_fields = false;
|
||||
$using_loaded_meta = false;
|
||||
if ( acf_get_data( $block_id . '_loaded_meta_values' ) ) {
|
||||
$skip_conditional_fields = true;
|
||||
$using_loaded_meta = true;
|
||||
}
|
||||
|
||||
acf_reset_validation_errors();
|
||||
foreach ( $field_objects as $field ) {
|
||||
/**
|
||||
* Skips validation of conditional fields in post meta blocks when
|
||||
* preloading or during the first AJAX render if preloading is disabled.
|
||||
*/
|
||||
if ( $skip_conditional_fields && ! empty( $field['conditional_logic'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip for nested fields - these don't work correctly on initial load of a saved block.
|
||||
if ( ! empty( $field['sub_fields'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we're using default values, or loaded meta we may have values which are about to be populated at field render, so shouldn't raise errors here.
|
||||
if ( $using_defaults || $using_loaded_meta ) {
|
||||
// Fields with conditional logic applied shouldn't be validated during first load as conditionals aren't respected.
|
||||
if ( ! empty( $field['conditional_logic'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we've got a empty value with a default value set and it's first load, don't produce a validation error as it will be substituted on render.
|
||||
if ( $field['required'] && empty( $field['value'] ) && ! empty( $field['default_value'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we're loading a few radio or select-like fields, without allow null, HTML will automatically select the first value on render, so skip here.
|
||||
if ( $field['required'] && in_array( $field['type'], array( 'radio', 'button_group', 'select' ), true ) && ! $field['allow_null'] ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$key = $field['key'];
|
||||
$value = $field['value'];
|
||||
acf_validate_value( $value, $field, "acf-{$block_id}[{$key}]" );
|
||||
|
||||
Reference in New Issue
Block a user