Commit 5716f6e72cd for woocommerce

commit 5716f6e72cdf13b2d6536646e44318317f71fcd6
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 12:15:39 2026 +0100

    Normalize global attribute names consistently on save and read (#71232)

    (cherry picked from commit 0daaeb741b53997093482f5b7dedbbf8fb2528a7)

    Co-authored-by: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>

diff --git a/plugins/woocommerce/includes/class-wc-install.php b/plugins/woocommerce/includes/class-wc-install.php
index 83cbe086fae..8d03f60c77c 100644
--- a/plugins/woocommerce/includes/class-wc-install.php
+++ b/plugins/woocommerce/includes/class-wc-install.php
@@ -1836,6 +1836,7 @@ class WC_Install {

 		// Clear table caches.
 		delete_transient( 'wc_attribute_taxonomies' );
+		WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
 		TaxesDataStore::flush_lookup_columns_cache();
 		OrdersStatsDataStore::flush_payment_method_column_cache();

diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller.php
index b3797ddd072..3fd09b880ac 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller.php
@@ -339,7 +339,7 @@ class WC_REST_Product_Attributes_V1_Controller extends WC_REST_Controller {
 			$id,
 			array(
 				'name'         => $request['name'],
-				'slug'         => wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ),
+				'slug'         => isset( $request['slug'] ) ? wc_sanitize_taxonomy_name( stripslashes( $request['slug'] ) ) : null,
 				'type'         => $request['type'],
 				'order_by'     => $request['order_by'],
 				'has_archives' => $request['has_archives'],
@@ -592,6 +592,8 @@ class WC_REST_Product_Attributes_V1_Controller extends WC_REST_Controller {
 			return new WP_Error( 'woocommerce_rest_attribute_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
 		}

+		$attribute->attribute_label = sanitize_text_field( (string) $attribute->attribute_label );
+
 		return $attribute;
 	}

diff --git a/plugins/woocommerce/includes/wc-attribute-functions.php b/plugins/woocommerce/includes/wc-attribute-functions.php
index 643b6a1717a..e1a9195e421 100644
--- a/plugins/woocommerce/includes/wc-attribute-functions.php
+++ b/plugins/woocommerce/includes/wc-attribute-functions.php
@@ -71,6 +71,11 @@ function wc_get_attribute_taxonomies() {
 		set_transient( 'wc_attribute_taxonomies', $raw_attribute_taxonomies );
 	}

+	// Stored labels are plain text for every reader, as the admin form stores them.
+	foreach ( (array) $raw_attribute_taxonomies as $raw_attribute_taxonomy ) {
+		$raw_attribute_taxonomy->attribute_label = sanitize_text_field( (string) $raw_attribute_taxonomy->attribute_label );
+	}
+
 	/**
 	 * Filter attribute taxonomies.
 	 *
@@ -527,7 +532,7 @@ function wc_get_attribute( $id ) {
  *     Array of attribute parameters.
  *
  *     @type int    $id           Unique identifier, used to update an attribute.
- *     @type string $name         Attribute name. Always required.
+ *     @type string $name         Attribute name. Always required. Stored as plain text (sanitize_text_field()).
  *     @type string $slug         Attribute alphanumeric identifier.
  *     @type string $type         Type of attribute.
  *                                Core by default accepts: 'select' and
@@ -548,13 +553,15 @@ function wc_create_attribute( $args ) {
 	$format = array( '%s', '%s', '%s', '%s', '%d' );

 	// Name is required.
+	$raw_name     = isset( $args['name'] ) && is_scalar( $args['name'] ) ? (string) $args['name'] : '';
+	$args['name'] = sanitize_text_field( $raw_name );
 	if ( empty( $args['name'] ) ) {
 		return new WP_Error( 'missing_attribute_name', __( 'Please, provide an attribute name.', 'woocommerce' ), array( 'status' => 400 ) );
 	}

-	// Set the attribute slug.
+	// Set the attribute slug. Derive it from the name as given so the default slug does not change with the label cleanup.
 	if ( empty( $args['slug'] ) ) {
-		$slug = wc_sanitize_taxonomy_name( $args['name'] );
+		$slug = wc_sanitize_taxonomy_name( $raw_name );
 	} else {
 		$slug = (string) preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( $args['slug'] ) );
 	}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller-tests.php
index 5cfce90080e..9f9f2e631d5 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-product-attributes-v1-controller-tests.php
@@ -87,5 +87,73 @@ class WC_REST_Product_Attributes_V1_Controller_Tests extends WC_REST_Unit_Test_C

 		$this->assertEquals( 201, $response->get_status() );
 	}
-}

+	/**
+	 * @testdox Reading a single attribute whose stored label contains markup returns a plain-text name.
+	 */
+	public function test_get_item_normalizes_stored_label_with_markup() {
+		wp_set_current_user( $this->admin_id );
+
+		$attribute_id = $this->insert_raw_attribute_row( 'Colour<b>bold</b>', 'restrawcolour' );
+
+		try {
+			$request  = new WP_REST_Request( 'GET', '/wc/v1/products/attributes/' . $attribute_id );
+			$response = $this->server->dispatch( $request );
+
+			$this->assertEquals( 200, $response->get_status() );
+			$this->assertSame( 'Colourbold', $response->get_data()['name'] );
+		} finally {
+			wc_delete_attribute( $attribute_id );
+		}
+	}
+
+	/**
+	 * @testdox Updating an attribute without a slug keeps the stored slug even when the normalized name would slugify differently.
+	 */
+	public function test_update_item_without_slug_keeps_existing_slug() {
+		global $wpdb;
+
+		wp_set_current_user( $this->admin_id );
+
+		$attribute_id = $this->insert_raw_attribute_row( 'Width <5cm', 'width' );
+
+		try {
+			$request = new WP_REST_Request( 'PUT', '/wc/v1/products/attributes/' . $attribute_id );
+			$request->set_body_params( array( 'order_by' => 'name' ) );
+			$response = $this->server->dispatch( $request );
+
+			$this->assertEquals( 200, $response->get_status() );
+			$this->assertSame( 'pa_width', $response->get_data()['slug'] );
+			$this->assertSame( 'name', $response->get_data()['order_by'] );
+			$this->assertSame( 'width', $wpdb->get_var( $wpdb->prepare( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id ) ) );
+		} finally {
+			wc_delete_attribute( $attribute_id );
+		}
+	}
+
+	/**
+	 * Insert an attribute row directly, bypassing wc_create_attribute(), and reset the attribute caches.
+	 *
+	 * @param string $label Raw label to store.
+	 * @param string $slug  Attribute slug (without the `pa_` prefix).
+	 * @return int Attribute ID.
+	 */
+	private function insert_raw_attribute_row( string $label, string $slug ): int {
+		global $wpdb;
+
+		$wpdb->insert(
+			$wpdb->prefix . 'woocommerce_attribute_taxonomies',
+			array(
+				'attribute_label'   => $label,
+				'attribute_name'    => $slug,
+				'attribute_type'    => 'select',
+				'attribute_orderby' => 'menu_order',
+				'attribute_public'  => 0,
+			)
+		);
+		delete_transient( 'wc_attribute_taxonomies' );
+		WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
+
+		return (int) $wpdb->insert_id;
+	}
+}
diff --git a/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
index 931a8b3b6ae..3137cf1cdea 100644
--- a/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
@@ -207,6 +207,76 @@ class WC_Attribute_Functions_Test extends \WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox wc_create_attribute() strips markup from the attribute name before storing it.
+	 */
+	public function test_wc_create_attribute_strips_markup_from_name(): void {
+		$id = wc_create_attribute(
+			array(
+				'name' => 'Color<b>bold</b>',
+				'slug' => 'markup-color',
+			)
+		);
+		$this->assertIsInt( $id );
+
+		try {
+			$this->assertSame( 'Colorbold', $this->get_stored_attribute_label( $id ) );
+			$this->assertSame( 'Colorbold', wc_get_attribute( $id )->name );
+
+			$result = wc_update_attribute( $id, array( 'name' => '<em>Size</em>' ) );
+			$this->assertIsInt( $result );
+			$this->assertSame( 'Size', $this->get_stored_attribute_label( $id ) );
+			$this->assertSame( 'Size', wc_get_attribute( $id )->name );
+
+			$error = wc_create_attribute( array( 'name' => '<b></b>' ) );
+			$this->assertWPError( $error );
+			$this->assertSame( 'missing_attribute_name', $error->get_error_code() );
+		} finally {
+			wc_delete_attribute( $id );
+		}
+	}
+
+	/**
+	 * @testdox The default slug is derived from the name as given, so cleaning the label does not change it.
+	 */
+	public function test_wc_create_attribute_default_slug_ignores_label_cleanup(): void {
+		$id = wc_create_attribute( array( 'name' => 'Width <5cm' ) );
+		$this->assertIsInt( $id );
+
+		try {
+			$attribute = wc_get_attribute( $id );
+			$this->assertSame( 'pa_width', $attribute->slug );
+			$this->assertSame( 'Width &lt;5cm', $attribute->name );
+		} finally {
+			wc_delete_attribute( $id );
+		}
+	}
+
+	/**
+	 * @testdox Attribute rows stored with markup are read back as plain text and produce plain-text taxonomy labels and messages.
+	 */
+	public function test_stored_attribute_label_with_markup_is_normalized_on_read(): void {
+		$attribute_id = $this->insert_raw_attribute_row( 'Colour<b>bold</b>', 'rawcolour' );
+
+		try {
+			$this->assertSame( 'Colourbold', wc_get_attribute( $attribute_id )->name );
+
+			// register_taxonomies() is a no-op once product_type exists.
+			unregister_taxonomy( 'product_type' );
+			WC_Post_Types::register_taxonomies();
+
+			$labels = get_taxonomy( 'pa_rawcolour' )->labels;
+			$this->assertSame( 'Colourbold', $labels->singular_name );
+			$messages = WC_Post_Types::updated_term_messages( array() );
+			foreach ( array( $labels->add_new_item, $labels->edit_item, $messages['pa_rawcolour'][1] ) as $text ) {
+				$this->assertStringContainsString( 'Colourbold', $text );
+				$this->assertStringNotContainsString( '<', $text );
+			}
+		} finally {
+			$this->clean_up_attribute_test_state( array( $attribute_id ), 'pa_rawcolour' );
+		}
+	}
+
 	/**
 	 * Test wc_create_attribute() function.
 	 */
@@ -810,6 +880,44 @@ class WC_Attribute_Functions_Test extends \WC_Unit_Test_Case {
 		return 'runtime-' . $context . '-' . strtolower( wp_generate_password( 5, false, false ) );
 	}

+	/**
+	 * Insert an attribute row directly, bypassing wc_create_attribute(), and reset the attribute caches.
+	 *
+	 * @param string $label Raw label to store.
+	 * @param string $slug  Attribute slug (without the `pa_` prefix).
+	 * @return int Attribute ID.
+	 */
+	private function insert_raw_attribute_row( string $label, string $slug ): int {
+		global $wpdb;
+
+		$wpdb->insert(
+			$wpdb->prefix . 'woocommerce_attribute_taxonomies',
+			array(
+				'attribute_label'   => $label,
+				'attribute_name'    => $slug,
+				'attribute_type'    => 'select',
+				'attribute_orderby' => 'menu_order',
+				'attribute_public'  => 0,
+			)
+		);
+		delete_transient( 'wc_attribute_taxonomies' );
+		WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
+
+		return (int) $wpdb->insert_id;
+	}
+
+	/**
+	 * Read the attribute label straight from the table, bypassing the normalizing readers.
+	 *
+	 * @param int $attribute_id Attribute ID.
+	 * @return string|null
+	 */
+	private function get_stored_attribute_label( int $attribute_id ): ?string {
+		global $wpdb;
+
+		return $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id ) );
+	}
+
 	/**
 	 * Removes database and runtime state created by an attribute deletion test.
 	 *