fix: global library edits check private and scheduled pages, name the backup on failure (P8a, P14)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,8 +15,11 @@ 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')";
|
||||
/** Every post that exists for the user: listed in the dry run. */
|
||||
private const LISTED = "post_status NOT IN ('trash', 'auto-draft', 'inherit')";
|
||||
|
||||
/** Posts that render now or will on their own: published, private, scheduled (ruling P8a). */
|
||||
private const LIVE = "post_status IN ('publish', 'private', 'future')";
|
||||
|
||||
public function __construct(private ?\wpdb $db = null)
|
||||
{
|
||||
@@ -29,12 +32,12 @@ class GlobalUsages
|
||||
$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)
|
||||
$db->prepare('SELECT COUNT(*) FROM ' . $db->posts . ' WHERE ' . self::where(self::LISTED), $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',
|
||||
'SELECT ID, post_title, post_status, post_type FROM ' . $db->posts . ' WHERE '
|
||||
. self::where(self::LISTED) . ' ORDER BY ID ASC LIMIT %d',
|
||||
$like,
|
||||
self::LIMIT
|
||||
),
|
||||
@@ -57,18 +60,17 @@ class GlobalUsages
|
||||
}
|
||||
|
||||
/**
|
||||
* IDs of every published post that shows $libraryId – no limit (ruling P8 checks edit_post on
|
||||
* each of them before a confirmed global edit).
|
||||
* IDs of every live post (published, private, scheduled) that shows $libraryId – no limit
|
||||
* (ruling P8a checks edit_post on each of them before a confirmed global edit).
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
public function publishedIds(int $libraryId): array
|
||||
public function liveIds(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",
|
||||
'SELECT ID FROM ' . $db->posts . ' WHERE ' . self::where(self::LIVE) . ' ORDER BY ID ASC',
|
||||
self::like($db, $libraryId)
|
||||
));
|
||||
// phpcs:enable
|
||||
@@ -76,13 +78,22 @@ class GlobalUsages
|
||||
return array_values(array_map('intval', is_array($ids) ? $ids : []));
|
||||
}
|
||||
|
||||
private function db(): \wpdb
|
||||
/**
|
||||
* The one condition of both queries: posts (no revisions) whose content holds an instance
|
||||
* (placeholder %s = like()), with the fixed status clause $status (a class constant).
|
||||
*/
|
||||
private static function where(string $status): string
|
||||
{
|
||||
return $this->db ?? $GLOBALS['wpdb'];
|
||||
return "post_content LIKE %s AND post_type <> 'revision' AND " . $status;
|
||||
}
|
||||
|
||||
private static function like(\wpdb $db, int $libraryId): string
|
||||
{
|
||||
return '%' . $db->esc_like('"globalModule":"' . $libraryId . '"') . '%';
|
||||
}
|
||||
|
||||
private function db(): \wpdb
|
||||
{
|
||||
return $this->db ?? $GLOBALS['wpdb'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user