PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.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 / Staging / Tasks / DatabaseAdjustmentTask.php
wp-staging / Staging / Tasks Last commit date
StagingSite 1 day ago StagingSiteCreate 1 day ago StagingSiteDelete 1 day ago StagingSiteReset 1 day ago StagingSiteUpdate 1 day ago DataAdjustmentTask.php 1 day ago DatabaseAdjustmentTask.php 1 day ago FileAdjustmentTask.php 1 day ago FileCopierTask.php 1 day ago StagingTask.php 1 day ago
DatabaseAdjustmentTask.php
181 lines
1 <?php
2
3 namespace WPStaging\Staging\Tasks;
4
5 use WPStaging\Framework\Adapter\Database;
6 use WPStaging\Framework\Database\TableService;
7 use WPStaging\Framework\Queue\SeekableQueueInterface;
8 use WPStaging\Framework\Job\Dto\StepsDto;
9 use WPStaging\Framework\Utils\Cache\Cache;
10 use WPStaging\Framework\Utils\Urls;
11 use WPStaging\Staging\Traits\WithStagingDatabase;
12 use WPStaging\Vendor\Psr\Log\LoggerInterface;
13
14 abstract class DatabaseAdjustmentTask extends DataAdjustmentTask
15 {
16 use WithStagingDatabase;
17
18
19
20
21 protected $database = null;
22
23
24
25
26 protected $wpdb = null;
27
28
29
30
31
32
33
34
35
36 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue, Urls $urls, Database $database)
37 {
38 parent::__construct($logger, $cache, $stepsDto, $taskQueue, $urls);
39 $this->database = $database;
40 }
41
42
43
44
45 public function setup()
46 {
47 $this->initStagingDatabase($this->getStagingSiteDto($this->jobDataDto->getCloneId()));
48 if ($this->tableService === null) {
49 $this->tableService = new TableService($this->stagingDb);
50 }
51
52 if ($this->wpdb === null) {
53 $this->wpdb = $this->stagingDb->getWpdb();
54 }
55 }
56
57
58
59
60
61
62 protected function isTableExists(string $tableName): bool
63 {
64 return $this->tableService->tableExists($tableName);
65 }
66
67
68
69
70
71
72 protected function isTableExcluded(string $tableNameWithoutPrefix): bool
73 {
74 $tableName = $this->getPrefixedStagingTableName($tableNameWithoutPrefix);
75
76 if (!$this->isTableExists($tableName)) {
77 return true;
78 }
79
80 if (in_array($tableNameWithoutPrefix, $this->jobDataDto->getExcludedTables())) {
81 return true;
82 }
83
84 return false;
85 }
86
87 protected function getPrefixedStagingTableName(string $tableName): string
88 {
89 return $this->jobDataDto->getDatabasePrefix() . $tableName;
90 }
91
92 protected function isOptionsTableExcluded(): bool
93 {
94 if ($this->isTableExcluded('options')) {
95 return true;
96 }
97
98 return false;
99 }
100
101
102
103
104
105
106
107 protected function insertOption(string $optionName, $optionValue, bool $autoload = false): bool
108 {
109
110 $this->deleteOption($optionName);
111
112 $optionTable = $this->getOptionsTableName();
113 return $this->executeQuery(
114 "INSERT INTO `{$optionTable}` (option_name, option_value, autoload) VALUES (%s, %s, %s)",
115 $optionName,
116 $optionValue,
117 $autoload ? 'on' : 'off'
118 );
119 }
120
121
122
123
124
125
126 protected function updateOption(string $optionName, string $optionValue): bool
127 {
128 $optionTable = $this->getOptionsTableName();
129 return $this->executeQuery(
130 "UPDATE `{$optionTable}` SET `option_value` = %s WHERE `option_name` = %s;",
131 $optionValue,
132 $optionName
133 );
134 }
135
136
137
138
139
140 protected function deleteOption(string $optionName): bool
141 {
142 $optionTable = $this->getOptionsTableName();
143 return $this->executeQuery(
144 "DELETE FROM `{$optionTable}` WHERE `option_name` = %s;",
145 $optionName
146 );
147 }
148
149 protected function getOptionsTableName(): string
150 {
151 return $this->getPrefixedStagingTableName('options');
152 }
153
154
155
156
157
158
159 protected function executeQuery(string $query, ...$parameters): bool
160 {
161 $result = $this->wpdb->query(
162 $this->wpdb->prepare(
163 $query,
164 $parameters
165 )
166 );
167
168 if ($result === false) {
169 $this->logger->debug("Database adjustment failed. Query: {$query}.");
170 return false;
171 }
172
173 return true;
174 }
175
176 protected function lastError(): string
177 {
178 return $this->wpdb->last_error;
179 }
180 }
181