*/ private static array $cache = array(); /** * Product ids {@see fetchRows()} has already queried for, including ones * with no qualifying row - so a product with no history is not * re-queried on every access. * * @var array */ private static array $primed = array(); /** * `woocommerce_product_export_product_query_args` callback: resolves this * batch's product ids with the SAME args WooCommerce is about to query * with, and primes the cache for all of them in one additional, batched * read. * * @param mixed $args Query args WooCommerce will pass to wc_get_products(). * @return mixed $args, unchanged. */ public static function primeFromQueryArgs( $args ) { if ( ! is_array( $args ) || ! function_exists( 'wc_get_products' ) ) { return $args; } $idArgs = $args; $idArgs['return'] = 'ids'; $idArgs['paginate'] = false; try { $ids = wc_get_products( $idArgs ); } catch ( \Throwable $e ) { $ids = array(); } if ( is_array( $ids ) && ! empty( $ids ) ) { self::fetchRows( $ids ); } return $args; } /** * @param int $productId Product id. * @return string Readable provenance summary, e.g. "Title: Brand-first * title (built-in); Long description: Apparel description, * version 3; ...; SEO meta: not recorded". */ public static function readable( int $productId ): string { self::ensurePrimed( $productId ); $labels = array( 'title' => __( 'Title', 'ai-product-tools' ), 'description' => __( 'Long description', 'ai-product-tools' ), 'short_description' => __( 'Short description', 'ai-product-tools' ), 'tags' => __( 'Tags', 'ai-product-tools' ), 'faq' => __( 'FAQ', 'ai-product-tools' ), ); $parts = array(); foreach ( $labels as $fieldType => $label ) { $row = self::$cache[ $productId . ':' . $fieldType ] ?? null; $parts[] = $label . ': ' . self::format( $row ); } $seoRow = self::$cache[ $productId . ':seo_meta_title' ] ?? self::$cache[ $productId . ':seo_meta_description' ] ?? null; $parts[] = __( 'SEO meta', 'ai-product-tools' ) . ': ' . self::format( $seoRow ); return implode( '; ', $parts ); } /** * @param int $productId Product id. * @return void */ private static function ensurePrimed( int $productId ): void { if ( ! isset( self::$primed[ $productId ] ) ) { self::fetchRows( array( $productId ) ); } } /** * @param array $productIds Product ids to fetch and mark primed, valid or not. * @return void */ private static function fetchRows( array $productIds ): void { $ids = array_values( array_unique( array_filter( array_map( 'intval', $productIds ), static function ( $id ) { return $id > 0; } ) ) ); if ( empty( $ids ) ) { return; } $rows = ( new SnapshotStore() )->newestCommittedFieldRows( $ids, self::FIELD_TYPES ); self::$cache = array_merge( self::$cache, $rows ); foreach ( $ids as $id ) { self::$primed[ $id ] = true; } } /** * @param array{blueprintId:int,blueprintName:string,blueprintRevision:int,blueprintBuiltin:bool}|null $row * @return string */ private static function format( ?array $row ): string { if ( null === $row ) { return __( 'not recorded', 'ai-product-tools' ); } $blueprintId = (int) ( $row['blueprintId'] ?? 0 ); $builtin = ! empty( $row['blueprintBuiltin'] ); $name = (string) ( $row['blueprintName'] ?? '' ); if ( 0 === $blueprintId && ! $builtin && '' === $name ) { return __( 'not recorded', 'ai-product-tools' ); } if ( $builtin ) { /* translators: %s: built-in template name. */ return sprintf( __( '%s (built-in)', 'ai-product-tools' ), $name ); } /* translators: 1: template name. 2: revision number. */ return sprintf( __( '%1$s, version %2$d', 'ai-product-tools' ), $name, (int) ( $row['blueprintRevision'] ?? 0 ) ); } /** * Test seam: clears every static cache so one test cannot leak state into another. * * @return void */ public static function resetForTesting(): void { self::$cache = array(); self::$primed = array(); } }