feat: Divi tools write library entries; global ones with dry run, confirm and backup (E2, F2)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\Divi\Library;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Pages (and Theme Builder layouts) that show global library entry $id: every living post whose
|
||||
* content has "globalModule":"<id>" – Divi's own way to find instances (string scan,
|
||||
* BlockParserStore, F10). Titles only for posts the user may read. Not final so tests can mock it.
|
||||
*/
|
||||
class GlobalUsages
|
||||
{
|
||||
public const LIMIT = 50;
|
||||
|
||||
private const WHERE = "post_content LIKE %s AND post_type <> 'revision' "
|
||||
. "AND post_status NOT IN ('trash', 'auto-draft', 'inherit')";
|
||||
|
||||
public function __construct(private ?\wpdb $db = null)
|
||||
{
|
||||
}
|
||||
|
||||
/** @return array{items: list<array{post_id: int, title: string, status: string, type: string}>, count: int} */
|
||||
public function of(int $libraryId): array
|
||||
{
|
||||
$db = $this->db();
|
||||
$like = self::like($db, $libraryId);
|
||||
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery
|
||||
$count = (int) $db->get_var(
|
||||
$db->prepare('SELECT COUNT(*) FROM ' . $db->posts . ' WHERE ' . self::WHERE, $like)
|
||||
);
|
||||
$rows = $db->get_results(
|
||||
$db->prepare(
|
||||
'SELECT ID, post_title, post_status, post_type FROM ' . $db->posts . ' WHERE ' . self::WHERE
|
||||
. ' ORDER BY ID ASC LIMIT %d',
|
||||
$like,
|
||||
self::LIMIT
|
||||
),
|
||||
'ARRAY_A'
|
||||
);
|
||||
// phpcs:enable
|
||||
|
||||
$items = [];
|
||||
foreach (is_array($rows) ? $rows : [] as $row) {
|
||||
$id = (int) $row['ID'];
|
||||
$items[] = [
|
||||
'post_id' => $id,
|
||||
'title' => current_user_can('read_post', $id) ? (string) $row['post_title'] : '(nicht lesbar)',
|
||||
'status' => (string) $row['post_status'],
|
||||
'type' => (string) $row['post_type'],
|
||||
];
|
||||
}
|
||||
|
||||
return ['items' => $items, 'count' => max($count, count($items))];
|
||||
}
|
||||
|
||||
/**
|
||||
* IDs of every published post that shows $libraryId – no limit (ruling P8 checks edit_post on
|
||||
* each of them before a confirmed global edit).
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
public function publishedIds(int $libraryId): array
|
||||
{
|
||||
$db = $this->db();
|
||||
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery
|
||||
$ids = $db->get_col($db->prepare(
|
||||
'SELECT ID FROM ' . $db->posts . " WHERE post_content LIKE %s AND post_type <> 'revision' "
|
||||
. "AND post_status = 'publish' ORDER BY ID ASC",
|
||||
self::like($db, $libraryId)
|
||||
));
|
||||
// phpcs:enable
|
||||
|
||||
return array_values(array_map('intval', is_array($ids) ? $ids : []));
|
||||
}
|
||||
|
||||
private function db(): \wpdb
|
||||
{
|
||||
return $this->db ?? $GLOBALS['wpdb'];
|
||||
}
|
||||
|
||||
private static function like(\wpdb $db, int $libraryId): string
|
||||
{
|
||||
return '%' . $db->esc_like('"globalModule":"' . $libraryId . '"') . '%';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user