PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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
QueryBuilder 2 years ago DbInfo.php 2 years ago ExcludedTables.php 4 years ago OptionPreservationHandler.php 2 years ago SearchReplace.php 2 years ago SelectedTables.php 2 years ago TableDto.php 5 years ago TableService.php 2 years ago TablesRenamer.php 2 years ago WpDbInfo.php 2 years ago WpOptionsInfo.php 2 years ago iDbInfo.php 2 years ago
WpOptionsInfo.php
88 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 $fInfo = $this->getFieldInfo('option_id', $optionTable);
26
27 // Check whether the flag have primary key and auto increment flag
28 if (isset($fInfo->flags) && ($fInfo->flags & MYSQLI_PRI_KEY_FLAG) && $fInfo->flags & MYSQLI_AUTO_INCREMENT_FLAG) {
29 return false;
30 }
31
32 if ($this->isPrimaryKeyIsOptionName($optionTable)) {
33 return false;
34 }
35
36 return true;
37 }
38
39 /**
40 * @param string $optionTable
41 * @return bool
42 */
43 public function isPrimaryKeyIsOptionName(string $optionTable): bool
44 {
45 $fInfo = $this->getFieldInfo('option_name', $optionTable);
46 // Abort if flag has no primary key
47 if (!(isset($fInfo->flags) && $fInfo->flags & MYSQLI_PRI_KEY_FLAG)) {
48 return false;
49 }
50
51 // Check if the field has a composite key
52 $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);
53 if (empty($results) || !is_array($results)) {
54 return true;
55 }
56
57 $found = 0;
58 while ($row = array_shift($results)) {
59 if ($row['CONSTRAINT_NAME'] === 'PRIMARY' && in_array($row['COLUMN_NAME'], ['option_name', 'option_id'])) {
60 $found++;
61 }
62
63 if ($found > 1) {
64 return false;
65 }
66 }
67
68 return true;
69 }
70
71 /**
72 * @param string $fieldName
73 * @param string $tableName
74 * @return false|object|null
75 */
76 protected function getFieldInfo(string $fieldName, string $tableName)
77 {
78 $result = $this->wpdb->dbh->query("SELECT {$fieldName} FROM {$tableName} LIMIT 1");
79 if (!is_object($result)) {
80 return false;
81 }
82
83 $fieldInfo = $result->fetch_field();
84 $result->free_result();
85 return $fieldInfo;
86 }
87 }
88