salmancreation

Elementor LMS Card Widget Development PHP

Aug 18th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.15 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Elementor LMS Card Widget.
  4.  *
  5.  * Elementor widget that displays a course card with dynamic controls.
  6.  *
  7.  * @since 1.0.0
  8.  */
  9. class Elementor_LMS_Card_Widget extends \Elementor\Widget_Base {
  10.  
  11.     /**
  12.      * Get widget name.
  13.      *
  14.      * @return string Widget name.
  15.      */
  16.     public function get_name() {
  17.         return 'lms-course-card';
  18.     }
  19.  
  20.     /**
  21.      * Get widget title.
  22.      *
  23.      * @return string Widget title.
  24.      */
  25.     public function get_title() {
  26.         return esc_html__( 'LMS Course Card', 'text-domain' );
  27.     }
  28.  
  29.     /**
  30.      * Get widget icon.
  31.      *
  32.      * @return string Widget icon.
  33.      */
  34.     public function get_icon() {
  35.         return 'eicon-post-card';
  36.     }
  37.  
  38.     /**
  39.      * Get widget categories.
  40.      *
  41.      * @return array Widget categories.
  42.      */
  43.     public function get_categories() {
  44.         return [ 'general' ];
  45.     }
  46.  
  47.     /**
  48.      * Get widget keywords.
  49.      *
  50.      * @return array Widget keywords.
  51.      */
  52.     public function get_keywords() {
  53.         return [ 'lms', 'course', 'card', 'tutorlms', 'learnpress' ];
  54.     }
  55.  
  56.     /**
  57.      * Register widget controls.
  58.      *
  59.      * This method adds different types of controls to the widget,
  60.      * which will be displayed in the Elementor editor.
  61.      */
  62.     protected function _register_controls() {
  63.  
  64.         $this->start_controls_section(
  65.             'content_section',
  66.             [
  67.                 'label' => esc_html__( 'Course Content', 'text-domain' ),
  68.                 'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
  69.             ]
  70.         );
  71.  
  72.         // Control for selecting the source (LMS or manual)
  73.         $this->add_control(
  74.             'source_type',
  75.             [
  76.                 'label'   => esc_html__( 'Course Source', 'text-domain' ),
  77.                 'type'    => \Elementor\Controls_Manager::SELECT,
  78.                 'default' => 'manual',
  79.                 'options' => [
  80.                     'manual'    => esc_html__( 'Manual Content', 'text-domain' ),
  81.                     'tutorlms'  => esc_html__( 'TutorLMS Course', 'text-domain' ),
  82.                     'learnpress' => esc_html__( 'LearnPress Course', 'text-domain' ),
  83.                 ],
  84.             ]
  85.         );
  86.  
  87.         // Manual controls - show only if source is 'manual'
  88.         $this->add_control(
  89.             'manual_course_image',
  90.             [
  91.                 'label'     => esc_html__( 'Course Image', 'text-domain' ),
  92.                 'type'      => \Elementor\Controls_Manager::MEDIA,
  93.                 'default'   => [
  94.                     'url' => \Elementor\Utils::get_placeholder_image_src(),
  95.                 ],
  96.                 'condition' => [
  97.                     'source_type' => 'manual',
  98.                 ],
  99.             ]
  100.         );
  101.  
  102.         $this->add_control(
  103.             'manual_course_price',
  104.             [
  105.                 'label'     => esc_html__( 'Course Price', 'text-domain' ),
  106.                 'type'      => \Elementor\Controls_Manager::TEXT,
  107.                 'default'   => '$45.00',
  108.                 'placeholder' => esc_html__( 'e.g. $45.00', 'text-domain' ),
  109.                 'condition' => [
  110.                     'source_type' => 'manual',
  111.                 ],
  112.             ]
  113.         );
  114.  
  115.         $this->add_control(
  116.             'manual_course_category',
  117.             [
  118.                 'label'     => esc_html__( 'Category Name', 'text-domain' ),
  119.                 'type'      => \Elementor\Controls_Manager::TEXT,
  120.                 'default'   => 'Design',
  121.                 'condition' => [
  122.                     'source_type' => 'manual',
  123.                 ],
  124.             ]
  125.         );
  126.  
  127.         $this->add_control(
  128.             'manual_course_title',
  129.             [
  130.                 'label'     => esc_html__( 'Course Title', 'text-domain' ),
  131.                 'type'      => \Elementor\Controls_Manager::TEXT,
  132.                 'default'   => 'Learn With Advance Website Design (UX/UI) Course',
  133.                 'condition' => [
  134.                     'source_type' => 'manual',
  135.                 ],
  136.             ]
  137.         );
  138.  
  139.         $this->add_control(
  140.             'manual_author_image',
  141.             [
  142.                 'label'     => esc_html__( 'Author Image', 'text-domain' ),
  143.                 'type'      => \Elementor\Controls_Manager::MEDIA,
  144.                 'default'   => [
  145.                     'url' => \Elementor\Utils::get_placeholder_image_src(),
  146.                 ],
  147.                 'condition' => [
  148.                     'source_type' => 'manual',
  149.                 ],
  150.             ]
  151.         );
  152.  
  153.         $this->add_control(
  154.             'manual_author_name',
  155.             [
  156.                 'label'     => esc_html__( 'Author Name', 'text-domain' ),
  157.                 'type'      => \Elementor\Controls_Manager::TEXT,
  158.                 'default'   => 'Paul C. Deleon',
  159.                 'condition' => [
  160.                     'source_type' => 'manual',
  161.                 ],
  162.             ]
  163.         );
  164.  
  165.         $this->add_control(
  166.             'manual_course_lessons',
  167.             [
  168.                 'label'     => esc_html__( 'Number of Lessons', 'text-domain' ),
  169.                 'type'      => \Elementor\Controls_Manager::TEXT,
  170.                 'default'   => '80',
  171.                 'condition' => [
  172.                     'source_type' => 'manual',
  173.                 ],
  174.             ]
  175.         );
  176.  
  177.         $this->add_control(
  178.             'manual_course_students',
  179.             [
  180.                 'label'     => esc_html__( 'Number of Students', 'text-domain' ),
  181.                 'type'      => \Elementor\Controls_Manager::TEXT,
  182.                 'default'   => '80',
  183.                 'condition' => [
  184.                     'source_type' => 'manual',
  185.                 ],
  186.             ]
  187.         );
  188.        
  189.         // Control for selecting a specific TutorLMS Course
  190.         $this->add_control(
  191.             'tutorlms_course_id',
  192.             [
  193.                 'label'     => esc_html__( 'Select TutorLMS Course', 'text-domain' ),
  194.                 'type'      => \Elementor\Controls_Manager::TEXT, // In a real widget, you would use a dynamic select field
  195.                 'default'   => '',
  196.                 'condition' => [
  197.                     'source_type' => 'tutorlms',
  198.                 ],
  199.             ]
  200.         );
  201.        
  202.         // Control for selecting a specific LearnPress Course
  203.         $this->add_control(
  204.             'learnpress_course_id',
  205.             [
  206.                 'label'     => esc_html__( 'Select LearnPress Course', 'text-domain' ),
  207.                 'type'      => \Elementor\Controls_Manager::TEXT, // In a real widget, you would use a dynamic select field
  208.                 'default'   => '',
  209.                 'condition' => [
  210.                     'source_type' => 'learnpress',
  211.                 ],
  212.             ]
  213.         );
  214.  
  215.         $this->end_controls_section();
  216.  
  217.     }
  218.  
  219.     /**
  220.      * Render widget output on the frontend.
  221.      *
  222.      * Written in PHP and uses the HTML structure.
  223.      */
  224.     protected function render() {
  225.         $settings = $this->get_settings_for_display();
  226.  
  227.         $course_image_url = '';
  228.         $course_price = '';
  229.         $course_category = '';
  230.         $course_title = '';
  231.         $course_link = '';
  232.         $author_image_url = '';
  233.         $author_name = '';
  234.         $lessons_count = '';
  235.         $students_count = '';
  236.         $rating_stars = ''; // To hold the dynamic star rating HTML
  237.  
  238.         // Switch based on the selected source type
  239.         switch ( $settings['source_type'] ) {
  240.             case 'tutorlms':
  241.                 // Check if TutorLMS is active
  242.                 if ( function_exists( 'tutor_utils' ) && ! empty( $settings['tutorlms_course_id'] ) ) {
  243.                     $course_id = $settings['tutorlms_course_id'];
  244.                     $course = get_post( $course_id );
  245.                    
  246.                     if ( $course ) {
  247.                         $course_image_url = get_the_post_thumbnail_url( $course_id, 'full' );
  248.                         $course_title = $course->post_title;
  249.                         $course_link = get_permalink( $course_id );
  250.                        
  251.                         // Get course price
  252.                         $price_html = tutor_utils()->get_course_price( $course_id, false );
  253.                         $course_price = strip_tags( $price_html );
  254.                        
  255.                         // Get author info
  256.                         $author_id = $course->post_author;
  257.                         $author_name = get_the_author_meta( 'display_name', $author_id );
  258.                         $author_image_url = get_avatar_url( $author_id );
  259.                        
  260.                         // Get category
  261.                         $categories = get_the_terms( $course_id, 'course-category' );
  262.                         if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
  263.                             $course_category = $categories[0]->name;
  264.                         }
  265.                        
  266.                         // Get lessons count
  267.                         $lessons_count = tutor_utils()->get_lesson_count_by_course( $course_id );
  268.                        
  269.                         // Get students count
  270.                         $students_count = tutor_utils()->get_total_students_by_course_id( $course_id );
  271.                        
  272.                         // Get rating
  273.                         $rating = tutor_utils()->get_course_average_rating( $course_id );
  274.                         $rating_value = $rating->rating_avg;
  275.                         $rating_stars = '';
  276.                         for ( $i = 1; $i <= 5; $i++ ) {
  277.                             if ( $i <= $rating_value ) {
  278.                                 $rating_stars .= '<i class="fas fa-star"></i>';
  279.                             } else {
  280.                                 $rating_stars .= '<i class="far fa-star"></i>';
  281.                             }
  282.                         }
  283.                     }
  284.                 }
  285.                 break;
  286.  
  287.             case 'learnpress':
  288.                 // Check if LearnPress is active
  289.                 if ( class_exists( 'LP_Global' ) && ! empty( $settings['learnpress_course_id'] ) ) {
  290.                     $course_id = $settings['learnpress_course_id'];
  291.                     $course = learn_press_get_course( $course_id );
  292.                    
  293.                     if ( $course ) {
  294.                         $course_image_url = $course->get_image( 'full' );
  295.                         $course_title = $course->get_title();
  296.                         $course_link = $course->get_permalink();
  297.                        
  298.                         // Get price
  299.                         $price_object = $course->get_price();
  300.                         $course_price = is_array( $price_object ) ? $price_object['price'] : $price_object;
  301.                         $course_price = wc_price( $course_price ); // Format with currency
  302.                        
  303.                         // Get author info
  304.                         $author_id = $course->get_author_id();
  305.                         $author_name = get_the_author_meta( 'display_name', $author_id );
  306.                         $author_image_url = get_avatar_url( $author_id );
  307.  
  308.                         // Get category
  309.                         $categories = get_the_terms( $course_id, 'course_category' );
  310.                         if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
  311.                             $course_category = $categories[0]->name;
  312.                         }
  313.                        
  314.                         // Get lessons and students count
  315.                         $lessons_count = $course->count_items( 'lp_lesson' );
  316.                         $students_count = $course->get_students();
  317.                        
  318.                         // Get rating
  319.                         $rating = learn_press_get_course_rate( $course_id, false );
  320.                         $rating_value = $rating['rated'];
  321.                         $rating_stars = '';
  322.                         for ( $i = 1; $i <= 5; $i++ ) {
  323.                             if ( $i <= $rating_value ) {
  324.                                 $rating_stars .= '<i class="fas fa-star"></i>';
  325.                             } else {
  326.                                 $rating_stars .= '<i class="far fa-star"></i>';
  327.                             }
  328.                         }
  329.                     }
  330.                 }
  331.                 break;
  332.  
  333.             case 'manual':
  334.             default:
  335.                 // Manual content from widget controls
  336.                 $course_image_url = $settings['manual_course_image']['url'];
  337.                 $course_price = $settings['manual_course_price'];
  338.                 $course_category = $settings['manual_course_category'];
  339.                 $course_title = $settings['manual_course_title'];
  340.                 $course_link = '#'; // Placeholder, add a link control if needed
  341.                 $author_image_url = $settings['manual_author_image']['url'];
  342.                 $author_name = $settings['manual_author_name'];
  343.                 $lessons_count = $settings['manual_course_lessons'];
  344.                 $students_count = $settings['manual_course_students'];
  345.                
  346.                 // For the star rating, we'll manually output 5 stars for now.
  347.                 $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>';
  348.                
  349.                 break;
  350.         }
  351.  
  352.         ?>
  353.  
  354.         <div class="single-courses-items box-shadow mt-0">
  355.             <div class="courses-image bg-cover" style="background-image: url('<?php echo esc_url($course_image_url); ?>');">
  356.                 <div class="post-cat">
  357.                     <span><?php echo esc_html($course_price); ?></span>
  358.                 </div>
  359.             </div>
  360.             <div class="courses-content">
  361.                 <a href="<?php echo esc_url($course_link); ?>" class="post-box-1"><?php echo esc_html($course_category); ?></a>
  362.                 <h3>
  363.                     <a href="<?php echo esc_url($course_link); ?>">
  364.                         <?php echo esc_html($course_title); ?>
  365.                     </a>
  366.                 </h3>
  367.                 <div class="client-items">
  368.                     <div class="content">
  369.                         <div class="client-img bg-cover" style="background-image: url('<?php echo esc_url($author_image_url); ?>');"></div>
  370.                         <p><?php echo esc_html($author_name); ?></p>
  371.                     </div>
  372.                     <div class="star">
  373.                         <?php echo $rating_stars; ?>
  374.                     </div>
  375.                 </div>
  376.                 <ul class="post-class">
  377.                     <li>
  378.                         <i class="far fa-books"></i>
  379.                         <?php echo esc_html($lessons_count); ?> Lessons
  380.                     </li>
  381.                     <li>
  382.                         <i class="far fa-user"></i>
  383.                         <?php echo esc_html($students_count); ?> Students
  384.                     </li>
  385.                     <li>
  386.                         <a href="<?php echo esc_url($course_link); ?>" class="theme-btn">Enroll Now</a>
  387.                     </li>
  388.                 </ul>
  389.             </div>
  390.         </div>
  391.  
  392.         <?php
  393.     }
  394. }
  395.  
Advertisement
Add Comment
Please, Sign In to add comment