| @@ -1,7 +1,7 @@ | ||
| 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_unapproved extends WP_Optimization { |
| 6 | 6 | |
| 7 | 7 | public $available_for_auto = true; |
| @@ -24,13 +24,15 @@ | ||
| 24 | 24 | public function preview($params) { |
| 25 | 25 | |
| 26 | 26 | $retention_subquery = ''; |
| 27 | 27 | |
| 28 | - if ('true' == $this->retention_enabled) { | |
| 28 | + if ('true' === $this->retention_enabled) { | |
| 29 | 29 | $retention_subquery = ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | // get data requested for preview. |
| 33 | + // `$this->wpdb->prepare` is global `$wpdb->prepare` | |
| 34 | + // phpcs:disable | |
| 33 | 35 | $sql = $this->wpdb->prepare(" |
| 34 | 36 | SELECT |
| 35 | 37 | c.comment_ID, |
| 36 | 38 | c.comment_author, |
| @@ -50,14 +52,15 @@ | ||
| 50 | 52 | ) |
| 51 | 53 | ); |
| 52 | 54 | |
| 53 | 55 | $comments = $this->wpdb->get_results($sql, ARRAY_A); |
| 56 | + // phpcs:enable | |
| 54 | 57 | |
| 55 | 58 | // fix empty revision titles. |
| 56 | 59 | if (!empty($comments)) { |
| 57 | 60 | foreach ($comments as $key => $comment) { |
| 58 | 61 | $comments[$key]['post_title'] = array( |
| 59 | - 'text' => '' == $comment['post_title'] ? '('.__('no title', 'wp-optimize').')' : $comment['post_title'], | |
| 62 | + 'text' => '' === $comment['post_title'] ? '('.__('no title', 'wp-optimize').')' : $comment['post_title'], | |
| 60 | 63 | 'url' => get_edit_post_link($comment['ID'], ''), |
| 61 | 64 | ); |
| 62 | 65 | $args = array( |
| 63 | 66 | 'action' => 'editcomment', |
| @@ -72,9 +75,9 @@ | ||
| 72 | 75 | |
| 73 | 76 | // get total count comments for optimization. |
| 74 | 77 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0' ".$retention_subquery.";"; |
| 75 | 78 | |
| 76 | - $total = $this->wpdb->get_var($sql); | |
| 79 | + $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used | |
| 77 | 80 | |
| 78 | 81 | return array( |
| 79 | 82 | 'id_key' => 'comment_ID', |
| 80 | 83 | 'columns' => array( |
| @@ -95,11 +98,13 @@ | ||
| 95 | 98 | /** |
| 96 | 99 | * Do actions after optimize() function. |
| 97 | 100 | */ |
| 98 | 101 | public function after_optimize() { |
| 102 | + // translators: %s is the number of unapproved comments deleted | |
| 99 | 103 | $message = sprintf(_n('%s unapproved comment deleted', '%s unapproved comments deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 100 | 104 | |
| 101 | 105 | if ($this->is_multisite_mode()) { |
| 106 | + // translators: %s is the number of sites | |
| 102 | 107 | $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 103 | 108 | } |
| 104 | 109 | |
| 105 | 110 | $this->logger->info($message); |
| @@ -118,9 +123,9 @@ | ||
| 118 | 123 | */ |
| 119 | 124 | public function optimize() { |
| 120 | 125 | $clean = "DELETE c, cm FROM `" . $this->wpdb->comments . "` c LEFT JOIN `" . $this->wpdb->commentmeta . "` cm ON c.comment_ID = cm.comment_id WHERE comment_approved = '0' "; |
| 121 | 126 | |
| 122 | - if ('true' == $this->retention_enabled) { | |
| 127 | + if ('true' === $this->retention_enabled) { | |
| 123 | 128 | $clean .= ' and c.comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 124 | 129 | } |
| 125 | 130 | |
| 126 | 131 | // if posted ids in params, then remove only selected items. used by preview widget. |
| @@ -138,8 +143,9 @@ | ||
| 138 | 143 | * Do actions after get_info() function. |
| 139 | 144 | */ |
| 140 | 145 | public function after_get_info() { |
| 141 | 146 | if ($this->found_count) { |
| 147 | + // translators: %s is the number of unapproved comments | |
| 142 | 148 | $message = sprintf(_n('%s unapproved comment found', '%s unapproved comments found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 143 | 149 | } else { |
| 144 | 150 | $message = __('No unapproved comments found', 'wp-optimize'); |
| 145 | 151 | } |
| @@ -144,13 +150,14 @@ | ||
| 144 | 150 | $message = __('No unapproved comments found', 'wp-optimize'); |
| 145 | 151 | } |
| 146 | 152 | |
| 147 | 153 | if ($this->is_multisite_mode()) { |
| 154 | + // translators: %s is the number of sites | |
| 148 | 155 | $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 149 | 156 | } |
| 150 | 157 | |
| 151 | 158 | // add preview link for output. |
| 152 | - if (0 != $this->found_count && null != $this->found_count) { | |
| 159 | + if ($this->found_count > 0) { | |
| 153 | 160 | $message = $this->get_preview_link($message); |
| 154 | 161 | } |
| 155 | 162 | |
| 156 | 163 | $this->register_output($message); |
| @@ -161,25 +168,27 @@ | ||
| 161 | 168 | */ |
| 162 | 169 | public function get_info() { |
| 163 | 170 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_approved = '0'"; |
| 164 | 171 | |
| 165 | - if ('true' == $this->retention_enabled) { | |
| 172 | + if ('true' === $this->retention_enabled) { | |
| 166 | 173 | $sql .= ' and comment_date < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 167 | 174 | } |
| 168 | 175 | $sql .= ';'; |
| 169 | 176 | |
| 170 | - $comments = $this->wpdb->get_var($sql); | |
| 177 | + $comments = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used | |
| 171 | 178 | $this->found_count += $comments; |
| 172 | 179 | } |
| 173 | 180 | |
| 181 | + /** | |
| 182 | + * Returns settings label | |
| 183 | + * | |
| 184 | + * @return string | |
| 185 | + */ | |
| 174 | 186 | public function settings_label() { |
| 175 | 187 | if ('true' == $this->retention_enabled) { |
| 188 | + // translators: %d is the number of weeks | |
| 176 | 189 | return sprintf(__('Remove unapproved comments which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 177 | 190 | } else { |
| 178 | 191 | return __('Remove unapproved comments', 'wp-optimize'); |
| 179 | 192 | } |
| 180 | - } | |
| 181 | - | |
| 182 | - public function get_auto_option_description() { | |
| 183 | - return __('Remove unapproved comments', 'wp-optimize'); | |
| 184 | 193 | } |
| 185 | 194 | } |