Skip to content

Commit

Permalink
Merge pull request #596 from tynanbe/clean_revision_meta
Browse files Browse the repository at this point in the history
Fix an issue where cleaning post revisions would fail when cleaning …
  • Loading branch information
hi-hai authored Dec 1, 2023
2 parents e3c18fd + 6566b53 commit 93338a9
Showing 1 changed file with 12 additions and 8 deletions.
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

0 comments on commit 93338a9

Please sign in to comment.