feat: lit/divi-dynamic-sources with text-like fields only (E12, F4)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\Divi\Pages;
|
||||
|
||||
use Closure;
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\Abilities\Annotations;
|
||||
use LIT\MCP\Abilities\JsonMap;
|
||||
use LIT\MCP\Integrations\Divi\Compose\DynamicContentRules;
|
||||
use LIT\MCP\Integrations\Divi\Compose\Placeholders;
|
||||
use LIT\MCP\Integrations\Divi\Library\DiviRoles;
|
||||
use LIT\MCP\Integrations\WordPress\PostGuard;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\FieldDefinition;
|
||||
use Throwable;
|
||||
use WP_Error;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* lit/divi-dynamic-sources (spec §5.7, E12): Divi's dynamic content options for a post type
|
||||
* (DynamicContentOptions::get_options with a sample entry of the type the user may edit, else 0;
|
||||
* no loop_* – S36; via DynamicContentMap::options()) with the placeholder that writes each, and
|
||||
* the fields of the type usable as {{field:<key>}}.
|
||||
*
|
||||
* Answer F4: only text-like fields are offered; relation/file fields show IDs in Divi (F26), Pods
|
||||
* table storage is not post meta, protected keys never (S12), unknown types not (ruling P9) – each
|
||||
* left out with its reason. Hidden fields (security audit M4) are neither offered nor named. The
|
||||
* field list needs the Divi role option read_dynamic_content_custom_fields (E3, F25).
|
||||
*/
|
||||
final class DiviDynamicSourcesAbility implements Ability
|
||||
{
|
||||
/** Field types Divi can show as text (arrays of strings are joined with ", ", F26). */
|
||||
private const TEXT_TYPES = ['string', 'text', 'number', 'integer', 'date', 'datetime', 'time', 'choice'];
|
||||
|
||||
private const ID_TYPES = ['relation', 'file'];
|
||||
|
||||
/** Candidates the default sample lookup checks for edit_post. */
|
||||
private const SAMPLE_CANDIDATES = 10;
|
||||
|
||||
/** @var Closure(string): int */
|
||||
private Closure $samplePost;
|
||||
|
||||
/** @var Closure(string): ?string */
|
||||
private Closure $podStorage;
|
||||
|
||||
/**
|
||||
* @param (Closure(string): int)|null $samplePost Post type → an entry to ask Divi with (0 = none).
|
||||
* @param (Closure(string): ?string)|null $podStorage Post type → Pods storage ("meta", "table");
|
||||
* null without a pod.
|
||||
*/
|
||||
public function __construct(
|
||||
private ContentSchema $schema,
|
||||
private DynamicContentMap $map,
|
||||
private DiviRoles $roles,
|
||||
?Closure $samplePost = null,
|
||||
?Closure $podStorage = null
|
||||
) {
|
||||
$this->samplePost = $samplePost ?? self::defaultSample(...);
|
||||
$this->podStorage = $podStorage ?? self::defaultStorage(...);
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'lit/divi-dynamic-sources';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Dynamic-Content-Quellen';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Listet die Dynamic-Content-Quellen, die Divi für einen Inhaltstyp anbietet (Beitragsdaten, Links, '
|
||||
. 'Website-Daten …) mit dem Platzhalter dazu ({{post:…}}, {{link:<ID>}}) bzw. dem Namen für die Vollform, '
|
||||
. 'und die Felder des Typs, die sich als {{field:<Schlüssel>}} eignen. Nur textartige Felder; Beziehungs-, '
|
||||
. 'Datei-, Tabellen- und unbekannte Felder stehen mit Grund unter omitted.';
|
||||
}
|
||||
|
||||
public function inputSchema(): array
|
||||
{
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => ['post_type' => ['type' => 'string', 'description' => 'z. B. "page" oder "person".']],
|
||||
'required' => ['post_type'],
|
||||
'additionalProperties' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function outputSchema(): ?array
|
||||
{
|
||||
$entry = static fn (array $properties): array => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => array_fill_keys($properties, ['type' => 'string']),
|
||||
'required' => $properties,
|
||||
],
|
||||
];
|
||||
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'post_type' => ['type' => 'string'],
|
||||
'sample_post_id' => ['type' => 'integer'],
|
||||
'sources' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'name' => ['type' => 'string'],
|
||||
'label' => ['type' => 'string'],
|
||||
'group' => ['type' => 'string'],
|
||||
'placeholder' => ['type' => ['string', 'null']],
|
||||
'settings' => ['type' => 'object'],
|
||||
],
|
||||
'required' => ['name', 'label', 'group', 'placeholder', 'settings'],
|
||||
],
|
||||
],
|
||||
'fields' => $entry(['key', 'label', 'type', 'source', 'placeholder']),
|
||||
'omitted' => $entry(['key', 'label', 'reason']),
|
||||
'notes' => ['type' => 'array', 'items' => ['type' => 'string']],
|
||||
],
|
||||
'required' => ['post_type', 'sample_post_id', 'sources', 'fields', 'omitted', 'notes'],
|
||||
];
|
||||
}
|
||||
|
||||
public function annotations(): array
|
||||
{
|
||||
return Annotations::read();
|
||||
}
|
||||
|
||||
public function isDirect(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permission(array $input): bool|WP_Error
|
||||
{
|
||||
$type = PostGuard::postType($input['post_type'] ?? null, $this->schema, 'edit_posts');
|
||||
|
||||
return $type instanceof WP_Error ? $type : true;
|
||||
}
|
||||
|
||||
public function execute(array $input): mixed
|
||||
{
|
||||
$type = PostGuard::postType($input['post_type'] ?? null, $this->schema, 'edit_posts');
|
||||
if ($type instanceof WP_Error) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
$postType = (string) $input['post_type'];
|
||||
$sample = ($this->samplePost)($postType);
|
||||
$notes = [
|
||||
'Quellen ohne Platzhalter in der Vollform als $variable({"type":"content","value":{"name":"<name>",'
|
||||
. '"settings":{…}}})$ mit den genannten Einstellungen schreiben.',
|
||||
];
|
||||
|
||||
$options = $this->map->options($sample);
|
||||
if ($options === null) {
|
||||
$notes[] = 'Divis Quellenliste ist nicht verfügbar; gezeigt werden die Quellen der Platzhalter {{post:…}}.';
|
||||
$options = [];
|
||||
foreach (DynamicContentRules::DEFAULT_DYNAMIC_CONTENT as $name => $settings) {
|
||||
$options[$name] = ['label' => $name, 'group' => '', 'settings' => $settings];
|
||||
}
|
||||
}
|
||||
|
||||
$sources = [];
|
||||
foreach ($options as $name => $option) {
|
||||
$sources[] = [
|
||||
'name' => (string) $name,
|
||||
'label' => $option['label'],
|
||||
'group' => $option['group'],
|
||||
'placeholder' => self::placeholder((string) $name),
|
||||
'settings' => JsonMap::of($option['settings']),
|
||||
];
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
$omitted = [];
|
||||
if ($this->roles->allows('read_dynamic_content_custom_fields')) {
|
||||
[$fields, $omitted] = $this->fields($postType);
|
||||
} else {
|
||||
$notes[] = 'Felder werden nicht gelistet: die Divi-Rolle erlaubt „Dynamic Content: benutzerdefinierte '
|
||||
. 'Felder lesen“ nicht (Divi → Rollen-Editor).';
|
||||
}
|
||||
|
||||
return [
|
||||
'post_type' => $postType,
|
||||
'sample_post_id' => $sample,
|
||||
'sources' => $sources,
|
||||
'fields' => $fields,
|
||||
'omitted' => $omitted,
|
||||
'notes' => $notes,
|
||||
];
|
||||
}
|
||||
|
||||
private static function placeholder(string $name): ?string
|
||||
{
|
||||
$post = array_search($name, Placeholders::POST, true);
|
||||
if ($post !== false) {
|
||||
return '{{post:' . $post . '}}';
|
||||
}
|
||||
|
||||
return str_starts_with($name, 'post_link_url_') ? '{{link:<ID>}}' : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: list<array<string, string>>, 1: list<array<string, string>>} Offered and omitted fields.
|
||||
*/
|
||||
private function fields(string $postType): array
|
||||
{
|
||||
$storage = ($this->podStorage)($postType);
|
||||
$fields = [];
|
||||
$omitted = [];
|
||||
foreach ($this->schema->fields($postType) as $field) {
|
||||
if ($field->status === FieldDefinition::HIDDEN) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entry = ['key' => $field->key, 'label' => $field->label];
|
||||
$reason = self::omission($field, $storage);
|
||||
if ($reason !== null) {
|
||||
$omitted[] = $entry + ['reason' => $reason];
|
||||
continue;
|
||||
}
|
||||
|
||||
$fields[] = $entry + [
|
||||
'type' => $field->type,
|
||||
'source' => $field->source,
|
||||
'placeholder' => '{{field:' . $field->key . '}}',
|
||||
];
|
||||
}
|
||||
|
||||
return [$fields, $omitted];
|
||||
}
|
||||
|
||||
private static function omission(FieldDefinition $field, ?string $storage): ?string
|
||||
{
|
||||
return match (true) {
|
||||
! DynamicContentRules::isMetaKey($field->key)
|
||||
=> 'geschütztes oder ungültiges Meta-Feld (beginnt mit „_“)',
|
||||
$field->source === 'pods' && $storage === 'table'
|
||||
=> 'Pods mit Tabellen-Speicherung – Divi liest nur Post-Meta',
|
||||
in_array($field->type, self::ID_TYPES, true)
|
||||
=> 'Beziehungs- oder Dateifeld – Divi zeigt davon nur IDs',
|
||||
$field->type === 'unknown'
|
||||
=> 'Feldtyp unbekannt – Divi zeigt womöglich keinen Text',
|
||||
! in_array($field->type, self::TEXT_TYPES, true)
|
||||
=> sprintf('Feldtyp „%s“ ist nicht textartig', $field->type),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
/** The most recently modified entry of the type the current user may edit, else 0. */
|
||||
private static function defaultSample(string $postType): int
|
||||
{
|
||||
$ids = get_posts([
|
||||
'post_type' => $postType,
|
||||
'post_status' => ['publish', 'draft', 'pending', 'private', 'future'],
|
||||
'posts_per_page' => self::SAMPLE_CANDIDATES,
|
||||
'orderby' => 'modified',
|
||||
'order' => 'DESC',
|
||||
'fields' => 'ids',
|
||||
'no_found_rows' => true,
|
||||
]);
|
||||
|
||||
foreach (is_array($ids) ? $ids : [] as $id) {
|
||||
if (current_user_can('edit_post', (int) $id)) {
|
||||
return (int) $id;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static function defaultStorage(string $postType): ?string
|
||||
{
|
||||
if (! function_exists('pods_api')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$pod = pods_api()->load_pod(['name' => $postType]);
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return is_object($pod) && method_exists($pod, 'get_storage') ? (string) $pod->get_storage() : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user