PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | includes/class-wp-optimize-database-information.php +84 -18 4.6.1 → 4.7.0 View file →
@@ -80,15 +80,16 @@
80 80 return false;
81 81 }
82 82
83 83 /**
84 - * Return table type by $table_name.
84 + * Return the table type by $table_name.
85 85 *
86 - * @param string $table_name Database table name.
86 + * @param string $table_name Database table name.
87 + * @param object|null $table_info Optional pre-fetched table status object (e.g. from a bulk fetch), to avoid a redundant lookup.
87 88 * @return string|boolean - returns false upon failure
88 89 */
89 - public function get_table_type($table_name) {
90 - $table_info = $this->get_table_status($table_name);
90 + public function get_table_type($table_name, $table_info = null) {
91 + if (null === $table_info) $table_info = $this->get_table_status($table_name);
91 92
92 93 if ($table_info) {
93 94 if (!$table_info->Engine && $this->is_view($table_name)) return self::VIEW;
94 95
@@ -115,8 +116,69 @@
115 116 return false;
116 117 }
117 118
118 119 /**
120 + * Count the tables in the database
121 + *
122 + * @param string $prefix The generic prefix of the tables in the database
123 + * @param bool $update if true, then force request to database and don't use cached values.
124 + * @return int
125 + */
126 + public function get_table_count($prefix = '', $update = false) {
127 + global $wpdb;
128 + static $table_counts = array();
129 +
130 + if (!$update && isset($table_counts[$prefix])) return $table_counts[$prefix];
131 +
132 + $table_counts[$prefix] = (int) $wpdb->get_var(
133 + $wpdb->prepare(
134 + "SELECT COUNT(1)
135 + FROM INFORMATION_SCHEMA.TABLES
136 + WHERE TABLE_SCHEMA = %s
137 + AND TABLE_NAME LIKE %s",
138 + DB_NAME,
139 + $prefix . '%'
140 + )
141 + );
142 +
143 + return $table_counts[$prefix];
144 + }
145 +
146 + /**
147 + * Get all tables names from information_schema
148 + *
149 + * @param string $prefix The global table prefix
150 + * @param int $offset Paginate the result to avoid request timeout for large table counts
151 + * @param int $limit Pagination size
152 + * @return array
153 + */
154 + public function get_tables_names($prefix = '', $offset = 0, $limit = null) {
155 + global $wpdb;
156 +
157 + $sql = "SELECT *
158 + FROM INFORMATION_SCHEMA.TABLES
159 + WHERE TABLE_SCHEMA = %s
160 + AND TABLE_NAME LIKE %s";
161 +
162 + $params = array(DB_NAME, $prefix . '_%');
163 +
164 + if (null !== $limit) {
165 + $sql .= " LIMIT %d, %d";
166 + $params[] = (int) $offset;
167 + $params[] = (int) $limit;
168 + }
169 +
170 + $result = $wpdb->get_results($wpdb->prepare($sql, $params)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql only contains static text and %s/%d placeholders, passed through $wpdb->prepare()
171 +
172 + $names = array();
173 + foreach ($result as $row) {
174 + $names[] = $row->TABLE_NAME;
175 + }
176 +
177 + return $names;
178 + }
179 +
180 + /**
119 181 * Returns result for query SHOW TABLE STATUS.
120 182 *
121 183 * @param bool $update refresh or no cached data
122 184 * @return array|object|stdClass[]|null
@@ -268,15 +330,16 @@
268 330
269 331 /**
270 332 * Returns true if table $table_name is optimizable
271 333 *
272 - * @param string $table_name Name of database table
334 + * @param string $table_name Name of database table
335 + * @param string|bool $table_type Optional pre-resolved table engine/type, to avoid a redundant lookup.
273 336 * @return bool
274 337 */
275 - public function is_table_optimizable($table_name) {
338 + public function is_table_optimizable($table_name, $table_type = null) {
276 339 $server_type = $this->get_server_type();
277 340 $server_version = $this->get_version();
278 - $table_type = $this->get_table_type($table_name);
341 + $table_type = null !== $table_type ? $table_type : $this->get_table_type($table_name);
279 342
280 343 // return true if table is MyISAM.
281 344 if (self::MYISAM_ENGINE === $table_type) return true;
282 345
@@ -303,15 +366,16 @@
303 366 return false;
304 367 }
305 368
306 369 /**
307 - * Returns true if table type is supported for optimization.
370 + * Returns true if the table type is supported for optimization.
308 371 *
309 - * @param string $table_name Name of database table
372 + * @param string $table_name Name of database table
373 + * @param string|bool $table_type Optional pre-resolved table engine/type, to avoid a redundant lookup.
310 374 * @return bool
311 375 */
312 - public function is_table_type_optimize_supported($table_name) {
313 - $table_type = $this->get_table_type($table_name);
376 + public function is_table_type_optimize_supported($table_name, $table_type = null) {
377 + $table_type = null !== $table_type ? $table_type : $this->get_table_type($table_name);
314 378
315 379 $supported_table_types = array(
316 380 self::MYISAM_ENGINE,
317 381 self::INNODB_ENGINE,
@@ -324,13 +388,14 @@
324 388
325 389 /**
326 390 * Returns true if table type is supported for repair.
327 391 *
328 - * @param string $table_name
392 + * @param string $table_name
393 + * @param string|bool $table_type Optional pre-resolved table engine/type, to avoid a redundant lookup.
329 394 * @return bool
330 395 */
331 - public function is_table_type_repair_supported($table_name) {
332 - $table_type = $this->get_table_type($table_name);
396 + public function is_table_type_repair_supported($table_name, $table_type = null) {
397 + $table_type = null !== $table_type ? $table_type : $this->get_table_type($table_name);
333 398
334 399 $supported_table_types = array(
335 400 self::MYISAM_ENGINE,
336 401 self::ARCHIVE_ENGINE,
@@ -403,9 +468,9 @@
403 468 $tables = $this->get_show_table_status();
404 469 $supported_tables = array();
405 470
406 471 foreach ($tables as $table) {
407 - if ('' === $table->Engine || $this->is_table_type_repair_supported($table->Name)) {
472 + if ('' === $table->Engine || $this->is_table_type_repair_supported($table->Name, $table->Engine)) {
408 473 $supported_tables[] = $table->Name;
409 474 }
410 475 }
411 476
@@ -416,14 +481,15 @@
416 481
417 482 /**
418 483 * Returns true if table needing repair.
419 484 *
420 - * @param string $table_name Database table name.
485 + * @param string $table_name Database table name.
486 + * @param string|bool $table_type Optional pre-resolved table engine/type, to avoid a redundant lookup.
421 487 */
422 - public function is_table_needing_repair($table_name) {
488 + public function is_table_needing_repair($table_name, $table_type = null) {
423 489 $table_statuses = $this->check_all_tables();
424 490
425 - if (!$this->is_table_type_repair_supported($table_name)) return false;
491 + if (!$this->is_table_type_repair_supported($table_name, $table_type)) return false;
426 492
427 493 return (array_key_exists($table_name, $table_statuses) && $table_statuses[$table_name]['corrupted']);
428 494 }
429 495