snapshots = $snapshots ?? new SnapshotStore(); } /** * @inheritDoc */ public function findings( int $product_id, WC_Product $product, array $definition ): array { unset( $product ); $items = $this->currentFaqItems( $product_id ); $count = count( $items ); if ( 0 === $count ) { return array( array( 'code' => 'faq_missing', 'severity' => 'warning', 'field' => 'faq', 'message' => __( 'No FAQ questions are set.', 'ai-product-tools' ), 'label' => __( 'no FAQ', 'ai-product-tools' ), ), ); } $findings = array(); $max_questions = isset( $definition['structure']['maxQuestions'] ) ? (int) $definition['structure']['maxQuestions'] : 8; if ( $count > $max_questions ) { $findings[] = array( 'code' => 'faq_over_max', 'severity' => 'warning', 'field' => 'faq', /* translators: %1$d: measured question count. %2$d: the template's allowed question count. */ 'message' => sprintf( __( 'There are %1$d FAQ questions; this template allows %2$d.', 'ai-product-tools' ), $count, $max_questions ), 'label' => __( 'too many FAQ questions', 'ai-product-tools' ), 'measured' => $count, 'limit' => $max_questions, ); } $answer_hard_max = $this->mostLenientAnswerHardMax( $definition ); $over_limit = 0; foreach ( $items as $item ) { $answer = isset( $item['answer'] ) ? (string) $item['answer'] : ''; if ( TextMetrics::charCount( $answer ) > $answer_hard_max ) { $over_limit++; } } if ( $over_limit > 0 ) { $findings[] = array( 'code' => 'faq_answer_too_long', 'severity' => 'warning', 'field' => 'faq', /* translators: %1$d: number of answers over the limit. %2$d: the template's hard character limit per answer. */ 'message' => sprintf( _n( '%1$d FAQ answer is over this template\'s %2$d character limit.', '%1$d FAQ answers are over this template\'s %2$d character limit.', $over_limit, 'ai-product-tools' ), $over_limit, $answer_hard_max ), 'label' => __( 'FAQ answer too long', 'ai-product-tools' ), 'measured' => $over_limit, 'limit' => $answer_hard_max, ); } return $findings; } /** * @inheritDoc */ public function provenanceFieldMap(): array { return array( 'faq' => 'faq' ); } /** * @param int $product_id Product id. * @return array */ private function currentFaqItems( int $product_id ): array { $raw = $this->snapshots->readCurrentFieldValue( $product_id, 'faq' ); $decoded = is_string( $raw ) && '' !== $raw ? json_decode( $raw, true ) : array(); return is_array( $decoded ) ? array_values( array_filter( $decoded, 'is_array' ) ) : array(); } /** * The stored FAQ list has no per-item group attribution, so a single * existing answer cannot be matched back to the ONE blueprint group * that produced it. Using the MOST LENIENT (highest) `answer.hardMax` * configured across every group avoids flagging an answer that fits * comfortably under some group's own limit, just because a DIFFERENT * group's limit is stricter. * * @param array $definition Clamped BlueprintDefinition. * @return int */ private function mostLenientAnswerHardMax( array $definition ): int { $groups = isset( $definition['structure']['groups'] ) && is_array( $definition['structure']['groups'] ) ? $definition['structure']['groups'] : array(); $max = DefinitionPrimitives::FAQ_ANSWER_HARD_MAX; if ( empty( $groups ) ) { return $max; } $highest = 0; foreach ( $groups as $group ) { $hard_max = isset( $group['answer']['hardMax'] ) ? (int) $group['answer']['hardMax'] : $max; $highest = max( $highest, $hard_max ); } return $highest; } }