Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Elementor LMS Card Widget.
- *
- * Elementor widget that displays a course card with dynamic controls.
- *
- * @since 1.0.0
- */
- class Elementor_LMS_Card_Widget extends \Elementor\Widget_Base {
- /**
- * Get widget name.
- *
- * @return string Widget name.
- */
- public function get_name() {
- return 'lms-course-card';
- }
- /**
- * Get widget title.
- *
- * @return string Widget title.
- */
- public function get_title() {
- return esc_html__( 'LMS Course Card', 'text-domain' );
- }
- /**
- * Get widget icon.
- *
- * @return string Widget icon.
- */
- public function get_icon() {
- return 'eicon-post-card';
- }
- /**
- * Get widget categories.
- *
- * @return array Widget categories.
- */
- public function get_categories() {
- return [ 'general' ];
- }
- /**
- * Get widget keywords.
- *
- * @return array Widget keywords.
- */
- public function get_keywords() {
- return [ 'lms', 'course', 'card', 'tutorlms', 'learnpress' ];
- }
- /**
- * Register widget controls.
- *
- * This method adds different types of controls to the widget,
- * which will be displayed in the Elementor editor.
- */
- protected function _register_controls() {
- $this->start_controls_section(
- 'content_section',
- [
- 'label' => esc_html__( 'Course Content', 'text-domain' ),
- 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
- ]
- );
- // Control for selecting the source (LMS or manual)
- $this->add_control(
- 'source_type',
- [
- 'label' => esc_html__( 'Course Source', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::SELECT,
- 'default' => 'manual',
- 'options' => [
- 'manual' => esc_html__( 'Manual Content', 'text-domain' ),
- 'tutorlms' => esc_html__( 'TutorLMS Course', 'text-domain' ),
- 'learnpress' => esc_html__( 'LearnPress Course', 'text-domain' ),
- ],
- ]
- );
- // Manual controls - show only if source is 'manual'
- $this->add_control(
- 'manual_course_image',
- [
- 'label' => esc_html__( 'Course Image', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::MEDIA,
- 'default' => [
- 'url' => \Elementor\Utils::get_placeholder_image_src(),
- ],
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_course_price',
- [
- 'label' => esc_html__( 'Course Price', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => '$45.00',
- 'placeholder' => esc_html__( 'e.g. $45.00', 'text-domain' ),
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_course_category',
- [
- 'label' => esc_html__( 'Category Name', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => 'Design',
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_course_title',
- [
- 'label' => esc_html__( 'Course Title', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => 'Learn With Advance Website Design (UX/UI) Course',
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_author_image',
- [
- 'label' => esc_html__( 'Author Image', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::MEDIA,
- 'default' => [
- 'url' => \Elementor\Utils::get_placeholder_image_src(),
- ],
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_author_name',
- [
- 'label' => esc_html__( 'Author Name', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => 'Paul C. Deleon',
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_course_lessons',
- [
- 'label' => esc_html__( 'Number of Lessons', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => '80',
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- $this->add_control(
- 'manual_course_students',
- [
- 'label' => esc_html__( 'Number of Students', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT,
- 'default' => '80',
- 'condition' => [
- 'source_type' => 'manual',
- ],
- ]
- );
- // Control for selecting a specific TutorLMS Course
- $this->add_control(
- 'tutorlms_course_id',
- [
- 'label' => esc_html__( 'Select TutorLMS Course', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT, // In a real widget, you would use a dynamic select field
- 'default' => '',
- 'condition' => [
- 'source_type' => 'tutorlms',
- ],
- ]
- );
- // Control for selecting a specific LearnPress Course
- $this->add_control(
- 'learnpress_course_id',
- [
- 'label' => esc_html__( 'Select LearnPress Course', 'text-domain' ),
- 'type' => \Elementor\Controls_Manager::TEXT, // In a real widget, you would use a dynamic select field
- 'default' => '',
- 'condition' => [
- 'source_type' => 'learnpress',
- ],
- ]
- );
- $this->end_controls_section();
- }
- /**
- * Render widget output on the frontend.
- *
- * Written in PHP and uses the HTML structure.
- */
- protected function render() {
- $settings = $this->get_settings_for_display();
- $course_image_url = '';
- $course_price = '';
- $course_category = '';
- $course_title = '';
- $course_link = '';
- $author_image_url = '';
- $author_name = '';
- $lessons_count = '';
- $students_count = '';
- $rating_stars = ''; // To hold the dynamic star rating HTML
- // Switch based on the selected source type
- switch ( $settings['source_type'] ) {
- case 'tutorlms':
- // Check if TutorLMS is active
- if ( function_exists( 'tutor_utils' ) && ! empty( $settings['tutorlms_course_id'] ) ) {
- $course_id = $settings['tutorlms_course_id'];
- $course = get_post( $course_id );
- if ( $course ) {
- $course_image_url = get_the_post_thumbnail_url( $course_id, 'full' );
- $course_title = $course->post_title;
- $course_link = get_permalink( $course_id );
- // Get course price
- $price_html = tutor_utils()->get_course_price( $course_id, false );
- $course_price = strip_tags( $price_html );
- // Get author info
- $author_id = $course->post_author;
- $author_name = get_the_author_meta( 'display_name', $author_id );
- $author_image_url = get_avatar_url( $author_id );
- // Get category
- $categories = get_the_terms( $course_id, 'course-category' );
- if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
- $course_category = $categories[0]->name;
- }
- // Get lessons count
- $lessons_count = tutor_utils()->get_lesson_count_by_course( $course_id );
- // Get students count
- $students_count = tutor_utils()->get_total_students_by_course_id( $course_id );
- // Get rating
- $rating = tutor_utils()->get_course_average_rating( $course_id );
- $rating_value = $rating->rating_avg;
- $rating_stars = '';
- for ( $i = 1; $i <= 5; $i++ ) {
- if ( $i <= $rating_value ) {
- $rating_stars .= '<i class="fas fa-star"></i>';
- } else {
- $rating_stars .= '<i class="far fa-star"></i>';
- }
- }
- }
- }
- break;
- case 'learnpress':
- // Check if LearnPress is active
- if ( class_exists( 'LP_Global' ) && ! empty( $settings['learnpress_course_id'] ) ) {
- $course_id = $settings['learnpress_course_id'];
- $course = learn_press_get_course( $course_id );
- if ( $course ) {
- $course_image_url = $course->get_image( 'full' );
- $course_title = $course->get_title();
- $course_link = $course->get_permalink();
- // Get price
- $price_object = $course->get_price();
- $course_price = is_array( $price_object ) ? $price_object['price'] : $price_object;
- $course_price = wc_price( $course_price ); // Format with currency
- // Get author info
- $author_id = $course->get_author_id();
- $author_name = get_the_author_meta( 'display_name', $author_id );
- $author_image_url = get_avatar_url( $author_id );
- // Get category
- $categories = get_the_terms( $course_id, 'course_category' );
- if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
- $course_category = $categories[0]->name;
- }
- // Get lessons and students count
- $lessons_count = $course->count_items( 'lp_lesson' );
- $students_count = $course->get_students();
- // Get rating
- $rating = learn_press_get_course_rate( $course_id, false );
- $rating_value = $rating['rated'];
- $rating_stars = '';
- for ( $i = 1; $i <= 5; $i++ ) {
- if ( $i <= $rating_value ) {
- $rating_stars .= '<i class="fas fa-star"></i>';
- } else {
- $rating_stars .= '<i class="far fa-star"></i>';
- }
- }
- }
- }
- break;
- case 'manual':
- default:
- // Manual content from widget controls
- $course_image_url = $settings['manual_course_image']['url'];
- $course_price = $settings['manual_course_price'];
- $course_category = $settings['manual_course_category'];
- $course_title = $settings['manual_course_title'];
- $course_link = '#'; // Placeholder, add a link control if needed
- $author_image_url = $settings['manual_author_image']['url'];
- $author_name = $settings['manual_author_name'];
- $lessons_count = $settings['manual_course_lessons'];
- $students_count = $settings['manual_course_students'];
- // For the star rating, we'll manually output 5 stars for now.
- $rating_stars = '<i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i>';
- break;
- }
- ?>
- <div class="single-courses-items box-shadow mt-0">
- <div class="courses-image bg-cover" style="background-image: url('<?php echo esc_url($course_image_url); ?>');">
- <div class="post-cat">
- <span><?php echo esc_html($course_price); ?></span>
- </div>
- </div>
- <div class="courses-content">
- <a href="<?php echo esc_url($course_link); ?>" class="post-box-1"><?php echo esc_html($course_category); ?></a>
- <h3>
- <a href="<?php echo esc_url($course_link); ?>">
- <?php echo esc_html($course_title); ?>
- </a>
- </h3>
- <div class="client-items">
- <div class="content">
- <div class="client-img bg-cover" style="background-image: url('<?php echo esc_url($author_image_url); ?>');"></div>
- <p><?php echo esc_html($author_name); ?></p>
- </div>
- <div class="star">
- <?php echo $rating_stars; ?>
- </div>
- </div>
- <ul class="post-class">
- <li>
- <i class="far fa-books"></i>
- <?php echo esc_html($lessons_count); ?> Lessons
- </li>
- <li>
- <i class="far fa-user"></i>
- <?php echo esc_html($students_count); ?> Students
- </li>
- <li>
- <a href="<?php echo esc_url($course_link); ?>" class="theme-btn">Enroll Now</a>
- </li>
- </ul>
- </div>
- </div>
- <?php
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment