PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 / Finish.php
wp-staging / Backend / Modules / Jobs Last commit date
Cleaners 3 months ago Exceptions 5 years ago Cancel.php 7 months ago CancelUpdate.php 7 months ago Cloning.php 2 weeks ago CloningProcess.php 1 year ago Data.php 7 months ago Database.php 3 months ago Delete.php 4 months ago Directories.php 4 months ago Files.php 1 week ago Finish.php 2 months ago Job.php 2 weeks ago JobExecutable.php 7 months ago Logs.php 3 years ago PreserveDataFirstStep.php 1 month ago PreserveDataSecondStep.php 1 month ago ProcessLock.php 1 year ago Scan.php 4 months ago SearchReplace.php 5 months ago TotalStepsAreNumberOfTables.php 5 years ago Updating.php 2 weeks ago
Finish.php
195 lines
1 <?php
2
3 namespace WPStaging\Backend\Modules\Jobs;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Analytics\Actions\AnalyticsStagingCreate;
7 use WPStaging\Framework\Analytics\Actions\AnalyticsStagingReset;
8 use WPStaging\Framework\Analytics\Actions\AnalyticsStagingUpdate;
9 use WPStaging\Framework\Logger\EventLoggerConst;
10 use WPStaging\Staging\Sites;
11 use WPStaging\Framework\Traits\EventLoggerTrait;
12 use WPStaging\Framework\Utils\Urls;
13 use WPStaging\Staging\Jobs\StagingSiteCreate;
14
15 /**
16 * Class Finish
17 * @package WPStaging\Backend\Modules\Jobs
18 */
19 class Finish extends Job
20 {
21 use EventLoggerTrait;
22
23 /**
24 * Clone Key
25 * @var string
26 */
27 private $clone = '';
28
29 /**
30 * @var Urls
31 */
32 private $urls;
33
34 /**
35 * Start Module
36 * @return object
37 * @throws \Exception
38 */
39 public function start()
40 {
41 $this->urls = WPStaging::make(Urls::class);
42
43 // sanitize the clone name before saving
44 $this->clone = preg_replace("#\W+#", '-', strtolower($this->options->clone));
45
46 $this->deleteCacheFiles();
47
48 // Prepare clone records & save scanned directories for delete job later
49 $this->prepareCloneDataRecords();
50
51 $this->options->isRunning = false;
52
53 $return = [
54 "directoryName" => $this->options->cloneDirectoryName,
55 "path" => trailingslashit($this->options->destinationDir),
56 "url" => $this->getDestinationUrl(),
57 "number" => $this->options->cloneNumber,
58 "version" => WPStaging::getVersion(),
59 "status" => 'finished',
60 "prefix" => $this->options->prefix,
61 "last_msg" => $this->logger->getLastLogMsg(),
62 "job" => $this->options->currentJob,
63 "percentage" => 100,
64 ];
65
66 $processType = EventLoggerConst::PROCESS_PREFIX_CLONE;
67 $successMessage = "Staging site successfully created";
68 switch ($this->options->mainJob) {
69 case Job::STAGING:
70 WPStaging::make(AnalyticsStagingCreate::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options);
71 break;
72 case Job::UPDATE:
73 $processType = EventLoggerConst::PROCESS_PREFIX_CLONE_UPDATE;
74 $successMessage = 'Staging site successfully updated';
75 WPStaging::make(AnalyticsStagingUpdate::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options);
76 break;
77 case Job::RESET:
78 $processType = EventLoggerConst::PROCESS_PREFIX_CLONE_RESET;
79 $successMessage = 'Staging site successfully reset';
80 WPStaging::make(AnalyticsStagingReset::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options);
81 break;
82 }
83
84 do_action(StagingSiteCreate::ACTION_CLONING_COMPLETE, $this->options);
85
86 $this->logger->info("" . $successMessage);
87 $this->logCloneCompleted($processType);
88 return (object) $return;
89 }
90
91 /**
92 * Delete Cache Files
93 * @throws \Exception
94 */
95 protected function deleteCacheFiles()
96 {
97 $this->log("Finish: Deleting clone job's cache files...");
98
99 $this->cloneOptionCache->delete();
100 $this->filesIndexCache->delete();
101
102 $this->log("Finish: Clone job's cache files have been deleted!");
103 }
104
105 /**
106 * Prepare clone records. Without this clone data will not get updated in Sites::STAGING_SITES_OPTION during updating process.
107 *
108 * @return bool
109 */
110 protected function prepareCloneDataRecords()
111 {
112 // Check if clones still exist
113 $this->log("Finish: Verifying existing clones...");
114
115 // Clone data already exists
116 if (isset($this->options->existingClones[$this->options->clone])) {
117 if ($this->isMultisiteAndPro()) {
118 $this->options->existingClones[$this->options->clone]['url'] = $this->getDestinationUrl();
119 }
120
121 $this->options->existingClones[$this->options->clone]['datetime'] = time();
122 $this->options->existingClones[$this->options->clone]['status'] = 'finished';
123 $this->options->existingClones[$this->options->clone]['prefix'] = $this->options->prefix;
124 $this->options->existingClones[$this->options->clone]['isCronEnabled'] = empty($this->options->isCronEnabled) ? false : true;
125 $this->options->existingClones[$this->options->clone]['isEmailsAllowed'] = (bool) $this->options->isEmailsAllowed;
126 $this->options->existingClones[$this->options->clone]['uploadsSymlinked'] = (bool) $this->options->uploadsSymlinked;
127 $this->options->existingClones[$this->options->clone]['includedTables'] = $this->options->tables;
128 $this->options->existingClones[$this->options->clone]['excludeSizeRules'] = $this->options->excludeSizeRules;
129 $this->options->existingClones[$this->options->clone]['excludeGlobRules'] = $this->options->excludeGlobRules;
130 $this->options->existingClones[$this->options->clone]['excludedDirectories'] = $this->options->excludedDirectories;
131 $this->options->existingClones[$this->options->clone]['extraDirectories'] = $this->options->extraDirectories;
132 $this->options->existingClones[$this->options->clone]['isWooSchedulerEnabled'] = empty($this->options->isWooSchedulerEnabled) ? false : true;
133 $this->options->existingClones[$this->options->clone]['isEmailsReminderEnabled'] = empty($this->options->isEmailsReminderEnabled) ? false : true;
134 $this->options->existingClones[$this->options->clone]['isAutoUpdatePlugins'] = empty($this->options->isAutoUpdatePlugins) ? false : true;
135 update_option(Sites::STAGING_SITES_OPTION, $this->options->existingClones, false);
136 $this->log("Finish: The job finished!");
137 return true;
138 }
139
140 $this->log("Finish: {$this->options->clone}'s clone job's data is not in database, generating data");
141
142 $this->options->existingClones[$this->clone] = [
143 "directoryName" => $this->options->cloneDirectoryName,
144 "path" => trailingslashit($this->options->destinationDir),
145 "url" => $this->getDestinationUrl(),
146 "number" => $this->options->cloneNumber,
147 "version" => WPStaging::getVersion(),
148 "status" => "finished",
149 "prefix" => $this->options->prefix,
150 "datetime" => time(),
151 "databaseUser" => $this->options->databaseUser,
152 "databasePassword" => $this->options->databasePassword,
153 "databaseDatabase" => $this->options->databaseDatabase,
154 "databaseServer" => $this->options->databaseServer,
155 "databasePrefix" => $this->options->databasePrefix,
156 "databaseSsl" => (bool)$this->options->databaseSsl,
157 "isEmailsAllowed" => (bool) $this->options->isEmailsAllowed,
158 "uploadsSymlinked" => (bool) $this->options->uploadsSymlinked,
159 "includedTables" => $this->options->tables,
160 "excludeSizeRules" => $this->options->excludeSizeRules,
161 "excludeGlobRules" => $this->options->excludeGlobRules,
162 "excludedDirectories" => $this->options->excludedDirectories,
163 "extraDirectories" => $this->options->extraDirectories,
164 "networkClone" => $this->isNetworkClone(),
165 ];
166
167 if (update_option(Sites::STAGING_SITES_OPTION, $this->options->existingClones) === false) {
168 $this->log("Finish: Failed to save {$this->options->clone}'s clone job data to database'");
169 return false;
170 }
171
172 return true;
173 }
174
175 /**
176 * Get destination Hostname depending on whether WP has been installed in sub dir or not
177 * @return string
178 */
179 private function getDestinationUrl()
180 {
181 if (!empty($this->options->cloneHostname)) {
182 return $this->options->cloneHostname;
183 }
184
185 // if this is single site
186 if (!$this->isMultisiteAndPro()) {
187 return trailingslashit(get_site_url()) . $this->options->cloneDirectoryName;
188 }
189
190 // The relative path to the main multisite without appending a trailingslash e.g. wordpress
191 $multisitePath = defined('PATH_CURRENT_SITE') ? PATH_CURRENT_SITE : '/';
192 return rtrim($this->urls->getBaseUrl(), '/\\') . $multisitePath . $this->options->cloneDirectoryName;
193 }
194 }
195