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