-
Notifications
You must be signed in to change notification settings - Fork 0
/
learndash-import.js
61 lines (48 loc) · 1.82 KB
/
learndash-import.js
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
var runImportBtn = document.getElementById("run-import");
var runDeleteAllDataBtn = document.getElementById("delete-all-data");
var runHomeBtn = document.getElementById("go-back-to-main");
var file_frame;
if(runImportBtn)
runImportBtn.addEventListener('click', runImport);
if(runDeleteAllDataBtn)
runDeleteAllDataBtn.addEventListener('click', runDeleteAllData);
if(runHomeBtn)
runHomeBtn.addEventListener('click', runHome);
function runDeleteAllData() {
if(confirm("Are you sure you want to delete all the LearnDash Course / Quiz / Question data?"))
window.location.href = "/wp-admin/admin.php?page=learndash-import&delete=true";
}
function runHome() {
window.location.href = "/wp-admin/admin.php?page=learndash-import";
}
function runImport() {
// If the media frame already exists, reopen it.
if (file_frame) {
file_frame.open();
return;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
frame: 'select',
button: {
text: "Add Course CSV File"
},
multiple: false,
library: {
type: ['application/json']
}
});
// When an image is selected, run a callback.
file_frame.on('select', function () {
// We set multiple to false so only get one image from the uploader
var attachment = file_frame.state().get('selection').first().toJSON();
var url = attachment.url;
var hiddenForm = document.getElementById("hidden-submit-form");
var hiddenUrlField = document.getElementById("hidden-url-field");
hiddenUrlField.setAttribute("value", url);
hiddenForm.setAttribute("action", "/wp-admin/admin.php?page=learndash-import&run=true");
hiddenForm.submit();
});
// Finally, open the modal
file_frame.open();
}