-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-ui-tester.php
293 lines (232 loc) · 8.02 KB
/
new-ui-tester.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
292
293
<?php
if ( class_exists( 'PSOURCESnapshot_New_Ui_Tester' ) ) {
return;
}
class PSOURCESnapshot_New_Ui_Tester {
public function dashboard() {
$this->render( 'dashboard' );
}
public function snapshots() {
if ( isset( $_REQUEST['snapshot-action'] ) && 'new' === $_REQUEST['snapshot-action'] ) {
$this->render( 'snapshots/snapshot', false, array( 'action' => 'add', 'item' => array() ) );
return;
}
if ( isset( $_REQUEST['item'], PSOURCESnapshot::instance()->config_data['items'][ sanitize_text_field( $_REQUEST['item'] ) ] ) ) {
$item = PSOURCESnapshot::instance()->config_data['items'][ sanitize_text_field( $_REQUEST['item'] ) ];
$snapshot_action = 'default';
if ( isset( $_REQUEST['snapshot-action'] ) ) {
$snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
}
$force_backup = false;
switch ( $snapshot_action ) {
case 'backup':
$force_backup = true;
case 'edit':
$this->render( 'snapshots/snapshot', false, array( 'action' => 'update', 'item' => $item, 'force_backup' => $force_backup ) );
break;
case 'restore':
if ( ( isset( $_GET['snapshot-data-item'] ) ) && ( isset( $item['data'][ intval( $_GET['snapshot-data-item'] ) ] ) ) ) {
$data_item_key = intval( $_GET['snapshot-data-item'] );
$this->render( 'snapshots/restore', false, array( 'item' => $item, 'data_item_key' => $data_item_key ) );
} else {
$this->render( 'snapshots/item', false, array( 'item' => $item ) );
}
break;
default:
$this->render( 'snapshots/item', false, array( 'item' => $item ) );
}
} else {
$snapshots = PSOURCESnapshot::instance()->config_data['items'];
$count_all_snapshots = count( $snapshots );
$all_destinations = PSOURCESnapshot::instance()->config_data['destinations'];
$filter = ( isset( $_GET['destination'] ) ) ? sanitize_text_field( $_GET['destination'] ) : '';
if ( $filter !== '' && isset( $all_destinations[ $filter ] ) ) {
$filtred_snapshot = array();
foreach ( $snapshots as $key => $snapshot ) {
if ( isset( $snapshot['destination'] ) && $snapshot['destination'] === $filter ) {
$filtred_snapshot[ $key ] = $snapshot;
}
}
$snapshots = $filtred_snapshot;
}
$results_count = count( $snapshots );
$per_page = 20;
//Max number of pages
$max_pages = ceil( $results_count / $per_page );
$paged = ( ! isset( $_GET['paged'] ) ) ? 1 : intval( $_GET['paged'] );
$offset = $per_page * ( $paged - 1 );
$data = array(
'snapshots' => $snapshots,
'results_count' => $results_count,
'count_all_snapshots' => $count_all_snapshots,
'per_page' => $per_page,
'max_pages' => $max_pages,
'paged' => $paged,
'offset' => $offset,
'filter' => $filter
);
$this->render( "snapshots", false, $data );
}
}
/*public function create_snapshot(){
$this->render( "snapshots/partials/create-snapshot-progress", "Create Snapshot" );
}*/
public function destinations() {
$snapshot_action = 'default';
if ( isset( $_REQUEST['snapshot-action'] ) ) {
$snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
}
switch ( $snapshot_action ) {
case 'add':
case 'edit':
case 'update':
$this->render( 'destinations/add/index' );
break;
default:
$this->render( 'destinations' );
}
}
/*public function managed_backups() {
$model = new Snapshot_Model_Full_Backup;
$is_dashboard_active = $model->is_dashboard_active();
$is_dashboard_installed = $is_dashboard_active
? true
: $model->is_dashboard_installed();
$has_dashboard_key = $model->has_dashboard_key();
$is_client = $is_dashboard_installed && $is_dashboard_active && $has_dashboard_key;
$apiKey = $model->get_config( 'secret-key', '' );
$has_snapshot_key = $is_client && Snapshot_Model_Full_Remote_Api::get()->get_token() != false && ! empty( $apiKey );
if ( ! $is_client ) {
$this->render( "managed-backups/get-started", false, array( 'model' => $model ) );
} else if ( ! $has_snapshot_key ) {
$this->render( "managed-backups/activate", false, array( 'model' => $model ) );
} else {
$snapshot_action = 'default';
if ( isset( $_REQUEST['snapshot-action'] ) ) {
$snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
}
function __snapshot_sort_managed_backups_array ( $a, $b ){
return $b['timestamp'] - $a['timestamp'];
}
switch ( $snapshot_action ) {
case 'backup':
$this->render( "managed-backups/new-backup", false, array( 'model' => $model ) );
break;
case 'restore':
$item = false;
if ( isset( $_GET['item'] ) ) {
$item = $model->get_backup( sanitize_text_field( $_GET['item'] ) );
}
if ( $item ) {
$this->render( "managed-backups/restore", false, array( 'model' => $model, 'item' => $item ) );
break;
}
default:
$backups = $model->get_backups();
usort( $backups, '__snapshot_sort_managed_backups_array' );
$last_backup = reset( $backups );
$filter = ( isset( $_GET['date'] ) ) ? sanitize_text_field( $_GET['date'] ) : '';
$timestamps = wp_list_pluck( $backups, 'timestamp' );
$months = array();
foreach ( $timestamps as $key => $month ) {
$months[ date( 'mY', $month ) ] = date( 'F Y', $month );
}
if ( $filter !== '' && is_int( $filter ) ) {
$filtred_snapshot = array();
foreach ( $backups as $key => $snapshot ) {
if ( isset( $snapshot['timestamp'] ) && date( 'mY', $snapshot['timestamp'] ) === $filter ) {
$filtred_snapshot[ $key ] = $snapshot;
}
}
$backups = $filtred_snapshot;
}
$results_count = count( $backups );
$per_page = 20;
//Max number of pages
$max_pages = ceil( $results_count / $per_page );
$paged = ( ! isset( $_GET['paged'] ) ) ? 1 : intval( $_GET['paged'] );
$offset = $per_page * ( $paged - 1 );
$apiKey = $model->get_config( 'secret-key', '' );
$data = array(
"model" => $model,
"last_backup" => $last_backup,
"hasApikey" => ! empty( $apiKey ),
"apiKey" => $apiKey,
"apiKeyUrl" => $model->get_current_secret_key_link(),
'backups' => $backups,
'results_count' => $results_count,
'per_page' => $per_page,
'max_pages' => $max_pages,
'paged' => $paged,
'offset' => $offset,
'filter' => $filter,
'months' => $months
);
$this->render( 'managed_backups', false, $data );
}
}
}*/
public function import() {
$this->render( 'import' );
}
public function settings() {
$this->render( 'settings' );
}
/**
* @param string $file
* @param bool $deprecated
* @param array $params
* @param bool $return
* @param bool $footer
*
* @return string
*/
public function render( $file, $deprecated = false, $params = array(), $return = false, $footer = true ) {
$template_filename = "views/$file.php";
$template_file = plugin_dir_url( plugin_basename( __FILE__ ) ) . $template_filename;
if ( ! file_exists( $template_file ) ) {
$template_file = trailingslashit( dirname( __FILE__ ) ) . $template_filename;
}
if ( $return ) {
ob_start();
}
extract( $params, EXTR_SKIP );
include $template_file;
if ( $footer ) {
$this->render( 'common/footer', false, array(), false, false );
}
if ( $return ) {
return ob_get_clean();
}
foreach ( $params as $param ) {
unset( $param );
}
return null;
}
/**
* Print the form errors for a particular field, if there are any
*
* @param $field
*/
public function input_error_message( $field ) {
if ( ! isset( PSOURCESnapshot::instance()->form_errors[ $field ] ) ) {
return;
}
$field_errors = (array) PSOURCESnapshot::instance()->form_errors[ $field ];
echo '<div class="error-text">';
foreach ( $field_errors as $error ) {
echo '<p>', esc_html( $error ), '</p>';
}
echo '</div>';
}
public function input_error_class( $field, $echo = true ) {
if ( ! isset( PSOURCESnapshot::instance()->form_errors[ $field ] ) ) {
return '';
}
$class = ' validation-error';
if ( $echo ) {
echo $class;
}
return $class;
}
}