<?php
// Silence is golden.

	$settings = $this->get_settings_for_display();
    $id = $this->get_id();

    if( class_exists('SFWD_LMS') ) :
        $elementor_editor = \Elementor\Plugin::instance()->editor->is_edit_mode();
        if( $elementor_editor ) {
            $course_id = learndash_post_arr()[0];
        } else {
            $course_id = learndash_get_course_id();
        }

        if( is_user_logged_in() ) {
            $user_id = get_current_user_id();
        } else {
            $user_id = 0;
        }

        $has_access = sfwd_lms_has_access( $course_id, $user_id );
        $course_price_type = learndash_get_course_meta_setting($course_id, 'course_price_type');
        $course_price = learndash_get_course_meta_setting($course_id, 'course_price');
        $paypal_settings = get_option( 'learndash_settings_paypal' );
        ?>
        <div class="ld-course-enrollment-wrapper">
            <div class="ld-course-enrollment-top">
                <?php if(  $settings['course_period'] ): ?>
                    <span> <?php echo esc_html($settings['course_period']); ?> </span>
                <?php endif; ?>
                <?php  if( $course_price_type == 'paynow' && isset($course_price) ) : ?>
                    <h3><?php echo learndash_30_get_currency_symbol() . esc_html( $course_price ); ?>
                <?php endif; ?>
                <?php  if( $course_price_type == 'open' ) : ?>
                    <h3><?php echo esc_html__('Open','widgetkit-pro'); ?>
                <?php endif; ?>
                <?php  if( $course_price_type == 'free' ) : ?>
                    <h3><?php echo esc_html__('Free','widgetkit-pro'); ?>
                <?php endif; ?>
            </div>
            <?php 
                if( $has_access && is_user_logged_in() ): 
                    $userquery = learndash_get_users_for_course($course_id,array(),false);
                    if ( ( $userquery instanceof WP_User_Query ) && ( property_exists( $userquery, 'results' ) ) && ( ! empty( $userquery->results ) ) ) :
                        $user_ids = $userquery->get_results();
                        $i = 1;
            ?>
                <div class="ld-course-enrollment-user">
                    <span class="course-users">
                        <?php foreach( $user_ids as $singleuserid ) : ?>
                            <?php echo get_avatar( $singleuserid,'50' ); ?>
                            <?php if( $i == 4 ) break;  ?>
                        <?php $i++; endforeach; ?>
                    </span>
                    <span class="course-members"><?php echo esc_html( '+'.count( $user_ids ).' enrolled' ); ?></span>
                </div>
                <?php endif; ?>
            <?php endif; ?>
            
            <div class="ld-course-enrollment-description">
                <?php if( $settings['course_enrollment_descrition'] ) :  ?>
                    <p> <?php echo esc_html($settings['course_enrollment_descrition']); ?> </p>
                <?php endif; ?>
            </div>
            <div class="ld-course-enrollment-bottom">
                <?php if( $has_access && is_user_logged_in() ) : ?>
                    <span class="course-status"><?php echo learndash_course_status( $course_id, $user_id ); ?></span>
                <?php else: ?>
                    <?php  if( $course_price_type == 'free' || $course_price_type == 'open' ) : ?>
                        <a class="btn-join" href="<?php echo esc_url( wp_login_url( get_permalink( $course_id ) ) ); ?>"><?php echo esc_html__( 'Login to Enroll', 'widgetkit-pro' ) ?></a>
                    <?php elseif( $course_price_type == 'paynow' ): ?>    
                    <?php
                        echo do_shortcode('[learndash_payment_buttons course_id="'.$course_id.'"]');
                    ?>
                    <?php endif; ?>
                <?php endif; ?>

                <?php if( ! is_user_logged_in() ): ?>
                    <?php if( $settings['course_enrollment_back_text'] ) :  ?>
                        <p><?php echo esc_html($settings['course_enrollment_back_text']); ?></p>
                    <?php endif; ?>
                <?php endif; ?>    
            </div>
        </div>
    <?php
        else :
    ?>
        <div class="plugin-active-massage">
            <h3><?php esc_html_e('Please activate LearnDash LMS plugin first', 'widgetkit-pro');?></h3>
        </div>
    <?php
    endif;