WC_REST_Product_Attributes_Controller::generate_unique_slug()privateWC 1.0

Generates a unique slug for a given attribute name. We do this so that we can create more than one attribute with the same name.

Method of the class: WC_REST_Product_Attributes_Controller{}

No Hooks.

Return

String. The auto-generated slug

Usage

// private - for code of main (parent) class only
$result = $this->generate_unique_slug( $attribute_name );
$attribute_name(string) (required)
The attribute name to generate a slug for.

WC_REST_Product_Attributes_Controller::generate_unique_slug() code WC 9.5.1

private function generate_unique_slug( $attribute_name ) {
	global $wpdb;

	$root_slug = wc_sanitize_taxonomy_name( $attribute_name );

	$results = $wpdb->get_results(
		$wpdb->prepare( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name LIKE %s ORDER BY attribute_id DESC LIMIT 1", $root_slug . '%' )
	);

	// The slug is already unique!
	if ( empty( $results ) ) {
		return $root_slug;
	}

	$last_created_slug = $results[0]->attribute_name;
	$suffix = intval( substr( $last_created_slug, strrpos( $last_created_slug, '-' ) + 1 ) );

	return $root_slug . '-' . ( $suffix + 1 );
}