fix: status tools report a hook throwing during the write as partial_write

publish/unpublish/schedule/trash run their WordPress call through StatusWrite:
a throwing hook becomes WriteFailed (rollback incomplete, with post_id) and is
logged with result partial instead of surfacing as internal_error.

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:23:09 +02:00
co-authored by Claude Opus 5.5
parent e57b53d0e5
commit 563d9e8d15
9 changed files with 165 additions and 5 deletions
@@ -98,7 +98,13 @@ final class PostPublishAbility implements Ability
$data['edit_date'] = true;
}
$result = wp_update_post($data, true);
$result = StatusWrite::run(
static fn () => wp_update_post($data, true),
$this->name(),
$this->log,
$post,
'WordPress hat beim Veröffentlichen einen Fehler gemeldet.'
);
if ($result instanceof WP_Error) {
return Errors::invalidInput(
'WordPress hat das Veröffentlichen abgelehnt: ' . $result->get_error_message()
@@ -108,13 +108,20 @@ final class PostScheduleAbility implements Ability
}
$postId = (int) $post->ID;
$result = wp_update_post([
$data = [
'ID' => $postId,
'post_status' => 'future',
'post_date' => $date->format('Y-m-d H:i:s'),
'post_date_gmt' => $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s'),
'edit_date' => true,
], true);
];
$result = StatusWrite::run(
static fn () => wp_update_post($data, true),
$this->name(),
$this->log,
$post,
'WordPress hat beim Planen einen Fehler gemeldet.'
);
if ($result instanceof WP_Error) {
return Errors::invalidInput('WordPress hat das Planen abgelehnt: ' . $result->get_error_message());
}
@@ -113,7 +113,14 @@ final class PostTrashAbility implements Ability
);
}
if (! wp_trash_post($postId)) {
$trashed = StatusWrite::run(
static fn () => wp_trash_post($postId),
$this->name(),
$this->log,
$post,
'WordPress hat beim Verschieben in den Papierkorb einen Fehler gemeldet.'
);
if (! $trashed) {
return Errors::unsupported(
'WordPress hat das Verschieben in den Papierkorb abgelehnt.',
'Eintrag in WordPress prüfen (ein anderes Plugin kann das Verschieben verhindern).'
@@ -91,7 +91,13 @@ final class PostUnpublishAbility implements Ability
->toArray();
}
$result = wp_update_post(['ID' => $postId, 'post_status' => 'draft'], true);
$result = StatusWrite::run(
static fn () => wp_update_post(['ID' => $postId, 'post_status' => 'draft'], true),
$this->name(),
$this->log,
$post,
'WordPress hat beim Zurückziehen einen Fehler gemeldet.'
);
if ($result instanceof WP_Error) {
return Errors::invalidInput('WordPress hat das Zurückziehen abgelehnt: ' . $result->get_error_message());
}
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\WordPress\Status;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\ChangeLog\ChangeLog;
use Throwable;
use WP_Post;
/**
* Runs the one WordPress call of a status tool (wp_update_post(), wp_trash_post()). If a
* hook throws inside it, the row may already be written – the entry's state is unknown,
* so this is a partial write (logged as such), never a neutral internal error.
*/
final class StatusWrite
{
/**
* @param callable(): mixed $write
* @throws WriteFailed When $write throws.
*/
public static function run(callable $write, string $ability, ChangeLog $log, WP_Post $post, string $reason): mixed
{
try {
return $write();
} catch (Throwable $e) {
$postId = (int) $post->ID;
$log->recordPartial(
$ability,
(string) $post->post_type,
(string) $postId,
sprintf('"%s": Statusänderung abgebrochen, Stand unklar', $post->post_title)
);
throw new WriteFailed(
$reason,
false,
$postId,
null,
$e,
'',
sprintf('Status von Eintrag %d ist möglicherweise schon geändert worden.', $postId),
'Den Aufruf nicht einfach wiederholen. Zuerst mit lit-content-get Status und Inhalt prüfen und dem '
. 'Nutzer Bescheid geben.'
);
}
}
}