snapshots = $snapshots ?? new SnapshotStore(); } /** * @inheritDoc */ public function findings( int $product_id, WC_Product $product, array $definition ): array { unset( $product ); $title = (string) ( $this->snapshots->readCurrentFieldValue( $product_id, 'seo_meta_title' ) ?? '' ); $description = (string) ( $this->snapshots->readCurrentFieldValue( $product_id, 'seo_meta_description' ) ?? '' ); $keyword = (string) ( $this->snapshots->readCurrentFieldValue( $product_id, 'seo_focus_keyword' ) ?? '' ); $findings = array(); if ( '' === trim( $title ) ) { $findings[] = array( 'code' => 'meta_title_missing', 'severity' => 'warning', 'field' => 'meta_title', 'message' => __( 'The meta title is empty.', 'ai-product-tools' ), 'label' => __( 'no meta title', 'ai-product-tools' ), ); } else { $title_finding = $this->lengthFinding( $title, (int) ( $definition['structure']['title']['hardMax'] ?? 60 ), 'meta_title', 'meta_title_length', __( 'Meta title', 'ai-product-tools' ) ); if ( null !== $title_finding ) { $findings[] = $title_finding; } } if ( '' === trim( $description ) ) { $findings[] = array( 'code' => 'meta_description_missing', 'severity' => 'warning', 'field' => 'meta_description', 'message' => __( 'The meta description is empty.', 'ai-product-tools' ), 'label' => __( 'no meta description', 'ai-product-tools' ), ); } else { $description_finding = $this->lengthFinding( $description, (int) ( $definition['structure']['description']['hardMax'] ?? 160 ), 'meta_description', 'meta_description_length', __( 'Meta description', 'ai-product-tools' ) ); if ( null !== $description_finding ) { $findings[] = $description_finding; } } $keyword_source = (string) ( $definition['structure']['keyword']['source'] ?? 'focus_keyword' ); $trimmed_keyword = trim( $keyword ); if ( 'focus_keyword' === $keyword_source && '' === $trimmed_keyword ) { $findings[] = array( 'code' => 'focus_keyword_missing', 'severity' => 'warning', 'field' => 'focus_keyword', 'message' => __( 'This template uses a focus keyword, and none is set.', 'ai-product-tools' ), 'label' => __( 'no focus keyword', 'ai-product-tools' ), ); } if ( '' !== $trimmed_keyword ) { $combined = trim( $title . ' ' . $description ); $count = SeoMetaAssembly::countOccurrences( $combined, $trimmed_keyword ); if ( 0 === $count ) { $findings[] = array( 'code' => 'focus_keyword_unused', 'severity' => 'info', 'field' => 'focus_keyword', 'message' => __( 'The focus keyword does not appear in the current meta title or description.', 'ai-product-tools' ), 'label' => __( 'focus keyword unused', 'ai-product-tools' ), ); } } return $findings; } /** * @inheritDoc */ public function provenanceFieldMap(): array { return array( 'meta_title' => 'seo_meta_title', 'meta_description' => 'seo_meta_description', ); } /** * @param string $text Current field text. * @param int $hard_max The Blueprint's hard character ceiling for this field. * @param string $field CheckFinding.field. * @param string $code CheckFinding.code. * @param string $subject Translated subject noun for the message ("Meta title"/"Meta description"). * @return array|null */ private function lengthFinding( string $text, int $hard_max, string $field, string $code, string $subject ): ?array { $length = TextMetrics::charCount( $text ); if ( $length <= $hard_max ) { return null; } return array( 'code' => $code, 'severity' => 'warning', 'field' => $field, /* translators: %1$s: "Meta title" or "Meta description". %2$d: measured character count. %3$d: the template's hard character limit. */ 'message' => sprintf( __( '%1$s is %2$d characters; this template allows %3$d.', 'ai-product-tools' ), $subject, $length, $hard_max ), /* translators: %s: "meta title" or "meta description". */ 'label' => sprintf( __( '%s too long', 'ai-product-tools' ), mb_strtolower( $subject, 'UTF-8' ) ), 'measured' => $length, 'limit' => $hard_max, ); } }