Cleaners
5 years ago
Exceptions
5 years ago
Cancel.php
3 years ago
CancelUpdate.php
4 years ago
Cloning.php
3 years ago
CloningProcess.php
3 years ago
Data.php
3 years ago
Database.php
3 years ago
Delete.php
3 years ago
Directories.php
3 years ago
Files.php
3 years ago
Finish.php
3 years ago
Job.php
3 years ago
JobExecutable.php
3 years ago
Logs.php
3 years ago
PreserveDataFirstStep.php
3 years ago
PreserveDataSecondStep.php
3 years ago
ProcessLock.php
3 years ago
Scan.php
3 years ago
SearchReplace.php
3 years ago
TotalStepsAreNumberOfTables.php
5 years ago
Updating.php
3 years ago
Verify.php
3 years ago
Finish.php
206 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Core\Utils\Helper; |
| 7 | use WPStaging\Framework\Analytics\Actions\AnalyticsStagingCreate; |
| 8 | use WPStaging\Framework\Analytics\Actions\AnalyticsStagingReset; |
| 9 | use WPStaging\Framework\Analytics\Actions\AnalyticsStagingUpdate; |
| 10 | use WPStaging\Framework\Staging\Sites; |
| 11 | use WPStaging\Framework\Adapter\SourceDatabase; |
| 12 | |
| 13 | /** |
| 14 | * Class Finish |
| 15 | * @package WPStaging\Backend\Modules\Jobs |
| 16 | */ |
| 17 | class Finish extends Job |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * Clone Key |
| 22 | * @var string |
| 23 | */ |
| 24 | private $clone = ''; |
| 25 | |
| 26 | /** |
| 27 | * @var SourceDatabase |
| 28 | */ |
| 29 | private $sourceDatabase; |
| 30 | |
| 31 | /** |
| 32 | * Start Module |
| 33 | * @return object |
| 34 | * @throws \Exception |
| 35 | */ |
| 36 | public function start() |
| 37 | { |
| 38 | $this->sourceDatabase = WPStaging::make(SourceDatabase::class); |
| 39 | $this->sourceDatabase->setOptions($this->options); |
| 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->addExcludedFilesInCloneDB(); |
| 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 | switch ($this->options->mainJob) { |
| 67 | case 'cloning': |
| 68 | WPStaging::make(AnalyticsStagingCreate::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options); |
| 69 | break; |
| 70 | case 'updating': |
| 71 | WPStaging::make(AnalyticsStagingUpdate::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options); |
| 72 | break; |
| 73 | case 'resetting': |
| 74 | WPStaging::make(AnalyticsStagingReset::class)->enqueueFinishEvent($this->options->jobIdentifier, $this->options); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | do_action('wpstg_cloning_complete', $this->options); |
| 79 | |
| 80 | $this->logger->info("################## FINISH ##################"); |
| 81 | |
| 82 | return (object) $return; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Delete Cache Files |
| 87 | * @throws \Exception |
| 88 | */ |
| 89 | protected function deleteCacheFiles() |
| 90 | { |
| 91 | $this->log("Finish: Deleting clone job's cache files..."); |
| 92 | |
| 93 | $this->cache->delete("clone_options"); |
| 94 | $this->cache->delete("files_to_copy"); |
| 95 | |
| 96 | $this->log("Finish: Clone job's cache files have been deleted!"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add excluded files data in clone database as option |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | protected function addExcludedFilesInCloneDB() |
| 105 | { |
| 106 | $this->log("Finish: Adding Excluded files option..."); |
| 107 | if (isset($this->options->tmpExcludedFilesFullPath)) { |
| 108 | if ($this->sourceDatabase->addOrUpdateClonedSiteOption(Sites::STAGING_EXCLUDED_FILES_OPTION, array_unique($this->options->tmpExcludedFilesFullPath)) === false) { |
| 109 | $this->log("Finish: Failed to add excluded files option in clone database!"); |
| 110 | } |
| 111 | unset($this->options->tmpExcludedFilesFullPath); |
| 112 | } |
| 113 | $this->log("Finish: Successfully added excluded files option in clone database!"); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Prepare clone records |
| 118 | * |
| 119 | * @todo check if this is being used, remove otherwise. |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | protected function prepareCloneDataRecords() |
| 124 | { |
| 125 | // Check if clones still exist |
| 126 | $this->log("Finish: Verifying existing clones..."); |
| 127 | |
| 128 | // Clone data already exists |
| 129 | if (isset($this->options->existingClones[$this->options->clone])) { |
| 130 | if ($this->isMultisiteAndPro()) { |
| 131 | $this->options->existingClones[$this->options->clone]['url'] = $this->getDestinationUrl(); |
| 132 | } |
| 133 | |
| 134 | $this->options->existingClones[$this->options->clone]['datetime'] = time(); |
| 135 | $this->options->existingClones[$this->options->clone]['status'] = 'finished'; |
| 136 | $this->options->existingClones[$this->options->clone]['prefix'] = $this->options->prefix; |
| 137 | $this->options->existingClones[$this->options->clone]['cronDisabled'] = isset($this->options->cronDisabled) ? (bool) $this->options->cronDisabled : false; |
| 138 | $this->options->existingClones[$this->options->clone]['emailsAllowed'] = (bool) $this->options->emailsAllowed; |
| 139 | $this->options->existingClones[$this->options->clone]['uploadsSymlinked'] = (bool) $this->options->uploadsSymlinked; |
| 140 | $this->options->existingClones[$this->options->clone]['includedTables'] = $this->options->tables; |
| 141 | $this->options->existingClones[$this->options->clone]['excludeSizeRules'] = $this->options->excludeSizeRules; |
| 142 | $this->options->existingClones[$this->options->clone]['excludeGlobRules'] = $this->options->excludeGlobRules; |
| 143 | $this->options->existingClones[$this->options->clone]['excludedDirectories'] = $this->options->excludedDirectories; |
| 144 | $this->options->existingClones[$this->options->clone]['extraDirectories'] = $this->options->extraDirectories; |
| 145 | update_option(Sites::STAGING_SITES_OPTION, $this->options->existingClones); |
| 146 | $this->log("Finish: The job finished!"); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | $this->log("Finish: {$this->options->clone}'s clone job's data is not in database, generating data"); |
| 151 | |
| 152 | $this->options->existingClones[$this->clone] = [ |
| 153 | "directoryName" => $this->options->cloneDirectoryName, |
| 154 | "path" => trailingslashit($this->options->destinationDir), |
| 155 | "url" => $this->getDestinationUrl(), |
| 156 | "number" => $this->options->cloneNumber, |
| 157 | "version" => WPStaging::getVersion(), |
| 158 | "status" => "finished", |
| 159 | "prefix" => $this->options->prefix, |
| 160 | "datetime" => time(), |
| 161 | "databaseUser" => $this->options->databaseUser, |
| 162 | "databasePassword" => $this->options->databasePassword, |
| 163 | "databaseDatabase" => $this->options->databaseDatabase, |
| 164 | "databaseServer" => $this->options->databaseServer, |
| 165 | "databasePrefix" => $this->options->databasePrefix, |
| 166 | "databaseSsl" => (bool)$this->options->databaseSsl, |
| 167 | "emailsAllowed" => (bool) $this->options->emailsAllowed, |
| 168 | "uploadsSymlinked" => (bool) $this->options->uploadsSymlinked, |
| 169 | "includedTables" => $this->options->tables, |
| 170 | "excludeSizeRules" => $this->options->excludeSizeRules, |
| 171 | "excludeGlobRules" => $this->options->excludeGlobRules, |
| 172 | "excludedDirectories" => $this->options->excludedDirectories, |
| 173 | "extraDirectories" => $this->options->extraDirectories, |
| 174 | "networkClone" => $this->isNetworkClone(), |
| 175 | ]; |
| 176 | |
| 177 | if (update_option(Sites::STAGING_SITES_OPTION, $this->options->existingClones) === false) { |
| 178 | $this->log("Finish: Failed to save {$this->options->clone}'s clone job data to database'"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get destination Hostname depending on whether WP has been installed in sub dir or not |
| 187 | * @return string |
| 188 | */ |
| 189 | private function getDestinationUrl() |
| 190 | { |
| 191 | |
| 192 | if (!empty($this->options->cloneHostname)) { |
| 193 | return $this->options->cloneHostname; |
| 194 | } |
| 195 | |
| 196 | // if this is single site |
| 197 | if (!$this->isMultisiteAndPro()) { |
| 198 | return trailingslashit(get_site_url()) . $this->options->cloneDirectoryName; |
| 199 | } |
| 200 | |
| 201 | // The relative path to the main multisite without appending a trailingslash e.g. wordpress |
| 202 | $multisitePath = defined('PATH_CURRENT_SITE') ? PATH_CURRENT_SITE : '/'; |
| 203 | return rtrim((new Helper())->getBaseUrl(), '/\\') . $multisitePath . $this->options->cloneDirectoryName; |
| 204 | } |
| 205 | } |
| 206 |