PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.9.5
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.9.5
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 / ExternalDatabaseConfiguration.php
wp-staging / Framework / Database Last commit date
Exporter 1 month ago QueryBuilder 2 years ago CustomTable.php 3 months ago DbInfo.php 5 days ago ExcludedTables.php 6 months ago ExternalDatabaseConfiguration.php 5 days ago OptionPreservationHandler.php 2 years ago SearchReplace.php 3 months ago SelectedTables.php 1 month ago TableDto.php 1 year ago TableService.php 10 months ago TablesRenamer.php 5 days ago WpDbInfo.php 10 months ago WpOptionsInfo.php 1 year ago iDbInfo.php 2 years ago
ExternalDatabaseConfiguration.php
85 lines
1 <?php
2
3 namespace WPStaging\Framework\Database;
4
5 use WPStaging\Framework\Adapter\Database\DatabaseException;
6
7 /**
8 * Handles classic external database selection and connection target validation.
9 */
10 final class ExternalDatabaseConfiguration
11 {
12 /**
13 * @param array|object $options
14 * @return bool
15 */
16 public function isEnabled($options): bool
17 {
18 $options = $this->normalizeOptions($options);
19
20 if (array_key_exists('useCustomDatabase', $options)) {
21 return filter_var($options['useCustomDatabase'], FILTER_VALIDATE_BOOLEAN);
22 }
23
24 return $this->hasConnectionTarget($options);
25 }
26
27 /**
28 * @param array|object $options
29 * @return bool
30 */
31 public function hasConnectionTarget($options): bool
32 {
33 $options = $this->normalizeOptions($options);
34
35 return $this->getValue($options, 'databaseServer') !== ''
36 && $this->getValue($options, 'databaseUser') !== ''
37 && $this->getValue($options, 'databaseDatabase') !== '';
38 }
39
40 /**
41 * @param array|object $options
42 * @return void
43 * @throws DatabaseException
44 */
45 public function validateConnectionTarget($options)
46 {
47 $options = $this->normalizeOptions($options);
48
49 if (!$this->hasConnectionTarget($options)) {
50 throw new DatabaseException(__('External database credentials are incomplete. Enter the database host, database name, and database user.', 'wp-staging'));
51 }
52
53 if (strpos($this->getValue($options, 'databaseServer'), '://') !== false) {
54 throw new DatabaseException(__('The external database server must be a hostname, IP address, socket, or host:port value without a URL scheme such as http:// or https://.', 'wp-staging'));
55 }
56 }
57
58 /**
59 * @param array|object $options
60 * @return array
61 */
62 private function normalizeOptions($options): array
63 {
64 if (is_array($options)) {
65 return $options;
66 }
67
68 if (is_object($options)) {
69 return get_object_vars($options);
70 }
71
72 return [];
73 }
74
75 /**
76 * @param array $options
77 * @param string $key
78 * @return string
79 */
80 private function getValue(array $options, string $key): string
81 {
82 return isset($options[$key]) ? trim((string)$options[$key]) : '';
83 }
84 }
85