    <?php
        use Elementor\Group_Control_Image_Size;

        $settings = $this->get_settings();
        $c_desktop = $settings['posts_column'];
        $word_count_of_excerpt = $settings['word_count_of_excerpt'];
        $excerpt_desc_word = $settings['excerpt_desc_word'];
        $read_more_button = $settings['read_more_button'];
        $course_custom_button_text = $settings['course_custom_button_text'];
        $cat_multi_slug = widgetkit_pro_array_get($settings, 'cat_dynamic_multiple_id');
        $post_num_to_show = widgetkit_pro_array_get($settings, 'post_num_to_show');
        $thumb_size = widgetkit_pro_array_get($settings, 'image_size');
    ?>
    
    <?php if (class_exists('SFWD_LMS')) : // Check LearnDash LMS plugin active?>
        <div class="row widgetkit-ld-courses widgetkit-ld-courses-list-element">
            <div class="column-grid column-desktop-<?php echo $c_desktop; ?>">
                <?php
                /**
                 * Query for the post
                 * custom post type
                 * post type : lp_course
                 * category taxonomy : course_category
                 */
                $count = 0;
                /**
                * if select any category for drop down
                * then assign that value to the $first_cat_id
                * if not then set it to null
                */
                if (isset($first_cat_ID)) {
                    $first_cat_ID = $first_cat_ID;
                } else {
                    $first_cat_ID = '';
                }
                if ($cat_multi_slug) :
                    $course = array(
                        'post_type' => 'sfwd-courses',
                        'posts_per_page' => $post_num_to_show,
                        'orderby' => $settings['orderby'],
                        'order' => $settings['order'],
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'ld_course_category',
                                'field'    => 'slug',
                                'terms' => $cat_multi_slug,
                            ),
                        ),
                    );

                else :
                    $course = array(
                        'post_type' => 'sfwd-courses',
                        'posts_per_page' => $post_num_to_show,
                        'orderby' => $settings['orderby'],
                        'order' => $settings['order'],
                    );  
                endif;
                

                $course_query = new WP_Query($course);
                if ($course_query->have_posts()):
                    while ($course_query->have_posts()):
                        $course_query->the_post();

                            global $post;
                            $post_id = $post->ID;
                            $course_id = $post_id;
                            $user_id = get_current_user_id();
                            $enable_video = get_post_meta($post->ID, '_learndash_course_grid_enable_video_preview', true);
                            $embed_code = get_post_meta($post->ID, '_learndash_course_grid_video_embed_code', true);
                            //$button_text = get_post_meta($post->ID, '_learndash_course_grid_custom_button_text', true);
                            $grid_short_desc = get_post_meta($post->ID, '_learndash_course_grid_short_description', true);


                            // Retrive oembed HTML if URL provided
                            if (preg_match('/^http/', $embed_code)) {
                                $embed_code = wp_oembed_get($embed_code, array('height' => 600, 'width' => 400));
                            }

                            //$button_text = isset($button_text) && !empty($button_text) ? $button_text : __('See more...', 'widgetkit-pro');
                            //$button_text = apply_filters('learndash_course_grid_custom_button_text', $button_text, $post_id);

                            $options = get_option('sfwd_cpt_options');
                            $currency = null;

                            if (!is_null($options)) {
                                if (isset($options['modules']) && isset($options['modules']['sfwd-courses_options']) && isset($options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'])) {
                                    $currency = $options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'];
                                }
                            }

                            if (is_null($currency)) {
                                $currency = 'USD';
                            }

                            $course_options = get_post_meta($post_id, "_sfwd-courses", true);
                            $post_meta = get_post_meta(get_the_ID());
                            $price = $course_options && isset($course_options['sfwd-courses_course_price']) ? $course_options['sfwd-courses_course_price'] : __('Free', 'widgetkit-pro');
                            // $short_description = @$course_options['sfwd-courses_course_short_description'];
                            $short_description = @$post_meta['_wkfe_ld_template_course_short_desc'];

                            $has_access = sfwd_lms_has_access($course_id, $user_id);
                            $is_completed = learndash_course_completed($user_id, $course_id);

                            if ($price == '') {
                                $price .= __('Free', 'widgetkit-pro');
                            }

                            if (is_numeric($price)) {
                                if ($currency == "USD") {
                                    $price = '$' . $price;
                                } else {
                                    $price .= ' ' . $currency;
                                }
                            }

                            $class = '';
                            $ribbon_text = '';

                            if ($has_access && !$is_completed) {
                                $class = 'ld_course_grid_price ribbon-enrolled';
                                $ribbon_text = __('Enrolled', 'widgetkit-pro');
                            } elseif ($has_access && $is_completed) {
                                $class = 'ld_course_grid_price';
                                $ribbon_text = __('Completed', 'widgetkit-pro');
                            } else {
                                $class = !empty($course_options['sfwd-courses_course_price']) ? 'ld_course_grid_price price_' . $currency : 'ld_course_grid_price free';
                                $ribbon_text = $price;
                            }
                        ?>

                        <div class="ld_course_grid">
                            <article id="post-<?php the_ID();?>" <?php post_class('course');?>>
                                <?php if (has_post_thumbnail()): ?>
                                    <div class="<?php echo esc_attr($class); ?>">
                                        <?php echo esc_attr($ribbon_text); ?>
                                    </div>
                                <?php endif;?>

                                <?php if (1 == $enable_video && !empty($embed_code)): ?>
                                    <div class="ld_course_grid_video_embed">
                                        <?php echo $embed_code; ?>
                                    </div>
                                    <?php elseif (has_post_thumbnail()): ?>
                                    <a href="<?php the_permalink();?>">
                                        <img src="<?php echo Group_Control_Image_Size::get_attachment_image_src(get_post_thumbnail_id(), 'image', $settings );?>" alt="<?php the_title(); ?>">
                                    </a>
                                <?php endif;?>

                                <div class="caption">
                                    <h3 class="wk-entry-title">
                                        <a href="<?php the_permalink();?>">
                                            <?php esc_html_e(get_the_title(), 'widgetkit-pro');?>
                                        </a>
                                    </h3>

                                    <?php if ($short_description):?>
                                        <p class="ld-entry-content short-description-meta-box">
                                            <?php //echo htmlspecialchars_decode(do_shortcode(wp_trim_words($short_description, $excerpt_desc_word, '' ) )); ?>
                                            <?php if($word_count_of_excerpt): ?>
                                                <?php echo wp_trim_words($short_description[0], $excerpt_desc_word, ''); ?>
                                            <?php else: ?>
                                                <?php echo $short_description[0]; ?>
                                            <?php endif; ?>
                                        </p>
                                    <?php else:?>
                                        <p class="ld-entry-content grid-short-desc-meta-box">
                                            <?php echo htmlspecialchars_decode($grid_short_desc); ?>
                                        </p>
                                    <?php endif;?>

                                    <?php $read_more_button = $settings['read_more_button'];?>
                                    <?php if ($read_more_button == 'yes'): ?>
                                        <p class="ld_course_grid_button">
                                            <a class="btn btn-primary" role="button" href="<?php the_permalink();?>" rel="bookmark">
                                                <?php echo esc_attr($course_custom_button_text); ?>
                                            </a>
                                        </p>
                                    <?php endif;?>
                                </div><!-- .entry-header -->
                            </article><!-- #post-## -->

                        </div>

                    <?php 
                    endwhile; 
                    wp_reset_postdata(); 
                    wp_reset_query(); 
                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;  ?>
