-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearndash-import.php
More file actions
371 lines (302 loc) · 13.3 KB
/
learndash-import.php
File metadata and controls
371 lines (302 loc) · 13.3 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
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
<?php
/*
Plugin Name: LearnDash JSON Import
Plugin URI: https://www.corduroybeach.com/
Description: A plugin for importing Courses / Quizzes / Questions and other things through the use of JSON
Version: 0.5.0
Author: Brandon Tassone
Author URI: https://www.corduroybeach.com/
*/
require_once("vendor/autoload.php");
global $wpdb;
// TODO: Debugging tool, remove Kint for 1.0.0
\Kint::$maxLevels = 0;
// Constants - Post Types
define('COURSES_POST_TYPE', 'sfwd-courses');
define('QUIZ_POST_TYPE', 'sfwd-quiz');
// Constants - Database Table Names
define('QUIZ_MASTER_TABLE', $wpdb->prefix . 'wp_pro_quiz_master');
define('QUIZ_QUESTION_TABLE', $wpdb->prefix . 'wp_pro_quiz_question');
define('QUIZ_PREREQ_TABLE', $wpdb->prefix . 'wp_pro_quiz_prerequisite');
define('WP_POSTMETA_TABLE', $wpdb->prefix . 'postmeta');
define('WP_POSTS_TABLE', $wpdb->prefix . 'posts');
// Register Filters
add_filter('upload_mimes', 'learndash_import_add_json_mime', 1, 1);
// Register Actions
add_action('admin_menu', 'learndash_import_menu');
add_action('admin_enqueue_scripts', 'learndash_import_styles');
add_action('admin_enqueue_scripts', 'learndash_import_javascript');
// Filter Functions
function learndash_import_add_json_mime($mime_types) {
$mime_types['json'] = 'application/json';
return $mime_types;
}
// Action Functions
function learndash_import_menu() {
add_menu_page('LearnDash Import', 'LearnDash Import', 'manage_options', 'learndash-import', 'learndash_import_menu_page');
}
function learndash_import_styles() {
wp_register_style('learndash-import-main-style', plugin_dir_url(__FILE__) . "learndash-import.css");
wp_enqueue_style('learndash-import-main-style');
}
function learndash_import_javascript() {
wp_register_script('learndash-import-main-script', plugin_dir_url(__FILE__) . "learndash-import.js", array(), '', true);
wp_enqueue_media();
wp_enqueue_script('learndash-import-main-script');
}
// Admin Menu Functions
function learndash_import_menu_page() {
$run = isset($_GET['run']) ? $_GET['run'] : '';
$delete = isset($_GET['delete']) ? $_GET['delete'] : '';
$import_url = isset($_POST['import_url']) ? $_POST['import_url'] : '';
$mock_json_structure = file_get_contents(plugin_dir_url(__FILE__) . "mock-json-structure.json");
?>
<form id="hidden-submit-form" method="post">
<input id="hidden-url-field" type="hidden" name="import_url" value="" />
</form>
<div class="wrap learndash-import-wrap">
<div class="main-action-wrap">
<button id="run-import">Upload & Import</button>
<button id="delete-all-data">Delete All LearnDash Data</button>
<button id="go-back-to-main">Home</button>
</div>
<?php
if($run && $import_url) {
$json_file = file_get_contents($import_url);
$data = json_decode($json_file);
?>
<div class="heading-wrap">
<div class="title-wrap">
<h1 class="heading-title">Importing Data</h1>
</div>
</div>
<div class="courses-import-container">
<?php
foreach($data as $course):
$course_id = $course->course_id;
$course_title = $course->course_title;
$wp_course_id = learndash_import_create_course($course_id, $course_title);
$quiz_count = 0;
$question_count = 0;
$answer_count = 0;
?>
<div class="course-item">
<?php
$quiz_prereq_info = array();
foreach($course->quizzes as $quiz):
$quiz_id = $quiz->quiz_id;
$quiz_title = $quiz->quiz_title;
$prereq_quiz_id = $quiz->prereq_quiz_id ?: 'NULL';
$prereq_info = learndash_import_create_quiz($quiz_id, $quiz_title, $wp_course_id, $prereq_quiz_id);
$quiz_prereq_info[] = $prereq_info;
foreach($quiz->questions as $question):
$question_text = $question->question_text;
$possible_answers = $question->possible_answers;
learndash_import_create_question($prereq_info["quiz_id_master"], $question_text, $possible_answers);
$answer_count += count($possible_answers);
$question_count++;
endforeach;
$quiz_count++;
endforeach;
// Update the recently imported prerequisites
learndash_import_set_prerequisites($quiz_prereq_info);
?>
<div class="title-row">
<label>Course: </label>
<span><?php echo $course_title; ?></span>
</div>
<div class="information-row">
<div>
<label>Number of quizzes imported</label>
<span><?php echo $quiz_count; ?></span>
</div>
<div>
<label>Number of questions imported</label>
<span><?php echo $question_count; ?></span>
</div>
<div>
<label>Number of answers imported</label>
<span><?php echo $answer_count; ?></span>
</div>
</div>
</div>
<?php
endforeach;
?>
</div>
<?php
}
if($delete) {
?>
<div class="title-wrap delete-data">
<h1 class="heading-title">Removing All LearnDash Data</h1>
</div>
<?php
$passed = learndash_import_delete_all_data();
$passed_text = $passed ? "Deleted everything successfully" : "Failed deleting a table somewhere.";
$passed_class = $passed ? "success" : "failure";
?>
<div class="deleted-tables-status <?php echo $passed_class; ?>">
<?php echo $passed_text; ?>
</div>
<?php
}
if(!$run && !$delete) {
?>
<div class="title-wrap description">
<h1 class="heading-title">LearnDash Import</h1>
</div>
<div style="margin-bottom: 30px;">
<p>
LearnDash Import is a JSON importer. Currently there are 2 supported actions Run Import which will run
the JSON import and Delete All LearnDash data. Best used when its a brand new site and you messed up
the import somehow and need to start again
</p>
<p>
Listed below is the current JSON template. More options will be added in the future
</p>
</div>
<div>
<h3>Example JSON Import Structure</h3>
<pre class="json-structure"><?php echo $mock_json_structure; ?></pre>
</div>
<?php
}
?>
</div>
<?php
}
// Program Functions
function learndash_import_create_course($course_id, $course_title) {
// Create the course
$wp_course_id = wp_insert_post(array(
"post_title" => $course_title,
"post_type" => COURSES_POST_TYPE,
"post_status" => "publish"
));
// Add the associated import id from the json for later reference
add_post_meta($wp_course_id, "imported_course_id", $course_id, true);
return $wp_course_id;
}
function learndash_import_create_quiz($quiz_id, $quiz_title, $course_id, $prereq_quiz_id) {
global $wpdb;
$quiz_master_inserted_ids = array();
// Create the quiz
$wp_quiz_id = wp_insert_post(array(
"post_title" => $quiz_title,
"post_type" => QUIZ_POST_TYPE,
"post_status" => "publish"
));
// Add the associated import id from the json for later reference
add_post_meta($wp_quiz_id, "imported_quiz_id", $quiz_id, true);
$quiz_insert_arr = array(
"name" => $quiz_title,
"text" => ""
);
$wpdb->insert(QUIZ_MASTER_TABLE, $quiz_insert_arr);
$quiz_id_master = $wpdb->insert_id;
add_post_meta($wp_quiz_id, "course_id", $course_id, true);
add_post_meta($wp_quiz_id, "lesson_id", 0, true);
add_post_meta($wp_quiz_id, "quiz_pro_id", $quiz_id_master, true);
add_post_meta($wp_quiz_id, "quiz_pro_id_$quiz_id_master", $quiz_id_master, true);
add_post_meta($wp_quiz_id, "_sfwd-quiz", array(
"sfwd-quiz_course" => $course_id,
"sfwd-quiz_quiz_pro" => $quiz_id_master,
"sfwd-quiz_repeats" => 0,
"sfwd-quiz_threshold" => 0.7,
"sfwd-quiz_passingpercentage" => 70,
"sfwd-quiz_lesson" => 0,
"sfwd-quiz_certificate" => 0
), true);
return array(
"wp_quiz_id" => $wp_quiz_id,
"quiz_id_master" => $quiz_id_master,
"quiz_uid" => $quiz_id,
"prereq_quiz_uid" => $prereq_quiz_id
);
}
function learndash_import_create_question($quiz_master_id, $question_text, $possible_answers){
global $wpdb;
$sort_next = $wpdb->get_results($wpdb->prepare("SELECT count(quiz_id) AS sort_next FROM wp_wp_pro_quiz_question WHERE quiz_id = %d", array($quiz_master_id)));
$sort_next = $sort_next[0]->sort_next;
$answer_types = array();
foreach($possible_answers as $possible_answer) {
$answer_id = $possible_answer->answer_id;
$answer_text = $possible_answer->answer_text;
$correct = $possible_answer->correct;
$answer_type = new WpProQuiz_Model_AnswerTypes();
$answer_type->setAnswer($answer_text);
$answer_type->setCorrect($correct);
$answer_types[] = $answer_type;
}
$answer_types = serialize($answer_types);
$wpdb->insert(QUIZ_QUESTION_TABLE, array(
"quiz_id" => $quiz_master_id,
"online" => 1,
"sort" => intval($sort_next) + 1,
"points" => 1,
"title" => "Question #" . (intval($sort_next) + 1),
"question" => $question_text,
"correct_same_text" => 0,
"tip_enabled" => 0,
"answer_type" => "multiple",
"show_points_in_box" => 0,
"answer_points_activated" => 0,
"answer_data" => $answer_types,
"category_id" => 0,
"answer_points_diff_modus_activated" => 0,
"disable_correct" => 0,
"matrix_sort_answer_criteria_width" => 0
));
}
function learndash_import_set_prerequisites($quiz_prereq_info) {
global $wpdb;
foreach($quiz_prereq_info as $prereq_info) {
if($prereq_info["prereq_quiz_uid"] != 'NULL') {
$wpdb->query($wpdb->prepare("UPDATE " . QUIZ_MASTER_TABLE . " SET prerequisite=%d WHERE id=%d", array(1, intval($prereq_info["quiz_id_master"]))));
$prereq_master_id = learndash_import_find_prereq_quiz_master_id($prereq_info["prereq_quiz_uid"]);
$wpdb->insert(QUIZ_PREREQ_TABLE, array(
"prerequisite_quiz_id" => intval($prereq_info["quiz_id_master"]),
"quiz_id" => $prereq_master_id
));
}
}
}
function learndash_import_find_prereq_quiz_master_id($prereq_quiz_uid){
global $wpdb;
$query = "SELECT meta_value FROM wp_postmeta WHERE post_id IN (
SELECT post_id FROM wp_postmeta WHERE meta_value = %s
) AND meta_key = 'quiz_pro_id'";
$master_id = $wpdb->get_results($wpdb->prepare($query, array($prereq_quiz_uid)));
$master_id = $master_id[0]->meta_value;
return $master_id;
}
function learndash_import_delete_all_data() {
global $wpdb;
$passed = true;
$tables = array(WP_POSTS_TABLE, QUIZ_MASTER_TABLE, QUIZ_QUESTION_TABLE, QUIZ_PREREQ_TABLE);
$wp_tables = array(WP_POSTS_TABLE);
echo "<pre class='rows-removed-container'>";
foreach($tables as $table) {
if(in_array($table, $wp_tables)) {
$query = array(
"posts_per_page" => -1,
"post_type" => array(QUIZ_POST_TYPE, COURSES_POST_TYPE)
);
$posts = get_posts($query);
foreach($posts as $post) {
wp_delete_post($post->ID);
}
$passed = count($posts);
} else {
$passed = $wpdb->query($wpdb->prepare("DELETE FROM $table", array()));
}
echo "Table: $table | Rows Deleted: $passed<br />";
if(!is_numeric($passed) && !$passed) {
echo "</pre>";
return false;
}
}
echo "</pre>";
return true;
}