PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Staging / Ajax / Repair.php
wp-staging / Staging / Ajax Last commit date
Create 1 day ago Delete 1 day ago Reset 1 day ago Update 1 day ago AbstractAjaxPrepare.php 1 day ago Create.php 1 day ago Delete.php 1 day ago DirectoryChildren.php 1 day ago Listing.php 1 day ago Repair.php 1 day ago Reset.php 1 day ago Setup.php 1 day ago SizeCalculator.php 1 day ago StagingSiteDataChecker.php 1 day ago Update.php 1 day 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
14
15
16 class Repair extends AbstractTemplateComponent
17 {
18
19
20
21 const REPAIR_EMAIL_SUBJECT = 'WP Staging - Staging Sites Option Corrupted';
22
23
24
25
26
27 const CORRUPTED_STAGING_SITE_OPTION_FILE_NAME = 'corrupted-staging-site-option.log';
28
29
30
31
32
33 const OPTION_PREFIX_FOR_STAGING_SITES_BACKUP = 'wpstg_staging_sites_backup_';
34
35
36 private $mailSender;
37
38
39 private $report;
40
41
42 private $directory;
43
44
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
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
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
107
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
116
117
118 private function initiateEmailNotification($corruptedOption, bool $fixed = false): bool
119 {
120
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
138
139
140 private function getAttachments(string $corruptedOptionFilePath): array
141 {
142 $attachments = $this->report->getBundledLogs();
143 $attachments[] = $corruptedOptionFilePath;
144
145 return $attachments;
146 }
147 }
148