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 / Backup / Ajax / BackupSpeedIndex.php
wp-staging / Backup / Ajax Last commit date
Backup 1 day ago FileList 1 day ago Restore 1 day ago Backup.php 1 day ago BackupDownloader.php 1 day ago BackupSizeCalculator.php 1 day ago BackupSpeedIndex.php 1 day ago BaseFileList.php 1 day ago BaseListing.php 1 day ago Delete.php 1 day ago Edit.php 1 day ago Explore.php 1 day ago ExploreCache.php 1 day ago FileInfo.php 1 day ago FileList.php 1 day ago Listing.php 1 day ago Parts.php 1 day ago Restore.php 1 day ago ScheduleList.php 1 day ago Upload.php 1 day ago
BackupSpeedIndex.php
200 lines
1 <?php
2
3 namespace WPStaging\Backup\Ajax;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Security\Auth;
7 use WPStaging\Framework\Utils\Sanitize;
8 use WPStaging\Framework\Utils\Math;
9
10 class BackupSpeedIndex
11 {
12
13 const OPTION_BACKUP_SPEED_FIRST_INDEX = 'wpstg_first_backup_speed_index';
14
15
16 const OPTION_BACKUP_SPEED_INDEX = 'wpstg_backup_speed_index';
17
18
19 const OPTION_SHOW_BACKUP_SPEED_MODAL = 'wpstg_backup_speed_modal_shown';
20
21
22 const WPSTG_DISABLE_BACKUP_SPEED_MODAL = true;
23
24
25 protected $firstBackupSpeedIndex = false;
26
27
28 protected $finalBackupSpeedIndex;
29
30
31 protected $currentBackupSpeedIndex;
32
33
34 protected $isBackupSpeedModalDisplayed = false;
35
36
37 protected $isBackupSlowerThanUsual;
38
39
40 protected $currentBackupSize;
41
42
43 protected $currentBackupTime = 1;
44
45
46 protected $utilsMath;
47
48
49 private $sanitize;
50
51
52 private $auth;
53
54
55
56
57
58
59 public function __construct(Auth $auth, Sanitize $sanitize, Math $utilsMath)
60 {
61 $this->auth = $auth;
62 $this->finalBackupSpeedIndex = get_option(self::OPTION_BACKUP_SPEED_INDEX);
63 $this->firstBackupSpeedIndex = get_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX);
64 $this->isBackupSpeedModalDisplayed = get_option(self::OPTION_SHOW_BACKUP_SPEED_MODAL);
65 $this->sanitize = $sanitize;
66 $this->utilsMath = $utilsMath;
67 $this->isBackupSlowerThanUsual = false;
68 }
69
70
71
72
73
74
75 public function ajaxMaybeShowModal()
76 {
77
78 if (self::WPSTG_DISABLE_BACKUP_SPEED_MODAL) {
79 return;
80 }
81
82 if (!$this->auth->isAuthenticatedRequest()) {
83 return;
84 }
85
86 if (WPStaging::isPro()) {
87 return;
88 }
89
90 $this->init();
91 $this->sendJsonResponse();
92 }
93
94
95
96
97
98
99 public function init()
100 {
101 if ($this->isBackupSpeedModalDisplayed) {
102 return;
103 }
104
105 $this->calculateCurrentBackupSpeedIndex();
106
107 if (!$this->finalBackupSpeedIndex && !$this->firstBackupSpeedIndex) {
108 $this->saveTempBackupSpeedIndex();
109 return;
110 }
111
112 if (!$this->finalBackupSpeedIndex) {
113 $this->calculateFinalBackupSpeedIndex();
114 return;
115 }
116
117 if ($this->finalBackupSpeedIndex > $this->currentBackupSpeedIndex) {
118 $this->isBackupSlowerThanUsual = true;
119 update_option(self::OPTION_SHOW_BACKUP_SPEED_MODAL, 'true');
120 }
121 }
122
123
124
125
126
127
128 public function sendJsonResponse()
129 {
130 wp_send_json(['isBackupSlowerThanUsual' => $this->isBackupSlowerThanUsual, 'isBackupSpeedModalDisplayed' => $this->isBackupSpeedModalDisplayed]);
131 }
132
133
134
135
136
137 private function saveTempBackupSpeedIndex()
138 {
139 add_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX, $this->currentBackupSpeedIndex);
140 }
141
142
143
144
145
146
147 private function calculateFinalBackupSpeedIndex()
148 {
149 $averageSpeedIndex = ($this->currentBackupSpeedIndex + $this->firstBackupSpeedIndex) / 2;
150 add_option(self::OPTION_BACKUP_SPEED_INDEX, $averageSpeedIndex);
151 $this->deleteTempBackupSpeedIndex();
152 }
153
154
155
156
157
158
159 private function calculateCurrentBackupSpeedIndex()
160 {
161 if (isset($_POST['size'])) {
162 $this->setCurrentBackupSize($this->sanitize->sanitizeString($_POST['size']));
163 }
164
165 if (!empty($_POST['time'])) {
166 $this->setCurrentBackupTime($this->sanitize->sanitizeInt($_POST['time']));
167 }
168
169 $fileSize = $this->utilsMath->convertUnitToMB($this->currentBackupSize);
170 $this->currentBackupSpeedIndex = ($fileSize / $this->currentBackupTime);
171 }
172
173
174
175
176
177 private function deleteTempBackupSpeedIndex()
178 {
179 delete_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX);
180 }
181
182
183
184
185
186 public function setCurrentBackupSize(string $currentBackupSize)
187 {
188 $this->currentBackupSize = $currentBackupSize;
189 }
190
191
192
193
194
195 public function setCurrentBackupTime(int $currentBackupTime)
196 {
197 $this->currentBackupTime = $currentBackupTime;
198 }
199 }
200