1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<?php
if (!defined('WPO_VERSION')) die('No direct access allowed');
/** * Class WP_Optimization_attachments */ class WP_Optimization_attachments extends WP_Optimization {
public $ui_sort_order = 4500;
public $available_for_auto = false;
public $auto_default = false;
/** * Display or hide optimization in optimizations list. * * @return bool */ public function display_in_optimizations_list() { return false; }
/** * Do actions after optimize() function. */ public function after_optimize() {
$message = sprintf(_n('%s orphaned attachment deleted', '%s orphaned attachments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
if ($this->is_multisite_mode()) { $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); }
$this->logger->info($message); $this->register_output($message);
}
/** * Do optimization. */ public function optimize() {
$sql = "SELECT p.ID FROM `".$this->wpdb->posts."` p LEFT JOIN `".$this->wpdb->posts."` pp ON pp.ID = p.post_parent WHERE p.post_parent > 0 AND p.post_type = 'attachment' AND pp.ID IS NULL;";
$attachment_ids = $this->wpdb->get_col($sql); $count_ids = count($attachment_ids);
if ($count_ids > 0) { foreach ($attachment_ids as $attachment_id) { wp_delete_attachment($attachment_id, true); } }
$this->processed_count += $count_ids;
}
/** * Do actions after get_info() function. */ public function after_get_info() {
if ($this->found_count) { $message = sprintf(_n('%s orphaned attachment found', '%s orphaned attachments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); } else { $message = __('No orphaned attachments found', 'wp-optimize'); }
if ($this->is_multisite_mode()) { $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); }
$this->register_output($message);
} /** * Estimate count of unoptimized items. */ public function get_info() {
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` p LEFT JOIN `" . $this->wpdb->posts . "` pp ON pp.ID = p.post_parent WHERE p.post_parent > 0 AND p.post_type = 'attachment' AND pp.ID IS NULL;"; $postmeta = $this->wpdb->get_var($sql);
$this->found_count += $postmeta;
}
/** * Returns settings label * * @return string|void */ public function settings_label() { return __('Remove orphaned attachments', 'wp-optimize'); } /** * Return description * N.B. This is not currently used; it was commented out in 1.9.1 * * @return string|void */ public function get_auto_option_description() { return __('Remove orphaned attachments', 'wp-optimize'); } }
|