service = $service ?? new TaxonomyGeneratorService(); } /** * @inheritDoc */ public function apply( array $outputs, array $context ): array { $term_id = (int) ( $context['termId'] ?? 0 ); $taxonomy = (string) ( $context['taxonomy'] ?? '' ); $overwrite = (string) ( $context['overwrite'] ?? 'replace' ); $content = isset( $outputs['termDescription'] ) ? trim( (string) $outputs['termDescription'] ) : ''; if ( '' === $content ) { return array( 'status' => 'skipped', 'fields' => array(), 'message' => __( 'No category description was generated.', 'ai-product-tools' ) ); } $term = function_exists( 'get_term' ) ? get_term( $term_id, $taxonomy ) : null; $current = ( $term instanceof \WP_Term ) ? (string) $term->description : ''; if ( 'empty_only' === $overwrite && '' !== trim( wp_strip_all_tags( $current ) ) ) { return array( 'status' => 'skipped', 'fields' => array( array( 'field' => 'termDescription', 'outcome' => 'skipped_not_empty' ) ), 'message' => __( 'The category description is not empty.', 'ai-product-tools' ), ); } $results = $this->service->applyDescriptions( array( array( 'term_id' => $term_id, 'taxonomy' => $taxonomy, 'content' => $content, 'writeMode' => 'overwrite', 'blueprint' => $this->blueprintFromContext( $context ), ), ) ); $ok = ! empty( $results[0]['success'] ); return array( 'status' => $ok ? 'applied' : 'skipped', 'fields' => array( array( 'field' => 'termDescription', 'outcome' => $ok ? 'written' : 'failed' ) ), 'message' => $ok ? __( 'Category description applied.', 'ai-product-tools' ) : (string) ( $results[0]['message'] ?? __( 'The write did not complete.', 'ai-product-tools' ) ), ); } /** * Build the {id,name,revision,builtin} attribution shape from the * ResultApplier context, or null when the context carries no blueprint * (a custom-prompts write) — mirrors DescriptionApplyAdapter's identical * helper. The context is server-trusted (ResultApplier's own resolved * blueprint), never client-echoed, so no sanitizer call is needed here. * * @param array $context Apply context from ResultApplier. * @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'] ), ); } }