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,11 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Content;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Abilities\WriteFailed; use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use Throwable; use Throwable;
@@ -136,29 +135,15 @@ final class ContentCreateAbility implements Ability
private function result(int $postId, string $postType, array $input, array $warnings): array 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. // The draft is saved – failures from here on are logged and reported as warnings only.
FollowUp::run( $change = (new PostChangeResult($this->name(), $this->log, $postId, 'der Entwurf', $warnings))
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input), ->afterSave($input);
$this->name() . ' lit_mcp_after_save', $post = $change->reread();
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; der Entwurf ist gespeichert.',
$warnings
);
$post = get_post($postId);
$title = $post instanceof WP_Post ? (string) $post->post_title : (string) $input['title']; $title = $post instanceof WP_Post ? (string) $post->post_title : (string) $input['title'];
$summary = sprintf('Entwurf "%s" angelegt', $title); $change->log($postType, 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
);
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) ->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')) ->urls((string) get_preview_post_link($postId), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -6,11 +6,11 @@ namespace LIT\MCP\Integrations\WordPress\Content;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev; use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Abilities\WriteFailed; use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\RevisionPoint; use LIT\MCP\Integrations\WordPress\RevisionPoint;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
@@ -178,31 +178,17 @@ final class ContentUpdateAbility implements Ability
$postType = (string) $post->post_type; $postType = (string) $post->post_type;
// The change is saved – failures from here on are logged and reported as warnings only. // The change is saved – failures from here on are logged and reported as warnings only.
FollowUp::run( $change = (new PostChangeResult($this->name(), $this->log, $postId, 'die Änderung', $warnings))
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input), ->afterSave($input);
$this->name() . ' lit_mcp_after_save', $updated = $change->reread($post) ?? $post;
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId);
$updated = $updated instanceof WP_Post ? $updated : $post;
$changedKeys = array_keys(array_intersect_key($input, array_flip(self::CHANGE_KEYS))); $changedKeys = array_keys(array_intersect_key($input, array_flip(self::CHANGE_KEYS)));
$summary = sprintf('"%s" geändert: %s', $updated->post_title, implode(', ', $changedKeys)); $summary = sprintf('"%s" geändert: %s', $updated->post_title, implode(', ', $changedKeys));
$logId = FollowUp::run( $change->log($postType, $summary, $revisionId);
fn () => $this->log->record($this->name(), $postType, (string) $postId, $summary, $revisionId),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
return ToolResult::ok(sprintf('%s (Status unverändert: %s).', $summary, $updated->post_status)) return $change->result(sprintf('%s (Status unverändert: %s).', $summary, $updated->post_status), $updated)
->changed((string) $postId, $postType, 'updated', (string) $updated->post_title) ->changed((string) $postId, $postType, 'updated', (string) $updated->post_title)
->rev(Rev::ofPost($updated))
->revisionId($revisionId) ->revisionId($revisionId)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw')) ->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\WordPress;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog;
use WP_Post;
/**
* What every post tool does once its write is saved: fire lit_mcp_after_save, re-read the
* post, write the change log and build the ToolResult with the new rev. From here on a
* failure must not turn the saved change into an error – listener and log failures are
* logged and reported as warnings ("… ist gespeichert").
*/
final class PostChangeResult
{
/** @var list<string> */
private array $warnings;
private ?int $logId = null;
/**
* @param string $saved What is saved, for the warnings, e.g. "die Änderung", "der Entwurf".
* @param list<string> $warnings Warnings collected before the write.
*/
public function __construct(
private string $ability,
private ChangeLog $log,
private int $postId,
private string $saved = 'die Änderung',
array $warnings = []
) {
$this->warnings = $warnings;
}
/**
* @param array<string, mixed> $input
* @param string $after The step just done, e.g. "Speichern", "Veröffentlichen".
*/
public function afterSave(array $input, string $after = 'Speichern'): self
{
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $this->postId, $this->ability, $input),
$this->ability . ' lit_mcp_after_save',
sprintf(
'Eine Erweiterung ist nach dem %s auf einen Fehler gestoßen; %s ist gespeichert.',
$after,
$this->saved
),
$this->warnings
);
return $this;
}
/** The saved post as stored now; $fallback when it cannot be read. */
public function reread(?WP_Post $fallback = null): ?WP_Post
{
$post = get_post($this->postId);
return $post instanceof WP_Post ? $post : $fallback;
}
public function log(string $objectType, string $summary, ?int $revisionId = null): self
{
$logId = FollowUp::run(
fn () => $this->log->record($this->ability, $objectType, (string) $this->postId, $summary, $revisionId),
$this->ability . ' change log',
sprintf('Änderungsprotokoll konnte nicht geschrieben werden; %s ist gespeichert.', $this->saved),
$this->warnings
);
$this->logId = is_int($logId) ? $logId : null;
return $this;
}
/** Result with the rev of $post, the log id and all warnings; callers add changed, urls, … */
public function result(string $summary, ?WP_Post $post): ToolResult
{
return ToolResult::ok($summary)
->rev($post instanceof WP_Post ? Rev::ofPost($post) : null)
->logId($this->logId)
->warnings($this->warnings);
}
}
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Revisions;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev; use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\RevisionPoint; use LIT\MCP\Integrations\WordPress\RevisionPoint;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; 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. // The restore is saved – failures from here on are logged and reported as warnings only.
$warnings = []; $change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Wiederherstellen');
FollowUp::run( $updated = $change->reread($post) ?? $post;
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;
$summary = sprintf('"%s" auf Stand vom %s zurückgesetzt', $updated->post_title, $revision->post_date); $summary = sprintf('"%s" auf Stand vom %s zurückgesetzt', $updated->post_title, $revision->post_date);
$postType = (string) $post->post_type; $postType = (string) $post->post_type;
$logId = FollowUp::run( $change->log($postType, $summary, $backupId);
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
);
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) ->changed((string) $postId, $postType, 'restored', (string) $updated->post_title)
->rev(Rev::ofPost($updated))
->revisionId($backupId) ->revisionId($backupId)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw')) ->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -5,10 +5,9 @@ declare(strict_types=1);
namespace LIT\MCP\Integrations\WordPress\Status; namespace LIT\MCP\Integrations\WordPress\Status;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use WP_Error; use WP_Error;
@@ -88,31 +87,17 @@ final class PostDuplicateAbility implements Ability
$copyId = $this->cloner->duplicate($original); $copyId = $this->cloner->duplicate($original);
// The copy is saved – failures from here on are logged and reported as warnings only. // The copy is saved – failures from here on are logged and reported as warnings only.
$warnings = []; $change = (new PostChangeResult($this->name(), $this->log, $copyId, 'die Kopie'))
FollowUp::run( ->afterSave($input, 'Anlegen der Kopie');
fn () => do_action('lit_mcp_after_save', $copyId, $this->name(), $input), $copy = $change->reread();
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Anlegen der Kopie auf einen Fehler gestoßen; die Kopie ist gespeichert.',
$warnings
);
$copy = get_post($copyId);
$copyTitle = $copy instanceof WP_Post ? (string) $copy->post_title : ''; $copyTitle = $copy instanceof WP_Post ? (string) $copy->post_title : '';
$postType = (string) $original->post_type; $postType = (string) $original->post_type;
$summary = sprintf('Kopie von "%s" (ID %d) angelegt', $original->post_title, $original->ID); $summary = sprintf('Kopie von "%s" (ID %d) angelegt', $original->post_title, $original->ID);
$logId = FollowUp::run( $change->log($postType, $summary);
fn () => $this->log->record($this->name(), $postType, (string) $copyId, $summary),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Kopie ist gespeichert.',
$warnings
);
return ToolResult::ok(sprintf('%s: Entwurf ID %d.', $summary, $copyId)) return $change->result(sprintf('%s: Entwurf ID %d.', $summary, $copyId), $copy)
->changed((string) $copyId, $postType, 'created', $copyTitle) ->changed((string) $copyId, $postType, 'created', $copyTitle)
->rev($copy instanceof WP_Post ? Rev::ofPost($copy) : null)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($copyId), (string) get_edit_post_link($copyId, 'raw')) ->urls((string) get_preview_post_link($copyId), (string) get_edit_post_link($copyId, 'raw'))
->warnings($warnings)
->with('original_id', (int) $original->ID) ->with('original_id', (int) $original->ID)
->toArray(); ->toArray();
} }
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Status;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev; use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use WP_Error; use WP_Error;
@@ -114,31 +114,12 @@ final class PostPublishAbility implements Ability
} }
// The status change is saved – failures from here on are logged and reported as warnings only. // The status change is saved – failures from here on are logged and reported as warnings only.
$warnings = []; $change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Veröffentlichen');
FollowUp::run( $change->log((string) $post->post_type, sprintf('"%s" veröffentlicht', $title));
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Veröffentlichen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$summary = sprintf('"%s" veröffentlicht', $title); return $change->result(sprintf('"%s" ist jetzt veröffentlicht.', $title), $change->reread($post))
$logId = FollowUp::run(
fn () => $this->log->record($this->name(), (string) $post->post_type, (string) $postId, $summary),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId);
$updated = $updated instanceof WP_Post ? $updated : $post;
return ToolResult::ok(sprintf('"%s" ist jetzt veröffentlicht.', $title))
->changed((string) $postId, (string) $post->post_type, 'published', $title) ->changed((string) $postId, (string) $post->post_type, 'published', $title)
->rev(Rev::ofPost($updated))
->logId(is_int($logId) ? $logId : null)
->urls((string) get_permalink($postId), $editUrl) ->urls((string) get_permalink($postId), $editUrl)
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -11,6 +11,7 @@ use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\Abilities\WriteFailed; use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\RevisionPoint; use LIT\MCP\Integrations\WordPress\RevisionPoint;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
@@ -221,24 +222,11 @@ final class PostReplaceWithCopyAbility implements Ability
$warnings[] = $trashWarning; $warnings[] = $trashWarning;
} }
FollowUp::run( $change = (new PostChangeResult($this->name(), $this->log, $originalId, 'die Änderung', $warnings))
fn () => do_action('lit_mcp_after_save', $originalId, $this->name(), $input), ->afterSave($input);
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$title = PostCloner::withoutSuffix((string) $copy->post_title); $title = PostCloner::withoutSuffix((string) $copy->post_title);
$summary = sprintf('Kopie ID %d in "%s" (ID %d) übernommen', $copyId, $title, $originalId); $summary = sprintf('Kopie ID %d in "%s" (ID %d) übernommen', $copyId, $title, $originalId);
$logId = FollowUp::run( $change->log($postType, $summary, $revisionId);
fn () => $this->log->record($this->name(), $postType, (string) $originalId, $summary, $revisionId),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($originalId);
$updated = $updated instanceof WP_Post ? $updated : $original;
$statusNote = sprintf( $statusNote = sprintf(
'; Status bleibt %s, %s, der vorherige Stand liegt als Sicherung ID %d im Papierkorb.', '; Status bleibt %s, %s, der vorherige Stand liegt als Sicherung ID %d im Papierkorb.',
$original->post_status, $original->post_status,
@@ -246,18 +234,15 @@ final class PostReplaceWithCopyAbility implements Ability
$backupId $backupId
); );
$result = ToolResult::ok($summary . $statusNote) $result = $change->result($summary . $statusNote, $change->reread($original))
->changed((string) $originalId, $postType, 'replaced', $title); ->changed((string) $originalId, $postType, 'replaced', $title);
if ($trashed) { if ($trashed) {
$result->changed((string) $copyId, $postType, 'trashed', (string) $copy->post_title); $result->changed((string) $copyId, $postType, 'trashed', (string) $copy->post_title);
} }
return $result return $result
->rev(Rev::ofPost($updated))
->revisionId($revisionId) ->revisionId($revisionId)
->logId(is_int($logId) ? $logId : null)
->urls((string) get_permalink($originalId), (string) get_edit_post_link($originalId, 'raw')) ->urls((string) get_permalink($originalId), (string) get_edit_post_link($originalId, 'raw'))
->warnings($warnings)
->with('backup_id', $backupId) ->with('backup_id', $backupId)
->toArray(); ->toArray();
} }
@@ -8,10 +8,9 @@ use DateTimeImmutable;
use DateTimeZone; use DateTimeZone;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use WP_Error; use WP_Error;
@@ -128,33 +127,16 @@ final class PostScheduleAbility implements Ability
} }
// The status change is saved – failures from here on are logged and reported as warnings only. // The status change is saved – failures from here on are logged and reported as warnings only.
$warnings = []; $change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Planen');
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Planen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$title = (string) $post->post_title; $title = (string) $post->post_title;
$summary = sprintf('"%s" geplant für %s', $title, $raw) $summary = sprintf('"%s" geplant für %s', $title, $raw)
. ($post->post_status === 'publish' ? ' (war veröffentlicht und ist bis dahin offline)' : ''); . ($post->post_status === 'publish' ? ' (war veröffentlicht und ist bis dahin offline)' : '');
$logId = FollowUp::run( $change->log((string) $post->post_type, $summary);
fn () => $this->log->record($this->name(), (string) $post->post_type, (string) $postId, $summary), $updated = $change->reread($post) ?? $post;
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId); return $change->result($summary . '.', $updated)
$updated = $updated instanceof WP_Post ? $updated : $post;
return ToolResult::ok($summary . '.')
->changed((string) $postId, (string) $post->post_type, 'scheduled', $title) ->changed((string) $postId, (string) $post->post_type, 'scheduled', $title)
->rev(Rev::ofPost($updated))
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw')) ->urls((string) get_preview_post_link($updated), (string) get_edit_post_link($postId, 'raw'))
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Status;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev; use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use WP_Error; use WP_Error;
@@ -121,23 +121,13 @@ final class PostTrashAbility implements Ability
} }
// The change is saved – a failure of the change log from here on is a warning only. // The change is saved – a failure of the change log from here on is a warning only.
$summary = sprintf('"%s" in den Papierkorb gelegt', $title); $change = (new PostChangeResult($this->name(), $this->log, $postId))
$warnings = []; ->log((string) $post->post_type, sprintf('"%s" in den Papierkorb gelegt', $title));
$logId = FollowUp::run(
fn () => $this->log->record($this->name(), (string) $post->post_type, (string) $postId, $summary),
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId); $summary = sprintf('"%s" liegt im Papierkorb und ist dort wiederherstellbar.', $title);
$updated = $updated instanceof WP_Post ? $updated : $post;
return ToolResult::ok(sprintf('"%s" liegt im Papierkorb und ist dort wiederherstellbar.', $title)) return $change->result($summary, $change->reread($post))
->changed((string) $postId, (string) $post->post_type, 'trashed', $title) ->changed((string) $postId, (string) $post->post_type, 'trashed', $title)
->rev(Rev::ofPost($updated))
->logId(is_int($logId) ? $logId : null)
->warnings($warnings)
->toArray(); ->toArray();
} }
@@ -6,14 +6,13 @@ namespace LIT\MCP\Integrations\WordPress\Status;
use LIT\MCP\Abilities\Ability; use LIT\MCP\Abilities\Ability;
use LIT\MCP\Abilities\Errors; use LIT\MCP\Abilities\Errors;
use LIT\MCP\Abilities\FollowUp;
use LIT\MCP\Abilities\Rev; use LIT\MCP\Abilities\Rev;
use LIT\MCP\Abilities\ToolResult; use LIT\MCP\Abilities\ToolResult;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Integrations\WordPress\PostGuard; use LIT\MCP\Integrations\WordPress\PostGuard;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema; use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use WP_Error; use WP_Error;
use WP_Post;
/** /**
* lit/post-unpublish: withdraws a published, scheduled or private entry back to draft. * lit/post-unpublish: withdraws a published, scheduled or private entry back to draft.
@@ -98,31 +97,14 @@ final class PostUnpublishAbility implements Ability
} }
// The status change is saved – failures from here on are logged and reported as warnings only. // The status change is saved – failures from here on are logged and reported as warnings only.
$warnings = []; $change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Zurückziehen');
FollowUp::run(
fn () => do_action('lit_mcp_after_save', $postId, $this->name(), $input),
$this->name() . ' lit_mcp_after_save',
'Eine Erweiterung ist nach dem Zurückziehen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
$warnings
);
$summary = sprintf('"%s" zurückgezogen (vorher %s, jetzt Entwurf)', $title, $post->post_status); $summary = sprintf('"%s" zurückgezogen (vorher %s, jetzt Entwurf)', $title, $post->post_status);
$logId = FollowUp::run( $change->log((string) $post->post_type, $summary);
fn () => $this->log->record($this->name(), (string) $post->post_type, (string) $postId, $summary), $updated = $change->reread($post) ?? $post;
$this->name() . ' change log',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
$warnings
);
$updated = get_post($postId); return $change->result($summary . '.', $updated)
$updated = $updated instanceof WP_Post ? $updated : $post;
return ToolResult::ok($summary . '.')
->changed((string) $postId, (string) $post->post_type, 'unpublished', $title) ->changed((string) $postId, (string) $post->post_type, 'unpublished', $title)
->rev(Rev::ofPost($updated))
->logId(is_int($logId) ? $logId : null)
->urls((string) get_preview_post_link($updated), $editUrl) ->urls((string) get_preview_post_link($updated), $editUrl)
->warnings($warnings)
->toArray(); ->toArray();
} }
} }
@@ -96,7 +96,7 @@ final class ContentCreateAbilityTest extends TestCase
Actions\expectDone('lit_mcp_after_save')->once()->with(42, 'lit/content-create', $input); Actions\expectDone('lit_mcp_after_save')->once()->with(42, 'lit/content-create', $input);
$post = $this->stubResultLinks(); $post = $this->stubResultLinks();
$log = Mockery::mock(ChangeLog::class); $log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/content-create', 'lit_person', '42', 'Entwurf "Anna" angelegt')->andReturn(7); $log->expects('record')->with('lit/content-create', 'lit_person', '42', 'Entwurf "Anna" angelegt', null)->andReturn(7);
$result = (new ContentCreateAbility($schema, $writer, $log))->execute($input); $result = (new ContentCreateAbility($schema, $writer, $log))->execute($input);
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress;
use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\Rev;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\WordPress\PostChangeResult;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
use WP_Post;
final class PostChangeResultTest extends TestCase
{
private string $errorLog = '';
private string|false $previousLog = false;
protected function setUp(): void
{
parent::setUp();
$this->errorLog = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
$this->previousLog = ini_set('error_log', $this->errorLog);
}
protected function tearDown(): void
{
ini_set('error_log', (string) $this->previousLog);
unlink($this->errorLog);
parent::tearDown();
}
public function testRunsTheHookLogsAndBuildsTheResult(): void
{
$post = new WP_Post(['ID' => 5, 'post_title' => 'Team']);
Actions\expectDone('lit_mcp_after_save')->once()->with(5, 'lit/content-update', ['post_id' => 5]);
Functions\when('get_post')->justReturn($post);
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/content-update', 'page', '5', 'geändert', 811)->andReturn(9);
$change = new PostChangeResult('lit/content-update', $log, 5, 'die Änderung', ['vorher']);
$change->afterSave(['post_id' => 5]);
$updated = $change->reread();
$change->log('page', 'geändert', 811);
$result = $change->result('Fertig.', $updated)->toArray();
self::assertSame($post, $updated);
self::assertSame('Fertig.', $result['summary']);
self::assertSame(Rev::ofPost($post), $result['rev']);
self::assertSame(9, $result['log_id']);
self::assertSame(['vorher'], $result['warnings']);
}
public function testFailuresAfterTheWriteBecomeWarnings(): void
{
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
throw new RuntimeException('listener kaputt');
});
Functions\when('get_post')->justReturn(null);
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->andThrow(new RuntimeException('log kaputt'));
$fallback = new WP_Post(['ID' => 5]);
$change = new PostChangeResult('lit/post-publish', $log, 5);
$change->afterSave([], 'Veröffentlichen');
$updated = $change->reread($fallback);
$change->log('page', 'x');
$result = $change->result('ok', $updated)->toArray();
self::assertSame($fallback, $updated);
self::assertNull($result['log_id']);
self::assertSame([
'Eine Erweiterung ist nach dem Veröffentlichen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
], $result['warnings']);
self::assertStringContainsString('listener kaputt', (string) file_get_contents($this->errorLog));
}
public function testWithoutAReadablePostTheRevIsNull(): void
{
Functions\when('get_post')->justReturn(null);
$change = new PostChangeResult('lit/content-create', Mockery::mock(ChangeLog::class), 5, 'der Entwurf');
self::assertNull($change->reread());
self::assertNull($change->result('ok', null)->toArray()['rev']);
}
}
@@ -46,7 +46,7 @@ final class PostDuplicateAbilityTest extends TestCase
$cloner->expects('duplicate')->with($this->original)->andReturn(77); $cloner->expects('duplicate')->with($this->original)->andReturn(77);
Actions\expectDone('lit_mcp_after_save')->once()->with(77, 'lit/post-duplicate', ['post_id' => 5]); Actions\expectDone('lit_mcp_after_save')->once()->with(77, 'lit/post-duplicate', ['post_id' => 5]);
$log = Mockery::mock(ChangeLog::class); $log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/post-duplicate', 'page', '77', 'Kopie von "Team" (ID 5) angelegt')->andReturn(8); $log->expects('record')->with('lit/post-duplicate', 'page', '77', 'Kopie von "Team" (ID 5) angelegt', null)->andReturn(8);
$result = $this->ability($cloner, $log)->execute(['post_id' => 5]); $result = $this->ability($cloner, $log)->execute(['post_id' => 5]);
@@ -36,7 +36,7 @@ final class PostPublishAbilityTest extends TestCase
Functions\expect('wp_update_post')->once()->with(['ID' => 5, 'post_status' => 'publish'], true)->andReturn(5); Functions\expect('wp_update_post')->once()->with(['ID' => 5, 'post_status' => 'publish'], true)->andReturn(5);
Functions\when('get_post_status')->justReturn('publish'); Functions\when('get_post_status')->justReturn('publish');
$log = Mockery::mock(ChangeLog::class); $log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/post-publish', 'page', '5', '"Team" veröffentlicht')->andReturn(9); $log->expects('record')->with('lit/post-publish', 'page', '5', '"Team" veröffentlicht', null)->andReturn(9);
$result = $this->ability($log)->execute(['post_id' => 5]); $result = $this->ability($log)->execute(['post_id' => 5]);
@@ -38,7 +38,7 @@ final class PostTrashAbilityTest extends TestCase
Functions\expect('wp_trash_post')->once()->with(5)->andReturn(new WP_Post(['ID' => 5])); Functions\expect('wp_trash_post')->once()->with(5)->andReturn(new WP_Post(['ID' => 5]));
Functions\expect('wp_delete_post')->never(); Functions\expect('wp_delete_post')->never();
$log = Mockery::mock(ChangeLog::class); $log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/post-trash', 'page', '5', '"Alt" in den Papierkorb gelegt')->andReturn(2); $log->expects('record')->with('lit/post-trash', 'page', '5', '"Alt" in den Papierkorb gelegt', null)->andReturn(2);
$result = $this->ability($log)->execute(['post_id' => 5, 'confirm' => true]); $result = $this->ability($log)->execute(['post_id' => 5, 'confirm' => true]);