Backup.php
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Backup for AIOSEO Settings.
*
* @since 4.0.0
*/
class Backup {
/**
* A the name of the option to save backups with.
*
* @since 4.00
*
* @var string
*/
private $optionsName = 'aioseo_settings_backup';
/**
* Get all backups.
*
* @return array An array of backups.
*/
public function all() {
$backups = json_decode( get_option( $this->optionsName ), true );
if ( empty( $backups ) ) {
$backups = [];
}
return $backups;
}
/**
* Creates a backup of the settings state.
*
* @since 4.0.0
*
* @return void
*/
public function create() {
$backupTime = time();
$options = $this->getOptions();
update_option( $this->optionsName . '_' . $backupTime, wp_json_encode( $options ) );
$backups = $this->all();
$backups[] = $backupTime;
update_option( $this->optionsName, wp_json_encode( $backups ) );
}
/**
* Deletes a backup of the settings.
*
* @since 4.0.0
*
* @return void
*/
public function delete( $backupTime ) {
delete_option( $this->optionsName . '_' . $backupTime );
$backups = $this->all();
foreach ( $backups as $key => $backup ) {
if ( $backup === $backupTime ) {
unset( $backups[ $key ] );
}
}
update_option( $this->optionsName, wp_json_encode( array_values( $backups ) ) );
}
/**
* Restores a backup of the settings.
*
* @since 4.0.0
*
* @return void
*/
public function restore( $backupTime ) {
$backup = json_decode( get_option( $this->optionsName . '_' . $backupTime ), true );
aioseo()->options->sanitizeAndSave( $backup['options'] );
aioseo()->internalOptions->sanitizeAndSave( $backup['internalOptions'] );
}
/**
* Get the options to save.
*
* @since 4.0.0
*
* @return array An array of options to save.
*/
private function getOptions() {
return [
'options' => aioseo()->options->all(),
'internalOptions' => aioseo()->internalOptions->all()
];
}
}