*/ private const SNAPSHOT_FIELD_TYPES = array( Generators::DESCRIPTION => array( 'description', 'short_description', 'tags' ), Generators::TITLE => array( 'title' ), Generators::SEO_META => array( 'seo_meta_title', 'seo_meta_description' ), Generators::FAQ => array( 'faq' ), Generators::IMAGE_ALT => array( 'image_alt' ), ); /** * @var GoverningBlueprintResolver */ private $governingResolver; /** * @var SnapshotStore */ private $snapshots; /** * @param GoverningBlueprintResolver|null $governingResolver Governing Blueprint resolver; a default instance is constructed when omitted. * @param SnapshotStore|null $snapshots Snapshot store; a default instance is constructed when omitted. */ public function __construct( ?GoverningBlueprintResolver $governingResolver = null, ?SnapshotStore $snapshots = null ) { $this->governingResolver = $governingResolver ?? new GoverningBlueprintResolver(); $this->snapshots = $snapshots ?? new SnapshotStore(); } /** * @param string $generator Candidate generator key. * @return bool Whether the Blueprint check supports this generator. */ public static function isValidGenerator( string $generator ): bool { return in_array( $generator, self::CHECK_GENERATORS, true ); } /** * @param string[] $generators Validated, deduped generator keys (1-5, every one a {@see self::CHECK_GENERATORS} member). * @param int[] $product_ids Validated, deduped, positive product ids (at least one). * @return array{items:array>,skipped:array,counts:array{ok:int,needsWork:int,conflict:int,notApplicable:int}} CheckResponse. Each item's `writtenRevisions` is additive to `provenance`: the row's own recorded revision per field, present whenever `provenance` has an entry for that field. */ public function check( array $generators, array $product_ids ): array { return $this->run( $generators, $product_ids, true ); } /** * The check itself. A subclass that runs with no user (a background job) * passes false to skip the per-user `edit_post` test; a product that no * longer exists is still reported in `skipped`. * * @param string[] $generators Generator keys. * @param int[] $product_ids Product ids. * @param bool $check_permission Whether each product needs the current user's edit_post. * @return array CheckResponse. */ protected function run( array $generators, array $product_ids, bool $check_permission ): array { list( $valid_ids, $skipped ) = $this->partitionProducts( $product_ids, $check_permission ); $counts = array( 'ok' => 0, 'needsWork' => 0, 'conflict' => 0, 'notApplicable' => 0 ); $items = array(); if ( ! empty( $valid_ids ) ) { $field_types = $this->neededSnapshotFieldTypes( $generators ); $provenance_rows = empty( $field_types ) ? array() : $this->snapshots->newestCommittedFieldRows( $valid_ids, $field_types ); foreach ( $generators as $generator ) { $this->checkOneGenerator( $generator, $valid_ids, $provenance_rows, $items, $counts ); } } return array( 'items' => $items, 'skipped' => $skipped, 'counts' => $counts ); } /** * @param int[] $product_ids Candidate product ids. * @param bool $check_permission Whether each product needs the current user's edit_post. * @return array{0:int[],1:array} */ private function partitionProducts( array $product_ids, bool $check_permission ): array { $valid = array(); $skipped = array(); foreach ( $product_ids as $product_id ) { $product = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false; if ( ! $product instanceof WC_Product ) { $skipped[] = array( 'productId' => $product_id, 'reason' => 'not_found' ); continue; } if ( $check_permission && ! current_user_can( 'edit_post', $product_id ) ) { $skipped[] = array( 'productId' => $product_id, 'reason' => 'forbidden' ); continue; } $valid[] = $product_id; } return array( $valid, $skipped ); } /** * @param string $generator Generator key. * @param int[] $valid_ids Permission-filtered, deduped product ids. * @param array> $provenance_rows SnapshotStore::newestCommittedFieldRows()'s result. * @param array> &$items CheckItem accumulator, appended in place. * @param array{ok:int,needsWork:int,conflict:int,notApplicable:int} &$counts Counts accumulator, mutated in place. * @return void */ private function checkOneGenerator( string $generator, array $valid_ids, array $provenance_rows, array &$items, array &$counts ): void { $checker = CheckerRegistry::create( $generator ); if ( null === $checker ) { return; } if ( ExecutionModeRegistry::ADVANCED === ExecutionModeRegistry::mode( $generator ) ) { foreach ( $valid_ids as $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product instanceof WC_Product ) { continue; } $items[] = array( 'productId' => $product_id, 'productName' => $this->plainProductName( $product ), 'generator' => $generator, 'blueprint' => array( 'id' => null, 'name' => '', 'revision' => null, 'builtin' => false, 'conflict' => false ), 'status' => 'not_applicable', 'findings' => array(), 'provenance' => array(), 'writtenRevisions' => array(), ); ++$counts['notApplicable']; } return; } $governing = $this->governingResolver->resolveForProducts( $generator, $valid_ids ); foreach ( $valid_ids as $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product instanceof WC_Product || ! isset( $governing[ $product_id ] ) ) { continue; } $g = $governing[ $product_id ]; if ( $g['conflict'] ) { $items[] = array( 'productId' => $product_id, 'productName' => $this->plainProductName( $product ), 'generator' => $generator, 'blueprint' => array( 'id' => null, 'name' => '', 'revision' => null, 'builtin' => false, 'conflict' => true ), 'status' => 'conflict', 'findings' => array(), 'provenance' => array(), 'writtenRevisions' => array(), ); ++$counts['conflict']; continue; } $findings = $checker->findings( $product_id, $product, $g['definition'] ); $status = $this->statusFor( $findings ); list( $provenance, $written_revisions ) = $this->buildProvenance( $checker, $product_id, $provenance_rows, $g ); $items[] = array( 'productId' => $product_id, 'productName' => $this->plainProductName( $product ), 'generator' => $generator, 'blueprint' => array( 'id' => $g['blueprintId'], 'name' => $g['name'], 'revision' => $g['revision'], 'builtin' => $g['builtin'], 'conflict' => false ), 'status' => $status, 'findings' => $findings, 'provenance' => $provenance, 'writtenRevisions' => $written_revisions, ); ++$counts[ 'ok' === $status ? 'ok' : 'needsWork' ]; } } /** * @param array> $findings CheckFinding[]. * @return string 'needs_work'|'ok'. */ private function statusFor( array $findings ): string { foreach ( $findings as $finding ) { if ( 'warning' === ( $finding['severity'] ?? '' ) ) { return 'needs_work'; } } return 'ok'; } /** * @param CheckerInterface $checker The generator's checker. * @param int $product_id Product id. * @param array> $provenance_rows SnapshotStore::newestCommittedFieldRows()'s result. * @param array{blueprintId:int|null,name:string,revision:int|null,builtin:bool} $governing Governing Blueprint shape for this product. * @return array{0:array,1:array} The provenance status map, and the written-revision map (the row's own `blueprintRevision`, present whenever the status map has an entry for that field) — the pair a CheckItem's `provenance`/`writtenRevisions` fields come from. */ private function buildProvenance( CheckerInterface $checker, int $product_id, array $provenance_rows, array $governing ): array { $provenance = array(); $written_revisions = array(); foreach ( $checker->provenanceFieldMap() as $contract_field => $snapshot_field_type ) { $row = $provenance_rows[ $product_id . ':' . $snapshot_field_type ] ?? null; $status = ProvenanceResolver::statusFor( $row, $governing['blueprintId'], $governing['revision'], $governing['builtin'], $governing['name'] ); if ( null !== $status ) { $provenance[ $contract_field ] = $status; $written_revisions[ $contract_field ] = (int) $row['blueprintRevision']; } } return array( $provenance, $written_revisions ); } /** * @param string[] $generators Generator keys. * @return string[] Distinct snapshot field_type values any of them needs. */ private function neededSnapshotFieldTypes( array $generators ): array { $types = array(); foreach ( $generators as $generator ) { $types = array_merge( $types, self::SNAPSHOT_FIELD_TYPES[ $generator ] ?? array() ); } return array_values( array_unique( $types ) ); } /** * @param WC_Product $product Product. * @return string Decoded, tag-stripped product name. */ private function plainProductName( WC_Product $product ): string { return html_entity_decode( wp_strip_all_tags( (string) $product->get_name() ), ENT_QUOTES, 'UTF-8' ); } }