refactor: PostChangeResult for the after-save block of the post tools
after_save hook, re-read, change log and ToolResult (rev, log_id, warnings) were repeated in nine post abilities; they now share one helper. Behaviour and warning texts are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -6,11 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Content;
|
||||
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\FollowUp;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\ToolResult;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\PostChangeResult;
|
||||
use LIT\MCP\Integrations\WordPress\PostGuard;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use Throwable;
|
||||
@@ -136,29 +135,15 @@ final class ContentCreateAbility implements Ability
|
||||
private function result(int $postId, string $postType, array $input, array $warnings): array
|
||||
{
|
||||
// The draft is saved – failures from here on are logged and reported as warnings only.
|
||||
FollowUp::run(
|
||||
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
|
||||
$this->name() . ' lit_mcp_after_save',
|
||||
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; der Entwurf ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
|
||||
$post = get_post($postId);
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $postId, 'der Entwurf', $warnings))
|
||||
->afterSave($input);
|
||||
$post = $change->reread();
|
||||
$title = $post instanceof WP_Post ? (string) $post->post_title : (string) $input['title'];
|
||||
$summary = sprintf('Entwurf "%s" angelegt', $title);
|
||||
$logId = FollowUp::run(
|
||||
fn () => $this->log->record($this->name(), $postType, (string) $postId, $summary),
|
||||
$this->name() . ' change log',
|
||||
'Änderungsprotokoll konnte nicht geschrieben werden; der Entwurf ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
$change->log($postType, sprintf('Entwurf "%s" angelegt', $title));
|
||||
|
||||
return ToolResult::ok(sprintf('"%s" als Entwurf angelegt (ID %d).', $title, $postId))
|
||||
return $change->result(sprintf('"%s" als Entwurf angelegt (ID %d).', $title, $postId), $post)
|
||||
->changed((string) $postId, $postType, 'created', $title)
|
||||
->rev($post instanceof WP_Post ? Rev::ofPost($post) : null)
|
||||
->logId(is_int($logId) ? $logId : null)
|
||||
->urls((string) get_preview_post_link($postId), (string) get_edit_post_link($postId, 'raw'))
|
||||
->warnings($warnings)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user