class-wizard-jobimport.php
8.84 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
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
<?php
/**
*
*/
class BackWPup_Pro_Wizard_JobImport extends BackWPup_Pro_Wizards {
/**
*
*/
public function __construct() {
$this->info[ 'ID' ] = 'JOBIMPORT';
$this->info[ 'name' ] = __( 'XML job import', 'backwpup' );
$this->info[ 'description' ] = __( 'Wizard for importing BackWPup jobs from an XML file', 'backwpup' );
$this->info[ 'URI' ] = __( 'http://backwpup.com', 'backwpup' );
$this->info[ 'author' ] = 'Inpsyde GmbH';
$this->info[ 'authorURI' ] = __( 'http://inpsyde.com', 'backwpup' );
$this->info[ 'version' ] = BackWPup::get_plugin_data( 'Version' );
$this->info[ 'cap' ] = 'backwpup_jobs_edit';
}
/**
* The name of the last button (execute button)
*
* @param $wizard_settings
* @return string
*/
public function get_last_button_name( array $wizard_settings ) {
return __( 'Import', 'backwpup' );
}
/**
* with steps has the wizard to to
*
* @param array $wizard_settings
*
* @return array
*/
public function get_steps( array $wizard_settings ) {
$steps = array();
$steps[0] = array( 'id' => 'FILE', 'name' => __( 'Import File', 'backwpup' ), 'description' => __( 'Upload XML job file for import', 'backwpup' ) );
$steps[1] = array( 'id' => 'SELECT', 'name' => __( 'Select items to import', 'backwpup' ), 'description' => __( 'Select which job should be imported or overwritten.', 'backwpup' ) );
return $steps;
}
/**
* called on page
*
* @param array $wizard_settings
*/
public function page( array $wizard_settings ) {
$import_xml = NULL;
if ( $wizard_settings[ 'wizard' ][ 'step' ] === 'FILE' ) {
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
$size = size_format( $bytes );
?>
<table class="form-table">
<tr>
<td>
<p><?php esc_html_e( 'Please upload your BackWPup job XML export file and we’ll import the jobs into BackWPup.', 'backwpup' ); ?></p>
<p>
<label for="upload"><?php esc_html_e( 'Choose a file from your computer:', 'backwpup' ); ?></label> (<?php printf( __('Maximum size: %s', 'backwpup' ), $size ); ?>)
<input type="file" id="upload" name="import" />
<input type="hidden" name="max_file_size" value="<?php echo esc_attr($bytes); ?>" />
</p>
</td>
</tr>
</table>
<?php
}
if ( $wizard_settings[ 'wizard' ][ 'step' ] === 'SELECT' ) {
?>
<table class="form-table">
<tr>
<td>
<?php
if ( ! empty( $wizard_settings[ 'file' ][ 'file' ] ) ) {
$import_xml = simplexml_load_file( $wizard_settings[ 'file' ][ 'file' ] );
}
if ( is_object($import_xml) && ! empty( $import_xml->job ) ) {
echo '<h3>' . __( 'Import Jobs', 'backwpup' ) . '</h3>';
$jobids = BackWPup_Option::get_job_ids();
foreach ( $import_xml->job as $job ) {
echo "<select name=\"importtype[" . esc_attr( $job->jobid ) . "]\" title=\"" . esc_attr__( 'Import Type',
'backwpup' ) . "\"><option value=\"not\">" . esc_html__( 'No Import',
'backwpup' ) . "</option>";
if ( in_array( $job->jobid, $jobids, true ) ) {
echo "<option value=\"over\">" . esc_html__( 'Overwrite','backwpup' ) . "</option><option value=\"append\">" . esc_html__( 'Append','backwpup' ) . "</option>";
} else {
echo "<option value=\"over\">" . esc_html__( 'Import', 'backwpup' ) . "</option>";
}
echo "</select>";
echo ' <span class="description">' . esc_html( $job->jobid ) . ". " . esc_html( $job->name ) . '</span><br />';
}
}
if ( is_object($import_xml) && ! empty( $import_xml->config ) ) {
?>
</td>
</tr>
<tr>
<td>
<h3><?php esc_html_e( 'Import Config', 'backwpup' ); ?></h3>
<p>
<input type="checkbox" value="1" name="import_config" id="import-config" />
<label for="import-config"><?php esc_html_e( 'Import BackWPup configuration', 'backwpup' ); ?></label>
</p>
<?php
}
?>
</td>
</tr>
</table>
<?php
}
}
/**
* called on page load to save form data
*
* @param array $wizard_settings
*
* @return array
*/
public function save( array $wizard_settings ) {
if ( isset( $wizard_settings[ 'wizard' ][ 'step'] ) && $wizard_settings[ 'wizard' ][ 'step' ] === 'FILE' ) {
if ( empty( $_FILES['import'] ) ) {
BackWPup_Admin::message( __( 'File is empty. Please upload something more substantial. This error could also caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'backwpup' ), TRUE );
return $wizard_settings;
}
$overrides = array( 'test_form' => FALSE, 'test_type' => FALSE );
//$_FILES['import']['name'] .= '.txt';
$wizard_settings[ 'file' ] = wp_handle_upload( $_FILES['import'], $overrides );
if ( isset( $wizard_settings[ 'file' ][ 'error' ] ) ) {
BackWPup_Admin::message( esc_html( $wizard_settings[ 'file' ][ 'error' ] ) );
return $wizard_settings;
}
else if ( ! is_readable( $wizard_settings[ 'file' ][ 'file' ] ) ) {
BackWPup_Admin::message( __( 'The export file could not be found at <code>%s</code>. This is likely due to an issue with permissions.', 'backwpup' ), esc_html( $wizard_settings[ 'file' ][ 'file' ] ), TRUE );
return $wizard_settings;
}
$import_xml = simplexml_load_file( $wizard_settings[ 'file' ][ 'file' ] );
if ( ! is_object( $import_xml ) ) {
BackWPup_Admin::message( __( 'Sorry, there has been a phrase error.', 'backwpup' ), TRUE );
return $wizard_settings;
}
if ( version_compare( $import_xml[ 'version' ], '3.0', '<=' ) ) {
BackWPup_Admin::message( sprintf( __( 'This Export file (version %s) may not be supported by this version of the importer.', 'backwpup' ), esc_html( $import_xml[ 'version' ] ) ), TRUE );
return $wizard_settings;
}
if ( !isset( $import_xml[ 'plugin' ] ) || $import_xml[ 'plugin' ] != 'BackWPup' ) {
BackWPup_Admin::message( sprintf( __( 'This is not a BackWPup XML file', 'backwpup' ), esc_html( $import_xml[ 'version' ] ) ), TRUE );
return $wizard_settings;
}
}
if ( isset( $wizard_settings[ 'wizard' ][ 'step'] ) && $wizard_settings[ 'wizard' ][ 'step' ] === 'SELECT' ) {
$wizard_settings[ 'select' ][ 'import_config' ] = ! empty( $_POST[ 'import_config' ] );
if ( is_array( $_POST[ 'importtype' ] ) ) {
$wizard_settings[ 'select' ][ 'importtype' ] = $_POST[ 'importtype' ];
}
}
return $wizard_settings;
}
/**
* called if last button clicked
*
* @param array $wizard_settings
*/
public function execute( array $wizard_settings ) {
if ( ! empty( $wizard_settings[ 'file' ][ 'file' ] ) ) {
$import_xml = simplexml_load_file( $wizard_settings[ 'file' ][ 'file' ] );
}
if ( empty( $import_xml ) ) {
return;
}
foreach ( $wizard_settings[ 'select' ][ 'importtype' ] as $id => $type ) {
if ( $type === 'not' || empty( $type ) )
continue;
//get data from xml
foreach ( $import_xml->job as $job ) {
if ( $job->jobid != $id )
continue;
foreach ( $job as $key => $option ) {
$import[ $id ][ $key ] = maybe_unserialize( (string)$option );
}
break;
}
if ( $type === 'append' ) {
$newjobid = BackWPup_Option::get_job_ids();
sort( $newjobid );
$import[ $id ][ 'jobid' ] = end( $newjobid ) + 1;
}
$import[ $id ][ 'activetype' ] = '';
unset( $import[ $id ][ 'cronnextrun' ] );
unset( $import[ $id ][ 'starttime' ] );
unset( $import[ $id ][ 'logfile' ] );
unset( $import[ $id ][ 'lastrun' ] );
unset( $import[ $id ][ 'lastruntime' ] );
unset( $import[ $id ][ 'lastbackupdownloadurl' ] );
foreach ( $import[ $id ] as $jobname => $jobvalue ) {
BackWPup_Option::update( $import[ $id ][ 'jobid' ], $jobname, $jobvalue );
}
//delete xml file
if ( is_file( $wizard_settings[ 'file' ][ 'file' ] ) ) {
unlink( $wizard_settings[ 'file' ][ 'file' ] );
}
echo '<div id="message" class="updated below-h2"><p>' . esc_html( sprintf( __( 'Job %1$s with id %2$d imported', 'backwpup'), $import[ $id ][ 'name' ], $import[ $id ][ 'jobid' ] ) ) . '</p></div>';
}
//get data from xml
if ( $wizard_settings[ 'select' ][ 'import_config' ] ) {
foreach ( (array)$import_xml->config as $key => $option ) {
update_site_option( 'backwpup_cfg_' . $key, maybe_unserialize( (string)$option ) );
}
echo '<div id="message" class="updated below-h2"><p>' . esc_html__( 'BackWPup config imported','backwpup') . '</p></div>';
}
}
/**
* @param $wizard_settings
*/
public function cancel( array $wizard_settings ) {
//delete xml file
if ( isset( $wizard_settings[ 'file' ][ 'file' ] ) && is_file( $wizard_settings[ 'file' ][ 'file' ] ) ) {
unlink( $wizard_settings[ 'file' ][ 'file' ] );
}
}
}