PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.2
4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 3.8.0 All 110 releases
wp-optimize / optimizations / attachments.php

attachments.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 2.2.2, at optimizations/attachments.php

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