generator = $generator ?? new ContentGenerator(); } /** * @inheritDoc */ public function apply( array $outputs, array $context ): array { $product_id = (int) ( $context['productId'] ?? 0 ); $overwrite = (string) ( $context['overwrite'] ?? 'replace' ); $product = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false; if ( ! ( $product instanceof \WC_Product ) ) { return array( 'status' => 'skipped', 'fields' => array(), 'message' => __( 'The product could not be found.', 'ai-product-tools' ) ); } $fields = array(); $text_content = array(); $tags_content = array(); $messages = array(); $this->planField( $fields, $text_content, 'description', 'description', $outputs, (string) $product->get_description(), $overwrite ); $this->planField( $fields, $text_content, 'shortDescription', 'short_description', $outputs, (string) $product->get_short_description(), $overwrite ); $this->planTags( $fields, $tags_content, $messages, $outputs, $product_id, $overwrite ); if ( empty( $text_content ) && empty( $tags_content ) ) { $message = ! empty( $messages ) ? implode( ' ', $messages ) : __( 'Nothing to apply.', 'ai-product-tools' ); return array( 'status' => 'skipped', 'fields' => $fields, 'message' => $message ); } $options = array( 'keepExistingDescription' => false, 'keepExistingTags' => false, 'executionPath' => 'blueprint', ); $blueprint = $this->blueprintFromContext( $context ); $text_ok = empty( $text_content ) ? null : (bool) $this->generator->applyContent( $product_id, $text_content, $options, $blueprint ); $tags_ok = empty( $tags_content ) ? null : (bool) $this->generator->applyContent( $product_id, $tags_content, $options, $blueprint ); if ( false === $text_ok ) { $fields = $this->markFieldsFailed( $fields, array( 'description', 'shortDescription' ) ); } if ( false === $tags_ok ) { $fields = $this->markFieldsFailed( $fields, array( 'tags' ) ); } if ( false === $text_ok || false === $tags_ok ) { $message = ( true === $text_ok || true === $tags_ok ) ? __( 'Some content 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 ); } $message = __( 'Description applied.', 'ai-product-tools' ); if ( ! empty( $messages ) ) { $message .= ' ' . implode( ' ', $messages ); } return array( 'status' => 'applied', 'fields' => $fields, 'message' => $message ); } /** * @param array &$fields Fields accumulator, updated in place. * @param array &$content applyContent() content map, updated in place. * @param string $output_key `$outputs` key. * @param string $content_key `applyContent()`'s content key. * @param array $outputs GenerationResult.outputs. * @param string $current_value Live current value. * @param string $overwrite 'replace' | 'empty_only'. * @return void */ private function planField( array &$fields, array &$content, string $output_key, string $content_key, array $outputs, string $current_value, string $overwrite ): void { $value = isset( $outputs[ $output_key ] ) ? (string) $outputs[ $output_key ] : ''; if ( '' === trim( $value ) ) { return; } if ( 'empty_only' === $overwrite && '' !== trim( wp_strip_all_tags( $current_value ) ) ) { $fields[] = array( 'field' => $output_key, 'outcome' => 'skipped_not_empty' ); return; } $content[ $content_key ] = $value; $fields[] = array( 'field' => $output_key, 'outcome' => 'written' ); } /** * @param array &$fields Fields accumulator, updated in place. * @param array &$content applyContent() content map, updated in place. * @param array &$messages Human-readable notices accumulator, updated in place. * @param array $outputs GenerationResult.outputs. * @param int $product_id Product id. * @param string $overwrite 'replace' | 'empty_only'. * @return void */ private function planTags( array &$fields, array &$content, array &$messages, array $outputs, int $product_id, string $overwrite ): void { $tags = isset( $outputs['tags'] ) && is_array( $outputs['tags'] ) ? array_values( array_filter( array_map( 'strval', $outputs['tags'] ) ) ) : array(); if ( empty( $tags ) ) { return; } if ( 'empty_only' === $overwrite ) { $existing = function_exists( 'wp_get_post_terms' ) ? wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'ids' ) ) : array(); if ( is_array( $existing ) && ! empty( $existing ) ) { $fields[] = array( 'field' => 'tags', 'outcome' => 'skipped_not_empty' ); return; } } $surviving = ResponseParser::parseTags( implode( ', ', $tags ), PHP_INT_MAX ); $dropped = array_values( array_diff( array_map( 'trim', $tags ), $surviving ) ); if ( empty( $surviving ) ) { $fields[] = array( 'field' => 'tags', 'outcome' => 'skipped_invalid' ); $messages[] = self::tagsRejectedMessage( $dropped ); return; } $content['tags'] = implode( ', ', $surviving ); $fields[] = array( 'field' => 'tags', 'outcome' => 'written' ); if ( ! empty( $dropped ) ) { $messages[] = self::tagsRejectedMessage( $dropped ); } } /** * @param array $dropped The input tags the legacy parser rejected. * @return string */ private static function tagsRejectedMessage( array $dropped ): string { return sprintf( /* translators: %s: comma-separated list of the tags that could not be applied. */ __( 'Tags cannot contain / ( ) [ ] { } < > or a backslash, so these were not applied: %s', 'ai-product-tools' ), implode( ', ', $dropped ) ); } /** * The `{id,name,revision,builtin}` blueprint shape (matching * `BlueprintIntegration::responseFields()`'s `blueprint` key) from an * apply `$context` ({@see ResultApplier}'s `blueprintId`/`blueprintName`/ * `blueprintRevision`/`blueprintBuiltin`), for `Desc\ContentGenerator::applyContent()`'s * optional blueprint parameter. Null for a custom-prompts context, which * carries none of these keys — applyContent() then records nothing. * * @param array $context Apply context (see ApplyAdapterInterface). * @return array{id:mixed,name:string,revision:mixed,builtin:bool}|null */ private function blueprintFromContext( array $context ): ?array { if ( ! isset( $context['blueprintId'] ) && ! isset( $context['blueprintName'] ) && ! isset( $context['blueprintRevision'] ) && ! isset( $context['blueprintBuiltin'] ) ) { return null; } return array( 'id' => $context['blueprintId'] ?? null, 'name' => (string) ( $context['blueprintName'] ?? '' ), 'revision' => $context['blueprintRevision'] ?? null, 'builtin' => ! empty( $context['blueprintBuiltin'] ), ); } /** * Flip the named fields' 'written' entry to 'failed' — used when ONE * applyContent() call covering only those fields did not land, without * touching a sibling group whose own call already succeeded. * * @param array $fields Field entries. * @param array $field_names Field names this call covered. * @return array */ private function markFieldsFailed( array $fields, array $field_names ): array { foreach ( $fields as $index => $field ) { if ( in_array( $field['field'] ?? '', $field_names, true ) && 'written' === ( $field['outcome'] ?? '' ) ) { $fields[ $index ]['outcome'] = 'failed'; } } return $fields; } }