-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathld-roadmap.php
More file actions
2356 lines (2069 loc) · 76.5 KB
/
Copy pathld-roadmap.php
File metadata and controls
2356 lines (2069 loc) · 76.5 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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: LearnDash Roadmap Enhanced
* Plugin URI: https://github.com/Lotmanine/learndash-roadmap-plugin
* Description: A visual, gamified roadmap for LearnDash courses with Memberium + Keap (CRM) integration: ownership-aware access gating, CRM tag automation on level completion, and Elementor-friendly body/post classes.
* Version: 1.7.1
* Author: Lotfi O.
* Author URI: https://oldev.site
* License: GPL-2.0-or-later
* Text Domain: ld-roadmap
*
* Changelog:
*
* 1.7.1 — 2026-04-07
* - Added: "Owned URL" field on roadmap items for external content destinations.
* - Added: Tooltip descriptions on all admin fields (items + bonus courses).
* - Changed: get_course_or_item_url() now checks _owned_url before falling back
* to the item permalink, enabling proper navigation for non-LearnDash items.
*
* 1.7.0 — 2026-04-08
* - Removed: Direct LearnDash auto-enrollment (ld_update_course_access) for
* bonus courses. All access now flows through the existing Keap → Memberium
* → LearnDash chain for consistency with the site's existing access model.
* - Added: Keap tag application via Memberium when a level is completed. Uses
* [memb_set_tag] shortcode through do_shortcode() to apply the configured
* bonus tag. Triggers the normal access flow instead of bypassing it.
* - Added: "Bonus Tag ID (Keap)" field on roadmap level taxonomy for storing
* the tag ID to apply on level completion.
* - Added: "Ownership Tag ID (Keap)" field on roadmap_item CPT for items that
* are not LearnDash courses. The plugin checks this tag via Memberium as a
* fallback when no LearnDash course is linked.
* - Improved: is_roadmap_item_owned() now supports two ownership paths —
* LearnDash course access (default) or Memberium tag check (fallback for
* non-LearnDash items).
*
* 1.6.0 — 2026-04-05
* - Added: Auto-enrollment for bonus courses. When a user reaches 100%
* progress on a level and a bonus LearnDash course is assigned, the
* plugin automatically enrolls them via ld_update_course_access().
* Note: This grants LearnDash access directly — no Keap tag is created.
* If Keap tag sync is needed later, a Keap API integration can be added.
* - Changed: "Bonus Sale Page URL" label renamed to "Bonus Course URL"
* in the admin UI. Internal meta key unchanged for backwards compatibility.
* - Improved: Bonus URL resolution now falls back to course permalink
* when no custom URL is set.
* - Improved: Added comprehensive PHPDoc comments and section headers
* throughout the entire plugin for maintainability.
* - Fixed: has-bonus-course body class now checks for bonus URL in addition
* to bonus course ID, so the bonus section displays when only a URL is set.
*
* 1.5.0 — 2026-04-05
* - Added: is_bonus_course_owned() helper to check if user owns the bonus course.
* - Added: roadmap_level_bonus_button_text shortcode for 3-state button labels
* (Mastery Bonus Locked / Claim Your Reward / Go To Course).
* - Added: bonus-owned / bonus-not-owned body classes on roadmap_level archives.
* - Added: CSS visibility rules for bonus-owned-btn / bonus-not-owned-btn.
* - Improved: handle_roadmap_level_bonus_text_shortcode() now returns three
* different messages based on level completion AND bonus ownership.
* - Improved: handle_roadmap_level_bonus_course_shortcode() info="url" now
* returns course permalink when user owns the bonus, sale URL otherwise.
*
* 1.4.1 — 2026-03-31
* - Added: JavaScript alert on bonus section button click when level is
* pending. Shows "Complete this level to get this bonus" and prevents
* navigation.
*
* 1.4.0 — 2026-03-31
* - Added: Optional "Bonus Display Name" and "Bonus Display Description"
* override fields on roadmap_level taxonomy. When filled, the shortcode
* uses these instead of the linked LearnDash course title/excerpt.
*
* 1.3.0 — 2026-03-30
* - Added: body_class filter injects level-completed / level-pending and
* has-bonus-course / no-bonus-course on roadmap_level archive pages.
* - Added: CSS visibility rules for .bonus-locked-btn / .bonus-unlocked-btn
* and .bonus-section based on body classes.
*
* 1.2.0 — 2026-03-30
* - Added: post_class filter injects rm-item-owned / rm-item-not-owned on
* roadmap_item loop wrappers for Elementor visibility toggling.
* - Added: Frontend CSS (wp_head) hides .owned-btn / .not-owned-btn based
* on parent ownership class. Only loads on roadmap pages.
*
* 1.1.0 — 2026-03-30
* - Fixed: roadmap_item_url now checks course ownership before returning URL;
* owned users are sent to course content, not the sales page.
* - Fixed: Smart quote encoding issue in roadmap_level_message "LET'S GET STARTED" string.
* - Improved: Consolidated duplicate ownership logic; get_roadmap_access_state()
* now delegates to is_roadmap_item_owned() as the single source of truth.
* - Added: get_course_or_item_url() helper for consistent URL resolution.
*
* 1.0.0 — Initial release by previous developer.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/* =========================================================================
* Main Plugin Class
* ========================================================================= */
/**
* Class LD_RoadMap_Core
*
* Core class for the LearnDash Road Map Enhanced plugin. Handles:
* - Custom Post Type (roadmap_item) and Taxonomy (roadmap_level) registration
* - Admin meta boxes for linking LearnDash courses to roadmap items
* - Taxonomy meta fields for bonus courses, display overrides, and URLs
* - Ownership detection and progress calculation per level
* - Keap tag application via Memberium when a level is completed
* - Frontend shortcodes for dynamic content in Elementor templates
* - Frontend CSS for conditional visibility (owned/locked states)
* - Frontend JS for bonus section modal alerts
*/
class LD_RoadMap_Core {
/* =====================================================================
* Constructor & Hook Registration
* ===================================================================== */
/**
* Register all WordPress hooks, actions, filters, and shortcodes.
*
* This is the single entry point that wires the plugin into WordPress.
* Each hook is grouped by concern: CPT/taxonomy, admin UI, frontend
* filters, frontend assets, and shortcodes.
*/
public function __construct() {
// --- CPT & Taxonomy ---
add_action( 'init', array( $this, 'register_roadmap_structures' ) );
// --- Admin meta boxes for roadmap_item ---
add_action( 'add_meta_boxes', array( $this, 'add_item_meta_boxes' ) );
add_action( 'save_post_roadmap_item', array( $this, 'save_item_meta_data' ) );
// --- Admin taxonomy fields for roadmap_level ---
add_action( 'roadmap_level_add_form_fields', array( $this, 'render_level_bonus_course_add_field' ) );
add_action( 'roadmap_level_edit_form_fields', array( $this, 'render_level_bonus_course_edit_field' ) );
add_action( 'created_roadmap_level', array( $this, 'save_level_bonus_course_meta' ) );
add_action( 'edited_roadmap_level', array( $this, 'save_level_bonus_course_meta' ) );
// --- Frontend filters (post classes, body classes) ---
add_filter( 'post_class', array( $this, 'add_roadmap_item_post_classes' ), 10, 3 );
add_filter( 'body_class', array( $this, 'add_roadmap_level_body_classes' ) );
// --- Frontend CSS & JS ---
add_action( 'wp_head', array( $this, 'print_roadmap_visibility_css' ) );
add_action( 'wp_footer', array( $this, 'print_roadmap_bonus_alert_js' ) );
// --- Shortcodes: Item-level ---
add_shortcode( 'roadmap_access_icon', array( $this, 'handle_roadmap_access_icon_shortcode' ) );
add_shortcode( 'roadmap_access_class', array( $this, 'handle_roadmap_access_class_shortcode' ) );
add_shortcode( 'roadmap_item_url', array( $this, 'handle_roadmap_item_url_shortcode' ) );
add_shortcode( 'roadmap_course_table', array( $this, 'render_course_table_shortcode' ) );
add_shortcode( 'roadmap_item_button_text', array( $this, 'handle_roadmap_item_button_text_shortcode' ) );
add_shortcode( 'roadmap_item_sale_page_url', array( $this, 'handle_roadmap_item_sale_page_url_shortcode' ) );
// --- Shortcodes: Level-level ---
add_shortcode( 'roadmap_level_progress', array( $this, 'handle_roadmap_level_progress_shortcode' ) );
add_shortcode( 'roadmap_level_message', array( $this, 'handle_roadmap_level_message_shortcode' ) );
add_shortcode( 'roadmap_level_bonus_text', array( $this, 'handle_roadmap_level_bonus_text_shortcode' ) );
add_shortcode( 'roadmap_level_bonus_button_text', array( $this, 'handle_roadmap_level_bonus_button_text_shortcode' ) );
add_shortcode( 'roadmap_level_status_class', array( $this, 'handle_roadmap_level_status_class_shortcode' ) );
add_shortcode( 'roadmap_level_progress_bar_text', array( $this, 'handle_roadmap_level_progress_bar_text_shortcode' ) );
add_shortcode( 'roadmap_level_bonus_course', array( $this, 'handle_roadmap_level_bonus_course_shortcode' ) );
add_shortcode( 'roadmap_level_has_bonus_course', array( $this, 'handle_roadmap_level_has_bonus_course_shortcode' ) );
add_shortcode( 'roadmap_level_bonus_sale_page_url', array( $this, 'handle_roadmap_level_bonus_sale_page_url_shortcode' ) );
}
/* =====================================================================
* CPT & Taxonomy Registration
* ===================================================================== */
/**
* Register the roadmap_item custom post type and roadmap_level taxonomy.
*
* - roadmap_item: Each post represents one course/product bundle in the roadmap.
* - roadmap_level: Hierarchical taxonomy that groups items into levels (e.g. "Level 1").
*
* Hooked to: init
*/
public function register_roadmap_structures() {
$item_labels = array(
'name' => __( 'RoadMap Items', 'ld-roadmap' ),
'singular_name' => __( 'RoadMap Item', 'ld-roadmap' ),
'menu_name' => __( 'RoadMap Items', 'ld-roadmap' ),
'name_admin_bar' => __( 'RoadMap Item', 'ld-roadmap' ),
'add_new' => __( 'Add New Road Map Item', 'ld-roadmap' ),
'add_new_item' => __( 'Add New Road Map Item', 'ld-roadmap' ),
'new_item' => __( 'New Road Map Item', 'ld-roadmap' ),
'edit_item' => __( 'Edit Road Map Item', 'ld-roadmap' ),
'view_item' => __( 'View Road Map Item', 'ld-roadmap' ),
'all_items' => __( 'All Road Map Items', 'ld-roadmap' ),
'search_items' => __( 'Search Road Map Items', 'ld-roadmap' ),
'parent_item_colon' => __( 'Parent Road Map Items:', 'ld-roadmap' ),
'not_found' => __( 'No Road Map Items found.', 'ld-roadmap' ),
'not_found_in_trash' => __( 'No Road Map Items found in Trash.', 'ld-roadmap' ),
);
$item_args = array(
'labels' => $item_labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'roadmap-item' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-location-alt',
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'excerpt' ),
'show_in_rest' => true,
);
register_post_type( 'roadmap_item', $item_args );
$tax_labels = array(
'name' => __( 'RoadMap Levels', 'ld-roadmap' ),
'singular_name' => __( 'RoadMap Level', 'ld-roadmap' ),
'search_items' => __( 'Search RoadMap Levels', 'ld-roadmap' ),
'all_items' => __( 'All RoadMap Levels', 'ld-roadmap' ),
'parent_item' => __( 'Parent RoadMap Level', 'ld-roadmap' ),
'parent_item_colon' => __( 'Parent RoadMap Level:', 'ld-roadmap' ),
'edit_item' => __( 'Edit RoadMap Level', 'ld-roadmap' ),
'update_item' => __( 'Update RoadMap Level', 'ld-roadmap' ),
'add_new_item' => __( 'Add New RoadMap Level', 'ld-roadmap' ),
'new_item_name' => __( 'New RoadMap Level Name', 'ld-roadmap' ),
'menu_name' => __( 'RoadMap Levels', 'ld-roadmap' ),
);
register_taxonomy(
'roadmap_level',
array( 'roadmap_item' ),
array(
'hierarchical' => true,
'labels' => $tax_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'show_in_rest' => true,
'show_in_nav_menus' => true,
'rewrite' => array( 'slug' => 'roadmap-level' ),
)
);
}
/* =====================================================================
* Admin Meta Boxes — Item Level
* ===================================================================== */
/**
* Register meta boxes on the roadmap_item edit screen.
*
* Two meta boxes:
* 1. "Linked LearnDash Courses" — checkbox list to link LD courses to this item.
* 2. "Sale Page URL" — optional URL for users who don't own the item.
*
* Hooked to: add_meta_boxes
*/
public function add_item_meta_boxes() {
add_meta_box(
'item_courses_selection',
__( 'Linked LearnDash Courses', 'ld-roadmap' ),
array( $this, 'render_item_course_selection_html' ),
'roadmap_item',
'normal',
'high'
);
add_meta_box(
'item_sale_page_url',
__( 'Sale Page URL', 'ld-roadmap' ),
array( $this, 'render_item_sale_page_url_meta_box' ),
'roadmap_item',
'side',
'default'
);
add_meta_box(
'item_ownership_tag_id',
__( 'Ownership Tag ID (Keap)', 'ld-roadmap' ),
array( $this, 'render_item_ownership_tag_meta_box' ),
'roadmap_item',
'side',
'default'
);
add_meta_box(
'item_owned_url',
__( 'Owned URL', 'ld-roadmap' ),
array( $this, 'render_item_owned_url_meta_box' ),
'roadmap_item',
'side',
'default'
);
}
/**
* Render the "Linked LearnDash Courses" meta box.
*
* Displays a searchable checkbox list of all published LD courses.
* Selected courses are stored in post meta '_linked_course_ids' as an array.
* Ownership of a roadmap_item is determined by whether the user has access
* to ALL linked courses.
*
* @param WP_Post $post Current post being edited.
*/
public function render_item_course_selection_html( $post ) {
wp_nonce_field( 'save_roadmap_item_courses', 'roadmap_item_nonce' );
$linked_courses = get_post_meta( $post->ID, '_linked_course_ids', true );
if ( ! is_array( $linked_courses ) ) {
$linked_courses = array();
}
$courses = get_posts(
array(
'post_type' => 'sfwd-courses',
'numberposts' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( empty( $courses ) ) {
echo '<p>' . esc_html__( 'No LearnDash courses found.', 'ld-roadmap' ) . '</p>';
return;
}
?>
<style>
.roadmap-search-container { margin-bottom: 15px; padding-top: 10px; }
#roadmap-course-search {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.07);
}
.roadmap-course-list {
max-height: 350px;
overflow-y: auto;
background: #fff;
border: 1px solid #ddd;
padding: 10px;
border-radius: 4px;
}
.roadmap-course-item {
padding: 6px 0;
display: flex;
align-items: center;
border-bottom: 1px solid #f0f0f0;
}
.roadmap-course-item:last-child { border-bottom: none; }
.roadmap-course-item.hidden { display: none; }
.roadmap-course-item input { margin-right: 12px; cursor: pointer; }
.roadmap-course-item label { cursor: pointer; font-size: 13px; flex: 1; }
.no-results-found { color: #888; padding: 10px; font-style: italic; }
</style>
<div class="roadmap-search-container">
<input
type="text"
id="roadmap-course-search"
placeholder="<?php echo esc_attr__( 'Type course name to filter...', 'ld-roadmap' ); ?>"
autocomplete="off"
/>
</div>
<div class="roadmap-course-list" id="roadmap-list-container">
<?php foreach ( $courses as $course ) : ?>
<?php
$course_id = absint( $course->ID );
$course_title = get_the_title( $course_id );
$is_checked = in_array( $course_id, $linked_courses, true ) ? 'checked="checked"' : '';
?>
<div class="roadmap-course-item" data-title="<?php echo esc_attr( strtolower( $course_title ) ); ?>">
<input
type="checkbox"
name="linked_course_ids[]"
id="course_<?php echo esc_attr( $course_id ); ?>"
value="<?php echo esc_attr( $course_id ); ?>"
<?php echo $is_checked; ?>
/>
<label for="course_<?php echo esc_attr( $course_id ); ?>">
<?php echo esc_html( $course_title ); ?>
<span style="color: #999; font-size: 11px;">
(<?php echo esc_html__( 'ID:', 'ld-roadmap' ); ?> <?php echo esc_html( $course_id ); ?>)
</span>
</label>
</div>
<?php endforeach; ?>
<div id="no-results" class="no-results-found" style="display:none;">
<?php echo esc_html__( 'No courses match your search.', 'ld-roadmap' ); ?>
</div>
</div>
<script>
(function() {
const searchInput = document.getElementById('roadmap-course-search');
const items = document.querySelectorAll('.roadmap-course-item');
const noResults = document.getElementById('no-results');
if (!searchInput || !items.length || !noResults) {
return;
}
searchInput.addEventListener('input', function() {
const query = this.value.toLowerCase().trim();
let matchFound = false;
items.forEach(function(item) {
const title = item.getAttribute('data-title') || '';
if (title.includes(query)) {
item.classList.remove('hidden');
matchFound = true;
} else {
item.classList.add('hidden');
}
});
noResults.style.display = matchFound ? 'none' : 'block';
});
})();
</script>
<p class="description">
<?php echo esc_html__( 'Select the LearnDash course(s) this item represents. The plugin uses this to check ownership via sfwd_lms_has_access(). Leave empty if the item is not a LearnDash course.', 'ld-roadmap' ); ?>
</p>
<?php
}
/**
* Render the "Sale Page URL" meta box on the roadmap_item sidebar.
*
* This URL is used when a user does NOT own the item — the Buy button
* links here instead of the LearnDash course page.
* Stored in post meta: '_sale_page_url'
*
* @param WP_Post $post Current post being edited.
*/
public function render_item_sale_page_url_meta_box( $post ) {
$sale_page_url = get_post_meta( $post->ID, '_sale_page_url', true );
?>
<p>
<label for="roadmap-item-sale-page-url">
<?php esc_html_e( 'Enter the sale page URL for this item.', 'ld-roadmap' ); ?>
</label>
</p>
<input
type="url"
id="roadmap-item-sale-page-url"
name="sale_page_url"
value="<?php echo esc_attr( $sale_page_url ); ?>"
placeholder="https://example.com/sale-page"
style="width:100%;"
/>
<p class="description">
<?php esc_html_e( 'The purchase/sales page URL. When a user does NOT own this item, the Buy button links here. Should always be set.', 'ld-roadmap' ); ?>
</p>
<?php
}
/**
* Render the "Ownership Tag ID (Keap)" meta box on the roadmap_item sidebar.
*
* Used for roadmap items that are NOT LearnDash courses (e.g. external pages,
* charting platforms, downloads). The plugin checks this Keap tag via Memberium
* to determine ownership instead of checking LearnDash access.
* Leave blank for regular LearnDash items.
*
* Stored in post meta: '_ownership_tag_id'
*
* Added in v1.7.0.
*
* @param WP_Post $post Current post being edited.
*/
public function render_item_ownership_tag_meta_box( $post ) {
$ownership_tag_id = absint( get_post_meta( $post->ID, '_ownership_tag_id', true ) );
?>
<p>
<label for="roadmap-item-ownership-tag-id">
<?php esc_html_e( 'Keap Tag ID for ownership check.', 'ld-roadmap' ); ?>
</label>
</p>
<input
type="number"
id="roadmap-item-ownership-tag-id"
name="ownership_tag_id"
value="<?php echo $ownership_tag_id > 0 ? esc_attr( $ownership_tag_id ) : ''; ?>"
placeholder="e.g. 1234"
style="width:100%;"
min="0"
step="1"
/>
<p class="description">
<?php esc_html_e( 'The Keap tag ID used to check ownership via Memberium for non-LearnDash items. The plugin calls memb_hasAnyTags() with this ID. Leave empty if the item uses a Linked LearnDash Course instead.', 'ld-roadmap' ); ?>
</p>
<?php
}
/**
* Render the "Owned URL" meta box on the roadmap_item sidebar.
*
* Used for roadmap items that are NOT LearnDash courses. When the user
* owns the item, the Review button links here instead of the course page.
* For LearnDash items, leave blank — the plugin uses the course permalink.
*
* Stored in post meta: '_owned_url'
*
* Added in v1.7.1.
*
* @param WP_Post $post Current post being edited.
*/
public function render_item_owned_url_meta_box( $post ) {
$owned_url = get_post_meta( $post->ID, '_owned_url', true );
?>
<p>
<label for="roadmap-item-owned-url">
<?php esc_html_e( 'Owned URL', 'ld-roadmap' ); ?>
</label>
</p>
<input
type="url"
id="roadmap-item-owned-url"
name="owned_url"
value="<?php echo esc_attr( $owned_url ); ?>"
placeholder="https://example.com/member-content"
style="width:100%;"
/>
<p class="description">
<?php esc_html_e( 'Where the Review button links when a user owns this item. Use this for external content (charting tools, downloads, etc.) that lives outside WordPress. Leave empty if the item has a Linked LearnDash Course — the plugin will use the course permalink automatically.', 'ld-roadmap' ); ?>
</p>
<?php
}
/**
* Save roadmap_item meta data when the post is saved.
*
* Saves four fields:
* - '_linked_course_ids' (array of ints) — LearnDash course IDs linked to this item.
* - '_sale_page_url' (string) — Optional sale page URL for non-owners.
* - '_ownership_tag_id' (int) — Optional Keap tag ID for non-LearnDash ownership check.
* - '_owned_url' (string) — Optional URL for owned non-LearnDash items (v1.7.1).
*
* Includes standard WordPress security checks: revision, autosave,
* capability, and nonce verification.
*
* Hooked to: save_post_roadmap_item
*
* @param int $post_id The roadmap_item post ID being saved.
*/
public function save_item_meta_data( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['roadmap_item_nonce'] ) ) {
return;
}
$nonce = sanitize_text_field( wp_unslash( $_POST['roadmap_item_nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'save_roadmap_item_courses' ) ) {
return;
}
// Save linked course IDs.
if ( isset( $_POST['linked_course_ids'] ) && is_array( $_POST['linked_course_ids'] ) ) {
$sanitized_ids = array_map( 'absint', wp_unslash( $_POST['linked_course_ids'] ) );
$sanitized_ids = array_filter( $sanitized_ids );
update_post_meta( $post_id, '_linked_course_ids', array_values( $sanitized_ids ) );
} else {
update_post_meta( $post_id, '_linked_course_ids', array() );
}
// Save sale page URL.
if ( isset( $_POST['sale_page_url'] ) ) {
$sale_page_url = esc_url_raw( wp_unslash( $_POST['sale_page_url'] ) );
if ( ! empty( $sale_page_url ) ) {
update_post_meta( $post_id, '_sale_page_url', $sale_page_url );
} else {
delete_post_meta( $post_id, '_sale_page_url' );
}
}
// v1.7.0: Save ownership tag ID.
if ( isset( $_POST['ownership_tag_id'] ) ) {
$ownership_tag_id = absint( wp_unslash( $_POST['ownership_tag_id'] ) );
if ( $ownership_tag_id > 0 ) {
update_post_meta( $post_id, '_ownership_tag_id', $ownership_tag_id );
} else {
delete_post_meta( $post_id, '_ownership_tag_id' );
}
}
// v1.7.1: Save owned URL.
if ( isset( $_POST['owned_url'] ) ) {
$owned_url = esc_url_raw( wp_unslash( $_POST['owned_url'] ) );
if ( ! empty( $owned_url ) ) {
update_post_meta( $post_id, '_owned_url', $owned_url );
} else {
delete_post_meta( $post_id, '_owned_url' );
}
}
}
/* =====================================================================
* Admin Meta Boxes — Level (Taxonomy) Level
* ===================================================================== */
/**
* Render bonus course fields on the "Add New RoadMap Level" screen.
*
* Displays:
* - Radio list to select one bonus LearnDash course (with search filter)
* - Override fields for display name and description
* - Bonus Course URL field
*
* Hooked to: roadmap_level_add_form_fields
*/
public function render_level_bonus_course_add_field() {
$courses = get_posts(
array(
'post_type' => 'sfwd-courses',
'numberposts' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
)
);
?>
<div class="form-field term-bonus-course-wrap">
<label for="roadmap-level-bonus-course-search"><?php esc_html_e( 'Bonus LearnDash Course', 'ld-roadmap' ); ?></label>
<input
type="text"
id="roadmap-level-bonus-course-search"
placeholder="<?php echo esc_attr__( 'Type course name to filter...', 'ld-roadmap' ); ?>"
autocomplete="off"
/>
<div class="roadmap-level-bonus-course-list" style="max-height:240px; overflow-y:auto; border:1px solid #ccd0d4; background:#fff; padding:10px; margin-top:8px;">
<p>
<label>
<input type="radio" name="bonus_course_id" value="0" checked="checked" />
<?php esc_html_e( 'No bonus course', 'ld-roadmap' ); ?>
</label>
</p>
<?php if ( ! empty( $courses ) ) : ?>
<?php foreach ( $courses as $course ) : ?>
<?php
$course_id = absint( $course->ID );
$course_title = get_the_title( $course_id );
?>
<p class="roadmap-level-bonus-course-item" data-title="<?php echo esc_attr( strtolower( $course_title ) ); ?>">
<label>
<input type="radio" name="bonus_course_id" value="<?php echo esc_attr( $course_id ); ?>" />
<?php echo esc_html( $course_title ); ?>
<span style="color:#777;">(ID: <?php echo esc_html( $course_id ); ?>)</span>
</label>
</p>
<?php endforeach; ?>
<?php endif; ?>
<p id="roadmap-level-bonus-no-results" style="display:none; color:#777;">
<?php esc_html_e( 'No courses match your search.', 'ld-roadmap' ); ?>
</p>
</div>
<!-- v1.7.0: Bonus Tag ID field -->
<div class="form-field term-bonus-tag-id-wrap" style="margin-top:12px;">
<label for="roadmap-level-bonus-tag-id">
<?php esc_html_e( 'Bonus Tag ID (Keap)', 'ld-roadmap' ); ?>
</label>
<input
type="number"
id="roadmap-level-bonus-tag-id"
name="bonus_tag_id"
value=""
placeholder="e.g. 1234"
min="0"
step="1"
/>
<p class="description">
<?php esc_html_e( 'The Keap tag ID the plugin applies via Memberium when a user completes this level at 100%. Required when the bonus needs to grant access (both LearnDash courses and external tools). Leave empty only if the bonus is a simple URL with no access control.', 'ld-roadmap' ); ?>
</p>
</div>
<div class="form-field term-bonus-display-name-wrap" style="margin-top:12px;">
<label for="roadmap-level-bonus-display-name">
<?php esc_html_e( 'Bonus Display Name (override)', 'ld-roadmap' ); ?>
</label>
<input
type="text"
id="roadmap-level-bonus-display-name"
name="bonus_display_name"
value=""
placeholder="<?php echo esc_attr__( 'Leave blank to use the LearnDash course title', 'ld-roadmap' ); ?>"
/>
<p class="description">
<?php esc_html_e( 'Override the bonus course title shown on the frontend. Leave blank to use the linked course title.', 'ld-roadmap' ); ?>
</p>
</div>
<div class="form-field term-bonus-display-desc-wrap" style="margin-top:12px;">
<label for="roadmap-level-bonus-display-desc">
<?php esc_html_e( 'Bonus Display Description (override)', 'ld-roadmap' ); ?>
</label>
<textarea
id="roadmap-level-bonus-display-desc"
name="bonus_display_desc"
rows="3"
placeholder="<?php echo esc_attr__( 'Leave blank to use the LearnDash course excerpt', 'ld-roadmap' ); ?>"
></textarea>
<p class="description">
<?php esc_html_e( 'Override the bonus course description shown on the frontend. Leave blank to use the linked course excerpt.', 'ld-roadmap' ); ?>
</p>
</div>
<!-- v1.6.0: Label changed from "Bonus Sale Page URL" to "Bonus Course URL". Meta key unchanged. -->
<div class="form-field term-bonus-sale-page-url-wrap" style="margin-top:12px;">
<label for="roadmap-level-bonus-sale-page-url">
<?php esc_html_e( 'Bonus Course URL', 'ld-roadmap' ); ?>
</label>
<input
type="url"
id="roadmap-level-bonus-sale-page-url"
name="bonus_sale_page_url"
value=""
placeholder="https://example.com/bonus-course-page"
/>
<p class="description">
<?php esc_html_e( 'The URL where the bonus button links. For external bonuses, this is the destination. For LearnDash bonuses, this overrides the default course permalink. Leave empty to use the course permalink.', 'ld-roadmap' ); ?>
</p>
</div>
<p class="description">
<?php esc_html_e( 'Select the LearnDash course awarded as a bonus when this level is completed. Leave empty if the bonus is an external URL only.', 'ld-roadmap' ); ?>
</p>
</div>
<script>
(function() {
const searchInput = document.getElementById('roadmap-level-bonus-course-search');
const items = document.querySelectorAll('.roadmap-level-bonus-course-item');
const noResults = document.getElementById('roadmap-level-bonus-no-results');
if (!searchInput || !noResults) {
return;
}
searchInput.addEventListener('input', function() {
const query = this.value.toLowerCase().trim();
let matchFound = false;
items.forEach(function(item) {
const title = item.getAttribute('data-title') || '';
if (title.includes(query)) {
item.style.display = '';
matchFound = true;
} else {
item.style.display = 'none';
}
});
noResults.style.display = matchFound ? 'none' : 'block';
});
})();
</script>
<?php
}
/**
* Render bonus course fields on the "Edit RoadMap Level" screen.
*
* Same fields as the Add screen, but pre-populated with saved values.
* Uses <tr> table rows to match the WordPress taxonomy edit form layout.
*
* Hooked to: roadmap_level_edit_form_fields
*
* @param WP_Term $term The taxonomy term being edited.
*/
public function render_level_bonus_course_edit_field( $term ) {
$selected_bonus_course = absint( get_term_meta( $term->term_id, '_bonus_course_id', true ) );
$bonus_sale_page_url = get_term_meta( $term->term_id, '_bonus_sale_page_url', true );
$bonus_tag_id = absint( get_term_meta( $term->term_id, '_bonus_tag_id', true ) );
$courses = get_posts(
array(
'post_type' => 'sfwd-courses',
'numberposts' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
)
);
?>
<tr class="form-field term-bonus-course-wrap">
<th scope="row">
<label for="roadmap-level-bonus-course-search"><?php esc_html_e( 'Bonus LearnDash Course', 'ld-roadmap' ); ?></label>
</th>
<td>
<input
type="text"
id="roadmap-level-bonus-course-search"
placeholder="<?php echo esc_attr__( 'Type course name to filter...', 'ld-roadmap' ); ?>"
autocomplete="off"
style="width:100%; max-width:420px;"
/>
<div class="roadmap-level-bonus-course-list" style="max-height:260px; overflow-y:auto; border:1px solid #ccd0d4; background:#fff; padding:10px; margin-top:8px; max-width:420px;">
<p>
<label>
<input type="radio" name="bonus_course_id" value="0" <?php checked( 0, $selected_bonus_course ); ?> />
<?php esc_html_e( 'No bonus course', 'ld-roadmap' ); ?>
</label>
</p>
<?php if ( ! empty( $courses ) ) : ?>
<?php foreach ( $courses as $course ) : ?>
<?php
$course_id = absint( $course->ID );
$course_title = get_the_title( $course_id );
?>
<p class="roadmap-level-bonus-course-item" data-title="<?php echo esc_attr( strtolower( $course_title ) ); ?>">
<label>
<input type="radio" name="bonus_course_id" value="<?php echo esc_attr( $course_id ); ?>" <?php checked( $selected_bonus_course, $course_id ); ?> />
<?php echo esc_html( $course_title ); ?>
<span style="color:#777;">(ID: <?php echo esc_html( $course_id ); ?>)</span>
</label>
</p>
<?php endforeach; ?>
<?php endif; ?>
<p id="roadmap-level-bonus-no-results" style="display:none; color:#777;">
<?php esc_html_e( 'No courses match your search.', 'ld-roadmap' ); ?>
</p>
</div>
<p class="description">
<?php esc_html_e( 'Select the LearnDash course awarded as a bonus when this level is completed. Leave empty if the bonus is an external URL only.', 'ld-roadmap' ); ?>
</p>
</td>
</tr>
<!-- v1.7.0: Bonus Tag ID field -->
<tr class="form-field term-bonus-tag-id-wrap">
<th scope="row">
<label for="roadmap-level-bonus-tag-id">
<?php esc_html_e( 'Bonus Tag ID (Keap)', 'ld-roadmap' ); ?>
</label>
</th>
<td>
<input
type="number"
id="roadmap-level-bonus-tag-id"
name="bonus_tag_id"
value="<?php echo $bonus_tag_id > 0 ? esc_attr( $bonus_tag_id ) : ''; ?>"
placeholder="e.g. 1234"
style="width:100%; max-width:420px;"
min="0"
step="1"
/>
<p class="description">
<?php esc_html_e( 'The Keap tag ID the plugin applies via Memberium when a user completes this level at 100%. Required when the bonus needs to grant access (both LearnDash courses and external tools). Leave empty only if the bonus is a simple URL with no access control.', 'ld-roadmap' ); ?>
</p>
</td>
</tr>
<?php
$bonus_display_name = get_term_meta( $term->term_id, '_bonus_display_name', true );
$bonus_display_desc = get_term_meta( $term->term_id, '_bonus_display_desc', true );
?>
<tr class="form-field term-bonus-display-name-wrap">
<th scope="row">
<label for="roadmap-level-bonus-display-name">
<?php esc_html_e( 'Bonus Display Name (override)', 'ld-roadmap' ); ?>
</label>
</th>
<td>
<input
type="text"
id="roadmap-level-bonus-display-name"
name="bonus_display_name"
value="<?php echo esc_attr( $bonus_display_name ); ?>"
placeholder="<?php echo esc_attr__( 'Leave blank to use the LearnDash course title', 'ld-roadmap' ); ?>"
style="width:100%; max-width:420px;"
/>
<p class="description">
<?php esc_html_e( 'Override the bonus course title shown on the frontend. Leave blank to use the linked course title.', 'ld-roadmap' ); ?>
</p>
</td>
</tr>
<tr class="form-field term-bonus-display-desc-wrap">
<th scope="row">
<label for="roadmap-level-bonus-display-desc">
<?php esc_html_e( 'Bonus Display Description (override)', 'ld-roadmap' ); ?>
</label>
</th>
<td>
<textarea
id="roadmap-level-bonus-display-desc"
name="bonus_display_desc"
rows="3"
placeholder="<?php echo esc_attr__( 'Leave blank to use the LearnDash course excerpt', 'ld-roadmap' ); ?>"
style="width:100%; max-width:420px;"
><?php echo esc_textarea( $bonus_display_desc ); ?></textarea>
<p class="description">
<?php esc_html_e( 'Override the bonus course description shown on the frontend. Leave blank to use the linked course excerpt.', 'ld-roadmap' ); ?>
</p>
</td>
</tr>
<!-- v1.6.0: Label changed from "Bonus Sale Page URL" to "Bonus Course URL". Meta key unchanged. -->
<tr class="form-field term-bonus-sale-page-url-wrap">
<th scope="row">
<label for="roadmap-level-bonus-sale-page-url">
<?php esc_html_e( 'Bonus Course URL', 'ld-roadmap' ); ?>
</label>
</th>
<td>
<input
type="url"
id="roadmap-level-bonus-sale-page-url"
name="bonus_sale_page_url"
value="<?php echo esc_attr( $bonus_sale_page_url ); ?>"
placeholder="https://example.com/bonus-course-page"
style="width:100%; max-width:420px;"
/>
<p class="description">
<?php esc_html_e( 'The URL where the bonus button links. For external bonuses, this is the destination. For LearnDash bonuses, this overrides the default course permalink. Leave empty to use the course permalink.', 'ld-roadmap' ); ?>
</p>
</td>
</tr>
<script>
(function() {
const searchInput = document.getElementById('roadmap-level-bonus-course-search');
const items = document.querySelectorAll('.roadmap-level-bonus-course-item');
const noResults = document.getElementById('roadmap-level-bonus-no-results');
if (!searchInput || !noResults) {
return;
}
searchInput.addEventListener('input', function() {
const query = this.value.toLowerCase().trim();
let matchFound = false;
items.forEach(function(item) {
const title = item.getAttribute('data-title') || '';
if (title.includes(query)) {
item.style.display = '';
matchFound = true;
} else {
item.style.display = 'none';
}
});
noResults.style.display = matchFound ? 'none' : 'block';
});
})();
</script>
<?php
}
/**
* Save bonus course term meta when a roadmap_level is created or updated.
*
* Saves five fields from $_POST:
* - '_bonus_course_id' — The selected LearnDash course ID (0 = none).
* - '_bonus_display_name' — Optional override for the bonus course title.
* - '_bonus_display_desc' — Optional override for the bonus course description.
* - '_bonus_sale_page_url' — Optional URL for the bonus course page.
* - '_bonus_tag_id' — Optional Keap tag ID for bonus access via Memberium.
*
* Hooked to: created_roadmap_level, edited_roadmap_level
*
* @param int $term_id The term ID being saved.