| @@ -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 | /** |
| 6 | 6 | * Class WP_Optimization_trash |
| 7 | 7 | */ |
| @@ -31,13 +31,15 @@ | ||
| 31 | 31 | public function preview($params) { |
| 32 | 32 | |
| 33 | 33 | $retention_subquery = ''; |
| 34 | 34 | |
| 35 | - if ('true' == $this->retention_enabled) { | |
| 35 | + if ('true' === $this->retention_enabled) { | |
| 36 | 36 | $retention_subquery = ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | // get data requested for preview. |
| 40 | + // `$this->wpdb->prepare` is global `$wpdb->prepare` | |
| 41 | + // phpcs:disable | |
| 40 | 42 | $sql = $this->wpdb->prepare( |
| 41 | 43 | "SELECT `ID`, `post_title`, `post_date`". |
| 42 | 44 | " FROM `" . $this->wpdb->posts . "`". |
| 43 | 45 | " WHERE post_status = 'trash'". |
| @@ -49,8 +51,9 @@ | ||
| 49 | 51 | ) |
| 50 | 52 | ); |
| 51 | 53 | |
| 52 | 54 | $posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 55 | + // phpcs:enable | |
| 53 | 56 | |
| 54 | 57 | // fix empty revision titles. |
| 55 | 58 | if (!empty($posts)) { |
| 56 | 59 | foreach ($posts as $key => $post) { |
| @@ -58,9 +61,9 @@ | ||
| 58 | 61 | 'post_status' => 'trash', |
| 59 | 62 | 'post_type' => 'post', |
| 60 | 63 | ); |
| 61 | 64 | $posts[$key]['post_title'] = array( |
| 62 | - 'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], | |
| 65 | + 'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], | |
| 63 | 66 | 'url' => add_query_arg($args, 'edit.php'), |
| 64 | 67 | ); |
| 65 | 68 | } |
| 66 | 69 | } |
| @@ -67,14 +70,14 @@ | ||
| 67 | 70 | |
| 68 | 71 | // get total count auto-draft for optimization. |
| 69 | 72 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'"; |
| 70 | 73 | |
| 71 | - if ('true' == $this->retention_enabled) { | |
| 74 | + if ('true' === $this->retention_enabled) { | |
| 72 | 75 | $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 73 | 76 | } |
| 74 | 77 | $sql .= ';'; |
| 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' => 'ID', |
| 80 | 83 | 'columns' => array( |
| @@ -93,11 +96,13 @@ | ||
| 93 | 96 | /** |
| 94 | 97 | * Do actions after optimize() function. |
| 95 | 98 | */ |
| 96 | 99 | public function after_optimize() { |
| 100 | + // translators: %s is the number of posts removed from trash | |
| 97 | 101 | $message = sprintf(_n('%s post removed from Trash', '%s posts removed from Trash', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 98 | 102 | |
| 99 | 103 | if ($this->is_multisite_mode()) { |
| 104 | + // translators: %s is the number of sites | |
| 100 | 105 | $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 101 | 106 | } |
| 102 | 107 | |
| 103 | 108 | $this->logger->info($message); |
| @@ -107,12 +112,13 @@ | ||
| 107 | 112 | /** |
| 108 | 113 | * Do optimization. |
| 109 | 114 | */ |
| 110 | 115 | public function optimize() { |
| 111 | - | |
| 116 | + // `$this->wpdb` is global `$wpdb` | |
| 117 | + // phpcs:disable | |
| 112 | 118 | $remove_ids_sql = "SELECT ID FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'"; |
| 113 | 119 | |
| 114 | - if ('true' == $this->retention_enabled) { | |
| 120 | + if ('true' === $this->retention_enabled) { | |
| 115 | 121 | $remove_ids_sql .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 116 | 122 | } |
| 117 | 123 | |
| 118 | 124 | // get trashed post ids. |
| @@ -119,9 +125,11 @@ | ||
| 119 | 125 | $post_remove_ids = $this->wpdb->get_col($remove_ids_sql); |
| 120 | 126 | |
| 121 | 127 | // if optimize called from preview dialog then get posted ids. |
| 122 | 128 | if (isset($this->data['ids'])) { |
| 123 | - $post_remove_ids = array_intersect($post_remove_ids, $this->data['ids']); | |
| 129 | + // Ensure ids are integers | |
| 130 | + $safe_ids = array_map('absint', $this->data['ids']); | |
| 131 | + $post_remove_ids = array_intersect($post_remove_ids, $safe_ids); | |
| 124 | 132 | } |
| 125 | 133 | |
| 126 | 134 | // remove related data for trashed posts. |
| 127 | 135 | if (!empty($post_remove_ids)) { |
| @@ -141,9 +149,9 @@ | ||
| 141 | 149 | } |
| 142 | 150 | |
| 143 | 151 | $clean = "DELETE FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'"; |
| 144 | 152 | |
| 145 | - if ('true' == $this->retention_enabled) { | |
| 153 | + if ('true' === $this->retention_enabled) { | |
| 146 | 154 | $clean .= ' AND post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 147 | 155 | } |
| 148 | 156 | |
| 149 | 157 | // if posted ids in params, then remove only selected items. used by preview widget. |
| @@ -155,9 +163,9 @@ | ||
| 155 | 163 | |
| 156 | 164 | // remove trashed posts. |
| 157 | 165 | $posttrash = $this->query($clean); |
| 158 | 166 | $this->processed_count += $posttrash; |
| 159 | - | |
| 167 | + // phpcs:enable | |
| 160 | 168 | } |
| 161 | 169 | |
| 162 | 170 | /** |
| 163 | 171 | * Do actions after get_info() function. |
| @@ -163,8 +171,9 @@ | ||
| 163 | 171 | * Do actions after get_info() function. |
| 164 | 172 | */ |
| 165 | 173 | public function after_get_info() { |
| 166 | 174 | if ($this->found_count > 0) { |
| 175 | + // translators: %s is the number of trashed posts | |
| 167 | 176 | $message = sprintf(_n('%s trashed post in your database', '%s trashed posts in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 168 | 177 | } else { |
| 169 | 178 | $message = __('No trashed posts found', 'wp-optimize'); |
| 170 | 179 | } |
| @@ -169,13 +178,14 @@ | ||
| 169 | 178 | $message = __('No trashed posts found', 'wp-optimize'); |
| 170 | 179 | } |
| 171 | 180 | |
| 172 | 181 | if ($this->is_multisite_mode()) { |
| 182 | + // translators: %s is the number of sites | |
| 173 | 183 | $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 174 | 184 | } |
| 175 | 185 | |
| 176 | 186 | // add preview link for output. |
| 177 | - if (0 != $this->found_count && null != $this->found_count) { | |
| 187 | + if ($this->found_count > 0) { | |
| 178 | 188 | $message = $this->get_preview_link($message); |
| 179 | 189 | } |
| 180 | 190 | |
| 181 | 191 | $this->register_output($message); |
| @@ -186,14 +196,14 @@ | ||
| 186 | 196 | */ |
| 187 | 197 | public function get_info() { |
| 188 | 198 | |
| 189 | 199 | $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->posts . "` WHERE post_status = 'trash'"; |
| 190 | - if ('true' == $this->retention_enabled) { | |
| 200 | + if ('true' === $this->retention_enabled) { | |
| 191 | 201 | $sql .= ' and post_modified < NOW() - INTERVAL ' . $this->retention_period . ' WEEK'; |
| 192 | 202 | } |
| 193 | 203 | $sql .= ';'; |
| 194 | 204 | |
| 195 | - $trash = $this->wpdb->get_var($sql); | |
| 205 | + $trash = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used | |
| 196 | 206 | $this->found_count += $trash; |
| 197 | 207 | |
| 198 | 208 | } |
| 199 | 209 | |
| @@ -199,24 +209,16 @@ | ||
| 199 | 209 | |
| 200 | 210 | /** |
| 201 | 211 | * Return settings label |
| 202 | 212 | * |
| 203 | - * @return string|void | |
| 213 | + * @return string | |
| 204 | 214 | */ |
| 205 | 215 | public function settings_label() { |
| 206 | 216 | |
| 207 | - if ('true' == $this->retention_enabled) { | |
| 217 | + if ('true' === $this->retention_enabled) { | |
| 218 | + // translators: %d is the number of weeks | |
| 208 | 219 | return sprintf(__('Clean trashed posts which are older than %d weeks', 'wp-optimize'), $this->retention_period); |
| 209 | 220 | } else { |
| 210 | 221 | return __('Clean all trashed posts', 'wp-optimize'); |
| 211 | 222 | } |
| 212 | - } | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * Return description | |
| 216 | - * | |
| 217 | - * @return string|void | |
| 218 | - */ | |
| 219 | - public function get_auto_option_description() { | |
| 220 | - return __('Remove trashed posts', 'wp-optimize'); | |
| 221 | 223 | } |
| 222 | 224 | } |