ApplyFiltersTrait.php
1 day ago
ArrayableTrait.php
1 day ago
BatchSizeCalculateTrait.php
1 day ago
BearerTokenTrait.php
1 day ago
BenchmarkTrait.php
1 day ago
BooleanTransientTrait.php
1 day ago
DatabaseSearchReplaceTrait.php
1 day ago
DbRowsGeneratorTrait.php
1 day ago
DebugLogTrait.php
1 day ago
DeveloperTimerTrait.php
1 day ago
EndOfLinePlaceholderTrait.php
1 day ago
EventLoggerTrait.php
1 day ago
FileScanToCacheTrait.php
1 day ago
FormatTrait.php
1 day ago
HttpRequestTrait.php
1 day ago
HydrateTrait.php
1 day ago
I18nTrait.php
1 day ago
IpResolverTrait.php
1 day ago
JobResponseTrait.php
1 day ago
MaintenanceTrait.php
7 months ago
MemoryExhaustTrait.php
1 day ago
MySQLRowsGeneratorTrait.php
1 day ago
NoticesTrait.php
1 day ago
PagesTrait.php
1 day ago
PropertyConstructor.php
1 day ago
RenameTmpDirectoryTrait.php
1 day ago
ResourceTrait.php
1 day ago
RestRequestTrait.php
1 day ago
RestoreFileExclusionTrait.php
1 day ago
SafeFileInfoTrait.php
1 day ago
SerializeTrait.php
1 day ago
SetTimeLimitTrait.php
1 day ago
SlashTrait.php
1 day ago
SqlCommentTrait.php
1 day ago
TablePrefixValidator.php
5 months ago
UrlTrait.php
1 day ago
ValueGetterTrait.php
1 day ago
WindowsOsTrait.php
1 day ago
EventLoggerTrait.php
306 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | use WPStaging\Backup\Storage\Providers; |
| 6 | use WPStaging\Backup\Storage\Traits\StorageIdNormalizerTrait; |
| 7 | use WPStaging\Core\WPStaging; |
| 8 | use WPStaging\Framework\Adapter\Directory; |
| 9 | use WPStaging\Framework\Filesystem\Filesystem; |
| 10 | use WPStaging\Framework\Logger\EventLoggerConst; |
| 11 | use WPStaging\Framework\Utils\Sanitize; |
| 12 | use WPStaging\Framework\Security\Auth; |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | trait EventLoggerTrait |
| 33 | { |
| 34 | use StorageIdNormalizerTrait; |
| 35 | |
| 36 | |
| 37 | protected $processStatusFailed = false; |
| 38 | |
| 39 | |
| 40 | protected $wpstgMaintenanceFile = 'maintenance'; |
| 41 | |
| 42 | |
| 43 | private $filePath; |
| 44 | |
| 45 | |
| 46 | private $filesystem; |
| 47 | |
| 48 | |
| 49 | protected $backupSettingsIdentifiers = [ |
| 50 | 'isExportingUploads' => EventLoggerConst::BACKUP_SETTING_UPLOADS, |
| 51 | 'isExportingThemes' => EventLoggerConst::BACKUP_SETTING_THEMES, |
| 52 | 'isExportingMuPlugins' => EventLoggerConst::BACKUP_SETTING_MU_PLUGINS, |
| 53 | 'isExportingPlugins' => EventLoggerConst::BACKUP_SETTING_PLUGINS, |
| 54 | 'isExportingOtherWpContentFiles' => EventLoggerConst::BACKUP_SETTING_OTHER_WP_CONTENT, |
| 55 | 'isExportingOtherWpRootFiles' => EventLoggerConst::BACKUP_SETTING_OTHER_ROOT, |
| 56 | 'isExportingDatabase' => EventLoggerConst::BACKUP_SETTING_DATABASE, |
| 57 | ]; |
| 58 | |
| 59 | |
| 60 | protected $backupStoragesIdentifiers = [ |
| 61 | Providers::IDENTIFIER_GOOGLE_DRIVE => EventLoggerConst::BACKUP_STORAGE_GOOGLE_DRIVE, |
| 62 | Providers::IDENTIFIER_AMAZON_S3 => EventLoggerConst::BACKUP_STORAGE_AMAZON_S3, |
| 63 | Providers::IDENTIFIER_DROPBOX => EventLoggerConst::BACKUP_STORAGE_DROPBOX, |
| 64 | Providers::IDENTIFIER_SFTP => EventLoggerConst::BACKUP_STORAGE_SFTP, |
| 65 | Providers::IDENTIFIER_DIGITALOCEAN_SPACES => EventLoggerConst::BACKUP_STORAGE_DIGITALOCEAN_SPACES, |
| 66 | Providers::IDENTIFIER_WASABI_S3 => EventLoggerConst::BACKUP_STORAGE_WASABI_S3, |
| 67 | Providers::IDENTIFIER_GENERIC_S3 => EventLoggerConst::BACKUP_STORAGE_GENERIC_S3, |
| 68 | Providers::IDENTIFIER_ONE_DRIVE => EventLoggerConst::BACKUP_STORAGE_ONE_DRIVE, |
| 69 | Providers::IDENTIFIER_PCLOUD => EventLoggerConst::BACKUP_STORAGE_PCLOUD, |
| 70 | ]; |
| 71 | |
| 72 | |
| 73 | private $sanitize; |
| 74 | |
| 75 | |
| 76 | private $process; |
| 77 | |
| 78 | |
| 79 | private $processPrefixes; |
| 80 | |
| 81 | |
| 82 | private $auth; |
| 83 | |
| 84 | |
| 85 | public function ajaxLogEventSuccess() |
| 86 | { |
| 87 | $this->init(); |
| 88 | if (!$this->auth->isAuthenticatedRequest()) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $process = isset($_POST['process']) ? $this->sanitize->sanitizeString($_POST['process']) : ''; |
| 93 | $this->process = $this->getProcessPrefix($process); |
| 94 | if (empty($this->process)) { |
| 95 | wp_send_json_error(); |
| 96 | } |
| 97 | |
| 98 | if ($this->process === EventLoggerConst::PROCESS_PREFIX_PUSH) { |
| 99 | $this->logPushCompleted(true); |
| 100 | wp_send_json_success(); |
| 101 | } |
| 102 | |
| 103 | $this->writeEventStatus($this->process); |
| 104 | wp_send_json_success(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | public function ajaxLogEventFailure() |
| 109 | { |
| 110 | $this->init(); |
| 111 | if (!$this->auth->isAuthenticatedRequest()) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $process = isset($_POST['process']) ? $this->sanitize->sanitizeString($_POST['process']) : ''; |
| 116 | $this->process = $this->getProcessPrefix($process); |
| 117 | if (empty($this->process)) { |
| 118 | wp_send_json_error(); |
| 119 | } |
| 120 | |
| 121 | $response = $this->updateFailedProcess($this->process); |
| 122 | if ($response) { |
| 123 | wp_send_json_success(); |
| 124 | } |
| 125 | |
| 126 | wp_send_json_error(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | public function logBackupUploadCompleted(array $storages = []) |
| 131 | { |
| 132 | |
| 133 | $storages = array_map([$this, 'normalizeStorageId'], $storages); |
| 134 | $storages = array_fill_keys($storages, true); |
| 135 | $processPrefix = EventLoggerConst::PROCESS_PREFIX_BACKUP_UPLOAD . '|' . $this->prepareJobSettings($this->backupStoragesIdentifiers, $storages); |
| 136 | $this->writeEventStatus($processPrefix); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | public function logBackupProcessCompleted($backupMeta) |
| 144 | { |
| 145 | $this->logProcessFromBackupSettings(EventLoggerConst::PROCESS_PREFIX_BACKUP, $backupMeta); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | public function logBackupRestoreCompleted($backupMeta) |
| 153 | { |
| 154 | $this->logProcessFromBackupSettings(EventLoggerConst::PROCESS_PREFIX_RESTORE, $backupMeta); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | public function logRemoteSyncCompleted($backupMeta) |
| 162 | { |
| 163 | $this->logProcessFromBackupSettings(EventLoggerConst::PROCESS_PREFIX_REMOTE_SYNC, $backupMeta); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | public function logCloneCompleted(string $processType = EventLoggerConst::PROCESS_PREFIX_CLONE) |
| 168 | { |
| 169 | $processType = empty($processType) ? EventLoggerConst::PROCESS_PREFIX_CLONE : $processType; |
| 170 | $this->writeEventStatus($processType); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | public function logPushCompleted(bool $afterReload = false) |
| 175 | { |
| 176 | $processPrefix = EventLoggerConst::PROCESS_PREFIX_PUSH; |
| 177 | if ($afterReload) { |
| 178 | $processPrefix = EventLoggerConst::PROCESS_PREFIX_PUSH_RELOAD; |
| 179 | } |
| 180 | |
| 181 | $this->writeEventStatus($processPrefix); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | public function logPushCancelled() |
| 186 | { |
| 187 | $this->writeEventStatus(EventLoggerConst::PROCESS_PREFIX_PUSH, $this->processStatusFailed); |
| 188 | } |
| 189 | |
| 190 | public function updateFailedProcess(string $processPrefix = ''): bool |
| 191 | { |
| 192 | if (empty($processPrefix)) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return $this->writeEventStatus($processPrefix, $this->processStatusFailed); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | public function logBackupExtractionCompleted() |
| 201 | { |
| 202 | $this->writeEventStatus(EventLoggerConst::PROCESS_PREFIX_BACKUP_EXTRACTION); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | private function initializeObjects() |
| 207 | { |
| 208 | $this->filePath = WPStaging::make(Directory::class)->getPluginUploadsDirectory() . $this->wpstgMaintenanceFile; |
| 209 | $this->filesystem = WPStaging::make(Filesystem::class); |
| 210 | } |
| 211 | |
| 212 | private function writeEventStatus(string $process, bool $status = true): bool |
| 213 | { |
| 214 | $this->initializeObjects(); |
| 215 | |
| 216 | $content = date('dmy') . $process . ($status === $this->processStatusFailed ? '-' : '+'); |
| 217 | |
| 218 | |
| 219 | clearstatcache(true, $this->filePath); |
| 220 | if (file_exists($this->filePath) && filesize($this->filePath) > 0) { |
| 221 | $content = "\n" . $content; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | return $this->filesystem->create($this->filePath, $content, 'ab'); |
| 226 | } |
| 227 | |
| 228 | private function prepareJobSettings(array $process, array $processSettings = []): string |
| 229 | { |
| 230 | $backupProcessPrefix = ''; |
| 231 | foreach ($process as $processIndex => $processPrefix) { |
| 232 | if (array_key_exists($processIndex, $processSettings) && $processSettings[$processIndex] === true) { |
| 233 | $backupProcessPrefix .= $processPrefix; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return $backupProcessPrefix; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | private function logProcessFromBackupSettings(string $processPrefixIdentifier, $backupMeta) |
| 246 | { |
| 247 | $processSettings = $this->extractBackupProcessSettings($backupMeta); |
| 248 | $processPrefix = $processPrefixIdentifier . '|' . $this->prepareJobSettings($this->backupSettingsIdentifiers, $processSettings); |
| 249 | $this->writeEventStatus($processPrefix); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | private function extractBackupProcessSettings($backupMeta): array |
| 257 | { |
| 258 | if (!is_object($backupMeta)) { |
| 259 | return []; |
| 260 | } |
| 261 | |
| 262 | $getterMap = [ |
| 263 | 'isExportingPlugins' => 'getIsExportingPlugins', |
| 264 | 'isExportingMuPlugins' => 'getIsExportingMuPlugins', |
| 265 | 'isExportingThemes' => 'getIsExportingThemes', |
| 266 | 'isExportingUploads' => 'getIsExportingUploads', |
| 267 | 'isExportingOtherWpContentFiles' => 'getIsExportingOtherWpContentFiles', |
| 268 | 'isExportingDatabase' => 'getIsExportingDatabase', |
| 269 | 'isExportingOtherWpRootFiles' => 'getIsExportingOtherWpRootFiles', |
| 270 | ]; |
| 271 | |
| 272 | $settings = []; |
| 273 | foreach ($getterMap as $settingKey => $getter) { |
| 274 | if (!is_callable([$backupMeta, $getter])) { |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | $settings[$settingKey] = $backupMeta->{$getter}(); |
| 279 | } |
| 280 | |
| 281 | return $settings; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | protected function getProcessPrefix(string $processName): string |
| 289 | { |
| 290 | return empty($this->processPrefixes[$processName]) ? '' : $this->processPrefixes[$processName]; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | protected function init() |
| 295 | { |
| 296 | $this->sanitize = WPStaging::make(Sanitize::class); |
| 297 | $this->auth = WPStaging::make(Auth::class); |
| 298 | $this->processPrefixes = [ |
| 299 | 'backup' => EventLoggerConst::PROCESS_PREFIX_BACKUP, |
| 300 | 'restore' => EventLoggerConst::PROCESS_PREFIX_RESTORE, |
| 301 | 'clone' => EventLoggerConst::PROCESS_PREFIX_CLONE, |
| 302 | 'push' => EventLoggerConst::PROCESS_PREFIX_PUSH, |
| 303 | ]; |
| 304 | } |
| 305 | } |
| 306 |