PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / lib-central / central / modules / backups.php

backups.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/team-updraft/lib-central/central/modules/backups.php

392 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.');
4
5 /**
6 * Handles Backups Commands
7 */
8 class UpdraftCentral_Backups_Commands extends UpdraftCentral_Commands {
9
10 private $switched = false;
11
12 /**
13 * Function that gets called before every action
14 *
15 * @param string $command a string that corresponds to UDC command to call a certain method for this class.
16 * @param array $data an array of data post or get fields
17 * @param array $extra_info extrainfo use in the udrpc_action, e.g. user_id
18 *
19 * link to udrpc_action main function in class UpdraftCentral_Listener
20 */
21 public function _pre_action($command, $data, $extra_info) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- This function is called from listener.php and $extra_info is being sent.
22 // Here we assign the current blog_id to a variable $blog_id
23 $blog_id = get_current_blog_id();
24 if (!empty($data['site_id'])) $blog_id = $data['site_id'];
25
26 if (function_exists('switch_to_blog') && is_multisite() && $blog_id) {
27 $this->switched = switch_to_blog($blog_id);
28 }
29 }
30
31 /**
32 * Function that gets called after every action
33 *
34 * @param string $command a string that corresponds to UDC command to call a certain method for this class.
35 * @param array $data an array of data post or get fields
36 * @param array $extra_info extrainfo use in the udrpc_action, e.g. user_id
37 *
38 * link to udrpc_action main function in class UpdraftCentral_Listener
39 */
40 public function _post_action($command, $data, $extra_info) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Unused parameter is present because the caller from UpdraftCentral_Listener class uses 3 arguments.
41 // Here, we're restoring to the current (default) blog before we switched
42 if ($this->switched) restore_current_blog();
43 }
44
45 /**
46 * Retrieves the UpdraftPlus plugin status, UpdraftVault storage usage status, Next backup
47 * schedule, etc. Used primarily by UpdraftCentral background process.
48 *
49 * @return array
50 */
51 public function get_status() {
52
53 if (!current_user_can('manage_options')) {
54 $response = array(
55 'status' => 'error',
56 'error_code' => 'insufficient_permission',
57 );
58 } else {
59
60 if (!function_exists('get_mu_plugins')) include_once(ABSPATH.'wp-admin/includes/plugin.php');
61 $mu_plugins = get_mu_plugins();
62
63 $is_premium = false;
64 if (defined('UPDRAFTPLUS_DIR') && file_exists(UPDRAFTPLUS_DIR.'/udaddons')) $is_premium = true;
65
66 // Set default response
67 $response = array(
68 'updraftplus_version' => '',
69 'is_premium' => $is_premium,
70 'installed' => false,
71 'active' => false,
72 'backup_count' => 0,
73 'has_mu_plugins' => !empty($mu_plugins) ? true : false,
74 'last_backup' => array(
75 'backup_nonce' => '',
76 'has_errors' => false,
77 'has_warnings' => false,
78 'has_succeeded' => false,
79 ),
80 'updraftvault' => array(
81 'site_connected' => false,
82 'storage' => array('quota_used' => '0 MB', 'quota' => '0 MB', 'percentage_usage' => '0.0%'),
83 ),
84 'meta' => array(),
85 );
86
87 if (class_exists('UpdraftPlus')) {
88 global $updraftplus;
89
90 $response['updraftplus_version'] = $updraftplus->version;
91 $response['updraftvault'] = $this->get_updraftvault_status();
92 $response['installed'] = true;
93 $response['active'] = true;
94 $response['meta'] = $this->get_filesystem_credentials_info();
95
96 $schedule = $this->get_next_backup_schedule();
97 if ($schedule) {
98 $response['next_backup_schedule'] = $schedule;
99 }
100
101 $backup_history = UpdraftPlus_Backup_History::add_jobdata(UpdraftPlus_Backup_History::get_history());
102
103 $response['backup_count'] = count($backup_history);
104
105 $updraft_last_backup = UpdraftPlus_Options::get_updraft_option('updraft_last_backup');
106 if ($updraft_last_backup) {
107 $response['last_backup']['backup_nonce'] = $updraft_last_backup['backup_nonce'];
108 if (isset($updraft_last_backup['backup_time'])) {
109 $response['last_backup']['backup_date'] = gmdate('n/j/Y', $updraft_last_backup['backup_time']);
110 $response['last_backup']['backup_time'] = $updraft_last_backup['backup_time'];
111 }
112
113 $errors = 0;
114 $warnings = 0;
115
116 if (is_array($updraft_last_backup['errors'])) {
117 foreach ($updraft_last_backup['errors'] as $err) {
118 $level = (is_array($err)) ? $err['level'] : 'error';
119 if ('warning' == $level) {
120 $warnings++;
121 } elseif ('error' == $level) {
122 $errors++;
123 }
124 }
125 }
126
127 if ($errors > 0) $response['last_backup']['has_errors'] = true;
128 if ($warnings > 0) $response['last_backup']['has_warnings'] = true;
129 if (isset($updraft_last_backup['success']) && $updraft_last_backup['success']) $response['last_backup']['has_succeeded'] = true;
130 }
131
132 } else {
133 if (!function_exists('get_plugins')) require_once(ABSPATH.'wp-admin/includes/plugin.php');
134 $plugins = get_plugins();
135 $key = 'updraftplus/updraftplus.php';
136
137 if (array_key_exists($key, $plugins)) {
138 $response['installed'] = true;
139 if (is_plugin_active($key)) $response['active'] = true;
140 }
141 }
142 }
143
144 return $this->_response($response);
145 }
146
147 /**
148 * Retrieves the next backup schedule for Files and Database backups
149 *
150 * @return string
151 */
152 private function get_next_backup_schedule() {
153
154 // Get the next (nearest) scheduled backups
155 $files = wp_next_scheduled('updraft_backup');
156 $db = wp_next_scheduled('updraft_backup_database');
157
158 if ($files && $db) {
159 $timestamp = min($files, $db); // Get the nearest schedule among the two schedules
160 } elseif ($files && !$db) {
161 $timestamp = $files;
162 } elseif (!$files && $db) {
163 $timestamp = $db;
164 } else {
165 $timestamp = null;
166 }
167
168 if (!empty($timestamp)) {
169 return gmdate('g:i A - D', $timestamp);
170 }
171
172 return false;
173 }
174
175 /**
176 * Retrieves the UpdrafVault storage usage status
177 *
178 * @return array
179 */
180 private function get_updraftvault_status() {
181
182 if (!class_exists('UpdraftCentral_UpdraftVault_Commands')) {
183 include_once(UPDRAFTPLUS_DIR.'/includes/updraftvault.php');
184 }
185
186 $updraftvault = new UpdraftCentral_UpdraftVault_Commands($this->rc);
187 $creds = $updraftvault->get_credentials();
188
189 $site_connected = false;
190 $storage = array('quota_used' => '0 MB', 'quota' => '0 MB', 'percentage_usage' => '0.0%');
191 $remote_service = false;
192
193 if (isset($creds['data'])) {
194 if (!isset($creds['data']['error']) && isset($creds['data']['accesskey'])) {
195 $site_connected = true;
196
197 $storage_objects_and_ids = UpdraftPlus_Storage_Methods_Interface::get_storage_objects_and_ids(array('updraftvault'));
198
199 if (isset($storage_objects_and_ids['updraftvault']['instance_settings'])) {
200 $instance_settings = $storage_objects_and_ids['updraftvault']['instance_settings'];
201 $instance_id = key($instance_settings);
202 $opts = $instance_settings[$instance_id];
203
204 if (!class_exists('UpdraftPlus_BackupModule_updraftvault')) {
205 include_once(UPDRAFTPLUS_DIR.'/methods/updraftvault.php');
206 }
207
208 $vault = new UpdraftPlus_BackupModule_updraftvault();
209 $vault->set_options($opts, false, $instance_id);
210
211 $quota_root = $opts['quota_root'];
212 $quota = $opts['quota'];
213
214 if (empty($quota_root)) {
215 // This next line is wrong: it lists the files *in this site's sub-folder*, rather than the whole Vault
216 $current_files = $vault->listfiles('');
217 } else {
218 $current_files = $vault->listfiles_with_path($quota_root, '', true);
219 }
220
221 if (!is_wp_error($current_files) && is_array($current_files)) {
222 $quota_used = 0;
223 foreach ($current_files as $file) {
224 $quota_used += $file['size'];
225 }
226
227 $storage = array(
228 'quota_used' => round($quota_used / 1048576, 1).' MB',
229 'quota' => round($quota / 1048576, 1).' MB',
230 'percentage_usage' => sprintf('%.1f', 100*$quota_used / $quota).'%',
231 );
232
233 $remote_service = array(
234 'name' => 'updraft_include_remote_service_updraftvault',
235 'value' => $instance_id,
236 );
237 }
238 }
239 }
240 }
241
242 return array(
243 'site_connected' => $site_connected,
244 'storage' => $storage,
245 'remote_service' => $remote_service,
246 );
247 }
248
249 /**
250 * Retrieves information whether filesystem credentials (e.g. FTP/SSH) are required
251 * when updating plugins
252 *
253 * @return array
254 */
255 private function get_filesystem_credentials_info() {
256
257 if (!function_exists('get_filesystem_method')) {
258 include_once(ABSPATH.'/wp-admin/includes/file.php');
259 }
260
261 $filesystem_method = get_filesystem_method(array(), WP_PLUGIN_DIR);
262
263 ob_start();
264 $filesystem_credentials_are_stored = request_filesystem_credentials(site_url());
265 $filesystem_form = strip_tags(ob_get_contents(), '<div><h2><p><input><label><fieldset><legend><span><em>');
266 ob_end_clean();
267
268 $request_filesystem_credentials = ('direct' != $filesystem_method && !$filesystem_credentials_are_stored);
269
270 return array(
271 'request_filesystem_credentials' => $request_filesystem_credentials,
272 'filesystem_form' => base64_encode($filesystem_form),
273 );
274 }
275
276 /**
277 * Retrieves the backup progress in terms of entities completed. Used primarily by UpdraftCentral
278 * for polling backup progress in the background.
279 *
280 * @param array $params Submitted arguments for the current request
281 * @return array
282 */
283 public function get_backup_progress($params) {
284
285 $nonce = isset($params['nonce']) ? $params['nonce'] : false;
286 $response = array('nonce' => $params['nonce']);
287
288 if (!current_user_can('manage_options')) {
289 $response['status'] = 'error';
290 $response['error_code'] = 'insufficient_permission';
291 } else {
292 global $updraftplus;
293
294 if ($nonce && $updraftplus && is_a($updraftplus, 'UpdraftPlus')) {
295
296 // Check the job is not still running.
297 $jobdata = $updraftplus->jobdata_getarray($nonce);
298
299 if (!empty($jobdata)) {
300 $response['status'] = 'in-progress';
301
302 $file_entities = 0;
303 $db_entities = 0;
304 $processed = 0;
305
306 if (isset($jobdata['backup_database']) && 'no' != $jobdata['backup_database']) {
307 $backup_database = $jobdata['backup_database'];
308 $db_entities += count($backup_database);
309
310 foreach ($backup_database as $whichdb => $info) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- In this check we only need the status contained in the $info for now.
311 $status = $info; // For default: 'wp'
312 if (is_array($info)) {
313 $status = $info['status'];
314 }
315
316 if ('finished' == $status) {
317 $processed++;
318 }
319 }
320 }
321
322 if (isset($jobdata['backup_files']) && 'no' != $jobdata['backup_files']) {
323 $file_entities = count($jobdata['job_file_entities']);
324
325 $backup_files = $jobdata['backup_files'];
326 if ('finished' == $backup_files) {
327 $processed += $file_entities;
328 } elseif (isset($jobdata['filecreating_substatus'])) {
329 $substatus = $jobdata['filecreating_substatus'];
330 $processed += max(0, intval($substatus['i']) - 1);
331 }
332 }
333
334 $response['progress'] = array(
335 'file_entities' => $file_entities,
336 'db_entities' => $db_entities,
337 'total_entities' => $file_entities+$db_entities,
338 'processed' => $processed,
339 'percentage' => floor(($processed/($file_entities+$db_entities))*100),
340 'nonce' => $nonce,
341 );
342
343 UpdraftPlus_Options::update_updraft_option('updraft_central_last_backup_progress', $response['progress'], false);
344 } else {
345 $last_backup = UpdraftPlus_Options::get_updraft_option('updraft_last_backup');
346 if ($nonce == $last_backup['backup_nonce']) {
347 $response['status'] = 'finished';
348 $response['progress'] = array('percentage' => 100);
349 $response['progress']['errors'] = $last_backup['errors'];
350 $response['progress']['backup_time'] = $last_backup['backup_time'];
351 $response['progress']['completed_time'] = gmdate('g:ia', $last_backup['backup_time']);
352 $response['progress']['completed_date'] = gmdate('M d, Y', $last_backup['backup_time']);
353
354 $errors = 0;
355 $warnings = 0;
356
357 if (!empty($last_backup['errors']) && is_array($last_backup['errors'])) {
358 foreach ($last_backup['errors'] as $err) {
359 $level = (is_array($err)) ? $err['level'] : 'error';
360 if ('warning' == $level) {
361 $warnings++;
362 } elseif ('error' == $level) {
363 $errors++;
364 }
365 }
366 }
367
368 $response['progress']['has_errors'] = ($errors > 0) ? true : false;
369 $response['progress']['has_warnings'] = ($warnings > 0) ? true : false;
370 } else {
371 // We might be too early to check the `updraft_last_backup` thus, we'll
372 // give it a few rounds to check by setting the status to "in-progress"
373 // and returning the last backup progress (if applicable).
374 $last_progress = UpdraftPlus_Options::get_updraft_option('updraft_central_last_backup_progress');
375
376 $response['status'] = 'in-progress';
377 if (!empty($last_progress) && isset($last_progress['nonce'])) {
378 $response['progress'] = $last_progress;
379
380 if ($nonce == $last_progress['nonce']) {
381 UpdraftPlus_Options::delete_updraft_option('updraft_central_last_backup_progress');
382 }
383 }
384 }
385 }
386 }
387 }
388
389 return $this->_response($response);
390 }
391 }
392