ACF_Taxonomy::ajax_validate_valuespublicACF 6.1

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_Taxonomy{}

Хуки из метода

Возвращает

true|false. validity status

Использование

$ACF_Taxonomy = new ACF_Taxonomy();
$ACF_Taxonomy->ajax_validate_values();

Список изменений

С версии 6.1 Введена.

Код ACF_Taxonomy::ajax_validate_values() ACF 6.4.2

public function ajax_validate_values() {
	if ( empty( $_POST['acf_taxonomy'] ) || empty( $_POST['acf_taxonomy']['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
		return false;
	}

	$taxonomy_key = acf_sanitize_request_args( wp_unslash( $_POST['acf_taxonomy']['taxonomy'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
	$taxonomy_key = is_string( $taxonomy_key ) ? $taxonomy_key : '';
	$valid        = true;

	if ( strlen( $taxonomy_key ) > 32 ) {
		$valid = false;
		acf_add_internal_post_type_validation_error( 'taxonomy', __( 'The taxonomy key must be under 32 characters.', 'acf' ) );
	}

	if ( preg_match( '/^[a-z0-9_-]*$/', $taxonomy_key ) !== 1 ) {
		$valid = false;
		acf_add_internal_post_type_validation_error( 'taxonomy', __( 'The taxonomy key must only contain lower case alphanumeric characters, underscores or dashes.', 'acf' ) );
	}

	if ( in_array( $taxonomy_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( 'taxonomy', $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 ( $taxonomy_key ) {
				return $item['taxonomy'] === $taxonomy_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( 'taxonomy', __( 'This taxonomy key is already in use by another taxonomy in ACF and cannot be used.', 'acf' ) );
			// If we're not already in use with another ACF taxonomy, check if we're registered, but not by ACF.
		} elseif ( empty( $matches ) && taxonomy_exists( $taxonomy_key ) ) {
			$valid = false;
			acf_add_internal_post_type_validation_error( 'taxonomy', __( 'This taxonomy key is already in use by another taxonomy registered outside of ACF and cannot be used.', 'acf' ) );
		}
	}

	$valid = apply_filters( "acf/{$this->hook_name}/ajax_validate_values", $valid, $_POST['acf_taxonomy'] ); // phpcs:ignore WordPress.Security -- Raw input send to hook for validation.

	return $valid;
}