Exporter
2 weeks ago
QueryBuilder
2 years ago
CustomTable.php
2 months ago
DbInfo.php
7 months ago
ExcludedTables.php
5 months ago
OptionPreservationHandler.php
2 years ago
SearchReplace.php
2 months ago
SelectedTables.php
2 weeks ago
TableDto.php
1 year ago
TableService.php
9 months ago
TablesRenamer.php
2 months ago
WpDbInfo.php
9 months ago
WpOptionsInfo.php
1 year ago
iDbInfo.php
2 years ago
WpOptionsInfo.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Adapter\Database; |
| 7 | |
| 8 | class WpOptionsInfo |
| 9 | { |
| 10 | /** @var mixed|Database */ |
| 11 | private $wpdb; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->wpdb = Wpstaging::make(Database::class)->getWpdb(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Check whether the wp_options table is missing primary key | auto increment |
| 20 | * @param string $optionTable |
| 21 | * @return bool |
| 22 | */ |
| 23 | public function isOptionTablePrimaryKeyMissing(string $optionTable): bool |
| 24 | { |
| 25 | if ($this->isSqliteTranslatorInstance()) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | $fInfo = $this->getFieldInfo('option_id', $optionTable); |
| 30 | |
| 31 | // Check whether the flag have primary key and auto increment flag |
| 32 | if (isset($fInfo->flags) && ($fInfo->flags & MYSQLI_PRI_KEY_FLAG) && $fInfo->flags & MYSQLI_AUTO_INCREMENT_FLAG) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if ($this->isPrimaryKeyIsOptionName($optionTable)) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param string $optionTable |
| 45 | * @return bool |
| 46 | */ |
| 47 | public function isPrimaryKeyIsOptionName(string $optionTable): bool |
| 48 | { |
| 49 | if ($this->isSqliteTranslatorInstance()) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | $fInfo = $this->getFieldInfo('option_name', $optionTable); |
| 54 | // Abort if flag has no primary key |
| 55 | if (!(isset($fInfo->flags) && $fInfo->flags & MYSQLI_PRI_KEY_FLAG)) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // Check if the field has a composite key |
| 60 | $results = $this->wpdb->get_results("SELECT `CONSTRAINT_NAME`,`COLUMN_NAME` FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `table_name`='{$optionTable}' AND `table_schema`=DATABASE()", ARRAY_A); |
| 61 | if (empty($results) || !is_array($results)) { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | $found = 0; |
| 66 | while ($row = array_shift($results)) { |
| 67 | if ($row['CONSTRAINT_NAME'] === 'PRIMARY' && in_array($row['COLUMN_NAME'], ['option_name', 'option_id'])) { |
| 68 | $found++; |
| 69 | } |
| 70 | |
| 71 | if ($found > 1) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $fieldName |
| 81 | * @param string $tableName |
| 82 | * @return false|object|null |
| 83 | */ |
| 84 | protected function getFieldInfo(string $fieldName, string $tableName) |
| 85 | { |
| 86 | $result = $this->wpdb->dbh->query("SELECT {$fieldName} FROM {$tableName} LIMIT 1"); |
| 87 | if (!is_object($result)) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | $fieldInfo = $result->fetch_field(); |
| 92 | $result->free_result(); |
| 93 | return $fieldInfo; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return bool |
| 98 | */ |
| 99 | private function isSqliteTranslatorInstance(): bool |
| 100 | { |
| 101 | return !empty($this->wpdb->dbh) && ($this->wpdb->dbh instanceof \WP_SQLite_Translator); // @phpstan-ignore-line |
| 102 | } |
| 103 | } |
| 104 |