Create
1 day ago
Delete
2 weeks ago
Reset
2 weeks ago
Update
2 weeks ago
AbstractAjaxPrepare.php
1 day ago
Create.php
2 weeks ago
Delete.php
1 year ago
Listing.php
8 months ago
Repair.php
7 months ago
Reset.php
2 weeks ago
Setup.php
2 weeks ago
StagingSiteDataChecker.php
3 months ago
Update.php
2 weeks ago
Repair.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Ajax; |
| 4 | |
| 5 | use WPStaging\Framework\Adapter\Directory; |
| 6 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 7 | use WPStaging\Framework\Mails\MailSender; |
| 8 | use WPStaging\Framework\Mails\Report\Report; |
| 9 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 10 | use WPStaging\Staging\Sites; |
| 11 | |
| 12 | /** |
| 13 | * Class Repair |
| 14 | * This class is responsible for fixing the corrupted staging sites option and report that corrupted option to us |
| 15 | */ |
| 16 | class Repair extends AbstractTemplateComponent |
| 17 | { |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | const REPAIR_EMAIL_SUBJECT = 'WP Staging - Staging Sites Option Corrupted'; |
| 22 | |
| 23 | /** |
| 24 | * The file name which will contain the corrupted staging site option |
| 25 | * @var string |
| 26 | */ |
| 27 | const CORRUPTED_STAGING_SITE_OPTION_FILE_NAME = 'corrupted-staging-site-option.log'; |
| 28 | |
| 29 | /** |
| 30 | * @var string |
| 31 | * It is proceeded with current timestamp when saving in database |
| 32 | */ |
| 33 | const OPTION_PREFIX_FOR_STAGING_SITES_BACKUP = 'wpstg_staging_sites_backup_'; |
| 34 | |
| 35 | /** @var MailSender */ |
| 36 | private $mailSender; |
| 37 | |
| 38 | /** @var Report */ |
| 39 | private $report; |
| 40 | |
| 41 | /** @var Directory */ |
| 42 | private $directory; |
| 43 | |
| 44 | /** @var Sites */ |
| 45 | private $sites; |
| 46 | |
| 47 | public function __construct(TemplateEngine $templateEngine, MailSender $mailSender, Report $report, Directory $directory, Sites $sites) |
| 48 | { |
| 49 | parent::__construct($templateEngine); |
| 50 | $this->mailSender = $mailSender; |
| 51 | $this->report = $report; |
| 52 | $this->directory = $directory; |
| 53 | $this->sites = $sites; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return void |
| 58 | */ |
| 59 | public function ajaxFixOption() |
| 60 | { |
| 61 | if (!$this->canRenderAjax()) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | $corruptedOption = get_option(Sites::STAGING_SITES_OPTION, null); |
| 66 | $fixed = $this->backupOldOption($corruptedOption); |
| 67 | $this->sites->updateStagingSites([]); |
| 68 | |
| 69 | $mailSent = $this->initiateEmailNotification($corruptedOption, $fixed); |
| 70 | |
| 71 | if ($mailSent) { |
| 72 | wp_send_json_success([ |
| 73 | 'message' => esc_html__('Staging sites option has been fixed.', 'wp-staging'), |
| 74 | ]); |
| 75 | } |
| 76 | |
| 77 | wp_send_json_success([ |
| 78 | 'message' => esc_html__('Staging sites option has been fixed but issue not reported.', 'wp-staging'), |
| 79 | ]); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return void |
| 84 | */ |
| 85 | public function ajaxReportOption() |
| 86 | { |
| 87 | if (!$this->canRenderAjax()) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $corruptedOption = get_option(Sites::STAGING_SITES_OPTION, null); |
| 92 | $mailSent = $this->initiateEmailNotification($corruptedOption); |
| 93 | |
| 94 | if ($mailSent) { |
| 95 | wp_send_json_success([ |
| 96 | 'message' => esc_html__('Staging sites option issue reported.', 'wp-staging'), |
| 97 | ]); |
| 98 | } |
| 99 | |
| 100 | wp_send_json_error([ |
| 101 | 'message' => esc_html__('Issue not reported.', 'wp-staging'), |
| 102 | ]); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param array|string|null $corruptedOption |
| 107 | * @return bool |
| 108 | */ |
| 109 | private function backupOldOption($corruptedOption): bool |
| 110 | { |
| 111 | return add_option(self::OPTION_PREFIX_FOR_STAGING_SITES_BACKUP . time(), $corruptedOption, '', false); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param string|array|null $corruptedOption |
| 116 | * @return bool |
| 117 | */ |
| 118 | private function initiateEmailNotification($corruptedOption, bool $fixed = false): bool |
| 119 | { |
| 120 | // No need to translate, this email will be send to our support email address |
| 121 | $emailBody = 'The user staging sites option has been corrupted. '; |
| 122 | if ($fixed) { |
| 123 | $emailBody .= 'The option has been fixed and the corrupted data has been backed up. '; |
| 124 | } |
| 125 | |
| 126 | $emailBody .= 'Site logs and corrupted option data are attached'; |
| 127 | |
| 128 | $corruptedOptionFilePath = $this->directory->getLogDirectory() . self::CORRUPTED_STAGING_SITE_OPTION_FILE_NAME; |
| 129 | file_put_contents($corruptedOptionFilePath, $corruptedOption); |
| 130 | |
| 131 | $this->mailSender->setAttachments($this->getAttachments($corruptedOptionFilePath)); |
| 132 | $this->mailSender->setRecipient(Report::WPSTG_SUPPORT_EMAIL); |
| 133 | return $this->mailSender->sendRequestForEmailNotification(self::REPAIR_EMAIL_SUBJECT, $emailBody); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param string $corruptedOptionFilePath |
| 138 | * @return array |
| 139 | */ |
| 140 | private function getAttachments(string $corruptedOptionFilePath): array |
| 141 | { |
| 142 | $attachments = $this->report->getBundledLogs(); |
| 143 | $attachments[] = $corruptedOptionFilePath; |
| 144 | |
| 145 | return $attachments; |
| 146 | } |
| 147 | } |
| 148 |