PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.2
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.2
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Database / WpOptionsInfo.php
wp-staging / Framework / Database Last commit date
Exporter 1 week ago QueryBuilder 1 week ago CustomTable.php 1 week ago DbInfo.php 1 week ago ExcludedTables.php 1 week ago ExternalDatabaseConfiguration.php 1 week ago OptionPreservationHandler.php 1 week ago SearchReplace.php 3 days ago SelectedTables.php 1 week ago TableDto.php 1 week ago TableService.php 1 week ago TablesRenamer.php 1 week ago WpDbInfo.php 1 week ago WpOptionsInfo.php 1 week ago iDbInfo.php 3 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
11 private $wpdb;
12
13 public function __construct()
14 {
15 $this->wpdb = Wpstaging::make(Database::class)->getWpdb();
16 }
17
18
19
20
21
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
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
45
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
55 if (!(isset($fInfo->flags) && $fInfo->flags & MYSQLI_PRI_KEY_FLAG)) {
56 return false;
57 }
58
59
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
81
82
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
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