WC_REST_Product_Attributes_V1_Controller::validate_attribute_slugprotectedWC 1.0

Deprecated since 3.2.0. It is no longer supported and may be removed in future releases. It is recommended to replace this function with the same one.

Validate attribute slug.

Method of the class: WC_REST_Product_Attributes_V1_Controller{}

No Hooks.

Returns

true|false|WP_Error.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_attribute_slug( $slug, $new_data );
$slug(string) (required)
The slug to validate.
$new_data(true|false)
If we are creating new data.
Default: true

Changelog

Deprecated since 3.2.0

WC_REST_Product_Attributes_V1_Controller::validate_attribute_slug() code WC 11.0.1

protected function validate_attribute_slug( $slug, $new_data = true ) {
	// Validate slug length against the byte budget left by WordPress's 32-byte taxonomy
	// name limit once the 'pa_' prefix is accounted for (see wc_get_attribute_slug_max_byte_length()).
	$slug_byte_length = strlen( $slug );
	$max_byte_length  = wc_get_attribute_slug_max_byte_length();
	if ( $slug_byte_length > $max_byte_length ) {
		return new WP_Error(
			'woocommerce_rest_invalid_product_attribute_slug_too_long',
			/* translators: %s: slug being validated */
			sprintf( __( 'Slug "%s" is too long. Please use a shorter slug.', 'woocommerce' ), $slug ),
			array(
				'status'                 => 400,
				'slug_byte_length'       => $slug_byte_length,
				'slug_byte_length_limit' => $max_byte_length,
			)
		);
	} elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
		/* translators: %s: slug being validated */
		return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
	} elseif ( $new_data && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) {
		/* translators: %s: slug being validated */
		return new WP_Error( 'woocommerce_rest_invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
	}

	return true;
}