PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
4.7.0 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 All 111 releases
wp-optimize / optimizations / attachments.php

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

114 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) 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 * Display or hide optimization in optimizations list.
18 *
19 * @return bool
20 */
21 public function display_in_optimizations_list() {
22 return false;
23 }
24
25 /**
26 * Do actions after optimize() function.
27 */
28 public function after_optimize() {
29
30 // translators: %s: number of orphaned attachments
31 $message = sprintf(_n('%s orphaned attachment deleted', '%s orphaned attachments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
32
33 if ($this->is_multisite_mode()) {
34 // translators: %s: number of sites
35 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
36 }
37
38 $this->logger->info($message);
39 $this->register_output($message);
40
41 }
42
43 /**
44 * Do optimization.
45 */
46 public function optimize() {
47
48 $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 > %d AND p.post_type = %s AND pp.ID IS NULL;";
49
50 $attachment_ids = $this->wpdb->get_col($this->wpdb->prepare($sql, 0, 'attachment')); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- `$this->wpdb` is `$wpdb`
51 $count_ids = count($attachment_ids);
52
53 if ($count_ids > 0) {
54 foreach ($attachment_ids as $attachment_id) {
55 wp_delete_attachment($attachment_id, true);
56 }
57 }
58
59 $this->processed_count += $count_ids;
60
61 }
62
63 /**
64 * Do actions after get_info() function.
65 */
66 public function after_get_info() {
67
68 if ($this->found_count) {
69 // translators: %s: number of orphaned attachments
70 $message = sprintf(_n('%s orphaned attachment found', '%s orphaned attachments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
71 } else {
72 $message = __('No orphaned attachments found', 'wp-optimize');
73 }
74
75 if ($this->is_multisite_mode()) {
76 // translators: %s: number of sites
77 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
78 }
79
80 $this->register_output($message);
81
82 }
83 /**
84 * Estimate count of unoptimized items.
85 */
86 public function get_info() {
87
88 $sql = "SELECT COUNT(*) FROM `{$this->wpdb->posts}` p LEFT JOIN `{$this->wpdb->posts}` pp ON pp.ID = p.post_parent WHERE p.post_parent > %d AND p.post_type = %s AND pp.ID IS NULL;";
89 $postmeta = $this->wpdb->get_var($this->wpdb->prepare($sql, 0, 'attachment')); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- `$this->wpdb` is `$wpdb`
90
91 $this->found_count += $postmeta;
92
93 }
94
95 /**
96 * Returns settings label
97 *
98 * @return string
99 */
100 public function settings_label() {
101 return __('Remove orphaned attachments', 'wp-optimize');
102 }
103
104 /**
105 * Return description
106 * N.B. This is not currently used; it was commented out in 1.9.1
107 *
108 * @return string
109 */
110 public function get_auto_option_description() {
111 return __('Remove orphaned attachments', 'wp-optimize');
112 }
113 }
114