class-ld-course-wizard.php
29.4 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?php
/**
* LearnDash class for displaying the course wizard.
*
* @package LearnDash
* @since 4.1.0
*/
use LearnDash\Core\Utilities\Cast;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'LearnDash_Course_Wizard' ) ) {
/**
* Course wizard class.
*/
class LearnDash_Course_Wizard {
const PLAYLIST_PROCESS_SERVER_ENDPOINT = 'https://licensing.learndash.com/services/wp-json/learndash-playlist-parser/v1';
const PLAYLIST_PROCESS_SERVER_SSL_VERIFY = true; // false only for local testing.
const HANDLE = 'learndash-course-wizard';
const LICENSE_KEY = 'nss_plugin_license_sfwd_lms';
const LICENSE_EMAIL_KEY = 'nss_plugin_license_email_sfwd_lms';
const STEP_URL_PROCESS = 'ld_cw_process';
const STEP_COURSE_CONFIG = 'ld_cw_config';
/**
* Init the course wizard registering WP hooks
*/
public function init() {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_footer-edit.php', array( $this, 'add_wizard_button' ) );
add_action( 'admin_post_ld_course_wizard_playlist_process', array( $this, 'process_url_action' ) );
add_action( 'admin_post_ld_course_wizard_create_course', array( $this, 'create_course_action' ) );
}
/**
* Add the course wizard button to the course list table
*/
public function add_wizard_button() {
$screen = get_current_screen();
if ( is_object( $screen ) && 'edit-' . learndash_get_post_type_slug( 'course' ) === $screen->id ) {
?>
<script>
window.onload = function() {
var settingsElem = document.getElementsByClassName("ld-global-header-new-settings");
var newEntityElem = document.getElementsByClassName("global-new-entity-button");
if (settingsElem.length === 1 && newEntityElem.length === 1) {
var wizardLink = document.createElement('a');
var wizardText = document.createTextNode('<?php esc_html_e( 'Create from Video Playlist', 'learndash' ); ?>');
wizardLink.setAttribute('href', "<?php echo esc_url( admin_url( 'admin.php?page=' . self::HANDLE ) ); ?>");
wizardLink.setAttribute('class', 'global-new-entity-button');
wizardLink.setAttribute('style', 'margin-right: 10px;');
wizardLink.appendChild(wizardText);
// add element
settingsElem[0].insertBefore(wizardLink, newEntityElem[0]);
}
};
</script>
<?php
}
}
/**
* Register the admin menu for the course wizard
*/
public function register_menu() {
add_menu_page(
__( 'Course Creation Wizard', 'learndash' ),
__( 'Course Creation Wizard', 'learndash' ),
LEARNDASH_ADMIN_CAPABILITY_CHECK,
self::HANDLE,
array( $this, 'render' )
);
// hide the admin menu item, the page stays available.
remove_menu_page( self::HANDLE );
}
/**
* Register the script
*/
public function enqueue_scripts() {
$screen = get_current_screen();
if ( is_object( $screen ) && 'toplevel_page_' . self::HANDLE === $screen->id ) {
wp_enqueue_style( 'ld-tailwindcss' );
wp_register_script(
self::HANDLE,
LEARNDASH_LMS_PLUGIN_URL . 'assets/js/learndash-course-wizard' . learndash_min_asset() . '.js',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
wp_localize_script(
self::HANDLE,
'ldCourseWizard',
array(
'valid_recurring_paypal_day_max' => learndash_billing_cycle_field_frequency_max( 'D' ),
'valid_recurring_paypal_week_max' => learndash_billing_cycle_field_frequency_max( 'W' ),
'valid_recurring_paypal_month_max' => learndash_billing_cycle_field_frequency_max( 'M' ),
'valid_recurring_paypal_year_max' => learndash_billing_cycle_field_frequency_max( 'Y' ),
'buttons' => array(
'youtube' => array(
'label' => __( 'Load data from YouTube', 'learndash' ),
'img_class' => 'ld-w-8 ld-inline ld-mr-1',
'img_src' => esc_url( LEARNDASH_LMS_PLUGIN_URL . 'assets/images/youtube_icon.png' ),
'img_alt' => esc_attr(
__( 'YouTube icon', 'learndash' )
),
),
'vimeo' => array(
'label' => __( 'Load data from Vimeo', 'learndash' ),
'img_class' => 'ld-w-6 ld-inline ld-mr-1',
'img_src' => esc_url( LEARNDASH_LMS_PLUGIN_URL . 'assets/images/vimeo_icon.png' ),
'img_alt' => esc_attr(
__( 'Vimeo icon', 'learndash' )
),
),
'wistia' => array(
'label' => __( 'Load data from Wistia', 'learndash' ),
'img_class' => 'ld-w-7 ld-inline',
'img_src' => esc_url( LEARNDASH_LMS_PLUGIN_URL . 'assets/images/wistia_icon.png' ),
'img_alt' => esc_attr(
__( 'Wistia icon', 'learndash' )
),
),
'default' => array(
'label' => __( 'Load', 'learndash' ),
),
),
)
);
wp_enqueue_script( self::HANDLE );
}
}
/**
* Process the playlist URL
*/
public function process_url_action() {
if ( ! isset( $_REQUEST['ld_course_wizard_playlist_process'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['ld_course_wizard_playlist_process'] ) ), 'ld_course_wizard_playlist_process' ) ) {
learndash_safe_redirect( admin_url( 'admin.php?page=' . self::HANDLE ) ); // wrong call.
}
$playlist_url = isset( $_REQUEST['playlist_url'] ) ? esc_url_raw( wp_unslash( $_REQUEST['playlist_url'] ) ) : '';
if ( empty( $playlist_url ) ) {
learndash_safe_redirect( admin_url( 'admin.php?page=' . self::HANDLE ) ); // no playlist URL.
}
// process the URL.
$this->process_url( $playlist_url );
}
/**
* Create the course
*/
public function create_course_action() {
if ( ! isset( $_REQUEST['ld_course_wizard_create_course'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['ld_course_wizard_create_course'] ) ), 'ld_course_wizard_create_course' ) ) {
learndash_safe_redirect( admin_url( 'admin.php?page=' . self::HANDLE ) ); // wrong call.
}
$playlist_url = isset( $_REQUEST['playlist_url'] ) ? esc_url_raw( wp_unslash( $_REQUEST['playlist_url'] ) ) : '';
if ( empty( $playlist_url ) ) {
learndash_safe_redirect( admin_url( 'admin.php?page=' . self::HANDLE ) ); // no playlist URL.
}
$transient_name = $this->get_processing_transient_key( $playlist_url );
$transient_data = get_transient( $transient_name );
if ( empty( $transient_data['playlist_data'] ) ) {
learndash_safe_redirect( admin_url( 'admin.php?page=' . self::HANDLE ) ); // no transient data.
}
// create the course.
$this->create_course_from_playlist(
$transient_data['playlist_data'],
isset( $_REQUEST['course_price_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['course_price_type'] ) ) : '',
isset( $_REQUEST['course_disable_lesson_progression'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['course_disable_lesson_progression'] ) ) : '',
isset( $_REQUEST['course_price'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['course_price'] ) ) : '',
isset( $_REQUEST['course_price_billing_number'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['course_price_billing_number'] ) ) : '',
isset( $_REQUEST['course_price_billing_interval'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['course_price_billing_interval'] ) ) : ''
);
// delete the transient and redirect to the course page.
$this->delete_processing_data( $playlist_url );
learndash_safe_redirect( admin_url( 'edit.php?post_type=' . learndash_get_post_type_slug( 'course' ) ) );
}
/**
* Call the API to process the playlist URL.
*
* @param string $playlist_url The playlist URL.
*/
public function process_url( $playlist_url ) {
// reset the transient.
$this->delete_processing_data( $playlist_url );
$return_url = admin_url( 'admin.php?page=' . self::HANDLE . '&u=' . rawurlencode( $playlist_url ) );
// request server to process the playlist url.
$args = array(
'sslverify' => self::PLAYLIST_PROCESS_SERVER_SSL_VERIFY,
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => (string) wp_json_encode(
array(
'playlist_url' => rawurlencode( $playlist_url ),
'license_email' => get_option( self::LICENSE_EMAIL_KEY ),
'license_key' => get_option( self::LICENSE_KEY ),
'site_url' => rawurlencode( site_url() ),
'return_url' => rawurlencode( $return_url ),
)
),
);
$request = wp_remote_post( self::PLAYLIST_PROCESS_SERVER_ENDPOINT . '/process_url', $args );
$body = json_decode( wp_remote_retrieve_body( $request ) );
if ( ! $body || ! empty( $body->message ) ) {
$this->update_processing_data( $playlist_url, 'error_message', ! empty( $body->message ) ? $body->message : __( 'Error on access LearnDash service. Please try it again in a few minutes.', 'learndash' ) );
learndash_safe_redirect( $return_url );
}
// next step: redirect to OAuth URL.
$this->update_processing_data( $playlist_url, 'error_message', null );
wp_redirect( $body->process_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
exit();
}
/**
* Get the transient key for the processing data.
*
* @param string $playlist_url Playlist URL.
* @return string Transient name.
*/
private function get_processing_transient_key( $playlist_url ) {
return 'ld_cw_' . md5( $playlist_url );
}
/**
* Update the temporary processing data
*
* @param string $playlist_url Playlist URL.
* @param string $key Key to update.
* @param mixed $value Value to update.
*/
private function update_processing_data( $playlist_url, $key, $value ) {
$transient_name = $this->get_processing_transient_key( $playlist_url );
$transient_data = get_transient( $transient_name );
if ( false === $transient_data ) {
$transient_data = array();
}
$transient_data[ $key ] = $value;
set_transient( $transient_name, $transient_data, DAY_IN_SECONDS );
}
/**
* Delete the temporary processing data
*
* @param string $playlist_url Playlist URL.
*/
private function delete_processing_data( $playlist_url ) {
$transient_name = $this->get_processing_transient_key( $playlist_url );
delete_transient( $transient_name );
}
/**
* Get the temporary processing data, analyzing the current processing step.
*/
private function get_processing_data() {
$process_data = array(
'current_step' => self::STEP_URL_PROCESS,
'can_create_course' => false,
'error_message' => null,
'playlist_data' => null,
'playlist_url' => null,
'try_again_url' => null,
);
// check if playlist URL is set.
$playlist_url = isset( $_GET['u'] ) ? esc_url_raw( wp_unslash( $_GET['u'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( empty( $playlist_url ) ) {
return $process_data;
}
// get the transient data.
$transient_name = $this->get_processing_transient_key( $playlist_url );
$transient_data = get_transient( $transient_name );
if ( false !== $transient_data ) {
// in case of error, we need to delete the transient.
if ( isset( $transient_data['error_message'] ) && ! empty( $transient_data['error_message'] ) ) {
$this->delete_processing_data( $playlist_url );
$process_data['error_message'] = $transient_data['error_message'];
} elseif ( ! isset( $transient_data['playlist_data'] ) || empty( $transient_data['playlist_data'] ) ) {
// we need to get the playlist data only if we are not loaded it yet.
$url_data = $this->get_playlist_data( $playlist_url );
// check if it's an error.
if ( is_string( $url_data ) ) {
$process_data['error_message'] = $url_data;
$this->update_processing_data( $playlist_url, 'error_message', $process_data['error_message'] );
} else {
$process_data['playlist_data'] = $url_data;
$this->update_processing_data( $playlist_url, 'playlist_data', $process_data['playlist_data'] );
}
} else {
// we already have the data. Then, only update the current data.
$process_data['playlist_data'] = $transient_data['playlist_data'];
}
} else {
if ( ! isset( $_GET['refresh'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$process_data['error_message'] = __( 'Request expired. Please try it again.', 'learndash' );
}
}
// define control data.
if ( ! empty( $process_data['error_message'] ) || ! empty( $process_data['playlist_data'] ) ) {
$process_data['current_step'] = self::STEP_COURSE_CONFIG;
}
$process_data['can_create_course'] = empty( $process_data['error_message'] ) && ! empty( $process_data['playlist_data'] );
$process_data['playlist_url'] = $playlist_url;
$process_data['try_again_url'] = add_query_arg(
array(
'u' => $playlist_url,
'refresh' => 1,
),
admin_url( 'admin.php?page=' . self::HANDLE )
);
return $process_data;
}
/**
* Get the playlist data.
*
* @param string $playlist_url Playlist URL.
* @return array|string Playlist data or error message.
*/
private function get_playlist_data( $playlist_url ) {
$encoded_playlist_url = rawurlencode( $playlist_url );
$request = wp_remote_get(
add_query_arg(
array(
'playlist_url' => $encoded_playlist_url,
'license_email' => rawurlencode(
Cast::to_string(
get_option( self::LICENSE_EMAIL_KEY )
)
),
'license_key' => get_option( self::LICENSE_KEY ),
'site_url' => rawurlencode( site_url() ),
'return_url' => rawurlencode( admin_url( 'admin.php?page=' . self::HANDLE . '&u=' . rawurlencode( $encoded_playlist_url ) ) ),
),
self::PLAYLIST_PROCESS_SERVER_ENDPOINT . '/url_data'
),
array(
'sslverify' => self::PLAYLIST_PROCESS_SERVER_SSL_VERIFY,
)
);
$body = json_decode( wp_remote_retrieve_body( $request ) );
if ( ! $body || ! empty( $body->message ) ) {
return isset( $body->message ) ? $body->message : __( 'Error on access LearnDash service. Please try it again in a few minutes.', 'learndash' );
}
return $body->playlist_data;
}
/**
* Render the course wizard page.
*/
public function render() {
// get the current processing data.
$process_data = $this->get_processing_data();
?>
<div class="ld-container ld-mx-auto">
<div class="ld-flex ld-flex-wrap ld-flex-col ld-items-center">
<div class="ld-flex ld-mt-6">
<p class="ld-text-4xl">
<?php
echo sprintf(
// translators: course.
esc_html_x(
'Create a %s from a video playlist.',
'placeholder: course',
'learndash'
),
esc_html( LearnDash_Custom_Label::label_to_lower( 'course' ) )
);
?>
</p>
</div>
<div class="ld-flex ld-mt-2">
<p class="ld-text-xl">
<?php
echo sprintf(
// translators: course.
esc_html_x(
'You can use a YouTube Playlist, a Vimeo Showcase or a Wistia Project URL to create a LearnDash %s in a few minutes.',
'placeholder: course',
'learndash'
),
esc_html( LearnDash_Custom_Label::label_to_lower( 'course' ) )
);
?>
</p>
</div>
<div class="ld-flex ld-flex-wrap ld-flex-col ld-items-center ld-mt-10 ld-w-full">
<?php
if ( ! learndash_is_learndash_license_valid() ) {
?>
<div class="notice notice-error">
<p><?php echo esc_html__( 'Please activate your license to use this feature.', 'learndash' ); ?></p>
</div>
<?php
} else {
if ( self::STEP_URL_PROCESS === $process_data['current_step'] ) {
$this->render_url_process_step( $process_data );
} elseif ( self::STEP_COURSE_CONFIG === $process_data['current_step'] ) {
$this->render_course_config_step( $process_data );
}
}
?>
</div>
<div class="ld-flex ld-mt-8">
<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . learndash_get_post_type_slug( 'course' ) ) ); ?>" class="button">
<?php
echo sprintf(
// translators: course.
esc_html_x(
'Back to %s list',
'placeholder: course',
'learndash'
),
esc_html( LearnDash_Custom_Label::label_to_lower( 'course' ) )
);
?>
</a>
<?php if ( $process_data['can_create_course'] ) { ?>
<form id="ld_cw_create_course_form" method="post" action="<?php echo esc_url_raw( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'ld_course_wizard_create_course', 'ld_course_wizard_create_course' ); ?>
<input type="hidden" name="action" value="ld_course_wizard_create_course"/>
<input type="hidden" name="playlist_url" value="<?php echo esc_url( $process_data['playlist_url'] ); ?>"/>
<button id="ld_cw_create_course_btn" style="margin-left: 10px" type="button" class="button button-primary"><?php echo esc_html__( 'Create the course', 'learndash' ); ?></button>
</form>
<?php } ?>
</div>
</div>
</div>
<?php
}
/**
* Render the URL process step.
*
* @param array $process_data Processing data.
*/
private function render_url_process_step( $process_data ) {
?>
<div class="ld-flex ld-flex-wrap ld-flex-col ld-bg-white ld-box-content ld-w-1/2 ld-p-8 ld-rounded-md ld-shadow-lg" id="ld-course-wizard_<?php echo esc_attr( self::STEP_URL_PROCESS ); ?>">
<form method="post" action="<?php echo esc_url_raw( admin_url( 'admin-post.php' ) ); ?>" class="ld-flex">
<?php wp_nonce_field( 'ld_course_wizard_playlist_process', 'ld_course_wizard_playlist_process' ); ?>
<input type="hidden" name="action" value="ld_course_wizard_playlist_process"/>
<input placeholder="<?php echo esc_attr__( 'Enter playlist URL', 'learndash' ); ?>" id="ld_cw_playlist_url" class="ld-w-full ld-mr-3" type="url" required name="playlist_url" value="<?php echo esc_url( $process_data['playlist_url'] ); ?>" required />
<button id="ld_cw_load_data_button" class="ld-rounded-md ld-bg-blue-600 ld-text-white ld-py-3 ld-px-4 ld-font-medium" type="submit">
<span><?php echo esc_html__( 'Load', 'learndash' ); ?></span>
</button>
</form>
</div>
<?php
}
/**
* Render the course config step.
*
* @param array $process_data Processing data.
*/
private function render_course_config_step( $process_data ) {
?>
<div class="ld-flex ld-flex-wrap ld-flex-col ld-bg-white ld-box-content ld-p-8 ld-border-4" id="ld-course-wizard_<?php echo esc_attr( self::STEP_COURSE_CONFIG ); ?>">
<?php if ( ! empty( $process_data['error_message'] ) ) { ?>
<div class="w-full ld-flex ld-bg-red-100 ld-py-5 ld-px-6 ld-mb-4 ld-text-base ld-text-red-700 ld-mb-3">
<?php echo esc_html( $process_data['error_message'] ); ?>
</div>
<a class="button button-primary" href="<?php echo esc_url( $process_data['try_again_url'] ); ?>"><?php echo esc_html__( 'Try it again', 'learndash' ); ?></a>
<?php } else { ?>
<div class="w-full ld-flex ld-bg-green-100 ld-py-5 ld-px-6 ld-mb-4 ld-text-base ld-text-green-700 ld-mb-3">
<?php echo esc_html( $this->generate_course_creation_message( $process_data ) ); ?>
</div>
<div class="w-full ld-flex ld-flex-wrap ld-flex-col ld-py-5 ld-mb-3">
<div class="w-full ld-flex">
<p class="ld-text-xl"><?php echo esc_html__( 'How users will gain access to the course?', 'learndash' ); ?></p>
</div>
<div class="w-full ld-flex ld-mt-2">
<fieldset>
<div>
<input type="radio" id="ld_cw_course_price_type_open" name="ld_cw_course_price_type" value="open" <?php checked( LEARNDASH_PRICE_TYPE_OPEN, LEARNDASH_DEFAULT_COURSE_PRICE_TYPE ); ?>>
<label class="ld-font-bold" for="ld_cw_course_price_type_open">
<?php echo esc_html__( 'Open', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'The course is not protected. Any user can access its content without the need to be logged-in or enrolled.', 'learndash' ); ?></p>
</div>
<div>
<input type="radio" id="ld_cw_course_price_type_free" name="ld_cw_course_price_type" value="free" <?php checked( LEARNDASH_PRICE_TYPE_FREE, LEARNDASH_DEFAULT_COURSE_PRICE_TYPE ); ?>>
<label class="ld-font-bold" for="ld_cw_course_price_type_free">
<?php echo esc_html__( 'Free', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'The course is protected. Registration and enrollment are required in order to access the content.', 'learndash' ); ?></p>
</div>
<div>
<input type="radio" id="ld_cw_course_price_type_buy_now" name="ld_cw_course_price_type" value="paynow" <?php checked( LEARNDASH_PRICE_TYPE_PAYNOW, LEARNDASH_DEFAULT_COURSE_PRICE_TYPE ); ?>>
<label class="ld-font-bold" for="ld_cw_course_price_type_buy_now">
<?php echo esc_html__( 'Buy now', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'The course is protected via the LearnDash built-in PayPal and/or Stripe. Users need to purchase the course (one-time fee) in order to gain access.', 'learndash' ); ?></p>
<div id="ld_cw_paynow_div" class="ld-ml-6 ld-py-4 ld-border-l-4" style="display: none">
<div class="ld-ml-2">
<label class="ld-mr-10" for="ld_cw_course_price_type_paynow_price">
<?php echo esc_html__( 'Course Price', 'learndash' ); ?>
</label>
<input type="text" id="ld_cw_course_price_type_paynow_price" name="ld_cw_course_price_type_paynow_price" value="">
</div>
</div>
</div>
<div>
<input type="radio" id="ld_cw_course_price_type_subscribe" name="ld_cw_course_price_type" value="subscribe" <?php checked( LEARNDASH_PRICE_TYPE_SUBSCRIBE, LEARNDASH_DEFAULT_COURSE_PRICE_TYPE ); ?>>
<label class="ld-font-bold" for="ld_cw_course_price_type_subscribe">
<?php echo esc_html__( 'Recurring', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'The course is protected via the LearnDash built-in PayPal and/or Stripe. Users need to purchase the course (recurring fee) in order to gain access.', 'learndash' ); ?></p>
<div id="ld_cw_subscribe_div" class="ld-ml-6 ld-py-4 ld-border-l-4" style="display: none">
<div class="ld-ml-2">
<label class="ld-mr-10" for="ld_cw_course_price_type_subscribe_price">
<?php echo esc_html__( 'Course Price', 'learndash' ); ?>
</label>
<input type="text" id="ld_cw_course_price_type_subscribe_price" name="ld_cw_course_price_type_subscribe_price" value="">
</div>
<div class="ld-mt-4 ld-ml-2">
<span class="ld-mr-10">
<?php echo esc_html__( 'Billing Cycle', 'learndash' ); ?>
</span>
<input size=5 type="number" id="ld_cw_course_price_billing_number" min=0 max=0 name="ld_cw_course_price_billing_number" value="">
<select id="ld_cw_course_price_billing_interval" name="ld_cw_course_price_billing_interval" value="">
<option value=""><?php echo esc_html__( 'select interval', 'learndash' ); ?></option>
<option value="D"><?php echo esc_html__( 'day(s)', 'learndash' ); ?></option>
<option value="W"><?php echo esc_html__( 'week(s)', 'learndash' ); ?></option>
<option value="M"><?php echo esc_html__( 'month(s)', 'learndash' ); ?></option>
<option value="Y"><?php echo esc_html__( 'year(s)', 'learndash' ); ?></option>
</select>
</div>
</div>
</div>
<div>
<input type="radio" id="ld_cw_course_price_type_closed" name="ld_cw_course_price_type" value="closed" <?php checked( LEARNDASH_PRICE_TYPE_CLOSED, LEARNDASH_DEFAULT_COURSE_PRICE_TYPE ); ?>>
<label class="ld-font-bold" for="ld_cw_course_price_type_closed">
<?php echo esc_html__( 'Closed', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'The course can only be accessed through admin enrollment (manual), group enrollment, or integration (shopping cart or membership) enrollment.', 'learndash' ); ?></p>
</div>
</fieldset>
</div>
</div>
<div class="w-full ld-flex ld-flex-wrap ld-flex-col">
<div class="w-full ld-flex">
<p class="ld-text-xl"><?php echo esc_html__( 'How users will interact with the content?', 'learndash' ); ?></p>
</div>
<div class="w-full ld-flex ld-mt-2">
<fieldset>
<div>
<input type="radio" id="ld_cw_course_progression_linear" name="ld_cw_course_progression" value="" checked="checked">
<label class="ld-font-bold" for="ld_cw_course_progression_linear">
<?php echo esc_html__( 'Linear form', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'Requires the user to progress through the course in the designated step sequence.', 'learndash' ); ?></p>
</div>
<div>
<input type="radio" id="ld_cw_course_progression_free" name="ld_cw_course_progression" value="on">
<label class="ld-font-bold" for="ld_cw_course_progression_free">
<?php echo esc_html__( 'Free form', 'learndash' ); ?>
</label>
<p class="ld-ml-6 ld-mb-3"><?php esc_html_e( 'Allows the user to move freely through the course without following the designated step sequence.', 'learndash' ); ?></p>
</div>
</fieldset>
</div>
</div>
<?php } ?>
</div>
<?php
}
/**
* Generate the course creation message based on the playlist_data
*
* @param array $process_data Processing data.
* @return string|false The course generation message or false if playlist_data is empty.
*/
private function generate_course_creation_message( $process_data ) {
if ( empty( $process_data['playlist_data'] ) ) {
return false;
}
$course_name = $process_data['playlist_data']->playlist_title;
$course_qty_lessons = $process_data['playlist_data']->playlist_count;
return sprintf(
// translators: placeholders: course name, lessons qty description.
esc_html_x( 'The course "%1$s" will be created with %2$s.', 'placeholders: course name, lessons qty description', 'learndash' ),
$course_name,
// translators: placeholders: number of lessons.
sprintf( _n( '%s lesson', '%s lessons', $course_qty_lessons, 'learndash' ), $course_qty_lessons )
);
}
/**
* Create a course based on the playlist.
*
* @param object $playlist_data - Playlist data. {
* { @type string $playlist_title Playlist title.
* @type string $playlist_description Playlist description.
* @type int $playlist_count Playlist count.
* @type array $playlist_items {
* @type string $video_title Video title.
* @type string $video_description Video description.
* @type string $video_id Video id.
* @type string $video_url Video url.
* } Playlist items.
* }
* @param string $course_price_type - Course price type.
* @param string $course_disable_lesson_progression - Course disable lesson progression.
* @param string $course_price - Course price, only for paynow and subscribe.
* @param string $course_price_billing_number - Course price billing cycle number, only for subscribe.
* @param string $course_price_billing_interval - Course price billing cycle interval, only for subscribe.
* @return int - Course post id
*/
private function create_course_from_playlist( $playlist_data, $course_price_type, $course_disable_lesson_progression, $course_price = null, $course_price_billing_number = null, $course_price_billing_interval = null ) {
$course_post = array(
'post_title' => sanitize_text_field( $playlist_data->playlist_title ),
'post_content' => sanitize_text_field( $playlist_data->playlist_description ),
'post_type' => learndash_get_post_type_slug( 'course' ),
'post_status' => 'publish',
);
$course_id = wp_insert_post( $course_post );
$shared_steps = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) === 'yes';
// lesson data.
foreach ( $playlist_data->playlist_items as $video_data ) {
$lesson_post = array(
'post_title' => sanitize_text_field( $video_data->video_title ),
'post_content' => sanitize_text_field( $video_data->video_description ),
'post_type' => learndash_get_post_type_slug( 'lesson' ),
'post_status' => 'publish',
'meta_input' => array(
'course_id' => $course_id,
),
);
$lesson_id = wp_insert_post( $lesson_post );
learndash_update_setting( $lesson_id, 'lesson_video_enabled', 'on' );
learndash_update_setting( $lesson_id, 'lesson_video_url', sanitize_text_field( $video_data->video_url ) );
if ( ! $shared_steps ) {
learndash_update_setting( $lesson_id, 'course', $course_id );
}
}
learndash_update_setting( $course_id, 'course_price_type', sanitize_text_field( $course_price_type ) );
learndash_update_setting( $course_id, 'course_disable_lesson_progression', sanitize_text_field( $course_disable_lesson_progression ) );
if ( 'paynow' === $course_price_type || 'subscribe' === $course_price_type ) {
learndash_update_setting( $course_id, 'course_price', sanitize_text_field( $course_price ) );
if ( 'subscribe' === $course_price_type ) {
learndash_update_setting( $course_id, 'course_price_billing_p3', sanitize_text_field( $course_price_billing_number ) );
learndash_update_setting( $course_id, 'course_price_billing_t3', sanitize_text_field( $course_price_billing_interval ) );
}
}
return $course_id;
}
}
}