|
| 1 | +<?php |
| 2 | +class WP_Widget_Course extends WP_Widget { |
| 3 | + |
| 4 | + /** constructor */ |
| 5 | + function __construct() { |
| 6 | + $widget_ops = array( |
| 7 | + 'classname' => 'cc-course', |
| 8 | + 'description' => 'This is a Vocabulary card with a selected course information', |
| 9 | + ); |
| 10 | + $control_ops = array(); |
| 11 | + parent::__construct( 'cc-course', 'CC Course card', $widget_ops, $control_ops ); |
| 12 | + } |
| 13 | + |
| 14 | + function widget( $args, $instance ) { |
| 15 | + if ( !empty( $instance['course'] ) ) { |
| 16 | + $title = ( !empty( $instance['title'] ) ) ? $instance['title'] : 'Course'; |
| 17 | + $course = get_post( $instance['course'] ); |
| 18 | + $description = do_excerpt( $course ); |
| 19 | + $url = get_permalink( $instance[ 'course' ] ); |
| 20 | + $course_title = get_the_title( $instance['course'] ); |
| 21 | + echo '<div class="widget course-card">'; |
| 22 | + echo Components::card_post( $instance['course'], true, false, false, false, true, true, true, $title, $description, $url, $course_title, false, false, 'vertical', 'Read more', 'small', 'is-info' ); |
| 23 | + echo '</div>'; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + function update( $new_instance, $old_instance ) { |
| 28 | + return $new_instance; |
| 29 | + } |
| 30 | + |
| 31 | + function get_courses() { |
| 32 | + $courses = new WP_Query( array( |
| 33 | + 'post_type' => 'cc_course', |
| 34 | + 'post_status' => 'publish', |
| 35 | + 'posts_per_page' => -1 |
| 36 | + ) ); |
| 37 | + if ( $courses->have_posts() ) { |
| 38 | + return $courses->posts; |
| 39 | + } else { |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + function form( $instance ) { |
| 45 | + echo '<p><label for="' . $this->get_field_id( 'title' ) . '">Title: <input type="text" name="' . $this->get_field_name( 'title' ) . '" id="' . $this->get_field_id( 'title' ) . '" value="' . $instance['title'] . '" class="widefat" /></label></p>'; |
| 46 | + echo '<p><label>Select Course: </label>'; |
| 47 | + $courses_list = $this->get_courses(); |
| 48 | + echo '<select class="widefat" id="' . $this->get_field_id( 'course' ) . '" name="' . $this->get_field_name( 'course' ) . '">'; |
| 49 | + echo '<option value="">Select Course</option>'; |
| 50 | + foreach ($courses_list as $course) { |
| 51 | + echo '<option value="' . $course->ID . '" ' . ( ( $instance['course'] == $course->ID ) ? 'selected="selected"' : '' ) . '>' . get_the_title($course->ID) . '</option>'; |
| 52 | + } |
| 53 | + echo '</select>'; |
| 54 | + echo '</p>'; |
| 55 | + } |
| 56 | +} |
| 57 | +function cc_course_card_widget_init() { |
| 58 | + register_widget( 'WP_Widget_Course' ); |
| 59 | +} |
| 60 | + |
| 61 | +add_action( 'widgets_init', 'cc_course_card_widget_init' ); |
0 commit comments