/
class.llms.admin.import.php
372 lines (294 loc) · 10.3 KB
/
class.llms.admin.import.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Admin Import Screen and form submission handling
*
* @package LifterLMS/Admin/Classes
*
* @since 3.3.0
* @version 6.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Admin Import Screen and form submission handling class
*
* @since 3.3.0
* @since 3.30.1 Explicitly include template functions during imports.
* @since 3.35.0 Initialize at `admin_init` instead of `init`.
* Import template from the admin views directory instead of the frontend templates directory.
* Improve error handling.
* @since 3.36.3 Fixed a typo where "$generator" was spelled "$generater".
* @since 3.37.3 Don't unslash uploaded file `tmp_name`.
* @since 6.0.0 Removed the deprecated `LLMS_Admin_Import::localize_stat()` method.
*/
class LLMS_Admin_Import {
/**
* Constructor
*
* @since 3.3.0
* @since 3.35.0 Initialize at `admin_init` instead of `init`.
* @since 4.8.0 Added hooks handling cloud imports and outputting WP help tabs.
*
* @return void
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'cloud_import' ) );
add_action( 'admin_init', array( $this, 'upload_import' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'current_screen', array( $this, 'add_help_tabs' ) );
}
/**
* Add WP_Screen help tabs
*
* @since 4.8.0
*
* @return WP_Screen|boolean Returns the WP_Screen on success or false if called outside of the intended screen context.
*/
public function add_help_tabs() {
$screen = $this->get_screen();
if ( ! $screen ) {
return false;
}
$screen->add_help_tab(
array(
'id' => 'llms_import_overview',
'title' => __( 'Overview', 'lifterlms' ),
'content' => $this->get_view( 'help-tab-overview' ),
)
);
$screen->set_help_sidebar( $this->get_view( 'help-sidebar' ) );
return $screen;
}
/**
* Handle form submission of a cloud import file
*
* @since 4.8.0
*
* @return WP_Error|boolean Returns `false` for nonce or user permission errors, `true` on success, or an error object.
*/
public function cloud_import() {
if ( ! llms_verify_nonce( 'llms_cloud_importer_nonce', 'llms-cloud-importer' ) || ! current_user_can( 'manage_lifterlms' ) ) {
return false;
}
$course_id = llms_filter_input( INPUT_POST, 'llms_cloud_import_course_id', FILTER_SANITIZE_NUMBER_INT );
if ( ! $course_id ) {
return $this->show_error( new WP_Error( 'llms-cloud-import-missing-id', __( 'Error: Missing course ID.', 'lifterlms' ) ) );
}
$res = LLMS_Export_API::get( array( $course_id ) );
if ( is_wp_error( $res ) ) {
return $this->show_error( $res );
}
return $this->handle_generation( $res );
}
/**
* Enqueue static assets used on the screen
*
* @since 4.8.0
*
* @return null|boolean Returns `null` when called outside of the intended screen context, `true` on success, or `false` on error.
*/
public function enqueue() {
if ( ! $this->get_screen() ) {
return null;
}
return llms()->assets->enqueue_style( 'llms-admin-importer' );
}
/**
* Convert an array of generated content IDs to a list of anchor tags to edit the generated content
*
* @since 4.7.0
*
* @param int[] $ids Array of object IDs. Either WP_Post IDs or WP_User IDs.
* @param string $type Object's type. Either "post" or "user".
* @return string A comma-separated list of HTML anchor tags.
*/
protected function get_generated_content_list( $ids, $type ) {
$list = array();
foreach ( $ids as $id ) {
if ( 'post' === $type ) {
$link = get_edit_post_link( $id );
$text = get_the_title( $id );
} elseif ( 'user' === $type ) {
$link = get_edit_user_link( $id );
$text = get_user_by( 'ID', $id )->display_name;
}
$list[] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $link ), $text );
}
return implode( ', ', $list );
}
/**
* Retrieve an instance of the WP_Screen for the import screen
*
* @since 4.8.0
*
* @return WP_Screen|boolean Returns a `WP_Screen` object when on the import screen, otherwise returns `false`.
*/
protected function get_screen() {
$screen = get_current_screen();
if ( $screen instanceof WP_Screen && 'lifterlms_page_llms-import' === $screen->id ) {
return $screen;
}
return false;
}
/**
* Retrieves the HTML of a view from the views/import directory.
*
* @since 4.8.0
*
* @param string $file The file basename of the view to retrieve.
* @return string The HTML content of the view.
*/
protected function get_view( $file ) {
ob_start();
include 'views/import/' . $file . '.php';
return ob_get_clean();
}
/**
* Retrieves a "Success" message providing information about the imported content.
*
* @since 4.7.0
*
* @param LLMS_Generator $generator Generator instance.
* @return string
*/
protected function get_success_message( $generator ) {
$msg = '<strong>' . __( 'Import Successful!', 'lifterlms' ) . '</strong><br>';
$msg .= '<ul>';
$generated = $generator->get_generated_content();
if ( ! empty( $generated['course'] ) ) {
// Translators: %s = comma-separated list of anchors to the imported courses.
$msg .= '<li>' . sprintf( __( 'Imported courses: %s', 'lifterlms' ), $this->get_generated_content_list( $generated['course'], 'post' ) ) . '</li>';
}
if ( ! empty( $generated['user'] ) ) {
// Translators: %s = comma-separated list of anchors to the imported users.
$msg .= '<li>' . sprintf( __( 'Imported users: %s', 'lifterlms' ), $this->get_generated_content_list( $generated['user'], 'user' ) ) . '</li>';
}
$msg .= '</ul>';
return $msg;
}
/**
* Instantiate and generate raw data via LLMS_Generator
*
* @since 4.8.0
*
* @param string|array $raw A JSON string or array or raw data which can be parsed by an LLMS_Generator instance.
* @return WP_Error|boolean On success, returns true or an error object on failure.
*/
protected function handle_generation( $raw ) {
$generator = new LLMS_Generator( $raw );
if ( is_wp_error( $generator->set_generator() ) ) {
return $this->show_error( $generator->error );
}
$generator->generate();
if ( $generator->is_error() ) {
return $this->show_error( $generator->error );
}
LLMS_Admin_Notices::flash_notice( $this->get_success_message( $generator ), 'success' );
return true;
}
/**
* Handle HTML output on the screen
*
* @since 3.3.0
* @since 3.35.0 Import template from the admin views directory instead of the frontend templates directory.
* @since 4.7.0 Moved logic for generating success message into its own method.
*
* @return void
*/
public static function output() {
include 'views/import.php';
}
/**
* Output an admin notice from a WP_Error object
*
* @since 4.8.0
*
* @param WP_Error $error WP_Error object.
* @return WP_Error Returns the same error passed into the object.
*/
protected function show_error( $error ) {
LLMS_Admin_Notices::flash_notice( $error->get_error_message(), 'error' );
return $error;
}
/**
* Handle form submission
*
* @since 3.3.0
* @since 3.30.1 Explicitly include template functions.
* @since 3.35.0 Validate nonce and user permissions before processing import data.
* Moved statistic localization into its own function.
* Updated return signature.
* @since 3.36.3 Fixed a typo where "$generator" was spelled "$generater".
* @since 3.37.3 Don't unslash uploaded file `tmp_name`.
* @since 4.8.0 Use helper methods `show_error()` and `handle_generation()`.
*
* @return boolean|WP_Error false for nonce or permission errors, WP_Error when an error is encountered, true on success.
*/
public function upload_import() {
if ( ! llms_verify_nonce( 'llms_importer_nonce', 'llms-importer' ) || ! current_user_can( 'manage_lifterlms' ) || empty( $_FILES['llms_import'] ) ) {
return false;
}
// Fixes an issue where hooks are loaded in an unexpected order causing template functions required to parse an import aren't available.
llms()->include_template_functions();
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$validate = $this->validate_upload( $_FILES['llms_import'] );
// File upload error.
if ( is_wp_error( $validate ) ) {
return $this->show_error( $validate );
}
$raw = ! empty( $_FILES['llms_import']['tmp_name'] ) ? file_get_contents( sanitize_text_field( $_FILES['llms_import']['tmp_name'] ) ) : array(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitizedr
return $this->handle_generation( $raw );
}
/**
* Validate the uploaded file
*
* @since 3.3.0
* @since 3.35.0 Fix undefined variable error.
*
* @link https://www.php.net/manual/en/features.file-upload.errors.php
*
* @param array $file array of file data.
* @return WP_Error|true
*/
private function validate_upload( $file ) {
if ( ! empty( $file['error'] ) ) {
switch ( $file['error'] ) {
case UPLOAD_ERR_INI_SIZE:
$msg = __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'lifterlms' );
break;
case UPLOAD_ERR_FORM_SIZE:
$msg = __( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'lifterlms' );
break;
case UPLOAD_ERR_PARTIAL:
$msg = __( 'The uploaded file was only partially uploaded.', 'lifterlms' );
break;
case UPLOAD_ERR_NO_FILE:
$msg = __( 'No file was uploaded.', 'lifterlms' );
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg = __( 'Missing a temporary folder.', 'lifterlms' );
break;
case UPLOAD_ERR_CANT_WRITE:
$msg = __( 'Failed to write file to disk.', 'lifterlms' );
break;
case UPLOAD_ERR_EXTENSION:
$msg = __( 'File upload stopped by extension.', 'lifterlms' );
break;
default:
$msg = __( 'Unknown upload error.', 'lifterlms' );
}
} else {
$info = pathinfo( $file['name'] );
if ( 'json' !== strtolower( $info['extension'] ) ) {
$msg = __( 'Only valid JSON files can be imported.', 'lifterlms' );
}
}
if ( ! empty( $msg ) ) {
return new WP_Error( 'llms_import_file_error', $msg );
}
return true;
}
}
return new LLMS_Admin_Import();