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