assignments = $assignments ?? new AssignmentStore(); $this->resolver = $resolver ?? new AssignmentResolver(); $this->blueprints = $blueprints ?? new BlueprintStore(); $this->entitlementPolicy = $entitlementPolicy ?? new EntitlementPolicy(); } /** * @param string $generator Generator key. * @param int[] $product_ids Product ids to resolve (already permission-filtered and deduped by the caller). * @return array * Keyed by product id. */ public function resolveForProducts( string $generator, array $product_ids ): array { if ( empty( $product_ids ) ) { return array(); } $rows = $this->assignments->listForGenerator( $generator ); $this->primeProductCaches( $product_ids ); $groups = array(); $signature_by_product = array(); foreach ( $product_ids as $product_id ) { $signature = 'cats:' . $this->categorySignature( $product_id ); $signature_by_product[ $product_id ] = $signature; $groups[ $signature ][] = $product_id; } $resolution_by_signature = array(); foreach ( $groups as $signature => $ids ) { $representative = $ids[0]; $resolution = $this->resolver->resolve( $generator, array( 'type' => 'product', 'id' => $representative ), null, null, $rows ); $resolution_by_signature[ $signature ] = is_wp_error( $resolution ) ? null : $resolution; } $definition_cache = array(); $result = array(); foreach ( $product_ids as $product_id ) { $resolution = $resolution_by_signature[ $signature_by_product[ $product_id ] ] ?? null; $result[ $product_id ] = null === $resolution ? array( 'blueprintId' => null, 'name' => '', 'revision' => null, 'builtin' => true, 'conflict' => false, 'definition' => array() ) : $this->governingShape( $generator, $resolution, $definition_cache ); } return $result; } /** * @param string $generator Generator key. * @param array $resolution AssignmentResolver::resolve()'s Resolution shape. * @param array $definition_cache Per-call cache of loaded/clamped definitions, passed by reference. * @return array{blueprintId:int|null,name:string,revision:int|null,builtin:bool,conflict:bool,definition:array} */ private function governingShape( string $generator, array $resolution, array &$definition_cache ): array { if ( null !== $resolution['conflict'] ) { return array( 'blueprintId' => null, 'name' => '', 'revision' => null, 'builtin' => false, 'conflict' => true, 'definition' => array() ); } $blueprint_id = $resolution['blueprintId']; if ( null === $blueprint_id ) { $template_key = TemplateCatalog::defaultKey( $generator ); $cache_key = 'builtin:' . $generator . ':' . $template_key; if ( ! isset( $definition_cache[ $cache_key ] ) ) { $template = TemplateCatalog::get( $generator, $template_key ); $raw_definition = null !== $template ? $template['definition'] : array(); $definition_cache[ $cache_key ] = array( 'name' => null !== $template ? (string) $template['name'] : (string) $resolution['blueprintName'], 'revision' => null, 'definition' => $this->entitlementPolicy->apply( $generator, $raw_definition )['definition'], ); } $cached = $definition_cache[ $cache_key ]; return array( 'blueprintId' => null, 'name' => $cached['name'], 'revision' => null, 'builtin' => true, 'conflict' => false, 'definition' => $cached['definition'] ); } $cache_key = 'blueprint:' . $blueprint_id; if ( ! isset( $definition_cache[ $cache_key ] ) ) { $blueprint = $this->blueprints->get( $blueprint_id ); $raw_definition = null !== $blueprint ? $blueprint['definition'] : array(); $definition_cache[ $cache_key ] = array( 'name' => null !== $blueprint ? (string) $blueprint['name'] : (string) $resolution['blueprintName'], 'revision' => null !== $blueprint ? (int) $blueprint['revision'] : null, 'definition' => $this->entitlementPolicy->apply( $generator, $raw_definition )['definition'], ); } $cached = $definition_cache[ $cache_key ]; return array( 'blueprintId' => $blueprint_id, 'name' => $cached['name'], 'revision' => $cached['revision'], 'builtin' => false, 'conflict' => false, 'definition' => $cached['definition'] ); } /** * @param int $product_id Product id. * @return string Sorted, comma-joined `product_cat` term ids. */ private function categorySignature( int $product_id ): string { if ( ! function_exists( 'wp_get_post_terms' ) ) { return ''; } $terms = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ); if ( function_exists( 'is_wp_error' ) && is_wp_error( $terms ) ) { return ''; } $ids = array_map( 'intval', (array) $terms ); sort( $ids ); return implode( ',', $ids ); } /** * Warm the WordPress term cache and WooCommerce product cache for a * batch of product ids before resolving each category signature, so * the per-product category lookups below and every * {@see AssignmentResolver::resolve()} call hit cache instead of * issuing their own query per product. Mirrors * {@see \AIPT\Core\ContentStructure\Assignment\AssignmentScope::primeProductCaches()}. * * @param int[] $product_ids Product ids to prime. * @return void */ private function primeProductCaches( array $product_ids ): void { if ( empty( $product_ids ) ) { return; } if ( function_exists( 'update_object_term_cache' ) ) { update_object_term_cache( $product_ids, 'product' ); } if ( function_exists( 'wc_get_products' ) ) { wc_get_products( array( 'include' => $product_ids, 'return' => 'objects', 'limit' => -1, ) ); } } }