PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.5.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.5.0
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
Exporter 1 year ago QueryBuilder 2 years ago DbInfo.php 6 months ago ExcludedTables.php 5 months ago OptionPreservationHandler.php 2 years ago SearchReplace.php 6 months ago SelectedTables.php 8 months ago TableDto.php 1 year ago TableService.php 9 months ago TablesRenamer.php 5 months ago WpDbInfo.php 8 months 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