PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
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 / SelectedTables.php
wp-staging / Framework / Database Last commit date
Exporter 1 day ago QueryBuilder 1 day ago CustomTable.php 1 day ago DbInfo.php 1 day ago ExcludedTables.php 1 day ago ExternalDatabaseConfiguration.php 1 day ago OptionPreservationHandler.php 1 day ago SearchReplace.php 1 day ago SelectedTables.php 1 day ago TableDto.php 1 day ago TableService.php 1 day ago TablesRenamer.php 1 day ago WpDbInfo.php 1 day ago WpOptionsInfo.php 1 day ago iDbInfo.php 2 years ago
SelectedTables.php
232 lines
1 <?php
2
3 namespace WPStaging\Framework\Database;
4
5 use wpdb;
6 use WPStaging\Core\WPStaging;
7 use WPStaging\Framework\Filesystem\Scanning\ScanConst;
8
9 class SelectedTables
10 {
11
12 private $includedTables = [];
13
14
15 private $excludedTables = [];
16
17
18 private $selectedTablesWithoutPrefix = [];
19
20
21 private $allTablesExcluded = false;
22
23
24 private $includeAllTables = false;
25
26
27 private $wpdb;
28
29
30 private $prefix;
31
32 public function __construct($includedTables = '', $excludedTables = '', $selectedTablesWithoutPrefix = '')
33 {
34 $this->includedTables = $includedTables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $includedTables);
35 $this->excludedTables = $excludedTables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $excludedTables);
36 $this->selectedTablesWithoutPrefix = $selectedTablesWithoutPrefix === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $selectedTablesWithoutPrefix);
37 $this->wpdb = null;
38 $this->prefix = null;
39 }
40
41
42
43
44
45 public function setIncludedTables($tables)
46 {
47 if (is_array($tables)) {
48 $this->includedTables = $tables;
49 return;
50 }
51
52 $this->includedTables = $tables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $tables);
53 }
54
55
56
57
58
59 public function setExcludedTables($tables)
60 {
61 if (is_array($tables)) {
62 $this->excludedTables = $tables;
63 return;
64 }
65
66 $this->excludedTables = $tables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $tables);
67 }
68
69
70
71
72
73 public function setSelectedTablesWithoutPrefix($tables)
74 {
75 if (is_array($tables)) {
76 $this->selectedTablesWithoutPrefix = $tables;
77 return;
78 }
79
80 $this->selectedTablesWithoutPrefix = $tables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $tables);
81 }
82
83
84
85
86
87 public function setAllTablesExcluded(bool $areAllTablesExcluded = false)
88 {
89 $this->allTablesExcluded = $areAllTablesExcluded;
90 }
91
92
93
94
95
96 public function shouldIncludeAllTables(bool $includeAllTables = false)
97 {
98 $this->includeAllTables = $includeAllTables;
99 }
100
101
102
103
104
105 public function getSelectedTables(bool $isNetworkClone = false)
106 {
107
108
109
110
111 if (!empty($this->includedTables)) {
112 return array_values(array_unique(array_merge($this->includedTables, $this->selectedTablesWithoutPrefix)));
113 }
114
115 $selectedTables = $this->getPrefixedTables($isNetworkClone);
116 return array_values(array_unique(array_merge($selectedTables, $this->selectedTablesWithoutPrefix)));
117 }
118
119
120
121
122
123
124
125
126
127 public function setDatabaseInfo($server, $username, $password, $database, $prefix, $useSsl = false)
128 {
129 if (empty($username) || empty($database)) {
130 $this->wpdb = WPStaging::getInstance()->get("wpdb");
131 } else {
132 if ($useSsl && !defined('MYSQL_CLIENT_FLAGS')) {
133 // phpcs:disable PHPCompatibility.Constants.NewConstants.mysqli_client_ssl_dont_verify_server_certFound
134 define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
135 }
136
137 $this->wpdb = new wpdb($username, str_replace("\\\\", "\\", $password), $database, $server);
138 }
139
140 $this->wpdb->prefix = $prefix;
141 $this->prefix = $prefix;
142 }
143
144
145
146
147
148 public function setWpdb($wpdb, $prefix)
149 {
150 $this->wpdb = $wpdb;
151 $this->wpdb->prefix = $prefix;
152 $this->prefix = $prefix;
153 }
154
155
156
157
158
159
160 public function getPrefixedTables($isNetworkClone, $includeSize = false)
161 {
162 if ($this->allTablesExcluded) {
163 return [];
164 }
165
166 if ($this->wpdb === null) {
167 $this->wpdb = WPStaging::getInstance()->get("wpdb");
168 }
169
170 if ($this->prefix === null) {
171 $this->prefix = WPStaging::getTablePrefix();
172 }
173
174 $sql = "SHOW TABLE STATUS";
175 $tables = $this->wpdb->get_results($sql);
176
177 $selectedTables = [];
178
179 foreach ($tables as $table) {
180 if (!$this->includeAllTables && !$this->isPrefixedTable($table->Name, $this->prefix, is_multisite(), is_main_site(), $isNetworkClone)) {
181 continue;
182 }
183
184 if (in_array($table->Name, $this->excludedTables)) {
185 continue;
186 }
187
188 if ($table->Comment === "VIEW") {
189 continue;
190 }
191
192 if (!$includeSize) {
193 $selectedTables[] = $table->Name;
194 continue;
195 }
196
197 $selectedTables[] = [
198 "name" => $table->Name,
199 "size" => ($table->Data_length + $table->Index_length),
200 ];
201 }
202
203 return $selectedTables;
204 }
205
206
207
208
209
210
211
212
213
214
215 public function isPrefixedTable($tableName, $tablePrefix, $isMultisite, $isMainsite, $isNetwork)
216 {
217 if (!empty($tablePrefix) && strpos($tableName, $tablePrefix) !== 0) {
218 return false;
219 }
220
221
222
223
224
225 if ($isMultisite && $isMainsite && !$isNetwork && preg_match('/^' . $tablePrefix . '\d+_/', $tableName)) {
226 return false;
227 }
228
229 return true;
230 }
231 }
232