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
OptionPreservationHandler.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | class OptionPreservationHandler |
| 10 | { |
| 11 | private $productionDb; |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | public function getLikeStatement(array $optionsNameToPreserve): string |
| 18 | { |
| 19 | $optionsToPreserveEscaped = esc_sql($optionsNameToPreserve); |
| 20 | |
| 21 | $likeStatement = ''; |
| 22 | $first = true; |
| 23 | foreach ($optionsToPreserveEscaped as $option) { |
| 24 | if ($first) { |
| 25 | $likeStatement .= " option_name LIKE '$option'"; |
| 26 | $first = false; |
| 27 | } else { |
| 28 | $likeStatement .= " OR option_name LIKE '$option'"; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return $likeStatement; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | public function getOptionsDataToPreserve(string $whereCondition, string $optionsTable) |
| 42 | { |
| 43 | return $this->productionDb->get_results( |
| 44 | sprintf( |
| 45 | "SELECT * FROM `$optionsTable` WHERE %s", |
| 46 | $whereCondition |
| 47 | ), |
| 48 | ARRAY_A |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | public function createInsertQuery(array $optionToPreserveData, string $optionsTableName): string |
| 58 | { |
| 59 | $sql = ''; |
| 60 | foreach ($optionToPreserveData as $option) { |
| 61 | $sql .= $this->productionDb->prepare( |
| 62 | "INSERT INTO `$optionsTableName` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s );\n", |
| 63 | $option['option_name'], |
| 64 | $option['option_value'], |
| 65 | $option['autoload'] |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | return $sql; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | public function deleteFromTable(string $whereCondition, string $tableName) |
| 78 | { |
| 79 | return $this->productionDb->query( |
| 80 | sprintf( |
| 81 | "DELETE FROM `$tableName` WHERE %s", |
| 82 | $whereCondition |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | public function setProductionDb($db) |
| 92 | { |
| 93 | $this->productionDb = $db; |
| 94 | } |
| 95 | } |
| 96 |