resolver = $resolver ?? new AssignmentResolver(); } /** * Build a group entry per the preview's own `blueprints` shape, from * PINNED resolution of $remaining (so confirm's echo reflects what will * actually run, not the preview's live snapshot). * * @param string $generator Generator key. * @param array $pin Loaded run pin (RunPinService::loadRunPin()'s shape). * @param int[] $remaining Product ids that passed every re-check. * @return array */ public function blueprintsEcho( string $generator, array $pin, array $remaining ): array { $revisions = array(); foreach ( (array) ( $pin['blueprints'] ?? array() ) as $entry ) { if ( is_array( $entry ) && isset( $entry['id'] ) ) { $revisions[ (int) $entry['id'] ] = isset( $entry['revision'] ) ? (int) $entry['revision'] : null; } } $groups = array(); $order = 0; foreach ( $remaining as $product_id ) { $resolution = $this->resolver->resolve( $generator, array( 'type' => 'product', 'id' => $product_id ), null, $pin['payload'] ); if ( $resolution instanceof WP_Error || null !== $resolution['conflict'] ) { continue; } $blueprint_id = $resolution['blueprintId']; $key = null === $blueprint_id ? 'builtin' : (string) $blueprint_id; if ( ! isset( $groups[ $key ] ) ) { $name = null === $blueprint_id /* translators: %s: built-in template name. */ ? sprintf( __( 'Built-in: %s', 'ai-product-tools' ), (string) $resolution['blueprintName'] ) : (string) $resolution['blueprintName']; $groups[ $key ] = array( 'blueprint_id' => $blueprint_id, 'name' => $name, 'revision' => null === $blueprint_id ? null : ( $revisions[ $blueprint_id ] ?? null ), 'builtin' => null === $blueprint_id, 'product_count' => 0, '_order' => $order++, ); } $groups[ $key ]['product_count']++; } $groups = array_values( $groups ); usort( $groups, static function ( array $a, array $b ): int { return $b['product_count'] <=> $a['product_count'] ?: $a['_order'] <=> $b['_order']; } ); foreach ( $groups as &$group ) { unset( $group['_order'] ); } unset( $group ); return $groups; } /** * @param array $applied Applied entries so far. * @param array $staged Staged entries so far. * @param array $held_back Held-back entries so far. * @param array $failed Failed entries so far. * @return string 'completed' | 'partial' | 'failed'. */ public function computeStatus( array $applied, array $staged, array $held_back, array $failed ): string { $succeeded = count( $applied ) + count( $staged ); $ran = $succeeded + count( $held_back ) + count( $failed ); if ( 0 === $ran || 0 === $succeeded ) { return 'failed'; } if ( $succeeded === $ran ) { return 'completed'; } return 'partial'; } /** * @param string $status Overall status. * @param string $generator Generator key. * @param array $run_pin_echo Response `run_pin` shape. * @param array $blueprints_echo Response `blueprints` shape. * @param array $applied Applied entries. * @param array $staged Staged entries. * @param array $held_back Held-back entries. * @param array $skipped Skipped entries. * @param array $failed Failed entries. * @param array $plan_limits Plan-limit entries. * @return array The confirm response shape (without `job_id`/`job_url`/`deep_link`, added by the caller when applicable). */ public function responseShape( string $status, string $generator, array $run_pin_echo, array $blueprints_echo, array $applied, array $staged, array $held_back, array $skipped, array $failed, array $plan_limits ): array { return array( 'status' => $status, 'generator' => $generator, 'run_pin' => $run_pin_echo, 'blueprints' => $blueprints_echo, 'applied' => $applied, 'staged' => $staged, 'held_back' => $held_back, 'skipped' => $skipped, 'failed' => $failed, 'plan_limits' => $plan_limits, 'message' => $this->buildMessage( $status, count( $applied ) + count( $staged ), count( $applied ) + count( $staged ) + count( $held_back ) + count( $failed ), count( $staged ), $this->countSnapshots( $applied ) ), ); } /** * @param array $applied Applied entries. * @return int Applied entries whose write took a History snapshot. */ private function countSnapshots( array $applied ): int { $count = 0; foreach ( $applied as $entry ) { if ( ! empty( $entry['snapshot_taken'] ) ) { ++$count; } } return $count; } /** * @param string $status Overall status. * @param int $succeeded Applied + staged count. * @param int $ran Every product that actually ran (applied + staged + held back + failed). * @param int $staged How many of the succeeded products were only sent to review. * @param int $snapshots How many applied products took a History snapshot. * @return string */ public function buildMessage( string $status, int $succeeded, int $ran, int $staged = 0, int $snapshots = 0 ): string { if ( 'queued' === $status ) { return __( 'Queued for background processing. Check Automation for progress.', 'ai-product-tools' ); } if ( 'deep_link' === $status ) { return __( 'Open the link to continue on the generator page.', 'ai-product-tools' ); } if ( 0 === $ran ) { return __( 'Nothing could be applied. See the skipped list for why.', 'ai-product-tools' ); } if ( 0 === $succeeded ) { return __( 'Nothing was applied. See the held back and failed entries for why.', 'ai-product-tools' ); } $applied = $succeeded - $staged; if ( 0 === $applied ) { /* translators: 1: number of products sent to review, 2: number of products that ran. */ return sprintf( __( 'Sent %1$d of %2$d to review. Nothing was applied yet.', 'ai-product-tools' ), $staged, $ran ); } if ( 0 === $staged ) { /* translators: 1: number of products applied, 2: number of products that ran. */ $message = sprintf( __( 'Applied %1$d of %2$d.', 'ai-product-tools' ), $applied, $ran ); } else { /* translators: 1: number of products applied, 2: number of products sent to review, 3: number of products that ran. */ $message = sprintf( __( 'Applied %1$d and sent %2$d to review, out of %3$d.', 'ai-product-tools' ), $applied, $staged, $ran ); } if ( $snapshots > 0 ) { $message .= ' ' . __( 'Previous versions are saved in History.', 'ai-product-tools' ); } return $message; } }