/
admin.actions.php
executable file
·291 lines (200 loc) · 8.42 KB
/
admin.actions.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Verify & save all the user settings
*
* Returns WP_Error object if there are errors when updating settings.
* Return (bool) true if no errors.
*
* Uses $_POST data
*
* @todo Should redirect on success
* @return mixed
*/
function hmbkp_option_save() {
if ( empty( $_POST['hmbkp_settings_submit'] ) )
return;
check_admin_referer( 'hmbkp_settings', 'hmbkp_settings_nonce' );
global $hmbkp_errors;
$hmbkp_errors = new WP_Error;
// Disable Automatic backups
if ( isset( $_POST['hmbkp_automatic'] ) && ! (bool) $_POST['hmbkp_automatic'] ) {
update_option( 'hmbkp_disable_automatic_backup', 'true' );
wp_clear_scheduled_hook( 'hmbkp_schedule_backup_hook' );
} else {
delete_option( 'hmbkp_disable_automatic_backup');
}
// Update schedule frequency settings. Or reset to default of daily.
if ( isset( $_POST['hmbkp_frequency'] ) && $_POST['hmbkp_frequency'] != 'daily' )
update_option( 'hmbkp_schedule_frequency', esc_attr( $_POST['hmbkp_frequency'] ) );
else
delete_option( 'hmbkp_schedule_frequency' );
// Clear schedule if settings have changed.
if ( wp_get_schedule( 'hmbkp_schedule_backup_hook' ) != get_option( 'hmbkp_schedule_frequency' ) )
wp_clear_scheduled_hook( 'hmbkp_schedule_backup_hook' );
if ( isset( $_POST['hmbkp_what_to_backup'] ) && $_POST['hmbkp_what_to_backup'] == 'files only' ) {
update_option( 'hmbkp_files_only', 'true' );
delete_option( 'hmbkp_database_only' );
} elseif ( isset( $_POST['hmbkp_what_to_backup'] ) && $_POST['hmbkp_what_to_backup'] == 'database only' ) {
update_option( 'hmbkp_database_only', 'true' );
delete_option( 'hmbkp_files_only' );
} else {
delete_option( 'hmbkp_database_only' );
delete_option( 'hmbkp_files_only' );
}
if ( isset( $_POST['hmbkp_backup_number'] ) && $max_backups = intval( $_POST['hmbkp_backup_number'] ) ) {
update_option( 'hmbkp_max_backups', intval( esc_attr( $_POST['hmbkp_backup_number'] ) ) );
} else {
delete_option( 'hmbkp_max_backups' );
// Only error if it is actually empty.
if ( isset( $_POST['hmbkp_backup_number'] ) )
$hmbkp_errors->add( 'invalid_no_backups', __( 'You have entered an invalid number of backups.', 'hmbkp' ) );
}
if ( isset( $_POST['hmbkp_email_address'] ) && ! is_email( $_POST['hmbkp_email_address'] ) && !empty( $_POST['hmbkp_email_address'] ) ) {
$hmbkp_errors->add( 'invalid_email', __( 'You have entered an invalid email address.', 'hmbkp' ) );
} elseif( isset( $_POST['hmbkp_email_address'] ) && !empty( $_POST['hmbkp_email_address'] ) ) {
update_option( 'hmbkp_email_address', $_POST['hmbkp_email_address'] );
} else {
delete_option( 'hmbkp_email_address' );
}
if ( isset( $_POST['hmbkp_excludes'] ) && ! empty( $_POST['hmbkp_excludes'] ) ) {
update_option( 'hmbkp_excludes', $_POST['hmbkp_excludes'] );
} else {
delete_option( 'hmbkp_excludes' );
}
delete_transient( 'hmbkp_estimated_filesize' );
if ( $hmbkp_errors->get_error_code() )
return $hmbkp_errors;
return true;
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_option_save' );
/**
* Delete the backup and then redirect
* back to the backups page
*/
function hmbkp_request_delete_backup() {
if ( ! isset( $_GET['hmbkp_delete'] ) || empty( $_GET['hmbkp_delete'] ) )
return;
hmbkp_delete_backup( $_GET['hmbkp_delete'] );
wp_redirect( remove_query_arg( 'hmbkp_delete' ), 303 );
exit;
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_delete_backup' );
/**
* Perform a manual backup and then
* redirect back to the backups page
*/
function hmbkp_request_do_backup() {
if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'hmbkp_backup_now' )
return;
hmbkp_do_backup();
wp_redirect( remove_query_arg( 'action' ), 303 );
exit;
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_do_backup' );
/**
* Perform a manual backup via ajax
*/
function hmbkp_ajax_request_do_backup() {
ignore_user_abort( true );
hmbkp_do_backup();
exit;
}
add_action( 'wp_ajax_hmbkp_backup', 'hmbkp_ajax_request_do_backup' );
/**
* Send the download file to the browser and
* then redirect back to the backups page
*/
function hmbkp_request_download_backup() {
if ( empty( $_GET['hmbkp_download'] ) )
return;
// Force the .htaccess to be rebuilt
if ( file_exists( hmbkp_path() . '/.htaccess' ) )
unlink( hmbkp_path() . '/.htaccess' );
hmbkp_path();
wp_redirect( add_query_arg( 'key', md5( HMBKP_SECURE_KEY ), str_replace( hmbkp_conform_dir( ABSPATH ), site_url(), base64_decode( $_GET['hmbkp_download'] ) ) ), 303 );
exit;
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_download_backup' );
function hmbkp_request_cancel_backup() {
if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'hmbkp_cancel' )
return;
hmbkp_cleanup();
wp_redirect( remove_query_arg( 'action' ), 303 );
exit;
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_cancel_backup' );
function hmbkp_dismiss_error() {
if ( empty( $_GET['action'] ) || $_GET['action'] !== 'hmbkp_dismiss_error' )
return;
hmbkp_cleanup();
wp_redirect( remove_query_arg( 'action' ), 303 );
exit;
}
add_action( 'admin_init', 'hmbkp_dismiss_error' );
/**
* Display the running status via ajax
*
* @return void
*/
function hmbkp_ajax_is_backup_in_progress() {
if ( ! hmbkp_in_progress() )
echo 0;
else
include( HMBKP_PLUGIN_PATH . '/admin.backup-button.php' );
exit;
}
add_action( 'wp_ajax_hmbkp_is_in_progress', 'hmbkp_ajax_is_backup_in_progress' );
/**
* Display the calculated size via ajax
*
* @return void
*/
function hmbkp_ajax_calculate_backup_size() {
echo hmbkp_calculate();
exit;
}
add_action( 'wp_ajax_hmbkp_calculate', 'hmbkp_ajax_calculate_backup_size' );
/**
* Test the cron response and if it's not 200 show a warning message
*
* @return void
*/
function hmbkp_ajax_cron_test() {
$response = wp_remote_get( site_url( 'wp-cron.php' ) );
if ( ! is_wp_error( $response ) && $response['response']['code'] != '200' )
echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( '%s is returning a %s response which could mean cron jobs aren\'t getting fired properly. BackUpWordPress relies on wp-cron to run scheduled back ups. See the %s for more details.', 'hmbkp' ), '<code>wp-cron.php</code>', '<code>' . $response['response']['code'] . '</code>', '<a href="http://wordpress.org/extend/plugins/backupwordpress/faq/">FAQ</a>' ) . '</p></div>';
else
echo 1;
exit;
}
add_action( 'wp_ajax_hmbkp_cron_test', 'hmbkp_ajax_cron_test' );
/**
* Handles changes in the defined Constants
* that users can define to control advanced
* settings
*
* @return null
*/
function hmbkp_constant_changes() {
// Check whether we need to disable the cron
if ( hmbkp_get_disable_automatic_backup() && wp_next_scheduled( 'hmbkp_schedule_backup_hook' ) )
wp_clear_scheduled_hook( 'hmbkp_schedule_backup_hook' );
// Or whether we need to re-enable it
if ( ! hmbkp_get_disable_automatic_backup() && ! wp_next_scheduled( 'hmbkp_schedule_backup_hook' ) )
hmbkp_setup_schedule();
// Allow the time of the daily backup to be changed
if ( wp_get_schedule( 'hmbkp_schedule_backup_hook' ) != get_option( 'hmbkp_schedule_frequency' ) )
hmbkp_setup_schedule();
// Reset if custom time is removed
if ( ( ( defined( 'HMBKP_DAILY_SCHEDULE_TIME' ) && ! HMBKP_DAILY_SCHEDULE_TIME ) || ! defined( 'HMBKP_DAILY_SCHEDULE_TIME' ) ) && get_option( 'hmbkp_schedule_frequency' ) == 'daily' && date( 'H:i', wp_next_scheduled( 'hmbkp_schedule_backup_hook' ) ) != '23:00' && ! hmbkp_get_disable_automatic_backup() )
hmbkp_setup_schedule();
// If a custom backup path has been set or changed
if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && hmbkp_conform_dir( HMBKP_PATH ) != ( $from = hmbkp_conform_dir( get_option( 'hmbkp_path' ) ) ) )
hmbkp_path_move( $from, HMBKP_PATH );
// If a custom backup path has been removed
if ( ( ( defined( 'HMBKP_PATH' ) && ! HMBKP_PATH ) || !defined( 'HMBKP_PATH' ) && hmbkp_conform_dir( hmbkp_path_default() ) != ( $from = hmbkp_conform_dir( get_option( 'hmbkp_path' ) ) ) ) )
hmbkp_path_move( $from, hmbkp_path_default() );
// If the custom path has changed and the new directory isn't writable
if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && hmbkp_conform_dir( HMBKP_PATH ) != ( $from = hmbkp_conform_dir( get_option( 'hmbkp_path' ) ) ) && $from != hmbkp_path_default() && !is_writable( HMBKP_PATH ) && is_dir( $from ) )
hmbkp_path_move( $from, hmbkp_path_default() );
}