HarunRRayhan

LearnDash bypass Drip-Feed for certain users

Aug 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.28 KB | None | 0 0
  1. /**
  2.  * Add Disable Drip-Feed (LearnDash) Metabox on user profile
  3.  * @description Disable drip-feed metabox for each users.
  4.  *
  5.  * @author HarunRRayhan
  6.  */
  7.  
  8. add_action( 'show_user_profile', 'hrx_user_disable_dripfeed' );
  9. add_action( 'edit_user_profile', 'hrx_user_disable_dripfeed' );
  10.  
  11. function hrx_user_disable_dripfeed($user) {
  12.     // Check if user is Admin
  13.     if( !current_user_can('manage_options', $user->ID) ) {
  14.         return;
  15.     }
  16.     // Get Already saved disabled drip courses ID
  17.     $disabled_drip_courses_id = get_the_author_meta( 'disabled_drip_courses', $user->ID );
  18.     ?>
  19.     <h3><?php _e("Disable Drip-Feed for Courses", "divi"); ?></h3>
  20.    
  21.     <table class="form-table">
  22.         <tr>
  23.             <!-- <th>
  24.                 <label for="disabled-drip-courses"><?php _e("Course List"); ?></label>
  25.             </th> -->
  26.             <td>
  27.                 <!-- <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br /> -->
  28.                 <?php
  29.  
  30.                 // Check if LearnDash exists
  31.                 if ( post_type_exists( 'sfwd-courses' ) ) {
  32.  
  33.                     // Course Query
  34.                     $course_args = array(
  35.                         'posts_per_page'   => -1,
  36.                         'orderby'          => 'date',
  37.                         'order'            => 'DESC',
  38.                         'post_type'        => 'sfwd-courses',
  39.                         'post_status'      => 'publish',
  40.                         'suppress_filters' => true
  41.                     );
  42.                     $courses_array = get_posts( $course_args );
  43.                    
  44.                     // Check if there any course available
  45.                     if($courses_array ):
  46.                         foreach ($courses_array as $course ) {
  47.                             setup_postdata( $course );
  48.                             // echo "<pre>";
  49.                             // print_r($course);
  50.                             // echo "</pre>";
  51.                             echo '<input type="checkbox" name="disabled-drip-courses[]" value="' . $course->ID . '" id="course-' . $course->ID  . '" ' . ((in_array($course->ID, $disabled_drip_courses_id)) ? 'checked' : '') . '> <label for="course-' . $course->ID  . '">' . $course->post_title . '</label><br>';
  52.                         }
  53.                         wp_reset_postdata();
  54.                     ?>
  55.  
  56.  
  57.                     <span class="description"><?php _e("Please select course which you want to disable for driping. Multiple courses allowed."); ?></span>
  58.                     <?php
  59.                    
  60.                     else:
  61.                         // If no couse
  62.                         ?>
  63.                     <p>
  64.                         <span class="description"><?php _e("There is no course in LearnDash. Please add a course first."); ?></span>
  65.                     </p>
  66.                 <?php
  67.                     endif;    
  68.                 } ?>
  69.             </td>
  70.         </tr>
  71.     </table>
  72.  
  73.     <?php
  74. }
  75.  
  76. /**
  77.  * Saving disabled courses ID
  78.  *
  79.  * @author HarunRRayhan
  80.  */
  81. add_action( 'personal_options_update', 'save_disabled_drip_courses_id' );
  82. add_action( 'edit_user_profile_update', 'save_disabled_drip_courses_id' );
  83. function save_disabled_drip_courses_id( $user_id ) {
  84.  
  85.     if ( !current_user_can('manage_options', $user_id ) ) { return false; }
  86.     update_user_meta( $user_id, 'disabled_drip_courses', $_POST['disabled-drip-courses'] );
  87. }
  88.  
  89. /**
  90.  * Debug LD Course Data
  91.  *
  92.  * @author HarunRRayhan
  93.  */
  94. function hrx_ld_course_data_debug() {
  95.     if(is_user_logged_in() && is_singular('sfwd-lessons')){
  96.  
  97.         global $post;
  98.         // echo "<h3>Course Data</h3>";
  99.         // echo "<pre>";
  100.         // print_r($post);
  101.         // echo "</pre>";
  102.  
  103.         echo "<h4> Course Meta</h4>";
  104.         echo "<pre>";
  105.         print_r(get_post_meta($post->ID, 'course_id')[0]);
  106.         echo "</pre>";
  107.  
  108.         // echo "<h4>All Lessons List</h4>";
  109.  
  110.     }
  111. }
  112. // add_action('wp_footer', 'hrx_ld_course_data_debug');
  113.  
  114. /**
  115.  * Bypass the LearnDash Lessons Drip-Feed for admin and selected users
  116.  *
  117.  * @author HarunRRayhan
  118.  */
  119. add_filter( 'ld_lesson_access_from', 'hrx_ld_lesson_access_from', 10, 3);
  120. function hrx_ld_lesson_access_from( $return, $lesson_id = 0, $user_id = 0 ) {
  121.  
  122.    
  123.     // If the lesson_id OR user_id are empty then just return
  124.     if ( ( empty( $lesson_id ) ) || ( empty( $user_id ) ) ) return $return;
  125.  
  126.    
  127.     // If the user is admin (can manage_options) then set the $return to false
  128.     if ( user_can( $user_id, 'manage_options' ) ) {
  129.         $return = false;
  130.     }
  131.  
  132.     // User's Disabled Drip-Feed Courses array()
  133.     $disabled_drip_courses_id = get_the_author_meta( 'disabled_drip_courses', $user_id );
  134.  
  135.     // Course ID for current Lesson
  136.     $course_id = get_post_meta($lesson_id, 'course_id')[0];
  137.  
  138.     // print_r($course_id, false);
  139.  
  140.     // Bypass Drip-Feed for User
  141.     if(in_array($course_id, $disabled_drip_courses_id)) {
  142.         $return = false;
  143.     }
  144.  
  145.  
  146.    
  147.     // If the user is group leader role then bypass
  148.     // UNCOMMENT THE LINES BELOW to EXCLUDE GROUP LEADERS USERS
  149.     //if ( user_can( $user_id, 'group_leader' ) ) {
  150.     //  $return = false;
  151.     //}
  152.    
  153.     return $return;
  154. }
Add Comment
Please, Sign In to add comment