vapvarun

Add course tab that the user created for BuddyPress and Learndash

Jul 15th, 2021 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. //Add course page that the user created
  2. add_action( 'bp_setup_nav', 'wbcom_learndash_profile_instructor' );
  3. function wbcom_learndash_profile_instructor() {
  4.         // Don't set up navigation if there's no member.
  5.     if ( ! is_user_logged_in() && ! bp_is_user() ) {
  6.         return;
  7.     }
  8.     $disabled_user = get_userdata( bp_displayed_user_id() );
  9.     $disabled_user_role = $disabled_user->roles;  
  10.     if( is_array( $disabled_user_role ) && ( in_array( 'ld_instructor', $disabled_user_role ) || in_array( 'administrator', $disabled_user_role ) ) ) {
  11.         $create_courses_name  = sprintf( 'My %s', \LearnDash_Custom_Label::get_label( 'courses' ) );
  12.         $create_courses_slug  = apply_filters( 'wbcom_learndash_profile_instructor_slug', 'courses' );
  13.         $course_access        = bp_core_can_edit_settings();
  14.         $bp_displayed_user_id = bp_displayed_user_id();
  15.         $bp_loggedin_user_id  = bp_loggedin_user_id();
  16.         $user_same            = ( $bp_displayed_user_id == $bp_loggedin_user_id ? true : false );
  17.         // Set slug to profile in case the xProfile component is not active
  18.         $slug = bp_get_profile_slug();
  19.         bp_core_new_nav_item(
  20.             array(
  21.                 'name'                    => $create_courses_name,
  22.                 'slug'                    => $create_courses_slug,
  23.                 'screen_function'         => 'wbcom_learndash_instructor_course_page',
  24.                 'position'                => 76,
  25.                 'default_subnav_slug'     => $create_courses_name,
  26.                 'show_for_displayed_user' => $course_access,
  27.             )
  28.         );
  29.     }
  30. }
  31.  
  32. function wbcom_learndash_instructor_course_page() {
  33.     add_action( 'bp_template_content', 'wbcom_learndash_instructor_course_page_content' );
  34.         bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
  35. }
  36. function wbcom_learndash_instructor_course_page_content() {
  37.     $user_id = bp_displayed_user_id();
  38.     $myCourse_args = array(
  39.         'post_type'   => 'sfwd-courses',
  40.         'post_status' => 'publish',
  41.         'author'      => $user_id,
  42.         'order'       => 'DESC',
  43.         'orderby'     => 'date',
  44.     );
  45.     $myCourses = new WP_Query( $myCourse_args );
  46.     if( $myCourses->have_posts() ) {
  47.         $view_to_render = isset( $_COOKIE['learnmate_course_view'] ) ? $_COOKIE['learnmate_course_view'] : 'lm-grid-view';
  48.         echo '<div id="lm-course-archive-data" class="' . $view_to_render . '">';
  49.         while( $myCourses->have_posts() ):
  50.             $myCourses->the_post();
  51.             learnmate_get_template( 'ld-template-parts/course-list-view.php' );
  52.         endwhile;
  53.         echo '</div>';
  54.         echo '<div class="lm-course-pagination-section">';
  55.             the_posts_pagination( array(
  56.                 'prev_text'          => __( '<i class="far fa-angle-double-left"></i>', 'reign-learndash-addon' ),
  57.                 'next_text'          => __( '<i class="far fa-angle-double-right"></i>', 'reign-learndash-addon' ),
  58.                 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'reign-learndash-addon' ) . ' </span>',
  59.             ) );
  60.         echo '</div>';
  61.     } else {
  62.         get_template_part( 'template-parts/content', 'none' );
  63.     }    
  64.     wp_reset_postdata();    
  65. }
  66. // Remove default course tab form profile for instructor and admin
  67. function wbcom_learndash_remove_course_nav() {
  68.     $disabled_user = get_userdata( bp_displayed_user_id() );
  69.     $disabled_user_role = $disabled_user->roles;
  70.     if( is_array( $disabled_user_role ) && ( in_array( 'ld_instructor', $disabled_user_role ) || in_array( 'administrator', $disabled_user_role ) ) ) {
  71.         bp_core_remove_nav_item( 'rla-courses', 'members' );
  72.     }
  73. }
  74. add_action( 'wp', 'wbcom_learndash_remove_course_nav' );
  75.  
Add Comment
Please, Sign In to add comment