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:
@@ -5,10 +5,9 @@ declare(strict_types=1);
|
||||
namespace LIT\MCP\Integrations\WordPress\Status;
|
||||
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
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\Schema\ContentSchema;
|
||||
use WP_Error;
|
||||
@@ -88,31 +87,17 @@ final class PostDuplicateAbility implements Ability
|
||||
$copyId = $this->cloner->duplicate($original);
|
||||
|
||||
// The copy is saved – failures from here on are logged and reported as warnings only.
|
||||
$warnings = [];
|
||||
FollowUp::run(
|
||||
fn () => do_action('lit_mcp_after_save', $copyId, $this->name(), $input),
|
||||
$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);
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $copyId, 'die Kopie'))
|
||||
->afterSave($input, 'Anlegen der Kopie');
|
||||
$copy = $change->reread();
|
||||
$copyTitle = $copy instanceof WP_Post ? (string) $copy->post_title : '';
|
||||
$postType = (string) $original->post_type;
|
||||
$summary = sprintf('Kopie von "%s" (ID %d) angelegt', $original->post_title, $original->ID);
|
||||
$logId = FollowUp::run(
|
||||
fn () => $this->log->record($this->name(), $postType, (string) $copyId, $summary),
|
||||
$this->name() . ' change log',
|
||||
'Änderungsprotokoll konnte nicht geschrieben werden; die Kopie ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
$change->log($postType, $summary);
|
||||
|
||||
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)
|
||||
->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'))
|
||||
->warnings($warnings)
|
||||
->with('original_id', (int) $original->ID)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Status;
|
||||
|
||||
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\Schema\ContentSchema;
|
||||
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.
|
||||
$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 Veröffentlichen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Veröffentlichen');
|
||||
$change->log((string) $post->post_type, sprintf('"%s" veröffentlicht', $title));
|
||||
|
||||
$summary = sprintf('"%s" veröffentlicht', $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);
|
||||
$updated = $updated instanceof WP_Post ? $updated : $post;
|
||||
|
||||
return ToolResult::ok(sprintf('"%s" ist jetzt veröffentlicht.', $title))
|
||||
return $change->result(sprintf('"%s" ist jetzt veröffentlicht.', $title), $change->reread($post))
|
||||
->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)
|
||||
->warnings($warnings)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ 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\RevisionPoint;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
@@ -221,24 +222,11 @@ final class PostReplaceWithCopyAbility implements Ability
|
||||
$warnings[] = $trashWarning;
|
||||
}
|
||||
|
||||
FollowUp::run(
|
||||
fn () => do_action('lit_mcp_after_save', $originalId, $this->name(), $input),
|
||||
$this->name() . ' lit_mcp_after_save',
|
||||
'Eine Erweiterung ist nach dem Speichern auf einen Fehler gestoßen; die Änderung ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $originalId, 'die Änderung', $warnings))
|
||||
->afterSave($input);
|
||||
$title = PostCloner::withoutSuffix((string) $copy->post_title);
|
||||
$summary = sprintf('Kopie ID %d in "%s" (ID %d) übernommen', $copyId, $title, $originalId);
|
||||
$logId = FollowUp::run(
|
||||
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;
|
||||
$change->log($postType, $summary, $revisionId);
|
||||
$statusNote = sprintf(
|
||||
'; Status bleibt %s, %s, der vorherige Stand liegt als Sicherung ID %d im Papierkorb.',
|
||||
$original->post_status,
|
||||
@@ -246,18 +234,15 @@ final class PostReplaceWithCopyAbility implements Ability
|
||||
$backupId
|
||||
);
|
||||
|
||||
$result = ToolResult::ok($summary . $statusNote)
|
||||
$result = $change->result($summary . $statusNote, $change->reread($original))
|
||||
->changed((string) $originalId, $postType, 'replaced', $title);
|
||||
if ($trashed) {
|
||||
$result->changed((string) $copyId, $postType, 'trashed', (string) $copy->post_title);
|
||||
}
|
||||
|
||||
return $result
|
||||
->rev(Rev::ofPost($updated))
|
||||
->revisionId($revisionId)
|
||||
->logId(is_int($logId) ? $logId : null)
|
||||
->urls((string) get_permalink($originalId), (string) get_edit_post_link($originalId, 'raw'))
|
||||
->warnings($warnings)
|
||||
->with('backup_id', $backupId)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
@@ -8,10 +8,9 @@ use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
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\Schema\ContentSchema;
|
||||
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.
|
||||
$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 Planen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Planen');
|
||||
$title = (string) $post->post_title;
|
||||
$summary = sprintf('"%s" geplant für %s', $title, $raw)
|
||||
. ($post->post_status === 'publish' ? ' (war veröffentlicht und ist bis dahin offline)' : '');
|
||||
$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
|
||||
);
|
||||
$change->log((string) $post->post_type, $summary);
|
||||
$updated = $change->reread($post) ?? $post;
|
||||
|
||||
$updated = get_post($postId);
|
||||
$updated = $updated instanceof WP_Post ? $updated : $post;
|
||||
|
||||
return ToolResult::ok($summary . '.')
|
||||
return $change->result($summary . '.', $updated)
|
||||
->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'))
|
||||
->warnings($warnings)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,10 @@ namespace LIT\MCP\Integrations\WordPress\Status;
|
||||
|
||||
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\Schema\ContentSchema;
|
||||
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.
|
||||
$summary = sprintf('"%s" in den Papierkorb gelegt', $title);
|
||||
$warnings = [];
|
||||
$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
|
||||
);
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $postId))
|
||||
->log((string) $post->post_type, sprintf('"%s" in den Papierkorb gelegt', $title));
|
||||
|
||||
$updated = get_post($postId);
|
||||
$updated = $updated instanceof WP_Post ? $updated : $post;
|
||||
$summary = sprintf('"%s" liegt im Papierkorb und ist dort wiederherstellbar.', $title);
|
||||
|
||||
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)
|
||||
->rev(Rev::ofPost($updated))
|
||||
->logId(is_int($logId) ? $logId : null)
|
||||
->warnings($warnings)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,14 +6,13 @@ namespace LIT\MCP\Integrations\WordPress\Status;
|
||||
|
||||
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\Schema\ContentSchema;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
$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 Zurückziehen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
|
||||
$warnings
|
||||
);
|
||||
|
||||
$change = (new PostChangeResult($this->name(), $this->log, $postId))->afterSave($input, 'Zurückziehen');
|
||||
$summary = sprintf('"%s" zurückgezogen (vorher %s, jetzt Entwurf)', $title, $post->post_status);
|
||||
$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
|
||||
);
|
||||
$change->log((string) $post->post_type, $summary);
|
||||
$updated = $change->reread($post) ?? $post;
|
||||
|
||||
$updated = get_post($postId);
|
||||
$updated = $updated instanceof WP_Post ? $updated : $post;
|
||||
|
||||
return ToolResult::ok($summary . '.')
|
||||
return $change->result($summary . '.', $updated)
|
||||
->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)
|
||||
->warnings($warnings)
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user