|
| 1 | +<?php |
| 2 | +class WP_Widget_Event extends WP_Widget { |
| 3 | + |
| 4 | + /** constructor */ |
| 5 | + function __construct() { |
| 6 | + $widget_ops = array( |
| 7 | + 'classname' => 'cc-event', |
| 8 | + 'description' => 'This is a Vocabulary event card ', |
| 9 | + ); |
| 10 | + $control_ops = array(); |
| 11 | + parent::__construct( 'cc-event', 'CC Event card', $widget_ops, $control_ops ); |
| 12 | + } |
| 13 | + |
| 14 | + function widget( $args, $instance ) { |
| 15 | + if ( !empty( $instance['event'] ) ) { |
| 16 | + $event_id = $insance[ 'event' ]; |
| 17 | + } else { |
| 18 | + $last_event = $this->get_last_event(); |
| 19 | + if ( !empty($last_event) ) { |
| 20 | + $event_id = $last_event->ID; |
| 21 | + } |
| 22 | + } |
| 23 | + if ( !empty( $event_id ) ) { |
| 24 | + echo '<div class="widget event-card">'; |
| 25 | + echo Components::card_post_event( $event_id, true ); |
| 26 | + echo '</div>'; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + function update( $new_instance, $old_instance ) { |
| 31 | + return $new_instance; |
| 32 | + } |
| 33 | + function get_last_event() { |
| 34 | + $events = new WP_Query( array( |
| 35 | + 'post_type' => 'cc_events', |
| 36 | + 'post_status' => 'publish', |
| 37 | + 'posts_per_page' => 1 |
| 38 | + ) ); |
| 39 | + if ($events->have_posts()) { |
| 40 | + return $events->posts[0]; |
| 41 | + } else { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + function get_events() { |
| 46 | + $events = new WP_Query( array( |
| 47 | + 'post_type' => 'cc_events', |
| 48 | + 'post_status' => 'publish', |
| 49 | + 'posts_per_page' => -1 |
| 50 | + ) ); |
| 51 | + if ( $events->have_posts() ) { |
| 52 | + return $events->posts; |
| 53 | + } else { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function form( $instance ) { |
| 59 | + 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>'; |
| 60 | + echo '<p><label>Select Course: </label>'; |
| 61 | + $event_list = $this->get_events(); |
| 62 | + echo '<select class="widefat" id="' . $this->get_field_id( 'event' ) . '" name="' . $this->get_field_name( 'event' ) . '">'; |
| 63 | + echo '<option value="">Select event</option>'; |
| 64 | + foreach ($event_list as $event) { |
| 65 | + echo '<option value="' . $event->ID . '" ' . ( ( $instance['event'] == $event->ID ) ? 'selected="selected"' : '' ) . '>' . get_the_title($event->ID) . '</option>'; |
| 66 | + } |
| 67 | + echo "<small>If you don't select an event it'll show the last one</small>"; |
| 68 | + echo '</select>'; |
| 69 | + echo '</p>'; |
| 70 | + } |
| 71 | +} |
| 72 | +function cc_event_card_widget_init() { |
| 73 | + register_widget( 'WP_Widget_Event' ); |
| 74 | +} |
| 75 | + |
| 76 | +add_action( 'widgets_init', 'cc_event_card_widget_init' ); |
0 commit comments