| @@ -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_revisions extends WP_Optimization { |
| 6 | 6 | |
| 7 | 7 | public $ui_sort_order = 1000; |
| @@ -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 post_modified < 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 `ID`, `post_title`, `post_date`". |
| 35 | 37 | " FROM `" . $this->wpdb->posts . "`". |
| 36 | 38 | " WHERE post_type = 'revision'". |
| @@ -42,13 +44,14 @@ | ||
| 42 | 44 | ) |
| 43 | 45 | ); |
| 44 | 46 | |
| 45 | 47 | $posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 48 | + // phpcs:enable | |
| 46 | 49 | |
| 47 | 50 | // fix empty revision titles. |
| 48 | 51 | if (!empty($posts)) { |
| 49 | 52 | foreach ($posts as $key => $post) { |
| 50 | - $posts[$key]['post_title'] = '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title']; | |
| 53 | + $posts[$key]['post_title'] = '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title']; | |
| 51 | 54 | } |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | // get total count revisions for optimization. |
| @@ -53,9 +56,9 @@ | ||
| 53 | 56 | |
| 54 | 57 | // get total count revisions for optimization. |
| 55 | 58 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'".$retention_subquery.";"; |
| 56 | 59 | |
| 57 | - $total = $this->wpdb->get_var($sql); | |
| 60 | + $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL statement is safe | |
| 58 | 61 | |
| 59 | 62 | return array( |
| 60 | 63 | 'id_key' => 'ID', |
| 61 | 64 | 'columns' => array( |
| @@ -74,11 +77,13 @@ | ||
| 74 | 77 | /** |
| 75 | 78 | * Do actions after optimize() function. |
| 76 | 79 | */ |
| 77 | 80 | public function after_optimize() { |
| 81 | + // translators: %s is number of post revisions deleted | |
| 78 | 82 | $message = sprintf(_n('%s post revision deleted', '%s post revisions deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 79 | 83 | |
| 80 | 84 | if ($this->is_multisite_mode()) { |
| 85 | + // translators: %s is number of sites | |
| 81 | 86 | $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 82 | 87 | } |
| 83 | 88 | |
| 84 | 89 | $this->logger->info($message); |
| @@ -89,9 +94,9 @@ | ||
| 89 | 94 | * Do optimization. |
| 90 | 95 | */ |
| 91 | 96 | public function optimize() { |
| 92 | 97 | |
| 93 | - if ('true' == $this->revisions_retention_enabled) { | |
| 98 | + if ('true' === $this->revisions_retention_enabled) { | |
| 94 | 99 | $this->optimize_by_posts(); |
| 95 | 100 | return; |
| 96 | 101 | } |
| 97 | 102 | |
| @@ -96,9 +101,9 @@ | ||
| 96 | 101 | } |
| 97 | 102 | |
| 98 | 103 | $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'"; |
| 99 | 104 | |
| 100 | - if ('true' == $this->retention_enabled) { | |
| 105 | + if ('true' === $this->retention_enabled) { | |
| 101 | 106 | $clean .= ' |
| 102 | 107 | AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 103 | 108 | } |
| 104 | 109 | |
| @@ -127,9 +132,9 @@ | ||
| 127 | 132 | " WHERE post_type = 'revision'". |
| 128 | 133 | " GROUP BY `post_parent`". |
| 129 | 134 | " ORDER BY `post_parent`"; |
| 130 | 135 | |
| 131 | - $results = $this->wpdb->get_results($sql, ARRAY_N); | |
| 136 | + $results = $this->wpdb->get_results($sql, ARRAY_N); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Safe no user input used | |
| 132 | 137 | $post_parents = array(); |
| 133 | 138 | $revisions = ''; |
| 134 | 139 | foreach ($results as $row) { |
| 135 | 140 | array_push($post_parents, $row[0]); |
| @@ -159,8 +164,9 @@ | ||
| 159 | 164 | * Do actions after get_info() function. |
| 160 | 165 | */ |
| 161 | 166 | public function after_get_info() { |
| 162 | 167 | if ($this->found_count > 0) { |
| 168 | + // translators: %s is number of post revisions | |
| 163 | 169 | $message = sprintf(_n('%s post revision in your database', '%s post revisions in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 164 | 170 | } else { |
| 165 | 171 | $message = __('No post revisions found', 'wp-optimize'); |
| 166 | 172 | } |
| @@ -165,8 +171,9 @@ | ||
| 165 | 171 | $message = __('No post revisions found', 'wp-optimize'); |
| 166 | 172 | } |
| 167 | 173 | |
| 168 | 174 | if ($this->is_multisite_mode()) { |
| 175 | + // translators: %s is number of sites | |
| 169 | 176 | $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 170 | 177 | } |
| 171 | 178 | |
| 172 | 179 | // add preview link to message. |
| @@ -177,9 +184,9 @@ | ||
| 177 | 184 | $this->register_output($message); |
| 178 | 185 | } |
| 179 | 186 | |
| 180 | 187 | public function get_info() { |
| 181 | - if ('true' == $this->revisions_retention_enabled) { | |
| 188 | + if ('true' === $this->revisions_retention_enabled) { | |
| 182 | 189 | $sql = "SELECT `post_parent`, GROUP_CONCAT(`ID`)". |
| 183 | 190 | " FROM `" . $this->wpdb->posts . "`". |
| 184 | 191 | " WHERE post_type = 'revision'". |
| 185 | 192 | " GROUP BY `post_parent`". |
| @@ -184,9 +191,9 @@ | ||
| 184 | 191 | " WHERE post_type = 'revision'". |
| 185 | 192 | " GROUP BY `post_parent`". |
| 186 | 193 | " ORDER BY `post_parent`"; |
| 187 | 194 | |
| 188 | - $results = $this->wpdb->get_results($sql, ARRAY_N); | |
| 195 | + $results = $this->wpdb->get_results($sql, ARRAY_N); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Safe, no user input used | |
| 189 | 196 | $post_parents = array(); |
| 190 | 197 | $revisions = ''; |
| 191 | 198 | foreach ($results as $row) { |
| 192 | 199 | array_push($post_parents, $row[0]); |
| @@ -204,14 +211,14 @@ | ||
| 204 | 211 | } |
| 205 | 212 | } else { |
| 206 | 213 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_type = 'revision'"; |
| 207 | 214 | |
| 208 | - if ('true' == $this->retention_enabled) { | |
| 215 | + if ('true' === $this->retention_enabled) { | |
| 209 | 216 | $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 210 | 217 | } |
| 211 | 218 | $sql .= ';'; |
| 212 | 219 | |
| 213 | - $revisions = $this->wpdb->get_var($sql); | |
| 220 | + $revisions = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL statement is safe | |
| 214 | 221 | |
| 215 | 222 | $this->found_count += $revisions; |
| 216 | 223 | } |
| 217 | 224 | } |
| @@ -222,23 +229,22 @@ | ||
| 222 | 229 | * @return string |
| 223 | 230 | */ |
| 224 | 231 | public function settings_label() { |
| 225 | 232 | |
| 226 | - if ('true' == $this->retention_enabled && 'true' == $this->revisions_retention_enabled) { | |
| 227 | - return sprintf(__('Clean post revisions which are older than %d weeks and keep at least %d revisions', 'wp-optimize'), $this->retention_period, $this->revisions_retention_count); | |
| 233 | + if ('true' === $this->retention_enabled && 'true' === $this->revisions_retention_enabled) { | |
| 234 | + // translators: %1$d is revisions retention period in weeks, %2$d is revisions retention count | |
| 235 | + return sprintf(__('Clean post revisions which are older than %1$d weeks and keep at least %2$d revisions', 'wp-optimize'), $this->retention_period, $this->revisions_retention_count); | |
| 228 | 236 | } |
| 229 | 237 | |
| 230 | - if ('true' == $this->retention_enabled && 'false' == $this->revisions_retention_enabled) { | |
| 238 | + if ('true' === $this->retention_enabled && 'false' === $this->revisions_retention_enabled) { | |
| 239 | + // translators: %d is revisions retention period in weeks | |
| 231 | 240 | return sprintf(__('Clean post revisions which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 232 | 241 | } |
| 233 | 242 | |
| 234 | - if ('false' == $this->retention_enabled && 'true' == $this->revisions_retention_enabled) { | |
| 243 | + if ('false' === $this->retention_enabled && 'true' === $this->revisions_retention_enabled) { | |
| 244 | + // translators: %d is number of revisions to retain | |
| 235 | 245 | return sprintf(__('Clean post revisions but keep at least %d revisions', 'wp-optimize'), $this->revisions_retention_count); |
| 236 | 246 | } |
| 237 | 247 | |
| 238 | - return __('Clean all post revisions', 'wp-optimize'); | |
| 239 | - } | |
| 240 | - | |
| 241 | - public function get_auto_option_description() { | |
| 242 | 248 | return __('Clean all post revisions', 'wp-optimize'); |
| 243 | 249 | } |
| 244 | 250 | } |