Commit 3aedfa52494 for woocommerce

commit 3aedfa5249480883111c4e0089de939324ca8c2c
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date:   Thu Nov 26 17:36:17 2026 -0800

    Abilities: Return extension fields on the terms in product-attributes-query (#71346)

    * Abilities: Return extension fields on product-attributes-query terms

    Co-authored-by: matticbot <matticbot@users.noreply.github.com>

    * Prime the terms of all attributes in one call

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Reset the prime callbacks after the attribute terms test

    The test registered a product_term prime callback and left it in place for
    the tests that run after it.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Use ExtensibleAbilitiesTestTrait in the product attributes query test

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Keep attribute terms a list when a get_terms filter drops one

    A get_terms filter that removes a term leaves a gap in the array keys,
    so the terms serialized as a JSON object instead of the array the
    output schema promises.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Hide the terms of an attribute the user cannot read

    product-attributes-query checked only the attribute manager permission, so
    it returned the terms of a taxonomy that product-terms-query refuses. It now
    uses the same taxonomy read check and returns an empty terms list.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Let the test trait turn the feature on in the attribute terms test

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    ---------

    Co-authored-by: zaguiini <luis.felipe.zaguini@automattic.com>
    Co-authored-by: matticbot <matticbot@users.noreply.github.com>
    Co-authored-by: Luis Felipe Zaguini <luisfelipezaguini@gmail.com>
    Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/attribute-terms-extension-fields b/plugins/woocommerce/changelog/attribute-terms-extension-fields
new file mode 100644
index 00000000000..ac13c4bf0e5
--- /dev/null
+++ b/plugins/woocommerce/changelog/attribute-terms-extension-fields
@@ -0,0 +1,4 @@
+Significance: patch
+Type: update
+
+Return extension fields, such as swatch colors, on the attribute terms that the product-attributes-query ability lists.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributesQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributesQuery.php
index efe1bcaf486..df096b2128e 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributesQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributesQuery.php
@@ -117,6 +117,10 @@ class ProductAttributesQuery extends AbstractDomainAbility implements AbilityDef
 			}
 			$formatted[] = $data;
 		}
