ApplyFiltersTrait.php
1 year ago
ArrayableTrait.php
1 year ago
BenchmarkTrait.php
3 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
1 year ago
DbRowsGeneratorTrait.php
4 years ago
DebugLogTrait.php
1 year ago
DeveloperTimerTrait.php
4 years ago
EndOfLinePlaceholderTrait.php
1 year ago
EventLoggerTrait.php
1 year ago
FileScanToCacheTrait.php
2 years ago
FormatTrait.php
1 year ago
HttpRequestTrait.php
1 year ago
HydrateTrait.php
1 year ago
I18nTrait.php
1 year ago
MaintenanceTrait.php
5 years ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
1 year ago
NoticesTrait.php
1 year ago
PropertyConstructor.php
5 years ago
ResourceTrait.php
2 years ago
RestoreFileExclusionTrait.php
1 year ago
SerializeTrait.php
1 year ago
SlashTrait.php
1 year ago
UrlTrait.php
1 year ago
ValueGetterTrait.php
2 years ago
WindowsOsTrait.php
1 year ago
EventLoggerTrait.php
180 lines
| 1 | <?php |
| 2 | namespace WPStaging\Framework\Traits; |
| 3 | use WPStaging\Core\WPStaging; |
| 4 | use WPStaging\Framework\Adapter\Directory; |
| 5 | use WPStaging\Framework\Filesystem\Filesystem; |
| 6 | use WPStaging\Framework\Utils\Sanitize; |
| 7 | use WPStaging\Framework\Security\Auth; |
| 8 | trait EventLoggerTrait |
| 9 | { |
| 10 | protected $processStatusFailed = false; |
| 11 | protected $wpstgMaintenanceFile = 'maintenance'; |
| 12 | protected $backupProcessPrefixIdentifier = 'B'; |
| 13 | protected $restoreProcessPrefixIdentifier = 'R'; |
| 14 | protected $cloneProcessPrefixIdentifier = 'C'; |
| 15 | protected $pushProcessPrefixIdentifier = 'P'; |
| 16 | private $filePath; |
| 17 | private $filesystem; |
| 18 | protected $backupSettingsIdentifiers = [ |
| 19 | 'isExportingUploads' => 'U', |
| 20 | 'isExportingThemes' => 'T', |
| 21 | 'isExportingMuPlugins' => 'MU', |
| 22 | 'isExportingPlugins' => 'P', |
| 23 | 'isExportingOtherWpContentFiles' => 'OW', |
| 24 | 'isExportingOtherWpRootFiles' => 'OR', |
| 25 | 'isExportingDatabase' => 'D' |
| 26 | ]; |
| 27 | protected $backupStoragesIdentifiers = [ |
| 28 | 'googleDrive' => 'GD', |
| 29 | 'amazonS3' => 'AS3', |
| 30 | 'dropbox' => 'DB', |
| 31 | 'sftp' => 'S', |
| 32 | 'digitalocean-spaces' => 'DOS', |
| 33 | 'wasabi-s3' => 'WS3', |
| 34 | 'generic-s3' => 'GS3', |
| 35 | 'one-drive' => 'OD', |
| 36 | ]; |
| 37 | protected $backupUploadPrefixIdentifier = 'BU'; |
| 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 === $this->pushProcessPrefixIdentifier) { |
| 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_fill_keys($storages, true); |
| 83 | $processPrefix = $this->backupUploadPrefixIdentifier . '|' . $this->prepareJobSettings($this->backupStoragesIdentifiers, $storages); |
| 84 | $this->writeEventStatus($processPrefix); |
| 85 | } |
| 86 | |
| 87 | public function logBackupProcessCompleted(array $processSettings = []) |
| 88 | { |
| 89 | $processPrefix = $this->backupProcessPrefixIdentifier . '|' . $this->prepareJobSettings($this->backupSettingsIdentifiers, $processSettings); |
| 90 | $this->writeEventStatus($processPrefix); |
| 91 | } |
| 92 | |
| 93 | public function logBackupRestoreCompleted($jobBackupMetadata) |
| 94 | { |
| 95 | $processSettings = [ |
| 96 | 'isExportingPlugins' => $jobBackupMetadata->getIsExportingPlugins(), |
| 97 | 'isExportingMuPlugins' => $jobBackupMetadata->getIsExportingMuPlugins(), |
| 98 | 'isExportingThemes' => $jobBackupMetadata->getIsExportingThemes(), |
| 99 | 'isExportingUploads' => $jobBackupMetadata->getIsExportingUploads(), |
| 100 | 'isExportingOtherWpContentFiles' => $jobBackupMetadata->getIsExportingOtherWpContentFiles(), |
| 101 | 'isExportingDatabase' => $jobBackupMetadata->getIsExportingDatabase(), |
| 102 | 'isExportingOtherWpRootFiles' => $jobBackupMetadata->getIsExportingOtherWpRootFiles(), |
| 103 | ]; |
| 104 | $processPrefix = $this->restoreProcessPrefixIdentifier . '|' . $this->prepareJobSettings($this->backupSettingsIdentifiers, $processSettings); |
| 105 | $this->writeEventStatus($processPrefix); |
| 106 | } |
| 107 | |
| 108 | public function logCloneCompleted() |
| 109 | { |
| 110 | $this->writeEventStatus($this->cloneProcessPrefixIdentifier); |
| 111 | } |
| 112 | |
| 113 | public function logPushCompleted(bool $afterReload = false) |
| 114 | { |
| 115 | $processPrefix = $this->pushProcessPrefixIdentifier; |
| 116 | if ($afterReload) { |
| 117 | $processPrefix .= 'R'; |
| 118 | } |
| 119 | $this->writeEventStatus($processPrefix); |
| 120 | } |
| 121 | |
| 122 | public function logPushCancelled() |
| 123 | { |
| 124 | $this->writeEventStatus($this->pushProcessPrefixIdentifier, $this->processStatusFailed); |
| 125 | } |
| 126 | |
| 127 | public function updateFailedProcess(string $processPrefix = ''): bool |
| 128 | { |
| 129 | if (empty($processPrefix)) { |
| 130 | return false; |
| 131 | } |
| 132 | return $this->writeEventStatus($processPrefix, $this->processStatusFailed); |
| 133 | } |
| 134 | |
| 135 | private function initializeObjects() |
| 136 | { |
| 137 | $this->filePath = WPStaging::make(Directory::class)->getPluginUploadsDirectory() . $this->wpstgMaintenanceFile; |
| 138 | $this->filesystem = WPStaging::make(Filesystem::class); |
| 139 | } |
| 140 | |
| 141 | private function writeEventStatus(string $process, bool $status = true): bool |
| 142 | { |
| 143 | $this->initializeObjects(); |
| 144 | $content = date('dm') . $process . ($status === $this->processStatusFailed ? '-' : '+'); |
| 145 | clearstatcache(true, $this->filePath); |
| 146 | if (file_exists($this->filePath) && filesize($this->filePath) > 0) { |
| 147 | $content = "\n" . $content; |
| 148 | } |
| 149 | return $this->filesystem->create($this->filePath, $content, 'ab'); |
| 150 | } |
| 151 | |
| 152 | private function prepareJobSettings(array $process, array $processSettings = []): string |
| 153 | { |
| 154 | $backupProcessPrefix = ''; |
| 155 | foreach ($process as $processIndex => $processPrefix) { |
| 156 | if (array_key_exists($processIndex, $processSettings) && $processSettings[$processIndex] === true) { |
| 157 | $backupProcessPrefix .= $processPrefix; |
| 158 | } |
| 159 | } |
| 160 | return $backupProcessPrefix; |
| 161 | } |
| 162 | |
| 163 | protected function getProcessPrefix(string $processName): string |
| 164 | { |
| 165 | return empty($this->processPrefixes[$processName]) ? '' : $this->processPrefixes[$processName]; |
| 166 | } |
| 167 | |
| 168 | protected function init() |
| 169 | { |
| 170 | $this->sanitize = WPStaging::make(Sanitize::class); |
| 171 | $this->auth = WPStaging::make(Auth::class); |
| 172 | $this->processPrefixes = [ |
| 173 | 'backup' => $this->backupProcessPrefixIdentifier, |
| 174 | 'restore' => $this->restoreProcessPrefixIdentifier, |
| 175 | 'clone' => $this->cloneProcessPrefixIdentifier, |
| 176 | 'push' => $this->pushProcessPrefixIdentifier, |
| 177 | ]; |
| 178 | } |
| 179 | } |
| 180 |