PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
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 2 years ago FileList 2 years ago Restore 3 years ago Backup.php 2 years ago BackupSpeedIndex.php 2 years ago Cancel.php 3 years ago Delete.php 3 years ago Edit.php 3 years ago FileInfo.php 3 years ago FileList.php 3 years ago Listing.php 2 years ago Parts.php 3 years ago PrepareJob.php 2 years ago Restore.php 2 years ago ScheduleList.php 2 years ago Status.php 3 years ago Upload.php 2 years ago
BackupSpeedIndex.php
196 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 /** @var string */
13 const OPTION_BACKUP_SPEED_FIRST_INDEX = 'wpstg_first_backup_speed_index';
14
15 /** @var string */
16 const OPTION_BACKUP_SPEED_INDEX = 'wpstg_backup_speed_index';
17
18 /** @var string */
19 const OPTION_SHOW_BACKUP_SPEED_MODAL = 'wpstg_backup_speed_modal_shown';
20
21 /** @var bool */
22 protected $firstBackupSpeedIndex = false;
23
24 /** @var string */
25 protected $finalBackupSpeedIndex;
26
27 /** @var float */
28 protected $currentBackupSpeedIndex;
29
30 /** @var bool false */
31 protected $isBackupSpeedModalDisplayed = false;
32
33 /** @var bool */
34 protected $isBackupSlowerThanUsual;
35
36 /** @var string */
37 protected $currentBackupSize;
38
39 /**
40 * @var int
41 */
42 protected $currentBackupTime = 1;
43
44 /** @var Sanitize */
45 private $sanitize;
46
47 /**
48 * @var Auth
49 */
50 private $auth;
51
52 /** @var Math */
53 protected $utilsMath;
54
55 /**
56 * @param Auth $auth
57 * @param Sanitize $sanitize
58 * @param Math $utilsMath
59 */
60 public function __construct(Auth $auth, Sanitize $sanitize, Math $utilsMath)
61 {
62 $this->auth = $auth;
63 $this->finalBackupSpeedIndex = get_option(self::OPTION_BACKUP_SPEED_INDEX);
64 $this->firstBackupSpeedIndex = get_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX);
65 $this->isBackupSpeedModalDisplayed = get_option(self::OPTION_SHOW_BACKUP_SPEED_MODAL);
66 $this->sanitize = $sanitize;
67 $this->utilsMath = $utilsMath;
68 $this->isBackupSlowerThanUsual = false;
69 }
70
71 /**
72 * Ajax handler to possibly show a modal based on backup speed
73 *
74 * @return void
75 */
76 public function ajaxMaybeShowModal()
77 {
78 if (!$this->auth->isAuthenticatedRequest()) {
79 return;
80 }
81
82 if (WPStaging::isPro()) {
83 return;
84 }
85
86 $this->init();
87 $this->sendJsonResponse();
88 }
89
90 /**
91 * Checks the backup speed, triggering actions based on the results.
92 *
93 * @return void
94 */
95 public function init()
96 {
97 if ($this->isBackupSpeedModalDisplayed) {
98 return;
99 }
100
101 $this->calculateCurrentBackupSpeedIndex();
102
103 if (!$this->finalBackupSpeedIndex && !$this->firstBackupSpeedIndex) {
104 $this->saveTempBackupSpeedIndex();
105 return;
106 }
107
108 if (!$this->finalBackupSpeedIndex) {
109 $this->calculateFinalBackupSpeedIndex();
110 return;
111 }
112
113 if ($this->finalBackupSpeedIndex > $this->currentBackupSpeedIndex) {
114 $this->isBackupSlowerThanUsual = true;
115 update_option(self::OPTION_SHOW_BACKUP_SPEED_MODAL, 'true');
116 }
117 }
118
119 /**
120 * Send a JSON response based on backup speed analysis.
121 *
122 * @return void
123 */
124 public function sendJsonResponse()
125 {
126 wp_send_json(['isBackupSlowerThanUsual' => $this->isBackupSlowerThanUsual, 'isBackupSpeedModalDisplayed' => $this->isBackupSpeedModalDisplayed]);
127 }
128
129 /**
130 * Create a temporary backup speed index by adding it as an option.
131 * @return void
132 */
133 private function saveTempBackupSpeedIndex()
134 {
135 add_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX, $this->currentBackupSpeedIndex);
136 }
137
138
139 /**
140 * Create the backup speed index by calculating an average and adding it as an option.
141 * @return void
142 */
143 private function calculateFinalBackupSpeedIndex()
144 {
145 $averageSpeedIndex = ($this->currentBackupSpeedIndex + $this->firstBackupSpeedIndex) / 2;
146 add_option(self::OPTION_BACKUP_SPEED_INDEX, $averageSpeedIndex);
147 $this->deleteTempBackupSpeedIndex();
148 }
149
150 /**
151 * Calculate the current backup speed based on file size and backup time.
152 * The higher the Index number, the faster the backup creation.
153 * @return void
154 */
155 private function calculateCurrentBackupSpeedIndex()
156 {
157 if (isset($_POST['size'])) {
158 $this->setCurrentBackupSize($this->sanitize->sanitizeString($_POST['size']));
159 }
160
161 if (isset($_POST['time'])) {
162 $this->setCurrentBackupTime($this->sanitize->sanitizeInt($_POST['time']));
163 }
164
165 $fileSize = $this->utilsMath->convertUnitToMB($this->currentBackupSize);
166 $this->currentBackupSpeedIndex = ($fileSize / $this->currentBackupTime);
167 }
168
169 /**
170 * Delete the temporary backup speed index option.
171 * @return void
172 */
173 private function deleteTempBackupSpeedIndex()
174 {
175 delete_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX);
176 }
177
178 /**
179 * @param string $currentBackupSize
180 * @return void
181 */
182 public function setCurrentBackupSize(string $currentBackupSize)
183 {
184 $this->currentBackupSize = $currentBackupSize;
185 }
186
187 /**
188 * @param int $currentBackupTime
189 * @return void
190 */
191 public function setCurrentBackupTime(int $currentBackupTime)
192 {
193 $this->currentBackupTime = $currentBackupTime;
194 }
195 }
196