+		$formatted = self::format_product_attribute_terms( $formatted );
+		if ( is_wp_error( $formatted ) ) {
+			return $formatted;
+		}

 		return array(
 			'attributes'  => $formatted,
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAttributeAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAttributeAbilityTrait.php
index 441ea257950..4830a40645c 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAttributeAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAttributeAbilityTrait.php
@@ -7,6 +7,8 @@ declare( strict_types=1 );

 namespace Automattic\WooCommerce\Internal\Abilities\Domain\Traits;

+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
+
 defined( 'ABSPATH' ) || exit;

 /**
@@ -14,6 +16,8 @@ defined( 'ABSPATH' ) || exit;
  */
 trait ProductAttributeAbilityTrait {

+	use ProductTermAbilityTrait;
+
 	/**
 	 * Check product attribute access, as the REST attribute endpoints do.
 	 *
@@ -25,7 +29,9 @@ trait ProductAttributeAbilityTrait {
 	}

 	/**
-	 * Format a global attribute and up to `$term_limit` of its terms, in the attribute's own term order.
+	 * Format a global attribute and load up to `$term_limit` of its terms, in the attribute's own term order.
+	 * The terms stay `WP_Term` objects until format_product_attribute_terms() formats them.
+	 * An attribute whose terms the user cannot read, as product-terms-query checks it, gets no terms.
 	 *
 	 * @param \stdClass $attribute  Row of wc_get_attribute_taxonomies().
 	 * @param int       $term_limit Most terms to return. Zero returns none.
@@ -60,7 +66,7 @@ trait ProductAttributeAbilityTrait {
 		}
 		$output['term_count'] = (int) $count;

-		if ( 0 === $term_limit || 0 === $output['term_count'] ) {
+		if ( 0 === $term_limit || 0 === $output['term_count'] || ! self::can_manage_product_terms( $taxonomy, 'read' ) ) {
 			return $output;
 		}

@@ -75,20 +81,33 @@ trait ProductAttributeAbilityTrait {
 			return $terms;
 		}

-		foreach ( $terms as $term ) {
-			$output['terms'][] = array(
-				'id'          => $term->term_id,
-				'name'        => html_entity_decode( $term->name, ENT_QUOTES | ENT_HTML5 ),
-				'slug'        => $term->slug,
-				'description' => $term->description,
-				'menu_order'  => (int) get_term_meta( $term->term_id, 'order', true ),
-				'count'       => $term->count,
-			);
-		}
+		$output['terms'] = array_values( $terms );

 		return $output;
 	}

+	/**
+	 * Format the terms of a list of attributes. Terms carry the same fields as in product-terms-query,
+	 * extension fields included. The prime callbacks run once, with the terms of every attribute.
+	 *
+	 * @param array[] $attributes Attributes from format_product_attribute().
+	 * @return array[]|\WP_Error The error of an extension field that failed.
+	 */
+	private static function format_product_attribute_terms( array $attributes ) {
+		ExtensibleAbilities::prime( 'product_term', array_merge( ...array_column( $attributes, 'terms' ) ) );
+		foreach ( $attributes as $index => $attribute ) {
+			foreach ( $attribute['terms'] as $key => $term ) {
+				$data = self::format_term( $term );
+				if ( is_wp_error( $data ) ) {
+					return $data;
+				}
+				$attributes[ $index ]['terms'][ $key ] = $data;
+			}
+		}
+
+		return $attributes;
+	}
+
 	/**
 	 * Get the attribute output schema.
 	 *
@@ -119,17 +138,20 @@ trait ProductAttributeAbilityTrait {
 				),
 				'terms'        => array(
 					'type'  => 'array',
-					'items' => array(
-						'type'                 => 'object',
-						'properties'           => array(
-							'id'          => array( 'type' => 'integer' ),
-							'name'        => array( 'type' => 'string' ),
-							'slug'        => array( 'type' => 'string' ),
-							'description' => array( 'type' => 'string' ),
-							'menu_order'  => array( 'type' => 'integer' ),
-							'count'       => array( 'type' => 'integer' ),
+					'items' => ExtensibleAbilities::add_fields_schema(
+						array(
+							'type'                 => 'object',
+							'properties'           => array(
+								'id'          => array( 'type' => 'integer' ),
+								'name'        => array( 'type' => 'string' ),
+								'slug'        => array( 'type' => 'string' ),
+								'description' => array( 'type' => 'string' ),
+								'menu_order'  => array( 'type' => 'integer' ),
+								'count'       => array( 'type' => 'integer' ),
+							),
+							'additionalProperties' => false,
 						),
-						'additionalProperties' => false,
+						'product_term'
 					),
 				),
 			),
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributesQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributesQueryTest.php
index 070a6aae53a..e6d1758fb77 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributesQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributesQueryTest.php
@@ -7,16 +7,20 @@ declare( strict_types=1 );

 namespace Automattic\WooCommerce\Tests\Internal\Abilities\Domain;

+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
 use Automattic\WooCommerce\Internal\Abilities\Ability;
 use Automattic\WooCommerce\Internal\Abilities\Domain\ProductAttributesQuery;
 use WC_Helper_Product;
 use WC_Unit_Test_Case;
+use Automattic\WooCommerce\Tests\Internal\Abilities\ExtensibleAbilitiesTestTrait;

 /**
  * Tests global attribute reads through the registered ability.
  */
 class ProductAttributesQueryTest extends WC_Unit_Test_Case {

+	use ExtensibleAbilitiesTestTrait;
+
 	/**
 	 * The System Under Test.
 	 *
@@ -24,6 +28,7 @@ class ProductAttributesQueryTest extends WC_Unit_Test_Case {
 	 */
 	private $sut;

+
 	/**
 	 * An administrator that every test acts as.
 	 *
@@ -45,6 +50,8 @@ class ProductAttributesQueryTest extends WC_Unit_Test_Case {
 	 */
 	public function setUp(): void {
 		parent::setUp();
+		$this->isolate_extensible_abilities();
+		update_option( 'woocommerce_feature_extensible_abilities_enabled', 'no' );
 		$this->sut = new Ability( ProductAttributesQuery::get_name(), ProductAttributesQuery::get_registration_args() );
 		wp_set_current_user( self::$administrator_id );
 	}
@@ -99,6 +106,23 @@ class ProductAttributesQueryTest extends WC_Unit_Test_Case {
 		$this->assertSame( 3, $none['attributes'][0]['term_count'] );
 	}

+	/**
+	 * @testdox Should return the terms as a list when a get_terms filter drops one.
+	 */
+	public function test_terms_stay_a_list_after_a_filter_drops_one(): void {
+		$attribute = WC_Helper_Product::create_attribute( 'Test Fabric', array( 'Wool', 'Silk' ) );
+		add_filter(
+			'get_terms',
+			static function ( $terms ) {
+				return is_array( $terms ) ? array_slice( $terms, 1, null, true ) : $terms;
+			}
+		);
+
+		$result = $this->sut->execute( array( 'include' => array( $attribute['attribute_id'] ) ) );
+
+		$this->assertSame( array( 0 ), array_keys( $result['attributes'][0]['terms'] ), 'The remaining term keeps key 0, so the terms serialize as a JSON list.' );
+	}
+
 	/**
 	 * @testdox Should filter by search and page through the attributes with a total.
 	 */
@@ -126,6 +150,81 @@ class ProductAttributesQueryTest extends WC_Unit_Test_Case {
 		$this->assertSame( array( 'Test Pattern' ), wp_list_pluck( $page['attributes'], 'name' ), 'Attributes are sorted by slug.' );
 	}

+	/**
+	 * @testdox Should return the product_term extension fields on each term, as product-terms-query does, and prime the terms of all attributes once.
+	 */
+	public function test_term_extension_fields(): void {
+		$attribute = WC_Helper_Product::create_attribute( 'Test Swatch', array( 'Red', 'Blue' ) );
+		$second    = WC_Helper_Product::create_attribute( 'Test Finish', array( 'Matte' ) );
+		$input     = array( 'include' => array( $attribute['attribute_id'], $second['attribute_id'] ) );
+		$primed    = array();
+		$this->enable_extensible_abilities(
+			static function () use ( &$primed ) {
+				ExtensibleAbilities::register_prime(
+					array(
+						'resource'  => 'product_term',
+						'namespace' => 'test-ext',
+						'callback'  => static function ( array $terms ) use ( &$primed ) {
+							$primed[] = count( $terms );
+						},
+					)
+				);
+				ExtensibleAbilities::register_field(
+					array(
+						'resource'      => 'product_term',
+						'namespace'     => 'test-ext',
+						'field'         => 'color',
+						'output_schema' => array(
+							'type'  => 'string',
+							'title' => 'Color',
+						),
+						'get_callback'  => static function ( \WP_Term $term ) {
+							return 'hex-' . $term->slug;
+						},
+					)
+				);
+			}
+		);
+		$ability = new \WP_Ability( ProductAttributesQuery::get_name(), ProductAttributesQuery::get_registration_args() );
+
+		$result = $ability->execute( $input );
+
+		$this->assertIsArray( $result );
+		$this->assertSame( array( 3 ), $primed, 'The prime callbacks run once, with the terms of both attributes.' );
+		$terms = array_merge( ...array_column( $result['attributes'], 'terms' ) );
+		$this->assertCount( 3, $terms );
+		foreach ( $terms as $term ) {
+			$this->assertSame( 'hex-' . $term['slug'], $term['extensions']['test-ext']['color'] );
+		}
+		$this->assertSame( 'string', $ability->get_output_schema()['properties']['attributes']['items']['properties']['terms']['items']['properties']['extensions']['properties']['test-ext']['properties']['color']['type'] );
+		update_option( 'woocommerce_feature_extensible_abilities_enabled', 'no' );
+		$this->assertArrayNotHasKey( 'extensions', $this->sut->execute( $input )['attributes'][0]['terms'][0] );
+	}
+
+	/**
+	 * @testdox Should return no terms for an attribute whose terms the user cannot read.
+	 */
+	public function test_hides_terms_the_user_cannot_read(): void {
+		$denied  = WC_Helper_Product::create_attribute( 'Test Denied', array( 'Red', 'Blue' ) );
+		$allowed = WC_Helper_Product::create_attribute( 'Test Allowed', array( 'Matte' ) );
+		add_filter(
+			'woocommerce_rest_check_permissions',
+			static function ( $permission, $context, $object_id, $type ) use ( $denied ) {
+				return $denied['attribute_taxonomy'] === $type ? false : $permission;
+			},
+			10,
+			4
+		);
+
+		$result = $this->sut->execute( array( 'include' => array( $denied['attribute_id'], $allowed['attribute_id'] ) ) );
+
+		$this->assertIsArray( $result );
+		$attributes = array_column( $result['attributes'], null, 'slug' );
+		$this->assertSame( array(), $attributes[ $denied['attribute_taxonomy'] ]['terms'] );
+		$this->assertSame( 2, $attributes[ $denied['attribute_taxonomy'] ]['term_count'] );
+		$this->assertSame( array( 'Matte' ), wp_list_pluck( $attributes[ $allowed['attribute_taxonomy'] ]['terms'], 'name' ) );
+	}
+
 	/**
 	 * @testdox Should accept a call without input.
 	 */