feat(compat): Profile, Gates und Pods über den Kompatibilitätskern
Neu: Tier, Profile, Compatibility, CapabilityLocked, CapabilityGate, CompatibilityComponent, ProvidesCompatibility, DiviProfile sowie PodsFeature, PodsCompatibility und PodsIntegration. Das CapabilityGate sperrt gesperrte und unbekannte Fähigkeits-IDs (übernimmt die Sperre unbekannter IDs vom DiviFeatureGate) und delegiert nur Divi ausdrücklich. Pods-Schema, -Lesen, -Schreiben und -Feldrechte laufen über usable(PodsFeature::…); Schreiben verlangt FieldRead und FieldWrite, damit ein Rollback keine Werte verliert. lit-site-status meldet Pods über die CompatibilityComponent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b511ef6348
commit
c85131603b
+36
-9
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\Pods;
|
||||
|
||||
use Closure;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\FieldDefinition;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\FieldSource;
|
||||
use RuntimeException;
|
||||
@@ -21,6 +22,10 @@ defined('ABSPATH') || exit;
|
||||
* They are mapped like Pods' own view does: hidden → HIDDEN; read_only → READONLY; restricted
|
||||
* and not permitted for the current user → READONLY with read_only_restricted, else HIDDEN.
|
||||
* write() refuses every field that is not writable before anything reaches Pods.
|
||||
*
|
||||
* Every part is gated by a compatibility capability ($usable, from Compatibility::usable): the schema (SchemaRead,
|
||||
* else no fields), reading (FieldRead, else null), writing (FieldWrite and FieldRead, else write() throws and the
|
||||
* fields are READONLY) and the permission check (FieldPermissions, else no field counts as permitted).
|
||||
*/
|
||||
final class PodsFieldSource implements FieldSource
|
||||
{
|
||||
@@ -45,8 +50,13 @@ final class PodsFieldSource implements FieldSource
|
||||
/** @var callable(object): ?bool Pods field => null (unrestricted) or whether the current user may use it. */
|
||||
private $permitted;
|
||||
|
||||
public function __construct(?callable $loadPod = null, ?callable $podFor = null, ?callable $permitted = null)
|
||||
{
|
||||
/** @param Closure(PodsFeature): bool $usable Whether a Pods capability may be used (a locked one is never called). */
|
||||
public function __construct(
|
||||
private Closure $usable,
|
||||
?callable $loadPod = null,
|
||||
?callable $podFor = null,
|
||||
?callable $permitted = null
|
||||
) {
|
||||
$this->loadPod = $loadPod ?? static fn (string $name): mixed => pods_api()->load_pod(['name' => $name]);
|
||||
$this->podFor = $podFor ?? static fn (string $name, int $id): mixed => pods($name, $id);
|
||||
$this->permitted = $permitted ?? static function (object $field): ?bool {
|
||||
@@ -59,11 +69,6 @@ final class PodsFieldSource implements FieldSource
|
||||
};
|
||||
}
|
||||
|
||||
public static function isAvailable(): bool
|
||||
{
|
||||
return function_exists('pods_api') && function_exists('pods');
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
{
|
||||
return 'pods';
|
||||
@@ -71,6 +76,10 @@ final class PodsFieldSource implements FieldSource
|
||||
|
||||
public function fieldsFor(string $postType): array
|
||||
{
|
||||
if (! ($this->usable)(PodsFeature::SchemaRead)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$pod = ($this->loadPod)($postType);
|
||||
if (! is_object($pod) || $pod->get_type() !== 'post_type') {
|
||||
return [];
|
||||
@@ -91,6 +100,10 @@ final class PodsFieldSource implements FieldSource
|
||||
|
||||
public function read(int $postId, FieldDefinition $field): mixed
|
||||
{
|
||||
if (! ($this->usable)(PodsFeature::FieldRead)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pod = ($this->podFor)((string) get_post_type($postId), $postId);
|
||||
if (! is_object($pod)) {
|
||||
return null;
|
||||
@@ -110,6 +123,10 @@ final class PodsFieldSource implements FieldSource
|
||||
|
||||
public function write(int $postId, string $postType, array $values, array $definitions): void
|
||||
{
|
||||
if (! $this->canWrite()) {
|
||||
throw new RuntimeException('Pods fields cannot be written: Pods is locked by the compatibility check.');
|
||||
}
|
||||
|
||||
$data = [];
|
||||
foreach ($values as $key => $value) {
|
||||
$status = isset($definitions[$key]) ? $definitions[$key]->status : null;
|
||||
@@ -180,11 +197,21 @@ final class PodsFieldSource implements FieldSource
|
||||
return FieldDefinition::HIDDEN;
|
||||
}
|
||||
|
||||
if (($this->permitted)($field) === false) {
|
||||
// Fail closed: without the permission check no field counts as permitted.
|
||||
if (! ($this->usable)(PodsFeature::FieldPermissions) || ($this->permitted)($field) === false) {
|
||||
return self::flag($field, 'read_only_restricted') ? FieldDefinition::READONLY : FieldDefinition::HIDDEN;
|
||||
}
|
||||
|
||||
return self::flag($field, 'read_only') ? FieldDefinition::READONLY : $open;
|
||||
$status = self::flag($field, 'read_only') ? FieldDefinition::READONLY : $open;
|
||||
|
||||
// Pods writing is locked (it needs FieldWrite and FieldRead) → nothing is writable.
|
||||
return $status !== FieldDefinition::HIDDEN && ! $this->canWrite() ? FieldDefinition::READONLY : $status;
|
||||
}
|
||||
|
||||
/** A write snapshots the value first (read), so FieldRead is as necessary as FieldWrite. */
|
||||
private function canWrite(): bool
|
||||
{
|
||||
return ($this->usable)(PodsFeature::FieldWrite) && ($this->usable)(PodsFeature::FieldRead);
|
||||
}
|
||||
|
||||
/** A Pods boolean option (stored as 1/'1'/true, like pods_v_bool()). */
|
||||
|
||||
Reference in New Issue
Block a user