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:
2026-09-26 01:22:34 +02:00
co-authored by Claude Opus 5.5
parent d831cf59ca
commit e57b53d0e5
15 changed files with 231 additions and 191 deletions
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Revisions;
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\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\RevisionPoint;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
@@ -124,32 +124,16 @@ final class RevisionRestoreAbility implements Ability
}
// The restore is saved – failures from here on are logged and reported as warnings only.
$warnings = [];
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Wiederherstellen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId);
$updated = $updated instanceof WP_Post ? $updated : $post;
$change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Wiederherstellen');
$updated = $change->reread($post) ?? $post;
$summary = sprintf('"%s" auf Stand vom %s zurückgesetzt', $updated->post_title, $revision->post_date);
$postType = (string) $post->post_type;
$logId = FollowUp::run(
fn () => $this->log->record($this->name(), $postType, (string) $postId, $summary, $backupId),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$change->log($postType, $summary, $backupId);
return ToolResult::ok($summary . sprintf(' (Status unverändert: %s).', $updated->post_status))
return $change->result($summary . sprintf(' (Status unverändert: %s).', $updated->post_status), $updated)
->changed((string) $postId, $postType, 'restored', (string) $updated->post_title)
->rev(Rev::ofPost($updated))
->revisionId($backupId)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray();
}