PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.0.4
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.0.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 / OptionPreservationHandler.php
wp-staging / Framework / Database Last commit date
QueryBuilder 2 years ago DbInfo.php 3 years ago ExcludedTables.php 4 years ago OptionPreservationHandler.php 2 years ago SearchReplace.php 3 years ago SelectedTables.php 2 years ago TableDto.php 5 years ago TableService.php 2 years ago TablesRenamer.php 2 years ago WpDbInfo.php 2 years ago iDbInfo.php 2 years ago
OptionPreservationHandler.php
94 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 return $likeStatement;
32 }
33
34 /**
35 * @param string $whereCondition
36 * @param string $optionsTable
37 *
38 * @return mixed An array containing options data to be preserved
39 */
40 public function getOptionsDataToPreserve(string $whereCondition, string $optionsTable)
41 {
42 return $this->productionDb->get_results(
43 sprintf(
44 "SELECT * FROM `$optionsTable` WHERE %s",
45 $whereCondition
46 ),
47 ARRAY_A
48 );
49 }
50
51 /**
52 * @param array $optionToPreserveData
53 * @param string $optionsTableName
54 * @return string
55 */
56 public function createInsertQuery(array $optionToPreserveData, string $optionsTableName): string
57 {
58 $sql = '';
59 foreach ($optionToPreserveData as $option) {
60 $sql .= $this->productionDb->prepare(
61 "INSERT INTO `$optionsTableName` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s );\n",
62 $option['option_name'],
63 $option['option_value'],
64 $option['autoload']
65 );
66 }
67 return $sql;
68 }
69
70 /**
71 * @param string $whereCondition
72 * @param string $tableName
73 * @return mixed
74 */
75 public function deleteFromTable(string $whereCondition, string $tableName)
76 {
77 return $this->productionDb->query(
78 sprintf(
79 "DELETE FROM `$tableName` WHERE %s",
80 $whereCondition
81 )
82 );
83 }
84
85 /**
86 * @param mixed $db
87 * @return void
88 */
89 public function setProductionDb($db)
90 {
91 $this->productionDb = $db;
92 }
93 }
94