WC_API_Products::validate_attribute_data()protectedWC 2.5.0

Validate attribute data.

Метод класса: WC_API_Products{}

Хуков нет.

Возвращает

true|false.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_attribute_data( $name, $slug, $type, $order_by, $new_data );
$name(строка) (обязательный)
-
$slug(строка) (обязательный)
-
$type(строка) (обязательный)
-
$order_by(строка) (обязательный)
-
$new_data(true|false)
-
По умолчанию: true

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

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

Код WC_API_Products::validate_attribute_data() WC 8.7.0

protected function validate_attribute_data( $name, $slug, $type, $order_by, $new_data = true ) {
	if ( empty( $name ) ) {
		throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 );
	}

	if ( strlen( $slug ) > 28 ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), 400 );
	} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), 400 );
	} elseif ( $new_data && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), 400 );
	}

	// Validate the attribute type
	if ( ! in_array( wc_clean( $type ), array_keys( wc_get_attribute_types() ) ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_type', sprintf( __( 'Invalid product attribute type - the product attribute type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_attribute_types() ) ) ), 400 );
	}

	// Validate the attribute order by
	if ( ! in_array( wc_clean( $order_by ), array( 'menu_order', 'name', 'name_num', 'id' ) ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_order_by', sprintf( __( 'Invalid product attribute order_by type - the product attribute order_by type must be any of these: %s', 'woocommerce' ), implode( ', ', array( 'menu_order', 'name', 'name_num', 'id' ) ) ), 400 );
	}

	return true;
}