Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue where cleaning post revisions would fail when cleaning … #596

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/db-optm.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,21 @@ private function _db_clean($type)
$postmeta = "`$wpdb->postmeta`";
$posts = "`$wpdb->posts`";

$sql_postmeta = "
$postmeta
LEFT JOIN $posts
ON $posts.ID = $postmeta.post_id
";
$sql_postmeta_join = function ($table) use ($postmeta, $posts) {
return "
$postmeta
CROSS JOIN $table
ON $posts.ID = $postmeta.post_id
";
};

$sql_where = "WHERE $posts.post_type = 'revision'";

$sql_add = $rev_age ? "AND $posts.post_modified < DATE_SUB( NOW(), INTERVAL $rev_age DAY )" : '';

if (!$rev_max) {
$sql_where = "$sql_where $sql_add";
$sql_postmeta = $sql_postmeta_join($posts);
$wpdb->query("DELETE $postmeta FROM $sql_postmeta $sql_where");
$wpdb->query("DELETE FROM $posts $sql_where");
} else {
Expand All @@ -178,13 +181,14 @@ private function _db_clean($type)
$res = $wpdb->get_results($sql);
$sql_where = "
$sql_where
AND $posts.post_parent = %d
ORDER BY $posts.ID
AND post_parent = %d
ORDER BY ID
LIMIT %d
";
$sql_postmeta = $sql_postmeta_join("(SELECT ID FROM $posts $sql_where) AS $posts");
foreach ($res as $v) {
$args = array($v->post_parent, $v->del_max);
$sql = $wpdb->prepare("DELETE $postmeta FROM $sql_postmeta $sql_where", $args);
$sql = $wpdb->prepare("DELETE $postmeta FROM $sql_postmeta", $args);
$wpdb->query($sql);
$sql = $wpdb->prepare("DELETE FROM $posts $sql_where", $args);
$wpdb->query($sql);
Expand Down
Loading