| 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 |
* 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 |
$message = sprintf(_n('%s orphaned attachment deleted', '%s orphaned attachments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 31 |
|
| 32 |
if ($this->is_multisite_mode()) { |
| 33 |
$message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 34 |
} |
| 35 |
|
| 36 |
$this->logger->info($message); |
| 37 |
$this->register_output($message); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Do optimization. |
| 43 |
*/ |
| 44 |
public function optimize() { |
| 45 |
|
| 46 |
$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;"; |
| 47 |
|
| 48 |
$attachment_ids = $this->wpdb->get_col($sql); |
| 49 |
$count_ids = count($attachment_ids); |
| 50 |
|
| 51 |
if ($count_ids > 0) { |
| 52 |
foreach ($attachment_ids as $attachment_id) { |
| 53 |
wp_delete_attachment($attachment_id, true); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$this->processed_count += $count_ids; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Do actions after get_info() function. |
| 63 |
*/ |
| 64 |
public function after_get_info() { |
| 65 |
|
| 66 |
if ($this->found_count) { |
| 67 |
$message = sprintf(_n('%s orphaned attachment found', '%s orphaned attachments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 68 |
} else { |
| 69 |
$message = __('No orphaned attachments found', 'wp-optimize'); |
| 70 |
} |
| 71 |
|
| 72 |
if ($this->is_multisite_mode()) { |
| 73 |
$message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 74 |
} |
| 75 |
|
| 76 |
$this->register_output($message); |
| 77 |
|
| 78 |
} |
| 79 |
/** |
| 80 |
* Estimate count of unoptimized items. |
| 81 |
*/ |
| 82 |
public function get_info() { |
| 83 |
|
| 84 |
$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;"; |
| 85 |
$postmeta = $this->wpdb->get_var($sql); |
| 86 |
|
| 87 |
$this->found_count += $postmeta; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns settings label |
| 93 |
* |
| 94 |
* @return string|void |
| 95 |
*/ |
| 96 |
public function settings_label() { |
| 97 |
return __('Remove orphaned attachments', 'wp-optimize'); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Return description |
| 102 |
* N.B. This is not currently used; it was commented out in 1.9.1 |
| 103 |
* |
| 104 |
* @return string|void |
| 105 |
*/ |
| 106 |
public function get_auto_option_description() { |
| 107 |
return __('Remove orphaned attachments', 'wp-optimize'); |
| 108 |
} |
| 109 |
} |
| 110 |
|