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>
243 lines
9.2 KiB
PHP
243 lines
9.2 KiB
PHP
<?php
|
||
|
||
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;
|
||
use Throwable;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
/**
|
||
* Pods field adapter (spec §6.1/§6.2): definitions from pods_api()->load_pod(),
|
||
* values read and written through the Pods API so relationships and caches stay
|
||
* consistent. Works with Pods 3.x (Pods\Whatsit\Pod / Pods\Whatsit\Field).
|
||
*
|
||
* Field restrictions (security audit M4): Pods enforces admin_only, restrict_role/-capability,
|
||
* read_only and hidden only in its forms – its PHP API (save() with from = 'save') skips them.
|
||
* 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
|
||
{
|
||
private const TYPES = [
|
||
'text' => 'string', 'website' => 'string', 'phone' => 'string', 'email' => 'string',
|
||
'password' => 'password', 'slug' => 'string', 'color' => 'string', 'oembed' => 'string',
|
||
'paragraph' => 'text', 'wysiwyg' => 'text', 'code' => 'text',
|
||
'number' => 'number', 'currency' => 'number',
|
||
'boolean' => 'boolean',
|
||
'date' => 'date', 'datetime' => 'datetime', 'time' => 'time',
|
||
'file' => 'file', 'avatar' => 'file',
|
||
];
|
||
private const LAYOUT_TYPES = ['heading', 'html'];
|
||
private const ID_TYPES = ['relation', 'file'];
|
||
|
||
/** @var callable(string): mixed */
|
||
private $loadPod;
|
||
|
||
/** @var callable(string, int): mixed */
|
||
private $podFor;
|
||
|
||
/** @var callable(object): ?bool Pods field => null (unrestricted) or whether the current user may use it. */
|
||
private $permitted;
|
||
|
||
/** @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 {
|
||
// Fail closed: without the Pods permission API no field counts as permitted.
|
||
if (! function_exists('pods_has_permissions') || ! function_exists('pods_permission')) {
|
||
return false;
|
||
}
|
||
|
||
return pods_has_permissions($field) ? (bool) pods_permission($field) : null;
|
||
};
|
||
}
|
||
|
||
public function id(): string
|
||
{
|
||
return 'pods';
|
||
}
|
||
|
||
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 [];
|
||
}
|
||
|
||
$fields = [];
|
||
foreach ($pod->get_fields() as $field) {
|
||
$type = (string) $field->get_type();
|
||
if (in_array($type, self::LAYOUT_TYPES, true)) {
|
||
continue;
|
||
}
|
||
|
||
$fields[] = $this->definition($field, $type);
|
||
}
|
||
|
||
return $fields;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
if (in_array($field->type, self::ID_TYPES, true)) {
|
||
$ids = $pod->field(['name' => $field->key, 'output' => 'ids', 'single' => ! $field->multiple]);
|
||
if ($field->multiple) {
|
||
return array_values(array_map('intval', array_filter((array) $ids, 'is_numeric')));
|
||
}
|
||
|
||
return is_numeric($ids) ? (int) $ids : null;
|
||
}
|
||
|
||
return $pod->field(['name' => $field->key, 'single' => true, 'raw' => true]);
|
||
}
|
||
|
||
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;
|
||
if ($status === FieldDefinition::READONLY || $status === FieldDefinition::HIDDEN) {
|
||
throw new RuntimeException(sprintf('Pods field %s is restricted for the current user.', $key));
|
||
}
|
||
|
||
$multiple = isset($definitions[$key]) && $definitions[$key]->multiple;
|
||
$data[$key] = $value ?? ($multiple ? [] : '');
|
||
}
|
||
|
||
$pod = ($this->podFor)($postType, $postId);
|
||
if (! is_object($pod)) {
|
||
throw new RuntimeException(sprintf('Pods object for %s #%d is not available.', $postType, $postId));
|
||
}
|
||
|
||
try {
|
||
$result = $pod->save($data, null, null, ['error_mode' => 'exception']);
|
||
} catch (Throwable $e) {
|
||
throw new RuntimeException('Pods could not save the fields: ' . $e->getMessage(), 0, $e);
|
||
}
|
||
|
||
if ($result === false || is_wp_error($result)) {
|
||
throw new RuntimeException('Pods could not save the fields.');
|
||
}
|
||
}
|
||
|
||
private function definition(object $field, string $type): FieldDefinition
|
||
{
|
||
$normalized = self::TYPES[$type] ?? 'unknown';
|
||
$choices = null;
|
||
$related = null;
|
||
|
||
if ($type === 'pick') {
|
||
if ((string) $field->get_arg('pick_object', '') === 'custom-simple') {
|
||
$normalized = 'choice';
|
||
$choices = $this->choices((string) $field->get_arg('pick_custom', ''));
|
||
} else {
|
||
$normalized = 'relation';
|
||
$name = $field->get_related_object_name();
|
||
$related = [
|
||
'object_type' => (string) $field->get_related_object_type(),
|
||
'name' => $name === null ? null : (string) $name,
|
||
];
|
||
}
|
||
} elseif ($normalized === 'file') {
|
||
$related = ['object_type' => 'media', 'name' => null];
|
||
}
|
||
|
||
return new FieldDefinition(
|
||
(string) $field->get_name(),
|
||
(string) $field->get_label(),
|
||
$normalized,
|
||
'pods',
|
||
$this->status($field, $normalized === 'unknown' ? FieldDefinition::UNKNOWN : FieldDefinition::WRITABLE),
|
||
(bool) $field->is_required(),
|
||
(bool) $field->is_multi_value(),
|
||
$choices,
|
||
$related,
|
||
(string) $field->get_arg('description', '')
|
||
);
|
||
}
|
||
|
||
/** $open: the status without restrictions (writable or unknown). */
|
||
private function status(object $field, string $open): string
|
||
{
|
||
if (self::flag($field, 'hidden')) {
|
||
return FieldDefinition::HIDDEN;
|
||
}
|
||
|
||
// 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;
|
||
}
|
||
|
||
$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()). */
|
||
private static function flag(object $field, string $name): bool
|
||
{
|
||
$value = $field->get_arg($name, false);
|
||
|
||
return $value === true || in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true);
|
||
}
|
||
|
||
/** @return list<array{value: string, label: string}> */
|
||
private function choices(string $raw): array
|
||
{
|
||
$choices = [];
|
||
foreach (preg_split('/\r\n|\r|\n/', trim($raw)) ?: [] as $line) {
|
||
$line = trim(trim($line), '|');
|
||
if ($line === '') {
|
||
continue;
|
||
}
|
||
|
||
$parts = explode('|', $line, 2);
|
||
$value = trim($parts[0]);
|
||
$choices[] = ['value' => $value, 'label' => trim($parts[1] ?? $value)];
|
||
}
|
||
|
||
return $choices;
|
||
}
|
||
}
|