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