| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dev4Press\Plugin\SweepPress\Basic; |
| 4 |
|
| 5 |
use Dev4Press\Plugin\SweepPress\Base\DB as CoreDB; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Removal extends CoreDB { |
| 13 |
protected $plugin_instance = 'removal'; |
| 14 |
|
| 15 |
private $run = true; |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
parent::__construct(); |
| 19 |
|
| 20 |
if ( SWEEPPRESS_SIMULATION ) { |
| 21 |
$this->run = false; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
private function _remove( $sql ) { |
| 26 |
if ( $this->run ) { |
| 27 |
$result = $this->query( $sql ); |
| 28 |
|
| 29 |
if ( $result === false ) { |
| 30 |
return new WP_Error( 'wpdb', $this->wpdb()->last_error ); |
| 31 |
} else { |
| 32 |
return absint( $result ); |
| 33 |
} |
| 34 |
} else { |
| 35 |
return 1; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function cron_jobs() { |
| 40 |
if ( $this->run ) { |
| 41 |
_set_cron_array( array() ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function commentmeta_orphans() { |
| 46 |
$sql = "DELETE m FROM $this->commentmeta m "; |
| 47 |
$sql .= "LEFT JOIN $this->comments p ON p.`comment_ID` = m.`comment_id` "; |
| 48 |
$sql .= "WHERE p.`comment_ID` IS NULL"; |
| 49 |
|
| 50 |
return $this->_remove( $sql ); |
| 51 |
} |
| 52 |
|
| 53 |
public function comments_orphans() { |
| 54 |
$sql = "DELETE c, m FROM $this->comments c "; |
| 55 |
$sql .= "LEFT JOIN $this->commentmeta m ON m.`comment_id` = c.`comment_ID` "; |
| 56 |
$sql .= "LEFT JOIN $this->posts p ON p.`ID` = c.`comment_post_ID` "; |
| 57 |
$sql .= "WHERE p.`ID` IS NULL"; |
| 58 |
|
| 59 |
return $this->_remove( $sql ); |
| 60 |
} |
| 61 |
|
| 62 |
public function comments_user_agent( int $keep_days = 0 ) { |
| 63 |
$keep_days --; |
| 64 |
|
| 65 |
$sql = "UPDATE $this->comments SET `comment_agent` = '' "; |
| 66 |
$sql .= "WHERE `comment_agent` != '' AND DATEDIFF(NOW(), `comment_date`) > %d"; |
| 67 |
$sql = $this->prepare( $sql, $keep_days ); |
| 68 |
|
| 69 |
return $this->_remove( $sql ); |
| 70 |
} |
| 71 |
|
| 72 |
public function comments_by_status( string $comment_status, string $comment_type, int $keep_days = 0 ) { |
| 73 |
$keep_days --; |
| 74 |
$actual_status = $comment_status == 'unapproved' ? '0' : ( $comment_status == 'approved' ? '1' : $comment_status ); |
| 75 |
|
| 76 |
$sql = "DELETE p, m FROM $this->comments p "; |
| 77 |
$sql .= "LEFT JOIN $this->commentmeta m ON m.`comment_id` = p.`comment_ID` "; |
| 78 |
$sql .= "WHERE p.`comment_approved` = %s AND DATEDIFF(NOW(), p.`comment_date`) > %d "; |
| 79 |
$sql .= "AND p.`comment_type` = %s"; |
| 80 |
$sql = $this->prepare( $sql, $actual_status, $keep_days, $comment_type ); |
| 81 |
|
| 82 |
return $this->_remove( $sql ); |
| 83 |
} |
| 84 |
|
| 85 |
public function comments_by_type( string $comment_type, int $keep_days = 0 ) { |
| 86 |
$keep_days --; |
| 87 |
|
| 88 |
$sql = "DELETE p, m FROM $this->comments p "; |
| 89 |
$sql .= "LEFT JOIN $this->commentmeta m ON m.`comment_id` = p.`comment_ID` "; |
| 90 |
$sql .= "WHERE p.`comment_type` = %s AND DATEDIFF(NOW(), p.`comment_date`) > %d"; |
| 91 |
$sql = $this->prepare( $sql, $comment_type, $keep_days ); |
| 92 |
|
| 93 |
return $this->_remove( $sql ); |
| 94 |
} |
| 95 |
|
| 96 |
public function akismet_meta_records( int $keep_days = 0 ) { |
| 97 |
$keep_days --; |
| 98 |
|
| 99 |
$sql = "DELETE m FROM $this->comments c INNER JOIN $this->commentmeta m ON m.`comment_id` = c.`comment_ID` "; |
| 100 |
$sql .= "WHERE m.`meta_key` IN ('" . join( "', '", sweeppress_akismet_meta_keys() ) . "') "; |
| 101 |
$sql .= "AND c.`comment_approved` = '1' AND DATEDIFF(NOW(), c.`comment_date`) > %d"; |
| 102 |
$sql = $this->prepare( $sql, $keep_days ); |
| 103 |
|
| 104 |
return $this->_remove( $sql ); |
| 105 |
} |
| 106 |
|
| 107 |
public function usermeta_orphans() { |
| 108 |
$sql = "DELETE m FROM $this->usermeta m "; |
| 109 |
$sql .= "LEFT JOIN $this->users p ON p.ID = m.user_id "; |
| 110 |
$sql .= "WHERE p.ID IS NULL"; |
| 111 |
|
| 112 |
return $this->_remove( $sql ); |
| 113 |
} |
| 114 |
|
| 115 |
public function termmeta_orphans() { |
| 116 |
$sql = "DELETE m FROM $this->termmeta m "; |
| 117 |
$sql .= "LEFT JOIN $this->terms p ON p.term_id = m.term_id "; |
| 118 |
$sql .= "WHERE p.term_id IS NULL"; |
| 119 |
|
| 120 |
return $this->_remove( $sql ); |
| 121 |
} |
| 122 |
|
| 123 |
public function terms_orphans() { |
| 124 |
$sql = "DELETE t, m FROM $this->terms t "; |
| 125 |
$sql .= "LEFT JOIN $this->termmeta m ON m.`term_id` = t.`term_id` "; |
| 126 |
$sql .= "LEFT JOIN $this->term_taxonomy x ON x.`term_id` = t.`term_id` "; |
| 127 |
$sql .= "WHERE x.`term_id` IS NULL"; |
| 128 |
|
| 129 |
return $this->_remove( $sql ); |
| 130 |
} |
| 131 |
|
| 132 |
public function posts_by_status( string $post_status, string $post_type, int $keep_days = 0 ) { |
| 133 |
$keep_days --; |
| 134 |
|
| 135 |
$taxonomies = get_object_taxonomies( $post_type ); |
| 136 |
|
| 137 |
if ( ! empty( $taxonomies ) ) { |
| 138 |
$sql = "DELETE r FROM $this->posts p "; |
| 139 |
$sql .= "INNER JOIN $this->term_relationships r ON p.ID = r.object_id "; |
| 140 |
$sql .= "INNER JOIN $this->term_taxonomy t ON t.term_taxonomy_id = r.term_taxonomy_id "; |
| 141 |
$sql .= "WHERE p.`post_status` = %s AND p.`post_type` = %s AND DATEDIFF(NOW(), p.`post_date`) > %d "; |
| 142 |
$sql .= "AND t.taxonomy IN ('" . join( "', '", $taxonomies ) . "')"; |
| 143 |
$sql = $this->prepare( $sql, $post_status, $post_type, $keep_days ); |
| 144 |
|
| 145 |
$inter = $this->_remove( $sql ); |
| 146 |
|
| 147 |
if ( is_wp_error( $inter ) ) { |
| 148 |
return $inter; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$sql = "DELETE p, m, c, t FROM $this->posts p "; |
| 153 |
$sql .= "LEFT JOIN $this->postmeta m ON m.`post_id` = p.`ID` "; |
| 154 |
$sql .= "LEFT JOIN $this->comments c ON c.`comment_post_ID` = p.`ID` "; |
| 155 |
$sql .= "LEFT JOIN $this->commentmeta t ON t.`comment_id` = c.`comment_ID` "; |
| 156 |
$sql .= "WHERE p.`post_status` = %s AND p.`post_type` = %s AND DATEDIFF(NOW(), p.`post_date`) > %d"; |
| 157 |
$sql = $this->prepare( $sql, $post_status, $post_type, $keep_days ); |
| 158 |
|
| 159 |
return $this->_remove( $sql ); |
| 160 |
} |
| 161 |
|
| 162 |
public function posts_revisions( string $post_type, int $keep_days = 0, array $post_status = array( 'publish' ) ) { |
| 163 |
$keep_days --; |
| 164 |
|
| 165 |
$sql = "DELETE r, m FROM $this->posts r "; |
| 166 |
$sql .= "INNER JOIN $this->posts p ON p.`ID` = r.`post_parent` "; |
| 167 |
$sql .= "LEFT JOIN $this->postmeta m ON m.`post_id` = r.`ID` "; |
| 168 |
$sql .= "WHERE r.post_type = 'revision' AND p.post_type = %s AND p.`post_status` IN ('" . join( "', '", $post_status ) . "')"; |
| 169 |
$sql .= "AND DATEDIFF(NOW(), r.`post_date`) > %d"; |
| 170 |
$sql = $this->prepare( $sql, $post_type, $keep_days ); |
| 171 |
|
| 172 |
return $this->_remove( $sql ); |
| 173 |
} |
| 174 |
|
| 175 |
public function posts_orphaned_revisions() { |
| 176 |
$sql = "DELETE r FROM $this->posts r "; |
| 177 |
$sql .= "LEFT JOIN $this->posts p ON p.ID = r.post_parent "; |
| 178 |
$sql .= "WHERE r.post_type = 'revision' AND p.ID IS NULL"; |
| 179 |
|
| 180 |
return $this->_remove( $sql ); |
| 181 |
} |
| 182 |
|
| 183 |
public function postmeta_orphans() { |
| 184 |
$sql = "DELETE m FROM $this->postmeta m "; |
| 185 |
$sql .= "LEFT JOIN $this->posts p ON p.ID = m.post_id "; |
| 186 |
$sql .= "WHERE p.ID IS NULL"; |
| 187 |
|
| 188 |
return $this->_remove( $sql ); |
| 189 |
} |
| 190 |
|
| 191 |
public function postmeta_by_key( string $meta_key ) { |
| 192 |
$sql = "DELETE m FROM $this->postmeta m WHERE m.`meta_key` = %s"; |
| 193 |
$sql = $this->prepare( $sql, $meta_key ); |
| 194 |
|
| 195 |
return $this->_remove( $sql ); |
| 196 |
} |
| 197 |
|
| 198 |
public function postmeta_oembeds() { |
| 199 |
$sql = "DELETE m FROM $this->postmeta m WHERE m.`meta_key` LIKE '_oembed_%'"; |
| 200 |
|
| 201 |
return $this->_remove( $sql ); |
| 202 |
} |
| 203 |
|
| 204 |
public function signups_inactive( int $keep_days = 0 ) { |
| 205 |
$keep_days --; |
| 206 |
|
| 207 |
$sql = "DELETE s FROM " . $this->wpdb()->signups . " s WHERE s.`active` = 0 AND DATEDIFF(NOW(), s.`registered`) > %d"; |
| 208 |
$sql = $this->prepare( $sql, $keep_days ); |
| 209 |
|
| 210 |
return $this->_remove( $sql ); |
| 211 |
} |
| 212 |
|
| 213 |
public function actionscheduler_log_orphaned_records() { |
| 214 |
$sql = "DELETE m FROM $this->actionscheduler_logs m "; |
| 215 |
$sql .= "LEFT JOIN $this->actionscheduler_actions p ON p.`action_id` = m.`action_id` "; |
| 216 |
$sql .= "WHERE p.`action_id` IS NULL"; |
| 217 |
|
| 218 |
return $this->_remove( $sql ); |
| 219 |
} |
| 220 |
|
| 221 |
public function actionscheduler_log_records( int $group_id, int $keep_days ) { |
| 222 |
$keep_days --; |
| 223 |
|
| 224 |
$sql = "DELETE l FROM $this->actionscheduler_logs l "; |
| 225 |
$sql .= "INNER JOIN $this->actionscheduler_actions a ON a.`action_id` = l.`action_id` "; |
| 226 |
$sql .= "WHERE a.`group_id` = %d AND DATEDIFF(NOW(), l.`log_date_gmt`) > %d"; |
| 227 |
$sql = $this->prepare( $sql, $group_id, $keep_days ); |
| 228 |
|
| 229 |
return $this->_remove( $sql ); |
| 230 |
} |
| 231 |
|
| 232 |
public function actionscheduler_actions_records_for_status( array $action_status, int $group_id, int $keep_days ) { |
| 233 |
$keep_days --; |
| 234 |
|
| 235 |
$sql = "DELETE a, l FROM $this->actionscheduler_actions a "; |
| 236 |
$sql .= "LEFT JOIN $this->actionscheduler_logs l ON a.`action_id` = l.`action_id` "; |
| 237 |
$sql .= "WHERE a.`status` IN ('" . join( "', '", $action_status ) . "') "; |
| 238 |
$sql .= " AND a.`group_id` = %d AND DATEDIFF(NOW(), a.`scheduled_date_gmt`) > %d"; |
| 239 |
$sql = $this->prepare( $sql, $group_id, $keep_days ); |
| 240 |
|
| 241 |
return $this->_remove( $sql ); |
| 242 |
} |
| 243 |
} |
| 244 |
|