← All changes
|
includes/class-wp-optimize-database-information.php
+119
-50
4.5.3
→
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,30 +481,29 @@ | ||
| 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 | |
| 430 | 496 | /** |
| 431 | - * Check if $table using by any of installed plugins. | |
| 497 | + * Check if any of plugins from $plugin_names is installed. | |
| 432 | 498 | * |
| 433 | - * @param string $table | |
| 499 | + * @param array $plugin_names | |
| 434 | 500 | * @return bool |
| 435 | 501 | */ |
| 436 | - public function is_table_using_by_plugin($table) { | |
| 437 | - $plugin_names = $this->get_table_plugin($table); | |
| 502 | + public function is_any_plugin_installed($plugin_names) { | |
| 438 | 503 | |
| 439 | - // if we can't determine which plugin use $table then return true. | |
| 440 | - if (!$plugin_names) { | |
| 441 | - return true; | |
| 504 | + if (empty($plugin_names)) { | |
| 505 | + return false; | |
| 442 | 506 | } |
| 443 | 507 | |
| 444 | 508 | // is WordPress core table or using by any of installed plugins then return true. |
| 445 | 509 | foreach ($plugin_names as $plugin_name) { |
| @@ -536,10 +600,8 @@ | ||
| 536 | 600 | |
| 537 | 601 | if (!in_array($wpo, $plugin_tables['tm_tasks'], true)) { |
| 538 | 602 | $plugin_tables['tm_tasks'][] = $wpo; |
| 539 | 603 | } |
| 540 | - | |
| 541 | - $plugin_tables = array_merge($plugin_tables, $this->get_learndash_tables()); | |
| 542 | 604 | |
| 543 | 605 | return $plugin_tables; |
| 544 | 606 | } |
| 545 | 607 | |
| @@ -562,20 +624,15 @@ | ||
| 562 | 624 | if (array_key_exists($table, $plugins_tables)) { |
| 563 | 625 | $plugins = $plugins_tables[$table]; |
| 564 | 626 | } |
| 565 | 627 | |
| 566 | - // special handling for tables when plugins use custom prefix. | |
| 567 | - foreach ($plugins_tables as $plugin_table => $plugin_names) { | |
| 568 | - if (false === strpos($plugin_table, '*')) { | |
| 569 | - continue; | |
| 628 | + // if has pro_quiz in name then try to match with learndash tables. | |
| 629 | + if (false !== stripos($original_table, 'pro_quiz')) { | |
| 630 | + $match_learndash_tables_plugin = $this->match_learndash_tables_plugin($original_table); | |
| 631 | + | |
| 632 | + if (!empty($match_learndash_tables_plugin)) { | |
| 633 | + $plugins = array_merge($plugins, $match_learndash_tables_plugin); | |
| 570 | 634 | } |
| 571 | - | |
| 572 | - // support for wildcard * in table names. | |
| 573 | - $pattern = '/^'.str_replace('\*', '.*', preg_quote($plugin_table, '/')).'$/'; | |
| 574 | - | |
| 575 | - if (preg_match($pattern, $original_table)) { | |
| 576 | - $plugins = array_merge($plugins, $plugin_names); | |
| 577 | - } | |
| 578 | 635 | } |
| 579 | 636 | |
| 580 | 637 | if (!empty($plugins)) { |
| 581 | 638 | return array_unique($plugins); |
| @@ -713,24 +770,36 @@ | ||
| 713 | 770 | } |
| 714 | 771 | } |
| 715 | 772 | |
| 716 | 773 | /** |
| 717 | - * Get a list of LearnDash tables with plugin slug for each of them. | |
| 774 | + * Check if the give table name belongs to a LearnDash plugin table and return the plugin slug. | |
| 718 | 775 | * |
| 776 | + * @param string $table_name The original table name with prefix to check if it belongs to LearnDash plugin. | |
| 719 | 777 | * @return array |
| 720 | 778 | */ |
| 721 | - private function get_learndash_tables() { | |
| 779 | + private function match_learndash_tables_plugin($table_name) { | |
| 780 | + | |
| 722 | 781 | $table_names = array( |
| 723 | - '*pro_quiz_category', | |
| 724 | - '*pro_quiz_form', | |
| 725 | - '*pro_quiz_lock', | |
| 726 | - '*pro_quiz_prerequisite', | |
| 727 | - '*pro_quiz_question', | |
| 728 | - '*pro_quiz_statistic', | |
| 729 | - '*pro_quiz_statistic_ref', | |
| 730 | - '*pro_quiz_template', | |
| 731 | - '*pro_quiz_toplist', | |
| 782 | + 'pro_quiz_category', | |
| 783 | + 'pro_quiz_form', | |
| 784 | + 'pro_quiz_lock', | |
| 785 | + 'pro_quiz_prerequisite', | |
| 786 | + 'pro_quiz_question', | |
| 787 | + 'pro_quiz_statistic', | |
| 788 | + 'pro_quiz_statistic_ref', | |
| 789 | + 'pro_quiz_template', | |
| 790 | + 'pro_quiz_toplist', | |
| 732 | 791 | ); |
| 733 | 792 | |
| 734 | - return array_fill_keys($table_names, array('sfwd-lms')); | |
| 793 | + $learndash_slug = 'sfwd-lms'; | |
| 794 | + | |
| 795 | + foreach ($table_names as $learndash_table) { | |
| 796 | + $table_suffix = substr($table_name, -strlen($learndash_table)); | |
| 797 | + | |
| 798 | + if ($table_suffix === $learndash_table) { | |
| 799 | + return array($learndash_slug); | |
| 800 | + } | |
| 801 | + } | |
| 802 | + | |
| 803 | + return array(); | |
| 735 | 804 | } |
| 736 | 805 | } |