PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 2 months ago FileList 4 months ago Restore 2 months ago Backup.php 2 years ago BackupDownloader.php 1 day ago BackupSizeCalculator.php 1 day ago BackupSpeedIndex.php 9 months ago BaseFileList.php 1 year ago BaseListing.php 9 months ago Delete.php 4 months ago Edit.php 4 months ago Explore.php 3 months ago ExploreCache.php 3 months ago FileInfo.php 9 months ago FileList.php 1 year ago Listing.php 7 months ago Parts.php 1 day ago Restore.php 2 years ago ScheduleList.php 1 year ago Upload.php 1 month 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 /** @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 string */
22 const WPSTG_DISABLE_BACKUP_SPEED_MODAL = true;
23
24 /** @var bool */
25 protected $firstBackupSpeedIndex = false;
26
27 /** @var string */
28 protected $finalBackupSpeedIndex;
29
30 /** @var float */
31 protected $currentBackupSpeedIndex;
32
33 /** @var bool false */
34 protected $isBackupSpeedModalDisplayed = false;
35
36 /** @var bool */
37 protected $isBackupSlowerThanUsual;
38
39 /** @var string */
40 protected $currentBackupSize;
41
42 /** @var int */
43 protected $currentBackupTime = 1;
44
45 /** @var Math */
46 protected $utilsMath;
47
48 /** @var Sanitize */
49 private $sanitize;
50
51 /** @var Auth */
52 private $auth;
53
54 /**
55 * @param Auth $auth
56 * @param Sanitize $sanitize
57 * @param Math $utilsMath
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 * Ajax handler to possibly show a modal based on backup speed
72 *
73 * @return void
74 */
75 public function ajaxMaybeShowModal()
76 {
77 // Bail early if modal is disabled via constant
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 * Checks the backup speed, triggering actions based on the results.
96 *
97 * @return void
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 * Send a JSON response based on backup speed analysis.
125 *
126 * @return void
127 */
128 public function sendJsonResponse()
129 {
130 wp_send_json(['isBackupSlowerThanUsual' => $this->isBackupSlowerThanUsual, 'isBackupSpeedModalDisplayed' => $this->isBackupSpeedModalDisplayed]);
131 }
132
133 /**
134 * Create a temporary backup speed index by adding it as an option.
135 * @return void
136 */
137 private function saveTempBackupSpeedIndex()
138 {
139 add_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX, $this->currentBackupSpeedIndex);
140 }
141
142
143 /**
144 * Create the backup speed index by calculating an average and adding it as an option.
145 * @return void
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 * Calculate the current backup speed based on file size and backup time.
156 * The higher the Index number, the faster the backup creation.
157 * @return void
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 * Delete the temporary backup speed index option.
175 * @return void
176 */
177 private function deleteTempBackupSpeedIndex()
178 {
179 delete_option(self::OPTION_BACKUP_SPEED_FIRST_INDEX);
180 }
181
182 /**
183 * @param string $currentBackupSize
184 * @return void
185 */
186 public function setCurrentBackupSize(string $currentBackupSize)
187 {
188 $this->currentBackupSize = $currentBackupSize;
189 }
190
191 /**
192 * @param int $currentBackupTime
193 * @return void
194 */
195 public function setCurrentBackupTime(int $currentBackupTime)
196 {
197 $this->currentBackupTime = $currentBackupTime;
198 }
199 }
200