taxonomyDiscoveryService = $taxonomyDiscoveryService ?: new TaxonomyDiscoveryService(); } /** * Build term context using selected content sources and sampled products. * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy name. * @param array $sources Selected source keys. * @param array $options Runtime options. * * @return array */ public function formatTermContext( int $term_id, string $taxonomy, array $sources, array $options ): array { $term_id = absint( $term_id ); $taxonomy = (string) sanitize_key( $taxonomy ); if ( '' === $taxonomy ) { return [ 'term_id' => $term_id, 'term_name' => '', 'term_slug' => '', 'taxonomy' => '', 'child_terms' => [], 'product_count' => 0, 'has_products' => false, ]; } $term = $this->taxonomyDiscoveryService->getTermById( $term_id, $taxonomy ); if ( ! $term ) { return [ 'term_id' => $term_id, 'term_name' => '', 'term_slug' => '', 'taxonomy' => $taxonomy, 'child_terms' => [], 'product_count' => 0, 'has_products' => false, ]; } $normalized_sources = $this->normalizeSources( $sources ); $child_terms = $this->hasSource( $normalized_sources, 'product_categories' ) ? $this->getChildTerms( $term_id, $taxonomy ) : []; $product_count = TermProductSampler::countCatalogProducts( $taxonomy, $this->arrayString( $term, 'slug' ), (int) $term['count'] ); $context = [ 'term_id' => (int) $term['term_id'], 'term_name' => (string) $term['name'], 'term_slug' => (string) $term['slug'], 'taxonomy' => $taxonomy, 'child_terms' => $child_terms, 'product_count' => $product_count, 'has_products' => $product_count > 0, ]; if ( $this->hasSource( $normalized_sources, 'term_description' ) ) { $context['existing_description'] = HtmlToText::convert( (string) $term['description'] ); } if ( $this->hasSource( $normalized_sources, 'parent_context' ) ) { $context['breadcrumb'] = (string) $term['breadcrumb']; $context['parent_term'] = ''; if ( ! empty( $term['parent'] ) ) { $parent_term = $this->taxonomyDiscoveryService->getTermById( (int) $term['parent'], $taxonomy ); if ( is_array( $parent_term ) ) { $context['parent_term'] = $this->arrayString( $parent_term, 'name' ); } } } $product_sources = [ 'product_names', 'product_descriptions', 'product_attributes', 'product_prices', 'product_tags', ]; $has_product_source = false; foreach ( $product_sources as $product_source ) { if ( $this->hasSource( $normalized_sources, $product_source ) ) { $has_product_source = true; break; } } if ( ! $context['has_products'] || ! $has_product_source ) { return $context; } $max_samples = $this->resolveMaxSamples( $options ); $products = $this->getSampledProducts( $taxonomy, $this->arrayString( $term, 'slug' ), $max_samples ); if ( empty( $products ) ) { return $context; } $context['product_summary'] = $this->buildProductSummary( $products, $normalized_sources ); return $context; } /** * @param array $sources Selected source list. * * @return string[] */ private function normalizeSources( array $sources ): array { $normalized = []; foreach ( $sources as $source ) { if ( ! is_string( $source ) ) { continue; } $sanitized = sanitize_key( $source ); if ( '' === $sanitized ) { continue; } $normalized[] = $sanitized; } return array_values( array_unique( $normalized ) ); } /** * @param array $sources Source list. * @param string $source Source key. * * @return bool */ private function hasSource( array $sources, string $source ): bool { return in_array( $source, $sources, true ); } /** * @param array $options Runtime options. * * @return int */ private function resolveMaxSamples( array $options ): int { $max_samples = isset( $options['maxProductSamples'] ) ? absint( $options['maxProductSamples'] ) : 10; if ( $max_samples < 1 ) { $max_samples = 1; } return min( 20, $max_samples ); } /** * @param int $term_id Term ID. * @param string $taxonomy Taxonomy name. * * @return string[] */ private function getChildTerms( int $term_id, string $taxonomy ): array { $children = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'parent' => $term_id, 'fields' => 'names', ] ); if ( is_wp_error( $children ) || empty( $children ) ) { return []; } return array_values( array_filter( array_map( 'strval', $children ) ) ); } /** * @param string $taxonomy Taxonomy name. * @param string $slug Term slug. * @param int $limit Sample limit. * * @return array */ private function getSampledProducts( string $taxonomy, string $slug, int $limit ): array { return TermProductSampler::sampleBySlug( $taxonomy, $slug, [ 'limit' => $limit, 'orderby' => 'date', 'order' => 'DESC', ] ); } /** * @param array $products Sampled WC_Product objects. * @param array $sources Selected source keys. * * @return array */ private function buildProductSummary( array $products, array $sources ): array { $summary = []; $names = []; $price_values = []; $collected_tags = []; foreach ( $products as $product ) { if ( ! $product || ! is_object( $product ) || ! method_exists( $product, 'get_name' ) ) { continue; } $formatted_product = ProductDataFormatter::getFormattedProductData( $product ); if ( $this->hasSource( $sources, 'product_names' ) ) { $name = isset( $formatted_product['name'] ) ? trim( (string) $formatted_product['name'] ) : ''; if ( '' !== $name ) { $names[] = $name; } } if ( $this->hasSource( $sources, 'product_prices' ) && method_exists( $product, 'get_price' ) ) { $raw_price = $product->get_price(); if ( '' !== $raw_price && null !== $raw_price ) { $price_values[] = (float) $raw_price; } } if ( $this->hasSource( $sources, 'product_tags' ) && method_exists( $product, 'get_tag_ids' ) ) { $tag_ids = $product->get_tag_ids(); foreach ( $tag_ids as $tag_id ) { $tag_term = get_term( $tag_id, 'product_tag' ); if ( $tag_term && ! is_wp_error( $tag_term ) ) { $collected_tags[] = $tag_term->name; } } } } if ( $this->hasSource( $sources, 'product_names' ) ) { $summary['names'] = array_values( array_unique( $names ) ); } if ( $this->hasSource( $sources, 'product_prices' ) ) { if ( empty( $price_values ) ) { $summary['price_range'] = [ 'min' => null, 'max' => null, ]; } else { $summary['price_range'] = [ 'min' => round( min( $price_values ), 2 ), 'max' => round( max( $price_values ), 2 ), ]; } } if ( $this->hasSource( $sources, 'product_tags' ) ) { $summary['tags'] = array_values( array_unique( $collected_tags ) ); } /** * Filter the sampled product summary. The Premium build adds * `descriptions` and `attributes` for the paid sources * (`product_descriptions`, `product_attributes`) when selected. * * @since 2.8.0 * @param array $summary Summary from the free sources. * @param array $products Sampled products. * @param string[] $sources Selected source keys. */ $filtered = apply_filters( 'aipt_taxonomy_product_summary', $summary, $products, $sources ); return is_array( $filtered ) ? $filtered : $summary; } /** * @param array $data Array data source. * @param string $key Array key. * * @return string */ private function arrayString( array $data, string $key ): string { if ( ! isset( $data[ $key ] ) || null === $data[ $key ] ) { return ''; } return is_string( $data[ $key ] ) ? $data[ $key ] : (string) $data[ $key ]; } }