'skipped', 'fields' => array(), 'message' => __( 'No alt text was generated.', 'ai-product-tools' ) ); } $review_store = new ContentReviewStore(); $items = array(); $fields = array(); $refusal = null; $any_failed = false; foreach ( $alt_map as $attachment_id => $alt_text ) { $attachment_id = (int) $attachment_id; $alt_text = trim( (string) $alt_text ); $field = 'att_' . $attachment_id; if ( '' === $alt_text ) { continue; } if ( $review_store->hasActiveReview( $product_id, 'image_alt', 'image_alt', $attachment_id ) ) { $fields[] = array( 'field' => $field, 'outcome' => 'failed' ); $refusal = $refusal ?? __( 'This image is already pending review in the Content Review queue.', 'ai-product-tools' ); $any_failed = true; continue; } $current = function_exists( 'get_post_meta' ) ? (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) : ''; if ( 'empty_only' === $overwrite && '' !== trim( $current ) ) { $fields[] = array( 'field' => $field, 'outcome' => 'skipped_not_empty' ); continue; } if ( array_key_exists( (string) $attachment_id, $existing_alt ) && $current !== (string) $existing_alt[ (string) $attachment_id ] ) { $fields[] = array( 'field' => $field, 'outcome' => 'failed' ); $refusal = $refusal ?? __( "This image's alt text changed since this result was generated. Regenerate or apply manually.", 'ai-product-tools' ); $any_failed = true; continue; } $items[ $attachment_id ] = array( 'attachment_id' => $attachment_id, 'alt_text' => $alt_text ); $fields[] = array( 'field' => $field, 'outcome' => 'written' ); } if ( empty( $items ) ) { return array( 'status' => 'skipped', 'fields' => $fields, 'message' => $refusal ?? __( 'Nothing to apply.', 'ai-product-tools' ), ); } $row_base = array_merge( array( 'product_id' => $product_id ), $this->blueprintRowFields( $context ) ); $any_written = false; foreach ( $items as $attachment_id => $item ) { $row = array_merge( $row_base, array( 'generated_value' => wp_json_encode( array( $item ) ) ) ); $outcome = $this->callApplier( 'image_alt', $row ); if ( null !== $outcome['refusalMessage'] || ! $outcome['ok'] ) { $any_failed = true; $refusal = $refusal ?? $outcome['refusalMessage']; $fields = $this->markFieldsFailed( $fields, array( 'att_' . $attachment_id ) ); continue; } $any_written = true; } if ( $any_failed ) { $message = $refusal ?? ( $any_written ? __( 'Some alt text was applied; the rest could not be written.', 'ai-product-tools' ) : __( 'The write did not complete.', 'ai-product-tools' ) ); return array( 'status' => 'skipped', 'fields' => $fields, 'message' => $message ); } return array( 'status' => 'applied', 'fields' => $fields, 'message' => __( 'Image alt text applied.', 'ai-product-tools' ) ); } }