refactor: Rev::matches replaces the inline rev checks
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:
@@ -32,4 +32,14 @@ final class Rev
|
|||||||
(string) $post->post_modified_gmt,
|
(string) $post->post_modified_gmt,
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether $input carries the current rev of $post (optimistic lock of the post tools).
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $input
|
||||||
|
*/
|
||||||
|
public static function matches(array $input, WP_Post $post): bool
|
||||||
|
{
|
||||||
|
return isset($input['rev']) && $input['rev'] === self::ofPost($post);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ final class ContentUpdateAbility implements Ability
|
|||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! isset($input['rev']) || $input['rev'] !== Rev::ofPost($post)) {
|
if (! Rev::matches($input, $post)) {
|
||||||
return Errors::revMismatch();
|
return Errors::revMismatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ final class RevisionRestoreAbility implements Ability
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! isset($input['rev']) || $input['rev'] !== Rev::ofPost($post)) {
|
if (! Rev::matches($input, $post)) {
|
||||||
return Errors::revMismatch();
|
return Errors::revMismatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ final class PostReplaceWithCopyAbility implements Ability
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! isset($input['rev']) || $input['rev'] !== Rev::ofPost($original)) {
|
if (! Rev::matches($input, $original)) {
|
||||||
return Errors::revMismatch();
|
return Errors::revMismatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,16 @@ final class RevAndJsonMapTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMatchesComparesInputRevWithThePostRev(): void
|
||||||
|
{
|
||||||
|
$post = new WP_Post(['post_title' => 'T']);
|
||||||
|
|
||||||
|
self::assertTrue(Rev::matches(['rev' => Rev::ofPost($post)], $post));
|
||||||
|
self::assertFalse(Rev::matches(['rev' => 'abcdefabcdef'], $post));
|
||||||
|
self::assertFalse(Rev::matches([], $post));
|
||||||
|
self::assertFalse(Rev::matches(['rev' => 123], $post));
|
||||||
|
}
|
||||||
|
|
||||||
public function testEmptyMapBecomesJsonObject(): void
|
public function testEmptyMapBecomesJsonObject(): void
|
||||||
{
|
{
|
||||||
self::assertInstanceOf(stdClass::class, JsonMap::of([]));
|
self::assertInstanceOf(stdClass::class, JsonMap::of([]));
|
||||||
|
|||||||
Reference in New Issue
Block a user