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
Updating.php
271 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use Exception; |
| 6 | use stdClass; |
| 7 | use WPStaging\Core\WPStaging; |
| 8 | use WPStaging\Framework\Database\SelectedTables; |
| 9 | use WPStaging\Framework\Filesystem\PathIdentifier; |
| 10 | use WPStaging\Framework\Filesystem\Scanning\ScanConst; |
| 11 | use WPStaging\Framework\Utils\Urls; |
| 12 | use WPStaging\Framework\Utils\Sanitize; |
| 13 | use WPStaging\Framework\Utils\WpDefaultDirectories; |
| 14 | use WPStaging\Framework\Traits\ValueGetterTrait; |
| 15 | |
| 16 | /** |
| 17 | * Class Updating |
| 18 | * @package WPStaging\Backend\Modules\Jobs |
| 19 | */ |
| 20 | class Updating extends Job |
| 21 | { |
| 22 | use ValueGetterTrait; |
| 23 | |
| 24 | /** |
| 25 | * External Database Used |
| 26 | * @var bool |
| 27 | */ |
| 28 | public $isExternalDb; |
| 29 | |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | private $mainJob; |
| 34 | |
| 35 | /** |
| 36 | * @var WpDefaultDirectories |
| 37 | */ |
| 38 | private $dirUtils; |
| 39 | |
| 40 | /** |
| 41 | * @var Sanitize |
| 42 | */ |
| 43 | private $sanitize; |
| 44 | |
| 45 | /** |
| 46 | * @var Urls |
| 47 | */ |
| 48 | private $urls; |
| 49 | |
| 50 | /** |
| 51 | * Initialize is called in \Job |
| 52 | */ |
| 53 | public function initialize() |
| 54 | { |
| 55 | $this->mainJob = Job::UPDATE; |
| 56 | $this->dirUtils = new WpDefaultDirectories(); |
| 57 | $this->sanitize = WPStaging::make(Sanitize::class); |
| 58 | $this->urls = WPStaging::make(Urls::class); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param $mainJob |
| 63 | */ |
| 64 | public function setMainJob($mainJob) |
| 65 | { |
| 66 | $this->mainJob = $mainJob; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return string |
| 71 | */ |
| 72 | public function getMainJob() |
| 73 | { |
| 74 | return $this->mainJob; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Save Chosen Cloning Settings |
| 79 | * @return bool |
| 80 | * @throws Exception |
| 81 | */ |
| 82 | public function save() |
| 83 | { |
| 84 | if (!isset($_POST) || !isset($_POST["cloneID"])) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Delete files to copy listing |
| 89 | $this->filesIndexCache->delete(); |
| 90 | |
| 91 | // Generate Options |
| 92 | $this->options->clone = preg_replace("#\W+#", '-', strtolower($this->sanitize->sanitizeString($_POST["cloneID"]))); |
| 93 | $this->options->cloneNumber = 1; |
| 94 | $this->options->includedDirectories = []; |
| 95 | $this->options->excludedDirectories = []; |
| 96 | $this->options->extraDirectories = []; |
| 97 | $this->options->excludeGlobRules = []; |
| 98 | $this->options->excludeSizeRules = []; |
| 99 | $this->options->excludedFiles = [ |
| 100 | '.htaccess', |
| 101 | '.DS_Store', |
| 102 | '*.git', |
| 103 | '*.svn', |
| 104 | '*.tmp', |
| 105 | 'desktop.ini', |
| 106 | '.gitignore', |
| 107 | '*.log', |
| 108 | 'object-cache.php', |
| 109 | 'web.config', // Important: Windows IIS configuration file. Do not copy this to the staging site is staging site is placed into subfolder |
| 110 | '.wp-staging-cloneable', // File which make staging site to be cloneable |
| 111 | ]; |
| 112 | |
| 113 | $this->options->excludedFilesFullPath = [ |
| 114 | PathIdentifier::IDENTIFIER_WP_CONTENT . 'db.php', |
| 115 | PathIdentifier::IDENTIFIER_WP_CONTENT . 'object-cache.php', |
| 116 | PathIdentifier::IDENTIFIER_WP_CONTENT . 'advanced-cache.php', |
| 117 | ]; |
| 118 | |
| 119 | // Define mainJob to differentiate between cloning, updating and pushing |
| 120 | $this->options->mainJob = $this->mainJob; |
| 121 | |
| 122 | // Only exclude wp-config.php during UPDATE not RESET |
| 123 | if ($this->excludeWpConfigDuringUpdate()) { |
| 124 | $this->options->excludedFilesFullPath[] = 'wp-config.php'; |
| 125 | } |
| 126 | |
| 127 | // Job |
| 128 | $this->options->job = new stdClass(); |
| 129 | $this->loadLegacyExistingClones(); |
| 130 | |
| 131 | // Make sure it is always enabled for free version |
| 132 | $this->options->isEmailsAllowed = true; |
| 133 | // Check if clone data already exists and use that one |
| 134 | if (isset($this->options->existingClones[$this->options->clone])) { |
| 135 | $currentStagingSite = $this->options->existingClones[$this->options->clone]; |
| 136 | $this->options->current = $this->options->clone; |
| 137 | $this->options->currentClone = $currentStagingSite; |
| 138 | $this->options->cloneName = $this->getValueFromArray('cloneName', $currentStagingSite); |
| 139 | $this->options->cloneDirectoryName = $this->getValueFromArray('directoryName', $currentStagingSite); |
| 140 | $this->options->cloneNumber = $this->getValueFromArray('number', $currentStagingSite); |
| 141 | $this->options->databaseUser = $this->getValueFromArray('databaseUser', $currentStagingSite); |
| 142 | $this->options->databasePassword = $this->getValueFromArray('databasePassword', $currentStagingSite); |
| 143 | $this->options->databaseDatabase = $this->getValueFromArray('databaseDatabase', $currentStagingSite); |
| 144 | $this->options->databaseServer = $this->getValueFromArray('databaseServer', $currentStagingSite); |
| 145 | $this->options->databasePrefix = $this->getValueFromArray('databasePrefix', $currentStagingSite); |
| 146 | $this->options->databaseSsl = $this->getValueFromArray('databaseSsl', $currentStagingSite); |
| 147 | $this->options->destinationHostname = $this->getValueFromArray('url', $currentStagingSite); |
| 148 | $this->options->uploadsSymlinked = $this->getValueFromArray('uploadsSymlinked', $currentStagingSite); |
| 149 | $this->options->prefix = $this->getValueFromArray('prefix', $currentStagingSite); |
| 150 | $this->options->isEmailsAllowed = $this->getValueFromArray('isEmailsAllowed', $currentStagingSite); |
| 151 | $this->options->networkClone = filter_var($this->getValueFromArray('networkClone', $currentStagingSite), FILTER_VALIDATE_BOOLEAN); |
| 152 | $this->options->homeHostname = $this->urls->getHomeUrlWithoutScheme(); |
| 153 | $this->options->useNewAdminAccount = $this->getValueFromArray('useNewAdminAccount', $currentStagingSite); |
| 154 | $this->options->adminEmail = $this->getValueFromArray('adminEmail', $currentStagingSite); |
| 155 | $this->options->adminPassword = $this->getValueFromArray('adminPassword', $currentStagingSite); |
| 156 | $this->options->isWooSchedulerEnabled = $this->getValueFromArray('isWooSchedulerEnabled', $currentStagingSite); |
| 157 | $this->options->isEmailsReminderEnabled = $this->getValueFromArray('isEmailsReminderEnabled', $currentStagingSite); |
| 158 | $this->options->isAutoUpdatePlugins = $this->getValueFromArray('isAutoUpdatePlugins', $currentStagingSite); |
| 159 | } else { |
| 160 | $job = 'update'; |
| 161 | if ($this->mainJob === Job::RESET) { |
| 162 | $job = 'reset'; |
| 163 | } |
| 164 | |
| 165 | wp_die(sprintf("Fatal Error: Can not %s clone because there is no clone data.", esc_html($job))); |
| 166 | } |
| 167 | |
| 168 | $this->isExternalDb = !(empty($this->options->databaseUser) && empty($this->options->databasePassword)); |
| 169 | |
| 170 | /** |
| 171 | * @see /WPStaging/Framework/CloningProcess/ExcludedPlugins.php to exclude plugins |
| 172 | * Only add other directories here |
| 173 | */ |
| 174 | $excludedDirectories = [ |
| 175 | PathIdentifier::IDENTIFIER_WP_CONTENT . 'cache', |
| 176 | ]; |
| 177 | |
| 178 | // Add upload folder to list of excluded directories for push if symlink option is enabled |
| 179 | if ($this->options->uploadsSymlinked) { |
| 180 | $excludedDirectories[] = PathIdentifier::IDENTIFIER_UPLOADS; |
| 181 | } |
| 182 | |
| 183 | $this->options->excludedDirectories = $excludedDirectories; |
| 184 | |
| 185 | $this->setTablesForUpdateJob(); |
| 186 | $this->setDirectoriesForUpdateJob(); |
| 187 | |
| 188 | if (defined('WPSTGPRO_VERSION') && $this->mainJob !== Job::RESET) { |
| 189 | $this->options->isEmailsAllowed = isset($_POST['isEmailsAllowed']) && $this->sanitize->sanitizeBool($_POST['isEmailsAllowed']); |
| 190 | $this->options->isWooSchedulerEnabled = isset($_POST['isWooSchedulerEnabled']) && $this->sanitize->sanitizeBool($_POST['isWooSchedulerEnabled']); |
| 191 | $this->options->isEmailsReminderEnabled = isset($_POST['isEmailsReminderEnabled']) && $this->sanitize->sanitizeBool($_POST['isEmailsReminderEnabled']); |
| 192 | $this->options->isAutoUpdatePlugins = isset($_POST['isAutoUpdatePlugins']) && $this->sanitize->sanitizeBool($_POST['isAutoUpdatePlugins']); |
| 193 | } |
| 194 | |
| 195 | $this->options->cloneDir = $this->options->existingClones[$this->options->clone]['path']; |
| 196 | $this->options->destinationDir = $this->getDestinationDir(); |
| 197 | $this->options->cloneHostname = $this->options->destinationHostname; |
| 198 | |
| 199 | // Process lock state |
| 200 | $this->options->isRunning = true; |
| 201 | $this->initializeLegacyStagingRun($this->mainJob); |
| 202 | |
| 203 | return $this->saveOptions(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get Destination Directory including staging subdirectory |
| 208 | * @return string |
| 209 | */ |
| 210 | private function getDestinationDir() |
| 211 | { |
| 212 | if (empty($this->options->cloneDir)) { |
| 213 | return trailingslashit(WPStaging::getWPpath() . $this->options->cloneDirectoryName); |
| 214 | } |
| 215 | |
| 216 | return trailingslashit($this->options->cloneDir); |
| 217 | } |
| 218 | |
| 219 | private function setDirectoriesForUpdateJob() |
| 220 | { |
| 221 | // Exclude Glob Rules |
| 222 | $this->options->excludeGlobRules = []; |
| 223 | if (!empty($_POST["excludeGlobRules"])) { |
| 224 | $this->options->excludeGlobRules = $this->sanitize->sanitizeExcludeRules($_POST["excludeGlobRules"]); |
| 225 | } |
| 226 | |
| 227 | $this->options->excludeSizeRules = []; |
| 228 | if (!empty($_POST["excludeSizeRules"])) { |
| 229 | $this->options->excludeSizeRules = $this->sanitize->sanitizeExcludeRules($_POST["excludeSizeRules"]); |
| 230 | } |
| 231 | |
| 232 | // Excluded Directories |
| 233 | $excludedDirectoriesRequest = isset($_POST["excludedDirectories"]) ? $this->sanitize->sanitizeString($_POST["excludedDirectories"]) : ''; |
| 234 | $excludedDirectoriesRequest = $this->dirUtils->getExcludedDirectories($excludedDirectoriesRequest); |
| 235 | $this->options->excludedDirectories = array_merge($this->options->excludedDirectories, $excludedDirectoriesRequest); |
| 236 | // Extra Directories |
| 237 | if (isset($_POST["extraDirectories"])) { |
| 238 | $this->options->extraDirectories = explode(ScanConst::DIRECTORIES_SEPARATOR, $this->sanitize->sanitizeString($_POST["extraDirectories"])); |
| 239 | } |
| 240 | |
| 241 | // delete uploads folder before copying if uploads is not symlinked |
| 242 | $this->options->deleteUploadsFolder = !$this->options->uploadsSymlinked && isset($_POST['cleanUploadsDir']) && $this->sanitize->sanitizeBool($_POST['cleanUploadsDir']); |
| 243 | // should not backup uploads during update process |
| 244 | $this->options->backupUploadsFolder = false; |
| 245 | // clean plugins and themes dir before updating |
| 246 | $this->options->deletePluginsAndThemes = isset($_POST['cleanPluginsThemes']) && $this->sanitize->sanitizeBool($_POST['cleanPluginsThemes']); |
| 247 | // set default statuses for backup of uploads dir and cleaning of uploads, themes and plugins dirs |
| 248 | $this->options->statusBackupUploadsDir = 'skipped'; |
| 249 | $this->options->statusContentCleaner = 'pending'; |
| 250 | } |
| 251 | |
| 252 | private function setTablesForUpdateJob() |
| 253 | { |
| 254 | // Included Tables / Prefixed Table - Excluded Tables |
| 255 | $includedTables = isset($_POST['includedTables']) ? $this->sanitize->sanitizeString($_POST['includedTables']) : ''; |
| 256 | $excludedTables = isset($_POST['excludedTables']) ? $this->sanitize->sanitizeString($_POST['excludedTables']) : ''; |
| 257 | $selectedTablesWithoutPrefix = isset($_POST['selectedTablesWithoutPrefix']) ? $this->sanitize->sanitizeString($_POST['selectedTablesWithoutPrefix']) : ''; |
| 258 | $selectedTables = new SelectedTables($includedTables, $excludedTables, $selectedTablesWithoutPrefix); |
| 259 | $selectedTables->setAllTablesExcluded(empty($_POST['allTablesExcluded']) ? false : $this->sanitize->sanitizeBool($_POST['allTablesExcluded'])); |
| 260 | $this->options->tables = $selectedTables->getSelectedTables($this->options->networkClone); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Start the cloning job |
| 265 | * not used but is abstract |
| 266 | */ |
| 267 | public function start() |
| 268 | { |
| 269 | } |
| 270 | } |
| 271 |