ACF_Post_Type::ajax_validate_values
Validates post type values before allowing save from the global $_POST object. Errors are added to the form using acf_add_internal_post_type_validation_error().
Метод класса: ACF_Post_Type{}
Хуки из метода
Возвращает
true|false. validity status
Использование
$ACF_Post_Type = new ACF_Post_Type(); $ACF_Post_Type->ajax_validate_values();
Список изменений
| С версии 6.1 | Введена. |
Код ACF_Post_Type::ajax_validate_values() ACF Post Type::ajax validate values ACF 6.4.2
public function ajax_validate_values() {
if ( empty( $_POST['acf_post_type']['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
return false;
}
$post_type_key = acf_sanitize_request_args( wp_unslash( $_POST['acf_post_type']['post_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
$post_type_key = is_string( $post_type_key ) ? $post_type_key : '';
$valid = true;
if ( strlen( $post_type_key ) > 20 ) {
$valid = false;
acf_add_internal_post_type_validation_error( 'post_type', __( 'The post type key must be under 20 characters.', 'acf' ) );
}
if ( preg_match( '/^[a-z0-9_-]*$/', $post_type_key ) !== 1 ) {
$valid = false;
acf_add_internal_post_type_validation_error( 'post_type', __( 'The post type key must only contain lower case alphanumeric characters, underscores or dashes.', 'acf' ) );
}
if ( in_array( $post_type_key, acf_get_wp_reserved_terms(), true ) ) {
$valid = false;
/* translators: %s a link to WordPress.org's Reserved Terms page */
$message = sprintf( __( 'This field must not be a WordPress <a href="%s" target="_blank">reserved term</a>.', 'acf' ), 'https://codex.wordpress.org/Reserved_Terms' );
acf_add_internal_post_type_validation_error( 'post_type', $message );
} else {
// Check if this post key exists in the ACF store for registered post types, excluding those which failed registration.
$store = acf_get_store( $this->store );
$post_id = (int) acf_maybe_get_POST( 'post_id', 0 );
$matches = array_filter(
$store->get_data(),
function ( $item ) use ( $post_type_key ) {
return $item['post_type'] === $post_type_key && empty( $item['not_registered'] );
}
);
$duplicates = array_filter(
$matches,
function ( $item ) use ( $post_id ) {
return $item['ID'] !== $post_id;
}
);
if ( $duplicates ) {
$valid = false;
acf_add_internal_post_type_validation_error( 'post_type', __( 'This post type key is already in use by another post type in ACF and cannot be used.', 'acf' ) );
} else {
// If we're not already in use with another ACF post type, check if we're registered, but not by ACF.
if ( empty( $matches ) && post_type_exists( $post_type_key ) ) {
$valid = false;
acf_add_internal_post_type_validation_error( 'post_type', __( 'This post type key is already in use by another post type registered outside of ACF and cannot be used.', 'acf' ) );
}
}
}
$valid = apply_filters( "acf/{$this->hook_name}/ajax_validate_values", $valid, $_POST['acf_post_type'] ); // phpcs:ignore WordPress.Security -- Raw input send to hook for validation.
return $valid;
}