| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_commentmeta extends WP_Optimization { |
| 6 |
|
| 7 |
public $ui_sort_order = 9000; |
| 8 |
|
| 9 |
private $processed_trash_count; |
| 10 |
|
| 11 |
private $processed_akismet_count; |
| 12 |
|
| 13 |
private $found_trash_count; |
| 14 |
|
| 15 |
private $found_akismet_count; |
| 16 |
|
| 17 |
/** |
| 18 |
* Do actions before optimize() function. |
| 19 |
*/ |
| 20 |
public function before_optimize() { |
| 21 |
$this->processed_trash_count = 0; |
| 22 |
$this->processed_akismet_count = 0; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Do actions after optimize() function. |
| 27 |
*/ |
| 28 |
public function after_optimize() { |
| 29 |
$message = sprintf(_n('%s unused comment metadata item removed', '%s unused comment metadata items removed', $this->processed_trash_count, 'wp-optimize'), number_format_i18n($this->processed_trash_count)); |
| 30 |
$message1 = sprintf(_n('%s unused akismet comment metadata item removed', '%s unused akismet comment metadata items removed', $this->processed_akismet_count, 'wp-optimize'), number_format_i18n($this->processed_akismet_count)); |
| 31 |
|
| 32 |
if ($this->is_multisite_mode()) { |
| 33 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 34 |
$message .= $blogs_count_text; |
| 35 |
$message1 .= $blogs_count_text; |
| 36 |
} |
| 37 |
|
| 38 |
$this->logger->info($message); |
| 39 |
$this->register_output($message); |
| 40 |
|
| 41 |
$this->logger->info($message1); |
| 42 |
$this->register_output($message1); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* TODO: The first query here (but not the second) used to be run on a cron run. |
| 47 |
* This needs reviewing when we review the whole cron-run set of options. |
| 48 |
*/ |
| 49 |
public function optimize() { |
| 50 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);"; |
| 51 |
|
| 52 |
$commentstrash_meta = $this->query($clean); |
| 53 |
$this->processed_trash_count += $commentstrash_meta; |
| 54 |
|
| 55 |
// TODO: still need to test now cleaning up comments meta tables - removing akismet related settings. |
| 56 |
$clean = "DELETE FROM `" . $this->wpdb->commentmeta . "` WHERE meta_key LIKE '%akismet%';"; |
| 57 |
|
| 58 |
$commentstrash_meta2 = $this->query($clean); |
| 59 |
$this->processed_akismet_count += $commentstrash_meta2; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Do actions before get_info(). |
| 64 |
*/ |
| 65 |
public function before_get_info() { |
| 66 |
|
| 67 |
$this->found_trash_count = 0; |
| 68 |
$this->found_akismet_count = 0; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Do actions after get_info() function. |
| 74 |
*/ |
| 75 |
public function after_get_info() { |
| 76 |
|
| 77 |
if ($this->found_trash_count > 0) { |
| 78 |
$message = sprintf(_n('%s orphaned comment meta data in your database', '%s orphaned comment meta data in your database', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)); |
| 79 |
} else { |
| 80 |
$message = __('No orphaned comment meta data in your database', 'wp-optimize'); |
| 81 |
} |
| 82 |
|
| 83 |
if ($this->found_akismet_count > 0) { |
| 84 |
$message1 = sprintf(_n('%s unused Akismet comment meta rows in your database', '%s unused Akismet meta rows in your database', $this->found_akismet_count, 'wp-optimize'), number_format_i18n($this->found_akismet_count)); |
| 85 |
} else { |
| 86 |
$message1 = __('No Akismet comment meta rows in your database', 'wp-optimize'); |
| 87 |
} |
| 88 |
|
| 89 |
if ($this->is_multisite_mode()) { |
| 90 |
$blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 91 |
$message .= $blogs_count_text; |
| 92 |
$message1 .= $blogs_count_text; |
| 93 |
} |
| 94 |
|
| 95 |
$this->register_output($message); |
| 96 |
$this->register_output($message1); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get count of unoptimized items. |
| 102 |
*/ |
| 103 |
public function get_info() { |
| 104 |
|
| 105 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);"; |
| 106 |
$commentmeta = $this->wpdb->get_var($sql); |
| 107 |
$this->found_trash_count += $commentmeta; |
| 108 |
|
| 109 |
$sql = "SELECT COUNT(*) FROM `".$this->wpdb->commentmeta."` WHERE meta_key LIKE '%akismet%';"; |
| 110 |
$akismetmeta = $this->wpdb->get_var($sql); |
| 111 |
$this->found_akismet_count += $akismetmeta; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
public function settings_label() { |
| 116 |
return __('Clean comment meta data', 'wp-optimize'); |
| 117 |
} |
| 118 |
|
| 119 |
public function get_auto_option_description() { |
| 120 |
return __('Clean comment meta data', 'wp-optimize'); |
| 121 |
} |
| 122 |
} |
| 123 |
|