QueryBuilder
2 years ago
DbInfo.php
2 years ago
ExcludedTables.php
4 years ago
OptionPreservationHandler.php
2 years ago
SearchReplace.php
1 year ago
SelectedTables.php
1 year ago
TableDto.php
1 year ago
TableService.php
1 year ago
TablesRenamer.php
1 year ago
WpDbInfo.php
2 years ago
WpOptionsInfo.php
1 year ago
iDbInfo.php
2 years ago
OptionPreservationHandler.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | /** |
| 6 | * This class creates insert sql queries with data to preserve |
| 7 | * on a live site after pushing a staging site |
| 8 | */ |
| 9 | class OptionPreservationHandler |
| 10 | { |
| 11 | private $productionDb; |
| 12 | |
| 13 | /** |
| 14 | * @param array $optionsNameToPreserve |
| 15 | * @return string |
| 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 | * @param string $whereCondition |
| 37 | * @param string $optionsTable |
| 38 | * |
| 39 | * @return mixed An array containing options data to be preserved |
| 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 | * @param array $optionToPreserveData |
| 54 | * @param string $optionsTableName |
| 55 | * @return string |
| 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 | * @param string $whereCondition |
| 74 | * @param string $tableName |
| 75 | * @return mixed |
| 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 | * @param mixed $db |
| 89 | * @return void |
| 90 | */ |
| 91 | public function setProductionDb($db) |
| 92 | { |
| 93 | $this->productionDb = $db; |
| 94 | } |
| 95 | } |
| 96 |