WC_API_Products::validate_attribute_data() protected WC 2.4.0
Validate attribute data.
{} It's a method of the class: WC_API_Products{}
No Hooks.
Return
true/false.
Usage
// protected - for code of main (parent) or child class $result = $this->validate_attribute_data( $name, $slug, $type, $order_by, $new_data );
- $name(string) (required)
- -
- $slug(string) (required)
- -
- $type(string) (required)
- -
- $order_by(string) (required)
- -
- $new_data(true/false)
- -
Changelog
Since 2.4.0 | Introduced. |
Code of WC_API_Products::validate_attribute_data() WC API Products::validate attribute data WC 5.0.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;
}