Skip to content

Commit 31945c3

Browse files
committed
add event widget
1 parent 876485e commit 31945c3

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

inc/custom-post-types/queulat-cc-events-cpt-plugin/class-cc-events-post-metabox.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Queulat\Forms\Element\Input_Url;
66
use Queulat\Forms\Element\WP_Editor;
77
use Queulat\Forms\Element\Input_Text;
8-
8+
use Queulat\Forms\Element\Select;
99

1010
class Event_Metabox extends Metabox
1111
{
@@ -26,9 +26,39 @@ public function data_updated($data, $post_id)
2626
}
2727
return true;
2828
}
29+
public function get_site_courses() {
30+
$courses = new WP_Query( array(
31+
'post_type' => 'cc_course',
32+
'post_status' => 'publish',
33+
'posts_per_page' => -1
34+
) );
35+
$return_array = array( '' => 'Select a Course');
36+
if ( $courses->have_posts() ) {
37+
foreach ($courses->posts as $course) {
38+
$return_array[$course->ID] = get_the_title( $course->ID );
39+
}
40+
} else {
41+
return false;
42+
}
43+
return $return_array;
44+
}
2945
public function get_fields(): array
3046
{
3147
return [
48+
Node_Factory::make(
49+
Select::class,
50+
[
51+
'name' => 'related_course',
52+
'label' => 'Related courses',
53+
'attributes' => [
54+
'class' => 'widefat'
55+
],
56+
'properties' => [
57+
'description' => 'If this event is related to a course please select'
58+
],
59+
'options' => $this->get_site_courses()
60+
]
61+
),
3262
Node_Factory::make(
3363
Input_Text::class,
3464
[
@@ -133,5 +163,5 @@ public function sanitize_data(array $data): array
133163
}
134164
}
135165

136-
new Event_Metabox('event', 'Event related data', 'ccgnevents', ['context' => 'normal']);
166+
new Event_Metabox('event', 'Event related data', 'cc_events', ['context' => 'normal']);
137167

inc/widgets.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
* CERTIFICATE WIDGETS
44
*/
55

6-
require STYLESHEETPATH . '/inc/widgets/cc-course-card.php';
6+
require STYLESHEETPATH . '/inc/widgets/cc-course-card.php';
7+
require STYLESHEETPATH . '/inc/widgets/cc-event-card.php';

inc/widgets/cc-event-card.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)