PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.9.5
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.9.5
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 / Backend / Modules / Jobs / Updating.php
wp-staging / Backend / Modules / Jobs Last commit date
Cleaners 4 months ago Exceptions 5 years ago Cancel.php 8 months ago CancelUpdate.php 8 months ago Cloning.php 6 days ago CloningProcess.php 6 days ago Data.php 8 months ago Database.php 6 days ago Delete.php 6 days ago Directories.php 5 months ago Files.php 1 month ago Finish.php 6 days ago Job.php 6 days ago JobExecutable.php 8 months ago Logs.php 3 years ago PreserveDataFirstStep.php 2 months ago PreserveDataSecondStep.php 2 months ago ProcessLock.php 1 year ago Scan.php 3 weeks ago SearchReplace.php 6 months ago TotalStepsAreNumberOfTables.php 5 years ago Updating.php 6 days ago
Updating.php
281 lines
1 <?php
2
3 namespace WPStaging\Backend\Modules\Jobs;
4
5 use Exception;
6 use stdClass;
7 use WPStaging\Core\WPStaging;
8 use WPStaging\Framework\Database\SelectedTables;
9 use WPStaging\Framework\Filesystem\PathIdentifier;
10 use WPStaging\Framework\Filesystem\Scanning\ScanConst;
11 use WPStaging\Framework\Utils\Urls;
12 use WPStaging\Framework\Utils\Sanitize;
13 use WPStaging\Framework\Utils\WpDefaultDirectories;
14 use WPStaging\Framework\Traits\ValueGetterTrait;
15
16 /**
17 * Class Updating
18 * @package WPStaging\Backend\Modules\Jobs
19 */
20 class Updating extends Job
21 {
22 use ValueGetterTrait;
23
24 /**
25 * External Database Used
26 * @var bool
27 */
28 public $isExternalDb;
29
30 /**
31 * @var string
32 */
33 private $mainJob;
34
35 /**
36 * @var WpDefaultDirectories
37 */
38 private $dirUtils;
39
40 /**
41 * @var Sanitize
42 */
43 private $sanitize;
44
45 /**
46 * @var Urls
47 */
48 private $urls;
49
50 /**
51 * Initialize is called in \Job
52 */
53 public function initialize()
54 {
55 $this->mainJob = Job::UPDATE;
56 $this->dirUtils = new WpDefaultDirectories();
57 $this->sanitize = WPStaging::make(Sanitize::class);
58 $this->urls = WPStaging::make(Urls::class);
59 }
60
61 /**
62 * @param $mainJob
63 */
64 public function setMainJob($mainJob)
65 {
66 $this->mainJob = $mainJob;
67 }
68
69 /**
70 * @return string
71 */
72 public function getMainJob()
73 {
74 return $this->mainJob;
75 }
76
77 /**
78 * Save Chosen Cloning Settings
79 * @return bool
80 * @throws Exception
81 */
82 public function save()
83 {
84 if (!isset($_POST) || !isset($_POST["cloneID"])) {
85 return false;
86 }
87
88 // Delete files to copy listing
89 $this->filesIndexCache->delete();
90
91 // Generate Options
92 $this->options->clone = preg_replace("#\W+#", '-', strtolower($this->sanitize->sanitizeString($_POST["cloneID"])));
93 $this->options->cloneNumber = 1;
94 $this->options->includedDirectories = [];
95 $this->options->excludedDirectories = [];
96 $this->options->extraDirectories = [];
97 $this->options->excludeGlobRules = [];
98 $this->options->excludeSizeRules = [];
99 $this->options->excludedFiles = [
100 '.htaccess',
101 '.DS_Store',
102 '*.git',
103 '*.svn',
104 '*.tmp',
105 'desktop.ini',
106 '.gitignore',
107 '*.log',
108 'object-cache.php',
109 'web.config', // Important: Windows IIS configuration file. Do not copy this to the staging site is staging site is placed into subfolder
110 '.wp-staging-cloneable', // File which make staging site to be cloneable
111 ];
112
113 $this->options->excludedFilesFullPath = [
114 PathIdentifier::IDENTIFIER_WP_CONTENT . 'db.php',
115 PathIdentifier::IDENTIFIER_WP_CONTENT . 'object-cache.php',
116 PathIdentifier::IDENTIFIER_WP_CONTENT . 'advanced-cache.php',
117 ];
118
119 // Define mainJob to differentiate between cloning, updating and pushing
120 $this->options->mainJob = $this->mainJob;
121
122 // Only exclude wp-config.php during UPDATE not RESET
123 if ($this->excludeWpConfigDuringUpdate()) {
124 $this->options->excludedFilesFullPath[] = 'wp-config.php';
125 }
126
127 // Job
128 $this->options->job = new stdClass();
129 $this->loadLegacyExistingClones();
130
131 // Make sure it is always enabled for free version
132 $this->options->isEmailsAllowed = true;
133 // Check if clone data already exists and use that one
134 if (isset($this->options->existingClones[$this->options->clone])) {
135 $currentStagingSite = $this->options->existingClones[$this->options->clone];
136 $this->options->current = $this->options->clone;
137 $this->options->currentClone = $currentStagingSite;
138 $this->options->cloneName = $this->getValueFromArray('cloneName', $currentStagingSite);
139 $this->options->cloneDirectoryName = $this->getValueFromArray('directoryName', $currentStagingSite);
140 $this->options->cloneNumber = $this->getValueFromArray('number', $currentStagingSite);
141 $this->options->databaseUser = $this->getValueFromArray('databaseUser', $currentStagingSite);
142 $this->options->databasePassword = $this->getValueFromArray('databasePassword', $currentStagingSite);
143 $this->options->databaseDatabase = $this->getValueFromArray('databaseDatabase', $currentStagingSite);
144 $this->options->databaseServer = $this->getValueFromArray('databaseServer', $currentStagingSite);
145 $this->options->databasePrefix = $this->getValueFromArray('databasePrefix', $currentStagingSite);
146 $this->options->databaseSsl = $this->getValueFromArray('databaseSsl', $currentStagingSite);
147 $this->options->useCustomDatabase = $this->externalDatabaseConfiguration->isEnabled($currentStagingSite);
148 $this->options->destinationHostname = $this->getValueFromArray('url', $currentStagingSite);
149 $this->options->uploadsSymlinked = $this->getValueFromArray('uploadsSymlinked', $currentStagingSite);
150 $this->options->prefix = $this->getValueFromArray('prefix', $currentStagingSite);
151 $this->options->isEmailsAllowed = $this->getValueFromArray('isEmailsAllowed', $currentStagingSite);
152 $this->options->networkClone = filter_var($this->getValueFromArray('networkClone', $currentStagingSite), FILTER_VALIDATE_BOOLEAN);
153 $this->options->homeHostname = $this->urls->getHomeUrlWithoutScheme();
154 $this->options->useNewAdminAccount = $this->getValueFromArray('useNewAdminAccount', $currentStagingSite);
155 $this->options->adminEmail = $this->getValueFromArray('adminEmail', $currentStagingSite);
156 $this->options->adminPassword = $this->getValueFromArray('adminPassword', $currentStagingSite);
157 $this->options->isWooSchedulerEnabled = $this->getValueFromArray('isWooSchedulerEnabled', $currentStagingSite);
158 $this->options->isEmailsReminderEnabled = $this->getValueFromArray('isEmailsReminderEnabled', $currentStagingSite);
159 $this->options->isAutoUpdatePlugins = $this->getValueFromArray('isAutoUpdatePlugins', $currentStagingSite);
160 } else {
161 $job = 'update';
162 if ($this->mainJob === Job::RESET) {
163 $job = 'reset';
164 }
165
166 wp_die(sprintf("Fatal Error: Can not %s clone because there is no clone data.", esc_html($job)));
167 }
168
169 if (!$this->options->useCustomDatabase) {
170 $this->options->databaseUser = '';
171 $this->options->databasePassword = '';
172 $this->options->databaseDatabase = '';
173 $this->options->databaseServer = 'localhost';
174 $this->options->databasePrefix = '';
175 $this->options->databaseSsl = false;
176 }
177
178 $this->isExternalDb = (bool)$this->options->useCustomDatabase;
179
180 /**
181 * @see /WPStaging/Framework/CloningProcess/ExcludedPlugins.php to exclude plugins
182 * Only add other directories here
183 */
184 $excludedDirectories = [
185 PathIdentifier::IDENTIFIER_WP_CONTENT . 'cache',
186 ];
187
188 // Add upload folder to list of excluded directories for push if symlink option is enabled
189 if ($this->options->uploadsSymlinked) {
190 $excludedDirectories[] = PathIdentifier::IDENTIFIER_UPLOADS;
191 }
192
193 $this->options->excludedDirectories = $excludedDirectories;
194
195 $this->setTablesForUpdateJob();
196 $this->setDirectoriesForUpdateJob();
197
198 if (defined('WPSTGPRO_VERSION') && $this->mainJob !== Job::RESET) {
199 $this->options->isEmailsAllowed = isset($_POST['isEmailsAllowed']) && $this->sanitize->sanitizeBool($_POST['isEmailsAllowed']);
200 $this->options->isWooSchedulerEnabled = isset($_POST['isWooSchedulerEnabled']) && $this->sanitize->sanitizeBool($_POST['isWooSchedulerEnabled']);
201 $this->options->isEmailsReminderEnabled = isset($_POST['isEmailsReminderEnabled']) && $this->sanitize->sanitizeBool($_POST['isEmailsReminderEnabled']);
202 $this->options->isAutoUpdatePlugins = isset($_POST['isAutoUpdatePlugins']) && $this->sanitize->sanitizeBool($_POST['isAutoUpdatePlugins']);
203 }
204
205 $this->options->cloneDir = $this->options->existingClones[$this->options->clone]['path'];
206 $this->options->destinationDir = $this->getDestinationDir();
207 $this->options->cloneHostname = $this->options->destinationHostname;
208
209 // Process lock state
210 $this->options->isRunning = true;
211 $this->initializeLegacyStagingRun($this->mainJob);
212
213 return $this->saveOptions();
214 }
215
216 /**
217 * Get Destination Directory including staging subdirectory
218 * @return string
219 */
220 private function getDestinationDir()
221 {
222 if (empty($this->options->cloneDir)) {
223 return trailingslashit(WPStaging::getWPpath() . $this->options->cloneDirectoryName);
224 }
225
226 return trailingslashit($this->options->cloneDir);
227 }
228
229 private function setDirectoriesForUpdateJob()
230 {
231 // Exclude Glob Rules
232 $this->options->excludeGlobRules = [];
233 if (!empty($_POST["excludeGlobRules"])) {
234 $this->options->excludeGlobRules = $this->sanitize->sanitizeExcludeRules($_POST["excludeGlobRules"]);
235 }
236
237 $this->options->excludeSizeRules = [];
238 if (!empty($_POST["excludeSizeRules"])) {
239 $this->options->excludeSizeRules = $this->sanitize->sanitizeExcludeRules($_POST["excludeSizeRules"]);
240 }
241
242 // Excluded Directories
243 $excludedDirectoriesRequest = isset($_POST["excludedDirectories"]) ? $this->sanitize->sanitizeString($_POST["excludedDirectories"]) : '';
244 $excludedDirectoriesRequest = $this->dirUtils->getExcludedDirectories($excludedDirectoriesRequest);
245 $this->options->excludedDirectories = array_merge($this->options->excludedDirectories, $excludedDirectoriesRequest);
246 // Extra Directories
247 if (isset($_POST["extraDirectories"])) {
248 $this->options->extraDirectories = explode(ScanConst::DIRECTORIES_SEPARATOR, $this->sanitize->sanitizeString($_POST["extraDirectories"]));
249 }
250
251 // delete uploads folder before copying if uploads is not symlinked
252 $this->options->deleteUploadsFolder = !$this->options->uploadsSymlinked && isset($_POST['cleanUploadsDir']) && $this->sanitize->sanitizeBool($_POST['cleanUploadsDir']);
253 // should not backup uploads during update process
254 $this->options->backupUploadsFolder = false;
255 // clean plugins and themes dir before updating
256 $this->options->deletePluginsAndThemes = isset($_POST['cleanPluginsThemes']) && $this->sanitize->sanitizeBool($_POST['cleanPluginsThemes']);
257 // set default statuses for backup of uploads dir and cleaning of uploads, themes and plugins dirs
258 $this->options->statusBackupUploadsDir = 'skipped';
259 $this->options->statusContentCleaner = 'pending';
260 }
261
262 private function setTablesForUpdateJob()
263 {
264 // Included Tables / Prefixed Table - Excluded Tables
265 $includedTables = isset($_POST['includedTables']) ? $this->sanitize->sanitizeString($_POST['includedTables']) : '';
266 $excludedTables = isset($_POST['excludedTables']) ? $this->sanitize->sanitizeString($_POST['excludedTables']) : '';
267 $selectedTablesWithoutPrefix = isset($_POST['selectedTablesWithoutPrefix']) ? $this->sanitize->sanitizeString($_POST['selectedTablesWithoutPrefix']) : '';
268 $selectedTables = new SelectedTables($includedTables, $excludedTables, $selectedTablesWithoutPrefix);
269 $selectedTables->setAllTablesExcluded(empty($_POST['allTablesExcluded']) ? false : $this->sanitize->sanitizeBool($_POST['allTablesExcluded']));
270 $this->options->tables = $selectedTables->getSelectedTables($this->options->networkClone);
271 }
272
273 /**
274 * Start the cloning job
275 * not used but is abstract
276 */
277 public function start()
278 {
279 }
280 }
281