| @@ -1,12 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (!defined('WPO_VERSION')) die('No direct access allowed'); | |
| 3 | +if (!defined('ABSPATH')) die('No direct access allowed'); | |
| 4 | 4 | |
| 5 | 5 | class WP_Optimization_commentmeta extends WP_Optimization { |
| 6 | 6 | |
| 7 | 7 | public $ui_sort_order = 9000; |
| 8 | 8 | |
| 9 | + public $available_for_auto = true; | |
| 10 | + | |
| 9 | 11 | public $available_for_saving = true; |
| 10 | 12 | |
| 11 | 13 | private $processed_trash_count; |
| 12 | 14 | |
| @@ -25,19 +27,21 @@ | ||
| 25 | 27 | */ |
| 26 | 28 | public function preview($params) { |
| 27 | 29 | |
| 28 | 30 | // get clicked comment type link. |
| 29 | - $type = isset($params['type']) && 'akismet' == $params['type'] ? 'akismet' : 'trash'; | |
| 31 | + $type = isset($params['type']) && 'akismet' === $params['type'] ? 'akismet' : 'trash'; | |
| 30 | 32 | |
| 31 | 33 | // get data requested for preview. |
| 34 | + // Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb` | |
| 35 | + // phpcs:disable | |
| 32 | 36 | $sql = $this->wpdb->prepare( |
| 33 | 37 | "SELECT * FROM `" . $this->wpdb->commentmeta . "`". |
| 34 | 38 | " WHERE ". |
| 35 | - ('trash' == $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). | |
| 36 | - ('akismet' == $type ? "meta_key LIKE" : ""). | |
| 39 | + ('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). | |
| 40 | + ('akismet' === $type ? "meta_key LIKE" : ""). | |
| 37 | 41 | " %s ORDER BY `comment_ID` LIMIT %d, %d;", |
| 38 | 42 | array( |
| 39 | - ('akismet' == $type ? '%akismet%' : ''), | |
| 43 | + ('akismet' === $type ? '%akismet%' : ''), | |
| 40 | 44 | $params['offset'], |
| 41 | 45 | $params['limit'], |
| 42 | 46 | ) |
| 43 | 47 | ); |
| @@ -42,14 +46,15 @@ | ||
| 42 | 46 | ) |
| 43 | 47 | ); |
| 44 | 48 | |
| 45 | 49 | $posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 50 | + // phpcs:enable | |
| 46 | 51 | |
| 47 | 52 | // fix empty revision titles. |
| 48 | 53 | if (!empty($posts)) { |
| 49 | 54 | foreach ($posts as $key => $post) { |
| 50 | 55 | $posts[$key]['post_title'] = array( |
| 51 | - 'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], | |
| 56 | + 'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], | |
| 52 | 57 | 'url' => get_edit_post_link($post['ID']), |
| 53 | 58 | ); |
| 54 | 59 | } |
| 55 | 60 | } |
| @@ -54,20 +59,23 @@ | ||
| 54 | 59 | } |
| 55 | 60 | } |
| 56 | 61 | |
| 57 | 62 | // get total count comments for optimization. |
| 63 | + // Suppress WordPress.DB.PreparedSQL.NotPrepared errors, these are safe and prepared. `$this->wpdb` is $wpdb` | |
| 64 | + // phpcs:disable | |
| 58 | 65 | $sql = $this->wpdb->prepare( |
| 59 | 66 | "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "`". |
| 60 | 67 | "WHERE ". |
| 61 | - ('trash' == $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). | |
| 62 | - ('akismet' == $type ? "meta_key LIKE " : ""). | |
| 68 | + ('trash' === $type ? "comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`)" : ""). | |
| 69 | + ('akismet' === $type ? "meta_key LIKE " : ""). | |
| 63 | 70 | " %s;", |
| 64 | 71 | array( |
| 65 | - ('akismet' == $type ? '%akismet%' : ''), | |
| 72 | + ('akismet' === $type ? '%akismet%' : ''), | |
| 66 | 73 | ) |
| 67 | 74 | ); |
| 68 | 75 | |
| 69 | 76 | $total = $this->wpdb->get_var($sql); |
| 77 | + // phpcs:enable | |
| 70 | 78 | |
| 71 | 79 | return array( |
| 72 | 80 | 'id_key' => 'meta_id', |
| 73 | 81 | 'columns' => array( |
| @@ -72,16 +80,16 @@ | ||
| 72 | 80 | 'id_key' => 'meta_id', |
| 73 | 81 | 'columns' => array( |
| 74 | 82 | 'meta_id' => __('ID', 'wp-optimize'), |
| 75 | 83 | 'comment_id' => __('Comment ID', 'wp-optimize'), |
| 76 | - 'meta_key' => __('Meta Key', 'wp-optimize'), | |
| 77 | - 'meta_value' => __('Meta Value', 'wp-optimize'), | |
| 84 | + 'meta_key' => __('Meta Key', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This is not a query | |
| 85 | + 'meta_value' => __('Meta Value', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- This is not a query | |
| 78 | 86 | ), |
| 79 | 87 | 'offset' => $params['offset'], |
| 80 | 88 | 'limit' => $params['limit'], |
| 81 | 89 | 'total' => $total, |
| 82 | 90 | 'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 83 | - 'message' => $total > 0 ? '' : __('No orphaned comment meta data in your database', 'wp-optimize'), | |
| 91 | + 'message' => $total > 0 ? '' : __('No orphaned comment metadata in your database', 'wp-optimize'), | |
| 84 | 92 | ); |
| 85 | 93 | } |
| 86 | 94 | |
| 87 | 95 | /** |
| @@ -95,12 +103,15 @@ | ||
| 95 | 103 | /** |
| 96 | 104 | * Do actions after optimize() function. |
| 97 | 105 | */ |
| 98 | 106 | public function after_optimize() { |
| 107 | + // translators: %s is a number of unused comment metadata item removed | |
| 99 | 108 | $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)); |
| 109 | + // translators: %s is a number of unused akismet comment metadata item removed | |
| 100 | 110 | $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)); |
| 101 | 111 | |
| 102 | 112 | if ($this->is_multisite_mode()) { |
| 113 | + // translators: %s is a number of sites | |
| 103 | 114 | $blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 104 | 115 | $message .= $blogs_count_text; |
| 105 | 116 | $message1 .= $blogs_count_text; |
| 106 | 117 | } |
| @@ -156,14 +167,16 @@ | ||
| 156 | 167 | */ |
| 157 | 168 | public function after_get_info() { |
| 158 | 169 | |
| 159 | 170 | if ($this->found_trash_count > 0) { |
| 160 | - $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)); | |
| 171 | + // translators: %s is a number of orphaned comment metadata | |
| 172 | + $message = sprintf(_n('%s orphaned comment metadata in your database', '%s orphaned comment metadata in your database', $this->found_trash_count, 'wp-optimize'), number_format_i18n($this->found_trash_count)); | |
| 161 | 173 | } else { |
| 162 | - $message = __('No orphaned comment meta data in your database', 'wp-optimize'); | |
| 174 | + $message = __('No orphaned comment metadata in your database', 'wp-optimize'); | |
| 163 | 175 | } |
| 164 | 176 | |
| 165 | 177 | if ($this->found_akismet_count > 0) { |
| 178 | + // translators: %s is a number of unused Akismet comment metadata | |
| 166 | 179 | $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)); |
| 167 | 180 | } else { |
| 168 | 181 | $message1 = __('No Akismet comment meta rows in your database', 'wp-optimize'); |
| 169 | 182 | } |
| @@ -168,8 +181,9 @@ | ||
| 168 | 181 | $message1 = __('No Akismet comment meta rows in your database', 'wp-optimize'); |
| 169 | 182 | } |
| 170 | 183 | |
| 171 | 184 | if ($this->is_multisite_mode()) { |
| 185 | + // translators: %s is a number of sites | |
| 172 | 186 | $blogs_count_text = ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 173 | 187 | $message .= $blogs_count_text; |
| 174 | 188 | $message1 .= $blogs_count_text; |
| 175 | 189 | } |
| @@ -192,21 +206,22 @@ | ||
| 192 | 206 | */ |
| 193 | 207 | public function get_info() { |
| 194 | 208 | |
| 195 | 209 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->commentmeta . "` WHERE comment_id NOT IN (SELECT comment_id FROM `" . $this->wpdb->comments . "`);"; |
| 196 | - $commentmeta = $this->wpdb->get_var($sql); | |
| 210 | + $commentmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used | |
| 197 | 211 | $this->found_trash_count += $commentmeta; |
| 198 | 212 | |
| 199 | 213 | $sql = "SELECT COUNT(*) FROM `".$this->wpdb->commentmeta."` WHERE meta_key LIKE '%akismet%';"; |
| 200 | - $akismetmeta = $this->wpdb->get_var($sql); | |
| 214 | + $akismetmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- this is safe, no user input used | |
| 201 | 215 | $this->found_akismet_count += $akismetmeta; |
| 202 | 216 | |
| 203 | 217 | } |
| 204 | 218 | |
| 219 | + /** | |
| 220 | + * Return settings label | |
| 221 | + * | |
| 222 | + * @return string | |
| 223 | + */ | |
| 205 | 224 | public function settings_label() { |
| 206 | - return __('Clean comment meta data', 'wp-optimize'); | |
| 207 | - } | |
| 208 | - | |
| 209 | - public function get_auto_option_description() { | |
| 210 | - return __('Clean comment meta data', 'wp-optimize'); | |
| 225 | + return __('Clean comment metadata', 'wp-optimize'); | |
| 211 | 226 | } |
| 212 | 227 | } |