$snapshot_fields The fields about to change, keyed by the names ContentApplyDispatcher::before() takes (`description`, `short_desc`, ...). Empty only with `finish_per_field`, when no field the write changes has a snapshot (an SEO focus keyword alone): the write still runs, and nothing is staged or finished. Finished all or nothing, an empty set is refused: no field could report that the write landed. * @param callable $write Writes the fields. Returns array: for each key of $snapshot_fields, whether that field landed. A key it leaves out did not land. With `finish_per_field` it returns the outcome of each staged field type instead. * @param array $options `marks`: array of content type to generator source, marked once the write landed (a short description is marked as `description`). * `mark_meta`: array more metadata of every mark in `marks`, as mark() takes it (an automation row's `job_id`). * `attribution`: array staged with the fields but not a field, so no landed flag is asked for it (the `blueprint_*` keys of the content template that produced the content). * `leave_staged_on_throw`: true to leave what was staged for the caller when the stage or the write throws, instead of rolling it back (ContentReviewQueue::approve() commits it, because the write may have landed). * `bumps`: int[] of post ids whose Product Editor edit token is bumped, in order, after the marks once the write landed (a meta write never moves post_modified, so an open grid row would keep a valid token). * `finish_per_field`: true to commit or roll back each staged field type on its own outcome, instead of all or nothing. The write then returns those outcomes keyed by staged field type (`seo_meta_title`, `seo_meta_description`, ...) as SnapshotStore::finalizeStagedByField() reads them: a staged type it leaves out is rolled back. The caller makes its own marks and bumps (with mark() and bump()), so `marks` and `bumps` are refused with it. * @return array For each key of $snapshot_fields, whether it landed (a bool); with `finish_per_field`, what the write returned. * @throws \InvalidArgumentException When `finish_per_field` comes with `marks` or `bumps`, or a write finished all or nothing names no snapshot field; before anything is staged or written. * @throws \Throwable What the stage or the write threw, after what was staged was rolled back (or left staged, see `leave_staged_on_throw`); what the commit threw, with nothing rolled back. */ public static function apply( int $product_id, string $source, array $snapshot_fields, callable $write, array $options = array() ): array { $per_field = ! empty( $options['finish_per_field'] ); if ( $per_field && ( ! empty( $options['marks'] ) || ! empty( $options['bumps'] ) ) ) { throw new \InvalidArgumentException( 'A product field write finished per field makes no marks and no bumps: its caller makes them.' ); } if ( ! $per_field && array() === $snapshot_fields ) { throw new \InvalidArgumentException( 'A product field write finished all or nothing needs a snapshot field to report on.' ); } $staged = array() !== $snapshot_fields; try { if ( $staged ) { ContentApplyDispatcher::before( $product_id, $snapshot_fields + (array) ( $options['attribution'] ?? array() ), $source ); } $reported = $write(); if ( ! is_array( $reported ) ) { throw new \UnexpectedValueException( 'A product field write must return an array of landed flags.' ); } } catch ( \Throwable $e ) { if ( $staged && empty( $options['leave_staged_on_throw'] ) ) { ContentApplyDispatcher::after( $product_id, false ); } throw $e; } if ( $per_field ) { if ( $staged ) { SnapshotStore::finalizeStagedByField( $product_id, $reported ); } return $reported; } $landed = array(); foreach ( array_keys( $snapshot_fields ) as $field ) { $landed[ $field ] = true === ( $reported[ $field ] ?? false ); } $all_landed = ! in_array( false, $landed, true ); ContentApplyDispatcher::after( $product_id, $all_landed ); if ( $all_landed ) { foreach ( (array) ( $options['marks'] ?? array() ) as $type => $generator ) { self::mark( $product_id, (string) $type, (string) $generator, (array) ( $options['mark_meta'] ?? array() ) ); } foreach ( (array) ( $options['bumps'] ?? array() ) as $post_id ) { self::bump( $product_id, (int) $post_id ); } } return $landed; } /** * Bump the edit token of a post a landed write changed. The write landed, * so a bump that throws is logged and nothing else: it neither undoes the * write nor changes how its snapshot is finished. A caller that finishes * per field calls this itself, after apply() returned or inside its write * right after one field landed (an attachment's token, the description * generator's tags). * * @param int $product_id Product id of the write. * @param int $post_id Post whose token is bumped: the product, or an attachment of it. * @return void */ public static function bump( int $product_id, int $post_id ): void { try { ProductEditorConcurrencyToken::bump_revision( $post_id ); } catch ( \Throwable $e ) { Logger::error( 'generation', 'Edit token bump after an applied write failed', array( 'product_id' => $product_id, 'post_id' => $post_id, 'error' => $e->getMessage(), ) ); } } /** * Mark a write that landed as AI content. The write landed, so a mark that * cannot be stored is logged and nothing else. A caller that finishes per * field calls this itself after apply() returned, or inside its write right * after one field landed (the description generator's tags). * * @param int $product_id Product id. * @param string $type Content type of the mark. * @param string $generator Generator source of the mark. * @param array $meta More mark metadata after the source, as ContentMarkService::mark() takes it (`model`). * @return void */ public static function mark( int $product_id, string $type, string $generator, array $meta = array() ): void { try { $marked = ContentMarkService::mark( $product_id, $type, array( 'gen' => $generator ) + $meta ); } catch ( \Throwable $e ) { Logger::error( 'generation', 'AI mark of an applied write failed', array( 'product_id' => $product_id, 'type' => $type, 'error' => $e->getMessage(), ) ); return; } if ( ! $marked ) { Logger::error( 'generation', 'AI mark of an applied write was not stored', array( 'product_id' => $product_id, 'type' => $type, ) ); } } }