Database
2 years ago
Database.php
2 years ago
DateTimeAdapter.php
4 years ago
Directory.php
2 years ago
Maintenance.php
4 years ago
PhpAdapter.php
4 years ago
SourceDatabase.php
3 years ago
WpAdapter.php
3 years ago
SourceDatabase.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Adapter; |
| 4 | |
| 5 | use stdClass; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use wpdb; |
| 8 | |
| 9 | class SourceDatabase |
| 10 | { |
| 11 | /** @var wpdb */ |
| 12 | private $wpdb; |
| 13 | |
| 14 | /** @var object */ |
| 15 | private $options; |
| 16 | |
| 17 | public function __construct($options = stdClass::class) |
| 18 | { |
| 19 | $this->wpdb = WPStaging::make('wpdb'); |
| 20 | $this->options = $options; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return bool |
| 25 | */ |
| 26 | public function isExternalDatabase() |
| 27 | { |
| 28 | return !(empty($this->options->databaseUser) || |
| 29 | empty($this->options->databasePassword) || |
| 30 | empty($this->options->databaseDatabase) || |
| 31 | empty($this->options->databaseServer)); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return object |
| 36 | */ |
| 37 | private function getExternalDb() |
| 38 | { |
| 39 | return new wpdb($this->options->databaseUser, str_replace("\\\\", "\\", $this->options->databasePassword), $this->options->databaseDatabase, $this->options->databaseServer); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Check if source database is a local or external one and get the corresponding database object |
| 44 | * |
| 45 | * @return wpdb |
| 46 | * |
| 47 | */ |
| 48 | public function getDatabase() |
| 49 | { |
| 50 | if ($this->isExternalDatabase()) { |
| 51 | return $this->getExternalDb(); |
| 52 | } |
| 53 | return $this->wpdb; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Add or update a cloned site option in the database. |
| 58 | * |
| 59 | * No need to serialize values. If the value needs to be serialized, |
| 60 | * then it will be serialized before it is inserted into the database. |
| 61 | * |
| 62 | * If the option does not exist, it will be created. |
| 63 | * |
| 64 | * @param string $optionName |
| 65 | * @param mixed $optionValue |
| 66 | * |
| 67 | * @return int|false int for the number of rows affected during the updating of the clone's DB, or false on failure. |
| 68 | */ |
| 69 | public function addOrUpdateClonedSiteOption($optionName, $optionValue) |
| 70 | { |
| 71 | if (!isset($this->options->prefix)) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | $cloneOptionsTable = $this->options->prefix . 'options'; |
| 76 | $cloneOptions = $this->wpdb->query("SELECT * FROM {$cloneOptionsTable} WHERE option_name='{$optionName}';"); |
| 77 | if (empty($cloneOptions)) { |
| 78 | $result = $this->addOption($optionName, $optionValue); |
| 79 | } else { |
| 80 | $result = $this->wpdb->update( |
| 81 | $cloneOptionsTable, |
| 82 | [ |
| 83 | 'option_value' => maybe_serialize($optionValue), |
| 84 | ], |
| 85 | ['option_name' => $optionName] |
| 86 | ); |
| 87 | } |
| 88 | return $result; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Add option to cloned site. |
| 93 | * |
| 94 | * No need to serialize values. If the value needs to be serialized, |
| 95 | * then it will be serialized before it is inserted into the database. |
| 96 | * |
| 97 | * @param string $optionName |
| 98 | * @param mixed $optionValue |
| 99 | * |
| 100 | * @return int|false int for the number of rows affected during the updating of the clone's DB, or false on failure. |
| 101 | */ |
| 102 | public function addOption($optionName, $optionValue) |
| 103 | { |
| 104 | if (!isset($this->options->prefix)) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | $cloneOptionsTable = $this->options->prefix . 'options'; |
| 109 | $cloneOptions = $this->wpdb->query("SELECT * FROM {$cloneOptionsTable} WHERE option_name='{$optionName}';"); |
| 110 | if (!empty($cloneOptions)) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | $result = $this->wpdb->insert( |
| 115 | $cloneOptionsTable, |
| 116 | [ |
| 117 | 'option_name' => $optionName, |
| 118 | 'option_value' => maybe_serialize($optionValue), |
| 119 | ] |
| 120 | ); |
| 121 | return $result; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param object $options |
| 126 | * @return void |
| 127 | */ |
| 128 | public function setOptions($options) |
| 129 | { |
| 130 | $this->options = $options; |
| 131 | } |
| 132 | } |
| 133 |