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