Advertisement
verygoodplugins

Untitled

Feb 5th, 2021
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 48.07 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7.  
  8.  
  9. class WPF_LearnDash extends WPF_Integrations_Base {
  10.  
  11.     /**
  12.      * Gets things started
  13.      *
  14.      * @access  public
  15.      * @since   1.0
  16.      * @return  void
  17.      */
  18.  
  19.     public function init() {
  20.  
  21.         $this->slug = 'learndash';
  22.  
  23.         add_action( 'learndash_course_completed', array( $this, 'course_completed' ), 5 );
  24.         add_action( 'learndash_lesson_completed', array( $this, 'lesson_completed' ), 5 );
  25.         add_action( 'learndash_quiz_completed', array( $this, 'quiz_completed' ), 5, 2 );
  26.         add_action( 'learndash_topic_completed', array( $this, 'topic_completed' ), 5 );
  27.         add_action( 'learndash_new_essay_submitted', array( $this, 'essay_submitted' ), 5, 2 );
  28.         add_action( 'ldadvquiz_answered', array( $this, 'quiz_answered' ), 10, 3 );
  29.         add_action( 'learndash_assignment_uploaded', array( $this, 'assignment_uploaded' ), 10, 2 );
  30.         add_action( 'learndash_update_user_activity', array( $this, 'update_user_activity' ) );
  31.         add_filter( 'learndash_access_redirect', array( $this, 'lesson_access_redirect' ), 10, 2 );
  32.  
  33.         // Content filtering
  34.         add_filter( 'learndash_content', array( $this, 'content_filter' ), 10, 2 );
  35.  
  36.         // Settings
  37.         add_action( 'add_meta_boxes', array( $this, 'configure_meta_box' ) );
  38.         add_action( 'wpf_meta_box_content', array( $this, 'meta_box_notice' ), 5, 2 );
  39.         add_action( 'wpf_meta_box_content', array( $this, 'meta_box_content' ), 40, 2 );
  40.  
  41.         add_filter( 'learndash_settings_fields', array( $this, 'course_settings_fields' ), 10, 2 );
  42.  
  43.         // Assignment settings
  44.         add_filter( 'learndash_settings_fields', array( $this, 'lesson_settings_fields' ), 10, 2 );
  45.  
  46.         // WPF stuff
  47.         add_filter( 'wpf_meta_field_groups', array( $this, 'add_meta_field_group' ), 15 );
  48.         add_filter( 'wpf_meta_fields', array( $this, 'prepare_meta_fields' ) );
  49.         add_filter( 'wpf_watched_meta_fields', array( $this, 'watch_meta_fields' ) );
  50.         add_filter( 'wpf_apply_tags_on_view', array( $this, 'maybe_stop_apply_tags_on_view' ), 10, 2 );
  51.         add_filter( 'wpf_post_access_meta', array( $this, 'inherit_permissions_from_course' ), 10, 2 );
  52.  
  53.         // Meta boxes
  54.         add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 20, 2 );
  55.         add_action( 'save_post', array( $this, 'save_meta_box_data' ), 20 );
  56.         add_action( 'wpf_meta_box_save', array( $this, 'meta_box_save' ), 20, 2 );
  57.  
  58.         // Admin course table
  59.         add_filter( 'display_post_states', array( $this, 'admin_table_post_states' ), 10, 2 );
  60.  
  61.         // Auto enrollments
  62.         add_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 9, 2 ); // This is 9 so that the user is in the correct groups by the time we go to update their courses
  63.         add_action( 'wpf_tags_modified', array( $this, 'update_course_access' ), 10, 2 );
  64.  
  65.         // Group linking
  66.         add_filter( 'learndash_settings_fields', array( $this, 'group_settings_fields' ), 10, 2 );
  67.         add_action( 'ld_added_group_access', array( $this, 'added_group_access' ), 10, 2 );
  68.         add_action( 'ld_removed_group_access', array( $this, 'removed_group_access' ), 10, 2 );
  69.  
  70.         // Group Leader Tracking
  71.         add_action( 'ld_added_leader_group_access', array( $this, 'added_group_leader_access' ), 10, 2 );
  72.         add_action( 'ld_removed_leader_group_access', array( $this, 'removed_group_leader_access' ), 10, 2 );
  73.  
  74.         // Course linking
  75.         add_action( 'learndash_update_course_access', array( $this, 'updated_course_access' ), 10, 4 );
  76.  
  77.         // Send auto-generated passwords on user registration
  78.         add_filter( 'random_password', array( $this, 'push_password' ) );
  79.  
  80.         // Export functions
  81.         add_filter( 'wpf_export_options', array( $this, 'export_options' ) );
  82.         add_filter( 'wpf_batch_learndash_courses_init', array( $this, 'batch_init' ) );
  83.         add_action( 'wpf_batch_learndash_courses', array( $this, 'batch_step' ) );
  84.  
  85.     }
  86.  
  87.     /**
  88.      * Applies tags when a LearnDash course is completed
  89.      *
  90.      * @access public
  91.      * @return void
  92.      */
  93.  
  94.     public function course_completed( $data ) {
  95.  
  96.         $settings = get_post_meta( $data['course']->ID, 'wpf-settings', true );
  97.  
  98.         if ( ! empty( $settings['apply_tags_ld'] ) ) {
  99.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld'], $data['user']->ID );
  100.         }
  101.  
  102.         update_user_meta( $data['user']->ID, 'ld_last_course_completed', get_the_title( $data['course']->ID ) );
  103.         update_user_meta( $data['user']->ID, 'ld_last_course_completed_date', current_time( 'timestamp' ) );
  104.  
  105.     }
  106.  
  107.     /**
  108.      * Applies tags when a LearnDash lesson is completed
  109.      *
  110.      * @access public
  111.      * @return void
  112.      */
  113.  
  114.     public function lesson_completed( $data ) {
  115.  
  116.         $settings = get_post_meta( $data['lesson']->ID, 'wpf-settings', true );
  117.  
  118.         if ( ! empty( $settings['apply_tags_ld'] ) ) {
  119.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld'], $data['user']->ID );
  120.         }
  121.  
  122.         update_user_meta( $data['user']->ID, 'ld_last_lesson_completed', get_the_title( $data['lesson']->ID ) );
  123.         update_user_meta( $data['user']->ID, 'ld_last_lesson_completed_date', current_time( 'timestamp' ) );
  124.  
  125.     }
  126.  
  127.     /**
  128.      * Applies tags when a LearnDash quiz is completed
  129.      *
  130.      * @access public
  131.      * @return void
  132.      */
  133.  
  134.     public function quiz_completed( $data, $user ) {
  135.  
  136.         if ( isset( $data['quiz']->ID ) ) {
  137.             $quiz_id = $data['quiz']->ID;
  138.         } else {
  139.             // For grading in the admin
  140.             $quiz_id = $data['quiz'];
  141.         }
  142.  
  143.         $settings = get_post_meta( $quiz_id, 'wpf-settings', true );
  144.  
  145.         if ( $data['pass'] == true && ! empty( $settings['apply_tags_ld'] ) ) {
  146.  
  147.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld'], $user->ID );
  148.  
  149.         } elseif ( $data['pass'] == false && ! empty( $settings['apply_tags_ld_quiz_fail'] ) ) {
  150.  
  151.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld_quiz_fail'], $user->ID );
  152.  
  153.         }
  154.  
  155.     }
  156.  
  157.  
  158.     /**
  159.      * Applies tags when a LearnDash topic is completed
  160.      *
  161.      * @access public
  162.      * @return void
  163.      */
  164.  
  165.     public function topic_completed( $data ) {
  166.  
  167.         $settings = get_post_meta( $data['topic']->ID, 'wpf-settings', true );
  168.  
  169.         if ( ! empty( $settings['apply_tags_ld'] ) ) {
  170.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld'], $data['user']->ID );
  171.         }
  172.  
  173.     }
  174.  
  175.     /**
  176.      * Applies tags when a LearnDash essay is submitted
  177.      *
  178.      * @access public
  179.      * @return void
  180.      */
  181.  
  182.     public function essay_submitted( $essay_id, $essay_args ) {
  183.  
  184.         $quid_pro_id = get_post_meta( $essay_id, 'quiz_id', true );
  185.  
  186.         $args = array(
  187.             'post_type'  => 'sfwd-quiz',
  188.             'fields'     => 'ids',
  189.             'meta_key'   => 'quiz_pro_id',
  190.             'meta_value' => $quid_pro_id,
  191.         );
  192.  
  193.         $quizzes = get_posts( $args );
  194.  
  195.         if ( empty( $quizzes ) ) {
  196.             return;
  197.         }
  198.  
  199.         $settings = get_post_meta( $quizzes[0], 'wpf-settings', true );
  200.  
  201.         if ( ! empty( $settings['apply_tags_ld_essay_submitted'] ) ) {
  202.  
  203.             wp_fusion()->user->apply_tags( $settings['apply_tags_ld_essay_submitted'], $essay_args['post_author'] );
  204.  
  205.         }
  206.  
  207.     }
  208.  
  209.     /**
  210.      * Sync quiz question answers to custom fields when quiz answered
  211.      *
  212.      * @access public
  213.      * @return void
  214.      */
  215.  
  216.     public function quiz_answered( $results, $quiz, $question_models ) {
  217.  
  218.         $contact_id = wp_fusion()->user->get_contact_id();
  219.  
  220.         if ( false === $contact_id ) {
  221.             return;
  222.         }
  223.  
  224.         $questions_and_answers = array();
  225.  
  226.         foreach ( $results as $key => $result ) {
  227.  
  228.             if ( ! empty( $result['e']['r'] ) ) {
  229.  
  230.                 $questions_and_answers[ $key ] = $result['e']['r'];
  231.  
  232.             } else {
  233.  
  234.                 // Essay questions
  235.                 $questions_and_answers[ $key ] = $_POST['data']['responses'][ $key ]['response'];
  236.  
  237.             }
  238.         }
  239.  
  240.         // Map the question IDs into post IDs
  241.         foreach ( $question_models as $post_id => $model ) {
  242.  
  243.             $answerData = $model->getAnswerData();
  244.  
  245.             foreach ( $questions_and_answers as $key => $result ) {
  246.  
  247.                 if ( $key == $model->getId() ) {
  248.  
  249.                     // Convert multiple choice from true / false into the selected option
  250.                     if ( is_array( $result ) ) {
  251.  
  252.                         foreach ( $result as $n => $multiple_choice_answer ) {
  253.  
  254.                             if ( true == $multiple_choice_answer ) {
  255.  
  256.                                 $answers = $model->getAnswerData();
  257.  
  258.                                 foreach ( $answers as $x => $answer ) {
  259.  
  260.                                     if ( $x == $n ) {
  261.  
  262.                                         $result = $answer->getAnswer();
  263.                                         break 2;
  264.  
  265.                                     }
  266.                                 }
  267.                             }
  268.                         }
  269.                     }
  270.  
  271.                     $questions_and_answers[ $post_id ] = $result;
  272.                     unset( $questions_and_answers[ $key ] );
  273.                 }
  274.             }
  275.         }
  276.  
  277.         $update_data = array();
  278.  
  279.         foreach ( $questions_and_answers as $post_id => $answer ) {
  280.  
  281.             $settings = get_post_meta( $post_id, 'wpf-settings-learndash', true );
  282.  
  283.             if ( ! empty( $settings ) && ! empty( $settings['crm_field'] ) ) {
  284.  
  285.                 $update_data[ $settings['crm_field'] ] = $answer;
  286.  
  287.             }
  288.         }
  289.  
  290.         if ( ! empty( $update_data ) ) {
  291.  
  292.             wpf_log( 'info', wpf_get_current_user_id(), 'Syncing <a href="' . get_edit_post_link( $quiz->getPostId() ) . '">' . $quiz->getName() . '</a> quiz answers to ' . wp_fusion()->crm->name . ':', array( 'meta_array_nofilter' => $update_data ) );
  293.  
  294.             wp_fusion()->crm->update_contact( $contact_id, $update_data, false );
  295.  
  296.         }
  297.  
  298.     }
  299.  
  300.     /**
  301.      * Apply tags when an assignment has been uploaded
  302.      *
  303.      * @access public
  304.      * @return void
  305.      */
  306.  
  307.     public function assignment_uploaded( $assignment_post_id, $assignment_meta ) {
  308.  
  309.         $settings = get_post_meta( $assignment_meta['lesson_id'], 'wpf-settings-learndash', true );
  310.  
  311.         if ( ! empty( $settings ) && ! empty( $settings['apply_tags_assignment_upload'] ) ) {
  312.  
  313.             wp_fusion()->user->apply_tags( $settings['apply_tags_assignment_upload'], $assignment_meta['user_id'] );
  314.  
  315.         }
  316.  
  317.     }
  318.  
  319.     /**
  320.      * Sync the last course progressed
  321.      *
  322.      * @access public
  323.      * @return void
  324.      */
  325.  
  326.     public function update_user_activity( $args ) {
  327.  
  328.         if ( ! empty( $args['course_id'] ) ) {
  329.  
  330.             remove_action( 'learndash_update_user_activity', array( $this, 'update_user_activity' ), 10, 1 );
  331.  
  332.             update_user_meta( $args['user_id'], 'ld_last_course_progressed', get_the_title( $args['course_id'] ) );
  333.  
  334.         }
  335.  
  336.     }
  337.  
  338.     /**
  339.      * Hide LD content if user doesn't have access
  340.      *
  341.      * @access public
  342.      * @return mixed Content
  343.      */
  344.  
  345.     public function content_filter( $content, $post ) {
  346.  
  347.         if ( wp_fusion()->access->user_can_access( $post->ID ) != true ) {
  348.             $content = wp_fusion()->access->get_restricted_content_message();
  349.         }
  350.  
  351.         return $content;
  352.  
  353.     }
  354.  
  355.     /**
  356.      * Remove standard "Apply to children" field from meta box
  357.      *
  358.      * @access public
  359.      * @return void
  360.      */
  361.  
  362.     public function configure_meta_box() {
  363.  
  364.         global $post;
  365.  
  366.         if ( empty( $post ) ) {
  367.             return;
  368.         }
  369.  
  370.         if ( $post->post_type == 'sfwd-courses' || $post->post_type == 'sfwd-lessons' || $post->post_type == 'sfwd-topic' ) {
  371.             remove_action( 'wpf_meta_box_content', 'apply_to_children', 35 );
  372.         }
  373.  
  374.     }
  375.  
  376.     /**
  377.      * Adds notice about inherited rules
  378.      *
  379.      * @access public
  380.      * @return void
  381.      */
  382.  
  383.     public function meta_box_notice( $post, $settings ) {
  384.  
  385.         if ( 'sfwd-lessons' != $post->post_type && 'sfwd-topic' != $post->post_type && 'sfwd-quiz' != $post->post_type ) {
  386.             return;
  387.         }
  388.  
  389.         $course_id = learndash_get_course_id( $post->ID );
  390.         $settings  = get_post_meta( $course_id, 'wpf-settings', true );
  391.  
  392.         if ( ! empty( $settings ) && ! empty( $settings['lock_content'] ) ) {
  393.  
  394.             $post_type_object = get_post_type_object( $post->post_type );
  395.  
  396.             echo '<div class="wpf-metabox-notice">';
  397.  
  398.             printf( __( 'If no access rules are specified here, this %1$s will inherit permissions from the course %2$s.', 'wp-fusion' ), strtolower( $post_type_object->labels->singular_name ), '<strong>' . get_the_title( $course_id ) . '</strong>' );
  399.  
  400.             $required_tags = array();
  401.  
  402.             if ( ! empty( $settings['allow_tags'] ) ) {
  403.                 $required_tags = array_merge( $required_tags, $settings['allow_tags'] );
  404.             }
  405.  
  406.             if ( ! empty( $settings['allow_tags_all'] ) ) {
  407.                 $required_tags = array_merge( $required_tags, $settings['allow_tags_all'] );
  408.             }
  409.  
  410.             if ( ! empty( $required_tags ) ) {
  411.  
  412.                 $required_tags = array_map( array( wp_fusion()->user, 'get_tag_label' ), $required_tags );
  413.  
  414.                 echo '<span class="notice-required-tags">' . sprintf( __( '(Required tag(s): %s)', 'wp-fusion' ), implode( ', ', $required_tags ) ) . '</span>';
  415.             }
  416.  
  417.             echo '</div>';
  418.  
  419.         }
  420.  
  421.     }
  422.  
  423.  
  424.     /**
  425.      * Adds LearnDash fields to WPF meta box
  426.      *
  427.      * @access public
  428.      * @return void
  429.      */
  430.  
  431.     public function meta_box_content( $post, $settings ) {
  432.  
  433.         if ( $post->post_type != 'sfwd-courses' && $post->post_type != 'sfwd-lessons' && $post->post_type != 'sfwd-topic' && $post->post_type != 'sfwd-quiz' ) {
  434.             return;
  435.         }
  436.  
  437.         $defaults = array(
  438.             'apply_tags_ld'                 => array(),
  439.             'apply_tags_ld_essay_submitted' => array(),
  440.             'apply_tags_ld_quiz_fail'       => array(),
  441.         );
  442.  
  443.         $settings = array_merge( $defaults, $settings );
  444.  
  445.         $settings['apply_children_lessons'] = false;
  446.  
  447.         echo '<p><label for="wpf-apply-tags-ld"><small>' . __( 'Apply these tags when marked complete' ) . ':</small></label>';
  448.  
  449.         wpf_render_tag_multiselect(
  450.             array(
  451.                 'setting'   => $settings['apply_tags_ld'],
  452.                 'meta_name' => 'wpf-settings',
  453.                 'field_id'  => 'apply_tags_ld',
  454.             )
  455.         );
  456.  
  457.         echo '</p>';
  458.  
  459.         if ( $post->post_type == 'sfwd-lessons' ) {
  460.  
  461.             $children = get_posts(
  462.                 array(
  463.                     'posts_per_page' => - 1,
  464.                     'post_type'      => array( 'sfwd-topic' ),
  465.                     'meta_key'       => 'lesson_id',
  466.                     'meta_value'     => $post->ID,
  467.                 )
  468.             );
  469.  
  470.             if ( count( $children ) > 0 ) {
  471.                 echo '<p><input class="checkbox" type="checkbox" id="wpf-apply-children-lessons" name="wpf-settings[apply_children_lessons]" value="1" ' . checked( $settings['apply_children_lessons'], 1, false ) . ' />';
  472.                 echo '<small>' . sprintf( __( 'Copy to %d related topics', 'wp-fusion' ), count( $children ) ) . '</small>';
  473.                 echo '</p>';
  474.             }
  475.         }
  476.  
  477.         if ( $post->post_type == 'sfwd-quiz' ) {
  478.  
  479.             echo '<p><label for="wpf-apply-tags-ld-quiz-fail"><small>' . __( 'Apply these tags when essay submitted', 'wp-fusion' ) . ':</small></label>';
  480.  
  481.             wpf_render_tag_multiselect(
  482.                 array(
  483.                     'setting'   => $settings['apply_tags_ld_essay_submitted'],
  484.                     'meta_name' => 'wpf-settings',
  485.                     'field_id'  => 'apply_tags_ld_essay_submitted',
  486.                 )
  487.             );
  488.  
  489.             echo '</p>';
  490.  
  491.             echo '<p><label for="wpf-apply-tags-ld-quiz-fail"><small>' . __( 'Apply these tags when quiz failed', 'wp-fusion' ) . ':</small></label>';
  492.  
  493.             wpf_render_tag_multiselect(
  494.                 array(
  495.                     'setting'   => $settings['apply_tags_ld_quiz_fail'],
  496.                     'meta_name' => 'wpf-settings',
  497.                     'field_id'  => 'apply_tags_ld_quiz_fail',
  498.                 )
  499.             );
  500.  
  501.             echo '</p>';
  502.  
  503.         }
  504.  
  505.     }
  506.  
  507.     /**
  508.      * Runs when WPF meta box is saved
  509.      *
  510.      * @access public
  511.      * @return void
  512.      */
  513.  
  514.     public function meta_box_save( $post_id, $data ) {
  515.  
  516.         if ( isset( $data['apply_children_lessons'] ) ) {
  517.  
  518.             $children = get_posts(
  519.                 array(
  520.                     'posts_per_page' => - 1,
  521.                     'post_type'      => array( 'sfwd-topic' ),
  522.                     'meta_key'       => 'lesson_id',
  523.                     'meta_value'     => $post_id,
  524.                 )
  525.             );
  526.  
  527.         }
  528.  
  529.         if ( isset( $children ) ) {
  530.  
  531.             unset( $data['apply_tags'] );
  532.             unset( $data['apply_tags_ld'] );
  533.             unset( $data['apply_children_courses'] );
  534.             unset( $data['apply_children_lessons'] );
  535.  
  536.             foreach ( $children as $child ) {
  537.                 update_post_meta( $child->ID, 'wpf-settings', $data );
  538.             }
  539.         }
  540.  
  541.     }
  542.  
  543.     /**
  544.      * Adds meta boxes
  545.      *
  546.      * @access public
  547.      * @return mixed
  548.      */
  549.  
  550.     public function add_meta_box( $post_id, $data ) {
  551.  
  552.         $admin_permissions = wp_fusion()->settings->get( 'admin_permissions' );
  553.  
  554.         if ( true == $admin_permissions && ! current_user_can( 'manage_options' ) ) {
  555.             return;
  556.         }
  557.  
  558.         add_meta_box( 'wpf-learndash-meta', __( 'WP Fusion - Course Settings', 'wp-fusion' ), array( $this, 'meta_box_callback' ), 'sfwd-courses' );
  559.         add_meta_box( 'wpf-learndash-meta', __( 'WP Fusion - Group Settings', 'wp-fusion' ), array( $this, 'meta_box_callback_groups' ), 'groups' );
  560.         add_meta_box( 'wpf-learndash-meta', __( 'WP Fusion - Question Settings', 'wp-fusion' ), array( $this, 'meta_box_callback_question' ), 'sfwd-question' );
  561.  
  562.     }
  563.  
  564.     /**
  565.      * Course meta box callback
  566.      *
  567.      * @access public
  568.      * @return mixed
  569.      */
  570.  
  571.     public function meta_box_callback( $post ) {
  572.  
  573.         echo '<p><span class="description">' . __( 'These options have been moved to the course\'s Settings panel.', 'wp-fusion' ) . '</span></p>';
  574.  
  575.     }
  576.  
  577.     /**
  578.      * Displays meta box content (groups)
  579.      *
  580.      * @access public
  581.      * @return mixed
  582.      */
  583.  
  584.     public function meta_box_callback_groups( $post ) {
  585.  
  586.         echo '<p><span class="description">' . __( 'These options have been moved to the Group\'s Settings panel.', 'wp-fusion' ) . '</span></p>';
  587.  
  588.     }
  589.  
  590.     /**
  591.      * Displays meta box content (question)
  592.      *
  593.      * @access public
  594.      * @return mixed HTML Output
  595.      */
  596.  
  597.     public function meta_box_callback_question( $post ) {
  598.  
  599.         wp_nonce_field( 'wpf_meta_box_learndash', 'wpf_meta_box_learndash_nonce' );
  600.  
  601.         $settings = array(
  602.             'crm_field' => array(),
  603.         );
  604.  
  605.         if ( get_post_meta( $post->ID, 'wpf-settings-learndash', true ) ) {
  606.             $settings = array_merge( $settings, get_post_meta( $post->ID, 'wpf-settings-learndash', true ) );
  607.         }
  608.  
  609.         echo '<table class="form-table"><tbody>';
  610.  
  611.         echo '<tr>';
  612.  
  613.         echo '<th scope="row"><label for="tag_link">' . __( 'Sync to field', 'wp-fusion' ) . ':</label></th>';
  614.         echo '<td>';
  615.  
  616.         wpf_render_crm_field_select( $settings['crm_field'], 'wpf-settings-learndash' );
  617.  
  618.         echo '<span class="description">' . sprintf( __( 'Sync answers to this question the selected custom field in %s.', 'wp-fusion' ), wp_fusion()->crm->name ) . '</span>';
  619.         echo '</td>';
  620.  
  621.         echo '</tr>';
  622.  
  623.         echo '</tbody></table>';
  624.  
  625.     }
  626.  
  627.  
  628.     /**
  629.      * Registers LD course fields
  630.      *
  631.      * @access public
  632.      * @return array Fields
  633.      */
  634.  
  635.     public function course_settings_fields( $fields, $metabox_key ) {
  636.  
  637.         if ( 'learndash-course-access-settings' == $metabox_key ) {
  638.  
  639.             $admin_permissions = wp_fusion()->settings->get( 'admin_permissions' );
  640.  
  641.             if ( true == $admin_permissions && ! current_user_can( 'manage_options' ) ) {
  642.                 return $fields;
  643.             }
  644.  
  645.             $new_options = array(
  646.                 'apply_tags_enrolled' => array(
  647.                     'name'             => 'apply_tags_enrolled',
  648.                     'label'            => __( 'Apply Tags - Enrolled', 'wp-fusion' ),
  649.                     'type'             => 'multiselect',
  650.                     'multiple'         => 'true',
  651.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  652.                     'desc'             => sprintf( __( 'These tags will be applied in %s when someone is enrolled in this course.', 'wp-fusion' ), wp_fusion()->crm->name ),
  653.                     'help_text'        => sprintf( __( 'For more information on these settings, %1$ssee our documentation%2$s.', 'wp-fusion' ), '<a href="https://wpfusion.com/documentation/learning-management/learndash/#course-specific-settings" target="_blank">', '</a>' ),
  654.                 ),
  655.                 'tag_link'            => array(
  656.                     'name'             => 'tag_link',
  657.                     'label'            => __( 'Link with Tag', 'wp-fusion' ),
  658.                     'type'             => 'multiselect',
  659.                     'multiple'         => 'true',
  660.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  661.                     'desc'             => sprintf( __( 'This tag will be applied in %1$s when a user is enrolled, and will be removed when a user is unenrolled. Likewise, if this tag is applied to a user from within %2$s, they will be automatically enrolled in this course. If this tag is removed, the user will be removed from the course.', 'wp-fusion' ), wp_fusion()->crm->name, wp_fusion()->crm->name ),
  662.                     'limit'            => 1,
  663.                     'help_text'        => sprintf( __( 'For more information on these settings, %1$ssee our documentation%2$s.', 'wp-fusion' ), '<a href="https://wpfusion.com/documentation/learning-management/learndash/#course-specific-settings" target="_blank">', '</a>' ),
  664.                 ),
  665.             );
  666.  
  667.             // Warning if course is open and a linked tag is set
  668.  
  669.             global $post;
  670.  
  671.             if ( is_object( $post ) ) {
  672.  
  673.                 $wpf_settings = get_post_meta( $post->ID, 'wpf-settings-learndash', true );
  674.  
  675.                 if ( ! empty( $wpf_settings ) && ! empty( $wpf_settings['tag_link'] ) ) {
  676.  
  677.                     $course_settings = get_post_meta( $post->ID, '_sfwd-courses', true );
  678.  
  679.                     if ( ! empty( $course_settings ) && isset( $course_settings['sfwd-courses_course_price_type'] ) ) {
  680.  
  681.                         if ( 'free' == $course_settings['sfwd-courses_course_price_type'] || 'open' == $course_settings['sfwd-courses_course_price_type'] ) {
  682.  
  683.                             $new_options['tag_link']['desc'] .= '<br /><br/><div class="ld-settings-info-banner ld-settings-info-banner-alert"><p>' . sprintf( __( '<strong>Note:</strong> Your course Access Mode is currently set to <strong>%s</strong>, for auto-enrollments to work correctly your course Access Mode should be set to "closed". If you\'ve just changed the setting you can ignore this message.', 'wp-fusion' ), $course_settings['sfwd-courses_course_price_type'] ) . '</p></div>';
  684.  
  685.                         }
  686.                     }
  687.                 }
  688.             }
  689.  
  690.             $fields = wp_fusion()->settings->insert_setting_after( 'course_access_list', $fields, $new_options );
  691.  
  692.         }
  693.  
  694.         return $fields;
  695.  
  696.     }
  697.  
  698.  
  699.  
  700.     /**
  701.      * Registers LD group fields
  702.      *
  703.      * @access public
  704.      * @return array Fields
  705.      */
  706.  
  707.     public function group_settings_fields( $fields, $metabox_key ) {
  708.  
  709.         if ( 'learndash-group-access-settings' == $metabox_key ) {
  710.  
  711.             $admin_permissions = wp_fusion()->settings->get( 'admin_permissions' );
  712.  
  713.             if ( true == $admin_permissions && ! current_user_can( 'manage_options' ) ) {
  714.                 return $fields;
  715.             }
  716.  
  717.             $new_options = array(
  718.                 'apply_tags_enrolled' => array(
  719.                     'name'             => 'apply_tags_enrolled',
  720.                     'label'            => __( 'Apply Tags - Enrolled', 'wp-fusion' ),
  721.                     'type'             => 'multiselect',
  722.                     'multiple'         => 'true',
  723.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  724.                     'desc'             => sprintf( __( 'These tags will be applied in %s when someone is enrolled in this group.', 'wp-fusion' ), wp_fusion()->crm->name ),
  725.                     'help_text'        => sprintf( __( 'For more information on these settings, %1$ssee our documentation%2$s.', 'wp-fusion' ), '<a href="https://wpfusion.com/documentation/learning-management/learndash/#groups" target="_blank">', '</a>' ),
  726.                 ),
  727.                 'tag_link'            => array(
  728.                     'name'             => 'tag_link',
  729.                     'label'            => __( 'Link with Tag', 'wp-fusion' ),
  730.                     'type'             => 'multiselect',
  731.                     'multiple'         => 'true',
  732.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  733.                     'desc'             => sprintf( __( 'This tag will be applied in %1$s when a user is enrolled, and will be removed when a user is unenrolled. Likewise, if this tag is applied to a user from within %2$s, they will be automatically enrolled in this group. If this tag is removed, the user will be removed from the group.', 'wp-fusion' ), wp_fusion()->crm->name, wp_fusion()->crm->name ),
  734.                     'limit'            => 1,
  735.                     'help_text'        => sprintf( __( 'For more information on these settings, %1$ssee our documentation%2$s.', 'wp-fusion' ), '<a href="https://wpfusion.com/documentation/learning-management/learndash/#groups" target="_blank">', '</a>' ),
  736.                 ),
  737.                 'leader_tag'          => array(
  738.                     'name'             => 'leader_tag',
  739.                     'label'            => __( 'Link Group Leader with Tag', 'wp-fusion' ),
  740.                     'type'             => 'multiselect',
  741.                     'multiple'         => 'true',
  742.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  743.                     'desc'             => sprintf( __( 'This tag will be applied in %1$s when a group leader is assigned, and will be removed when a group leader is removed. Likewise, if this tag is applied to a user from within %2$s, they will be automatically assigned leader in this group. If this tag is removed, the user will be removed from leadership of the group.', 'wp-fusion' ), wp_fusion()->crm->name, wp_fusion()->crm->name ),
  744.                     'limit'            => 1,
  745.                     'help_text'        => sprintf( __( 'For more information on these settings, %1$ssee our documentation%2$s.', 'wp-fusion' ), '<a href="https://wpfusion.com/documentation/learning-management/learndash/#groups" target="_blank">', '</a>' ),
  746.                 ),
  747.             );
  748.  
  749.             $fields = $fields + $new_options;
  750.  
  751.         }
  752.  
  753.         return $fields;
  754.  
  755.     }
  756.  
  757.  
  758.     /**
  759.      * Adds WPF settings to assignment upload section in lesson settings
  760.      *
  761.      * @access public
  762.      * @return array Options Fields
  763.      */
  764.  
  765.     public function lesson_settings_fields( $options_fields, $metabox_key ) {
  766.  
  767.         if ( 'learndash-lesson-display-content-settings' == $metabox_key || 'learndash-topic-display-content-settings' == $metabox_key ) {
  768.  
  769.             $new_options = array(
  770.                 'apply_tags_assignment_upload' => array(
  771.                     'name'             => 'apply_tags_assignment_upload',
  772.                     'label'            => esc_html__( 'Apply Tags', 'learndash' ),
  773.                     'type'             => 'multiselect',
  774.                     'multiple'         => 'true',
  775.                     'display_callback' => array( $this, 'display_wpf_tags_select' ),
  776.                     'parent_setting'   => 'lesson_assignment_upload',
  777.                     'desc'             => sprintf( __( 'Select tags to be applied to the student in %s when an assigment is uploaded.', 'wp-fusion' ), wp_fusion()->crm->name ),
  778.                 ),
  779.             );
  780.  
  781.             $options_fields = wp_fusion()->settings->insert_setting_after( 'assignment_upload_limit_size', $options_fields, $new_options );
  782.  
  783.         }
  784.  
  785.         return $options_fields;
  786.  
  787.     }
  788.  
  789.  
  790.  
  791.     /**
  792.      * Show post access controls in the posts table
  793.      *
  794.      * @access public
  795.      * @return array Post States
  796.      */
  797.  
  798.     public function admin_table_post_states( $post_states, $post ) {
  799.  
  800.         if ( 'sfwd-courses' != $post->post_type && 'groups' != $post->post_type ) {
  801.             return $post_states;
  802.         }
  803.  
  804.         $wpf_settings = get_post_meta( $post->ID, 'wpf-settings-learndash', true );
  805.  
  806.         if ( ! empty( $wpf_settings ) && ! empty( $wpf_settings['tag_link'] ) ) {
  807.  
  808.             $post_type_object = get_post_type_object( $post->post_type );
  809.  
  810.             $content = sprintf( __( 'This %1$s is linked for auto-enrollment with %2$s tag: ', 'wp-fusion' ), strtolower( $post_type_object->labels->singular_name ), wp_fusion()->crm->name );
  811.  
  812.             $content .= '<strong>' . wpf_get_tag_label( $wpf_settings['tag_link'][0] ) . '</strong>';
  813.  
  814.             $post_states['wpf_learndash'] = '<span class="dashicons dashicons-admin-links wpf-tip wpf-tip-bottom" data-tip="' . $content . '"></span>';
  815.  
  816.         }
  817.  
  818.         return $post_states;
  819.  
  820.     }
  821.  
  822.     /**
  823.      * Display tags select input for assignment upload setting
  824.      *
  825.      * @access public
  826.      * @return mixed HTML output
  827.      */
  828.  
  829.     public function display_wpf_tags_select( $field_args ) {
  830.  
  831.         global $post;
  832.  
  833.         $settings = get_post_meta( $post->ID, 'wpf-settings-learndash', true );
  834.  
  835.         if ( empty( $settings ) ) {
  836.             $settings = array();
  837.         }
  838.  
  839.         if ( ! isset( $settings[ $field_args['name'] ] ) ) {
  840.             $settings[ $field_args['name'] ] = array();
  841.         }
  842.  
  843.         $args = array(
  844.             'setting'   => $settings[ $field_args['name'] ],
  845.             'meta_name' => 'wpf-settings-learndash',
  846.             'field_id'  => $field_args['name'],
  847.         );
  848.  
  849.         if ( isset( $field_args['limit'] ) ) {
  850.             $args['limit'] = $field_args['limit'];
  851.         }
  852.  
  853.         wpf_render_tag_multiselect( $args );
  854.  
  855.         echo '<p style="margin-top:5px;" class="description">' . $field_args['desc'] . '</p>';
  856.  
  857.     }
  858.  
  859.     /**
  860.      * Runs when WPF meta box is saved on a course, lesson, or question
  861.      *
  862.      * @access public
  863.      * @return void
  864.      */
  865.  
  866.     public function save_meta_box_data( $post_id ) {
  867.  
  868.         if ( empty( $_POST['post_type'] ) || ! in_array( $_POST['post_type'], array( 'sfwd-courses', 'groups', 'sfwd-question', 'sfwd-topic' ) ) ) {
  869.             return;
  870.         }
  871.  
  872.         // As of LD 3.2.2 this runs on every lesson in the builder when the course is saved, so we'll check for that here to avoid having the lesson settings overwritten by the course
  873.         if ( $_POST['post_ID'] != $post_id ) {
  874.             return;
  875.         }
  876.  
  877.         // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  878.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  879.             return;
  880.         }
  881.  
  882.         if ( isset( $_POST['wpf-settings-learndash'] ) ) {
  883.             update_post_meta( $post_id, 'wpf-settings-learndash', $_POST['wpf-settings-learndash'] );
  884.         } else {
  885.             delete_post_meta( $post_id, 'wpf-settings-learndash' );
  886.         }
  887.  
  888.     }
  889.  
  890.  
  891.     /**
  892.      * Update user course enrollment when tags are modified
  893.      *
  894.      * @access public
  895.      * @return void
  896.      */
  897.  
  898.     public function update_course_access( $user_id, $user_tags ) {
  899.  
  900.         $linked_courses = get_posts(
  901.             array(
  902.                 'post_type'  => 'sfwd-courses',
  903.                 'nopaging'   => true,
  904.                 'meta_query' => array(
  905.                     array(
  906.                         'key'     => 'wpf-settings-learndash',
  907.                         'compare' => 'EXISTS',
  908.                     ),
  909.                 ),
  910.                 'fields'     => 'ids',
  911.             )
  912.         );
  913.  
  914.         // Update course access based on user tags
  915.         if ( ! empty( $linked_courses ) ) {
  916.  
  917.             $user_tags = wp_fusion()->user->get_tags( $user_id ); // Get them here for cases where the tags might have changed since wpf_tags_modified was triggered
  918.  
  919.             // See if user is enrolled
  920.             $enrolled_courses = learndash_user_get_enrolled_courses( $user_id, array() );
  921.  
  922.             // We won't look at courses a user is in because of a group
  923.             $groups_courses = learndash_get_user_groups_courses_ids( $user_id );
  924.  
  925.             // Don't bother with open courses since users are enrolled in them by default
  926.             $open_courses = learndash_get_open_courses();
  927.  
  928.             $enrolled_courses = array_diff( $enrolled_courses, $open_courses );
  929.  
  930.             foreach ( $linked_courses as $course_id ) {
  931.  
  932.                 $settings = get_post_meta( $course_id, 'wpf-settings-learndash', true );
  933.  
  934.                 if ( empty( $settings ) || empty( $settings['tag_link'] ) ) {
  935.                     continue;
  936.                 }
  937.  
  938.                 $tag_id = $settings['tag_link'][0];
  939.  
  940.                 if ( in_array( $course_id, $enrolled_courses ) ) {
  941.                     $is_enrolled = true;
  942.                 } else {
  943.                     $is_enrolled = false;
  944.                 }
  945.  
  946.                 // Prevent looping
  947.                 remove_action( 'learndash_update_course_access', array( $this, 'updated_course_access' ), 10, 4 );
  948.  
  949.                 if ( in_array( $tag_id, $user_tags ) && ! $is_enrolled && ! user_can( $user_id, 'manage_options' ) ) {
  950.  
  951.                     if ( in_array( $course_id, $groups_courses ) ) {
  952.  
  953.                         // We can't add someone to a course that they already have access to as part of a group
  954.  
  955.                         wpf_log( 'notice', $user_id, 'User could not be auto-enrolled in LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a> by linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong> because they already have access to that course as part of a LearnDash group.', array( 'source' => 'learndash' ) );
  956.                         continue;
  957.  
  958.                     }
  959.  
  960.                     // Logger
  961.                     wpf_log( 'info', $user_id, 'User auto-enrolled in LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a> by linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'learndash' ) );
  962.  
  963.                     ld_update_course_access( $user_id, $course_id, $remove = false );
  964.  
  965.                 } elseif ( ! in_array( $tag_id, $user_tags ) && $is_enrolled && ! user_can( $user_id, 'manage_options' ) ) {
  966.  
  967.                     if ( in_array( $course_id, $groups_courses ) ) {
  968.  
  969.                         // We can't add someone to a course that they already have access to as part of a group
  970.  
  971.                         wpf_log( 'notice', $user_id, 'User could not be un-enrolled from LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a> by linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong> because they have access to that course as part a LearnDash group.', array( 'source' => 'learndash' ) );
  972.                         continue;
  973.  
  974.                     }
  975.  
  976.                     // Logger
  977.                     wpf_log( 'info', $user_id, 'User un-enrolled from LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a> by linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'learndash' ) );
  978.  
  979.                     ld_update_course_access( $user_id, $course_id, $remove = true );
  980.  
  981.                 }
  982.  
  983.                 add_action( 'learndash_update_course_access', array( $this, 'updated_course_access' ), 10, 4 );
  984.  
  985.             }
  986.         }
  987.  
  988.     }
  989.  
  990.     /**
  991.      * Update user group enrollment when tags are modified
  992.      *
  993.      * @access public
  994.      * @return void
  995.      */
  996.  
  997.     public function update_group_access( $user_id, $user_tags ) {
  998.  
  999.         // Possibly update groups
  1000.         $linked_groups = get_posts(
  1001.             array(
  1002.                 'post_type'  => 'groups',
  1003.                 'nopaging'   => true,
  1004.                 'meta_query' => array(
  1005.                     array(
  1006.                         'key'     => 'wpf-settings-learndash',
  1007.                         'compare' => 'EXISTS',
  1008.                     ),
  1009.                 ),
  1010.                 'fields'     => 'ids',
  1011.             )
  1012.         );
  1013.  
  1014.         $updated = false;
  1015.  
  1016.         if ( ! empty( $linked_groups ) ) {
  1017.  
  1018.             $user_tags = wp_fusion()->user->get_tags( $user_id ); // Get them here for cases where the tags might have changed since wpf_tags_modified was triggered
  1019.  
  1020.             foreach ( $linked_groups as $group_id ) {
  1021.  
  1022.                 $settings = get_post_meta( $group_id, 'wpf-settings-learndash', true );
  1023.  
  1024.                 if ( empty( $settings ) ) {
  1025.                     continue;
  1026.                 }
  1027.  
  1028.                 // so EMPTY can be triggered where appropriate
  1029.                 $tag_id    = false;
  1030.                 $leader_id = false;
  1031.  
  1032.                 if ( ! empty( $settings['tag_link'] ) ) {
  1033.                     $tag_id = $settings['tag_link'][0];
  1034.                 }
  1035.  
  1036.                 if ( ! empty( $tag_id ) ) {
  1037.  
  1038.                     // Prevent looping
  1039.                     remove_action( 'ld_added_group_access', array( $this, 'added_group_access' ), 10, 2 );
  1040.                     remove_action( 'ld_removed_group_access', array( $this, 'removed_group_access' ), 10, 2 );
  1041.  
  1042.                     if ( in_array( $tag_id, $user_tags ) && learndash_is_user_in_group( $user_id, $group_id ) == false ) {
  1043.  
  1044.                         wpf_log( 'info', $user_id, 'User added to LearnDash group <a href="' . get_edit_post_link( $group_id ) . '" target="_blank">' . get_the_title( $group_id ) . '</a> by tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'learndash' ) );
  1045.  
  1046.                         ld_update_group_access( $user_id, $group_id, $remove = false );
  1047.  
  1048.                         $updated = true;
  1049.  
  1050.                     } elseif ( ! in_array( $tag_id, $user_tags ) && learndash_is_user_in_group( $user_id, $group_id ) != false ) {
  1051.  
  1052.                         wpf_log( 'info', $user_id, 'User removed from LearnDash group <a href="' . get_edit_post_link( $group_id ) . '" target="_blank">' . get_the_title( $group_id ) . '</a> by tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'learndash' ) );
  1053.  
  1054.                         ld_update_group_access( $user_id, $group_id, $remove = true );
  1055.  
  1056.                         $updated = true;
  1057.  
  1058.                     }
  1059.  
  1060.                     add_action( 'ld_added_group_access', array( $this, 'added_group_access' ), 10, 2 );
  1061.                     add_action( 'ld_removed_group_access', array( $this, 'removed_group_access' ), 10, 2 );
  1062.  
  1063.                 }
  1064.  
  1065.                 if ( ! empty( $settings['leader_tag'] ) ) {
  1066.                     $leader_id = $settings['leader_tag'][0];
  1067.                 }
  1068.  
  1069.                 if ( ! empty( $leader_id ) ) {
  1070.  
  1071.                     remove_action( 'ld_added_leader_group_access', array( $this, 'added_group_leader_access' ), 10, 2 );
  1072.                     remove_action( 'ld_removed_leader_group_access', array( $this, 'removed_group_leader_access' ), 10, 2 );
  1073.  
  1074.                     // get list of group leader IDs - so we can check later if the user is a leader in the group
  1075.                     // and we need to remove the user from the leader of that group accordingly.
  1076.                     $group_leader_ids = learndash_get_groups_administrator_ids( $group_id );
  1077.  
  1078.                     // if LEADER TAG is applied, and user is not a leader / administrator of this group
  1079.                     // add them as a group leader  NOTE: LOGGING IS DONE BY THE CALLBACK WITH TAG REMOVAL
  1080.                     if ( in_array( $leader_id, $user_tags ) && in_array( $user_id, $group_leader_ids ) == false ) {
  1081.  
  1082.                         wpf_log( 'info', $user_id, 'User added as leader to LearnDash group <a href="' . get_edit_post_link( $group_id ) . '" target="_blank">' . get_the_title( $group_id ) . '</a> by tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'group-action-learndash' ) );
  1083.  
  1084.                         ld_update_leader_group_access( $user_id, $group_id, $remove = false );
  1085.  
  1086.                         $updated = true;
  1087.  
  1088.                     } elseif ( ! in_array( $leader_id, $user_tags ) && in_array( $user_id, $group_leader_ids ) != false ) {
  1089.  
  1090.                         wpf_log( 'info', $user_id, 'User removed as leader from LearnDash group <a href="' . get_edit_post_link( $group_id ) . '" target="_blank">' . get_the_title( $group_id ) . '</a> by tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'group-action-learndash' ) );
  1091.  
  1092.                         ld_update_leader_group_access( $user_id, $group_id, $remove = true );
  1093.  
  1094.                         $updated = true;
  1095.  
  1096.                     }
  1097.  
  1098.                     add_action( 'ld_added_leader_group_access', array( $this, 'added_group_leader_access' ), 10, 2 );
  1099.                     add_action( 'ld_removed_leader_group_access', array( $this, 'removed_group_leader_access' ), 10, 2 );
  1100.  
  1101.                 }
  1102.             }
  1103.         }
  1104.  
  1105.         // Clear the courses / groups transients
  1106.  
  1107.         if ( $updated ) {
  1108.  
  1109.             delete_transient( 'learndash_user_courses_' . $user_id );
  1110.             delete_transient( 'learndash_user_groups_' . $user_id );
  1111.  
  1112.         }
  1113.  
  1114.     }
  1115.  
  1116.     /**
  1117.      * Don't apply tags on view when a LD-restricted lesson is viewed
  1118.      *
  1119.      * @access public
  1120.      * @return bool Proceed
  1121.      */
  1122.  
  1123.     public function maybe_stop_apply_tags_on_view( $proceed, $post_id ) {
  1124.  
  1125.         if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
  1126.  
  1127.             $access_from = ld_lesson_access_from( $post_id, wpf_get_current_user_id() );
  1128.  
  1129.             if ( $access_from > time() ) {
  1130.                 $proceed = false;
  1131.             }
  1132.         }
  1133.  
  1134.         return $proceed;
  1135.  
  1136.     }
  1137.  
  1138.     /**
  1139.      * LearnDash lessons and topics should inherit permissions from the parent course
  1140.      *
  1141.      * @access public
  1142.      * @return array Access Meta
  1143.      */
  1144.  
  1145.     public function inherit_permissions_from_course( $access_meta, $post_id ) {
  1146.  
  1147.         if ( empty( $access_meta ) || empty( $access_meta['lock_content'] ) ) {
  1148.  
  1149.             $post_type = get_post_type( $post_id );
  1150.  
  1151.             if ( 'sfwd-lessons' == $post_type || 'sfwd-topic' == $post_type || 'sfwd-quiz' == $post_type ) {
  1152.  
  1153.                 $course_id = learndash_get_course_id( $post_id );
  1154.  
  1155.                 if ( $course_id ) {
  1156.                     $access_meta = get_post_meta( $course_id, 'wpf-settings', true );
  1157.                 }
  1158.             }
  1159.         }
  1160.  
  1161.         return $access_meta;
  1162.  
  1163.     }
  1164.  
  1165.     /**
  1166.      * Run WPF's redirects on restricted LD lessons instead of letting LD take them to the course, so our login redirects work
  1167.      *
  1168.      * @access public
  1169.      * @return string Redirect Link
  1170.      */
  1171.  
  1172.     public function lesson_access_redirect( $link, $lesson_id ) {
  1173.  
  1174.         $course_id = learndash_get_course_id( $lesson_id );
  1175.  
  1176.         if ( ! wp_fusion()->access->user_can_access( $course_id ) ) {
  1177.  
  1178.             $redirect = wp_fusion()->access->get_redirect( $course_id );
  1179.  
  1180.             if ( ! empty( $redirect ) ) {
  1181.  
  1182.                 wp_fusion()->access->set_return_after_login( $lesson_id );
  1183.  
  1184.                 wp_redirect( $redirect, 302, 'WP Fusion; Post ID ' . $lesson_id );
  1185.                 exit();
  1186.  
  1187.             }
  1188.         }
  1189.  
  1190.         return $link;
  1191.  
  1192.     }
  1193.  
  1194.     /**
  1195.      * Applies group link tag when user added to group
  1196.      *
  1197.      * @access public
  1198.      * @return void
  1199.      */
  1200.  
  1201.     public function added_group_access( $user_id, $group_id ) {
  1202.  
  1203.         $settings = get_post_meta( $group_id, 'wpf-settings-learndash', true );
  1204.  
  1205.         if ( empty( $settings ) ) {
  1206.             return;
  1207.         }
  1208.  
  1209.         $apply_tags = array();
  1210.  
  1211.         if ( ! empty( $settings['tag_link'] ) ) {
  1212.             $apply_tags = array_merge( $apply_tags, $settings['tag_link'] );
  1213.         }
  1214.  
  1215.         if ( ! empty( $settings['apply_tags_enrolled'] ) ) {
  1216.             $apply_tags = array_merge( $apply_tags, $settings['apply_tags_enrolled'] );
  1217.         }
  1218.  
  1219.         if ( ! empty( $apply_tags ) ) {
  1220.  
  1221.             // Prevent looping
  1222.             remove_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1223.  
  1224.             wpf_log( 'info', $user_id, 'User was enrolled in LearnDash group <a href="' . admin_url( 'post.php?post=' . $group_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $group_id ) . '</a>. Applying tags.' );
  1225.  
  1226.             wp_fusion()->user->apply_tags( $apply_tags, $user_id );
  1227.  
  1228.             add_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1229.  
  1230.         }
  1231.  
  1232.     }
  1233.  
  1234.     /**
  1235.      * Applies group LEADER link tag when user added to group leader
  1236.      *
  1237.      * @access public
  1238.      * @return void
  1239.      */
  1240.  
  1241.     public function added_group_leader_access( $user_id, $group_id ) {
  1242.  
  1243.         $settings = get_post_meta( $group_id, 'wpf-settings-learndash', true );
  1244.  
  1245.         if ( empty( $settings ) ) {
  1246.             return;
  1247.         }
  1248.  
  1249.         $apply_tags = array();
  1250.  
  1251.         if ( ! empty( $settings['leader_tag'] ) ) {
  1252.             $apply_tags = array_merge( $apply_tags, $settings['leader_tag'] );
  1253.         }
  1254.  
  1255.         if ( ! empty( $apply_tags ) ) {
  1256.  
  1257.             // Prevent looping
  1258.             remove_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1259.  
  1260.             wpf_log( 'info', $user_id, 'User was enrolled as group-leader in LearnDash group <a href="' . admin_url( 'post.php?post=' . $group_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $group_id ) . '</a>. Applying tags.' );
  1261.  
  1262.             wp_fusion()->user->apply_tags( $apply_tags, $user_id );
  1263.  
  1264.             add_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1265.  
  1266.         }
  1267.  
  1268.     }
  1269.  
  1270.     /**
  1271.      * Removes group link tag when user removed from group
  1272.      *
  1273.      * @access public
  1274.      * @return void
  1275.      */
  1276.  
  1277.     public function removed_group_access( $user_id, $group_id ) {
  1278.  
  1279.         $settings = get_post_meta( $group_id, 'wpf-settings-learndash', true );
  1280.  
  1281.         if ( empty( $settings ) || empty( $settings['tag_link'] ) ) {
  1282.             return;
  1283.         }
  1284.  
  1285.         // Prevent looping
  1286.         remove_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1287.  
  1288.         wpf_log( 'info', $user_id, 'User was un-enrolled from LearnDash group <a href="' . admin_url( 'post.php?post=' . $group_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $group_id ) . '</a>. Removing linked tag.' );
  1289.  
  1290.         wp_fusion()->user->remove_tags( $settings['tag_link'], $user_id );
  1291.  
  1292.         add_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1293.  
  1294.     }
  1295.  
  1296.     /**
  1297.      * Removes group LEADER link tag when user removed from group
  1298.      *
  1299.      * @access public
  1300.      * @return void
  1301.      */
  1302.  
  1303.     public function removed_group_leader_access( $user_id, $group_id ) {
  1304.  
  1305.         $settings = get_post_meta( $group_id, 'wpf-settings-learndash', true );
  1306.  
  1307.         if ( empty( $settings ) || empty( $settings['leader_tag'] ) ) {
  1308.             return;
  1309.         }
  1310.  
  1311.         // Prevent looping
  1312.         remove_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1313.  
  1314.         wpf_log( 'info', $user_id, 'User was removed as Leader from LearnDash group <a href="' . admin_url( 'post.php?post=' . $group_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $group_id ) . '</a>. Removing linked tag.' );
  1315.  
  1316.         wp_fusion()->user->remove_tags( $settings['leader_tag'], $user_id );
  1317.  
  1318.         add_action( 'wpf_tags_modified', array( $this, 'update_group_access' ), 10, 2 );
  1319.  
  1320.     }
  1321.  
  1322.     /**
  1323.      * Applies / removes linked tags when user added to / removed from course
  1324.      *
  1325.      * @access public
  1326.      * @return void
  1327.      */
  1328.  
  1329.     public function updated_course_access( $user_id, $course_id, $access_list = array(), $remove = false ) {
  1330.  
  1331.         // Sync the name
  1332.  
  1333.         if ( $remove == false ) {
  1334.  
  1335.             update_user_meta( $user_id, 'ld_last_course_enrolled', get_the_title( $course_id ) );
  1336.  
  1337.         }
  1338.  
  1339.         // Apply the tags
  1340.  
  1341.         $settings = get_post_meta( $course_id, 'wpf-settings-learndash', true );
  1342.  
  1343.         if ( empty( $settings ) ) {
  1344.             return;
  1345.         }
  1346.  
  1347.         remove_action( 'wpf_tags_modified', array( $this, 'update_course_access' ), 10, 2 );
  1348.  
  1349.         if ( $remove == false ) {
  1350.  
  1351.             $apply_tags = array();
  1352.  
  1353.             if ( ! empty( $settings['tag_link'] ) ) {
  1354.                 $apply_tags = array_merge( $apply_tags, $settings['tag_link'] );
  1355.             }
  1356.  
  1357.             if ( ! empty( $settings['apply_tags_enrolled'] ) ) {
  1358.                 $apply_tags = array_merge( $apply_tags, $settings['apply_tags_enrolled'] );
  1359.             }
  1360.  
  1361.             if ( ! empty( $apply_tags ) ) {
  1362.  
  1363.                 wpf_log( 'info', $user_id, 'User was enrolled in LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a>. Applying tags.' );
  1364.  
  1365.                 wp_fusion()->user->apply_tags( $apply_tags, $user_id );
  1366.  
  1367.             }
  1368.         } elseif ( ! empty( $settings['tag_link'] ) ) {
  1369.  
  1370.             wpf_log( 'info', $user_id, 'User was unenrolled from LearnDash course <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '" target="_blank">' . get_the_title( $course_id ) . '</a>. Removing linked tag.' );
  1371.  
  1372.             wp_fusion()->user->remove_tags( $settings['tag_link'], $user_id );
  1373.  
  1374.         }
  1375.  
  1376.         add_action( 'wpf_tags_modified', array( $this, 'update_course_access' ), 10, 2 );
  1377.  
  1378.     }
  1379.  
  1380.     /**
  1381.      * Adds randomly generates passwords to POST data so it can be picked up by user_register()
  1382.      *
  1383.      * @access public
  1384.      * @return string Password
  1385.      */
  1386.  
  1387.     public function push_password( $password ) {
  1388.  
  1389.         if ( ! empty( $_POST ) ) {
  1390.             $_POST['user_pass'] = $password;
  1391.         }
  1392.  
  1393.         return $password;
  1394.  
  1395.     }
  1396.  
  1397.  
  1398.     /**
  1399.      * Adds LearnDash field group to meta fields list
  1400.      *
  1401.      * @access  public
  1402.      * @return  array Field groups
  1403.      */
  1404.  
  1405.     public function add_meta_field_group( $field_groups ) {
  1406.  
  1407.         $field_groups['learndash_progress'] = array(
  1408.             'title'  => 'LearnDash Progress',
  1409.             'fields' => array(),
  1410.         );
  1411.  
  1412.         return $field_groups;
  1413.  
  1414.     }
  1415.  
  1416.  
  1417.     /**
  1418.      * Adds LearnDash meta fields to WPF contact fields list
  1419.      *
  1420.      * @access  public
  1421.      * @return  array Meta Fields
  1422.      */
  1423.  
  1424.     public function prepare_meta_fields( $meta_fields ) {
  1425.  
  1426.         $meta_fields['ld_last_course_enrolled'] = array(
  1427.             'label' => 'Last Course Enrolled',
  1428.             'type'  => 'text',
  1429.             'group' => 'learndash_progress',
  1430.         );
  1431.  
  1432.         $meta_fields['ld_last_lesson_completed'] = array(
  1433.             'label' => 'Last Lesson Completed',
  1434.             'type'  => 'text',
  1435.             'group' => 'learndash_progress',
  1436.         );
  1437.  
  1438.         $meta_fields['ld_last_lesson_completed_date'] = array(
  1439.             'label' => 'Last Lesson Completed Date',
  1440.             'type'  => 'date',
  1441.             'group' => 'learndash_progress',
  1442.         );
  1443.  
  1444.         $meta_fields['ld_last_course_completed'] = array(
  1445.             'label' => 'Last Course Completed',
  1446.             'type'  => 'text',
  1447.             'group' => 'learndash_progress',
  1448.         );
  1449.  
  1450.         $meta_fields['ld_last_course_completed_date'] = array(
  1451.             'label' => 'Last Course Completed Date',
  1452.             'type'  => 'date',
  1453.             'group' => 'learndash_progress',
  1454.         );
  1455.  
  1456.         $meta_fields['ld_last_course_progressed'] = array(
  1457.             'label' => 'Last Course Progressed',
  1458.             'type'  => 'text',
  1459.             'group' => 'learndash_progress',
  1460.         );
  1461.  
  1462.         return $meta_fields;
  1463.  
  1464.     }
  1465.  
  1466.     /**
  1467.      * Sets up last lesson / last course fields for automatic sync
  1468.      *
  1469.      * @access  public
  1470.      * @return  array Meta Fields
  1471.      */
  1472.  
  1473.     public function watch_meta_fields( $meta_fields ) {
  1474.  
  1475.         $meta_fields[] = 'ld_last_lesson_completed';
  1476.         $meta_fields[] = 'ld_last_course_completed';
  1477.         $meta_fields[] = 'ld_last_course_enrolled';
  1478.         $meta_fields[] = 'ld_last_course_progressed';
  1479.  
  1480.         return $meta_fields;
  1481.  
  1482.     }
  1483.  
  1484.  
  1485.     /**
  1486.      * //
  1487.      * // BATCH TOOLS
  1488.      * //
  1489.      **/
  1490.  
  1491.     /**
  1492.      * Adds LearnDash courses to available export options
  1493.      *
  1494.      * @access public
  1495.      * @return array Options
  1496.      */
  1497.  
  1498.     public function export_options( $options ) {
  1499.  
  1500.         $options['learndash_courses'] = array(
  1501.             'label'   => __( 'LearnDash course enrollment statuses', 'wp-fusion' ),
  1502.             'title'   => __( 'Users', 'wp-fusion' ),
  1503.             'tooltip' => sprintf( __( 'For each user on your site, applies tags in %s based on their current LearnDash course enrollments, using the settings configured on each course. <br /><br />Note that this does not apply to course enrollments that have been granted via Groups. <br /><br />Note that this does not enroll or unenroll any users from courses, it just applies tags based on existing course enrollments.' ), wp_fusion()->crm->name ),
  1504.         );
  1505.  
  1506.         return $options;
  1507.  
  1508.     }
  1509.  
  1510.     /**
  1511.      * Gets users to be processed
  1512.      *
  1513.      * @access public
  1514.      * @return int Count
  1515.      */
  1516.  
  1517.     public function batch_init() {
  1518.  
  1519.         $args = array( 'fields' => 'ID' );
  1520.  
  1521.         $users = get_users( $args );
  1522.  
  1523.         wpf_log( 'info', 0, 'Beginning <strong>LearnDash course enrollment statuses</strong> batch operation on ' . count( $users ) . ' users', array( 'source' => 'batch-process' ) );
  1524.  
  1525.         return $users;
  1526.  
  1527.     }
  1528.  
  1529.     /**
  1530.      * Process user enrollments one at a time
  1531.      *
  1532.      * @access public
  1533.      * @return void
  1534.      */
  1535.  
  1536.     public function batch_step( $user_id ) {
  1537.  
  1538.         // Get courses
  1539.         $enrolled_courses = learndash_user_get_enrolled_courses( $user_id, array() );
  1540.  
  1541.         // We won't look at courses a user is in because of a group
  1542.         $groups_courses = learndash_get_user_groups_courses_ids( $user_id );
  1543.  
  1544.         $enrolled_courses = array_diff( $enrolled_courses, $groups_courses );
  1545.  
  1546.         if ( ! empty( $enrolled_courses ) ) {
  1547.  
  1548.             foreach ( $enrolled_courses as $course_id ) {
  1549.  
  1550.                 wpf_log( 'info', $user_id, 'Processing LearnDash course enrollment status for <a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '">' . get_the_title( $course_id ) . '</a>', array( 'source' => 'batch-process' ) );
  1551.  
  1552.                 $this->updated_course_access( $user_id, $course_id );
  1553.  
  1554.             }
  1555.         }
  1556.     }
  1557.  
  1558. }
  1559.  
  1560. new WPF_LearnDash();
  1561.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement