This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuf-events-post-types.php
167 lines (146 loc) · 6.09 KB
/
neuf-events-post-types.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Registers the event post type.
*
* This post type will be used to store events to our system. With events, we mean such events as concerts, movie screenings, parties and so on (as opposed to MouseOverEvents and the like).
*/
/* Create the fields the post type should have */
function neuf_events_post_type() {
$labels = array(
'name' => __( 'Events', 'neuf_event'),
'singular_name' => __( 'Event', 'neuf_event'),
'add_new' => __( 'Add New', 'neuf_event'),
'add_new_item' => __( 'Add New', 'neuf_event'),
'edit_item' => __( 'Edit Event', 'neuf_event'),
'new_item' => __( 'Add New Event', 'neuf_event'),
'view_item' => __( 'View Event', 'neuf_event'),
'search_items' => __( 'Search Events', 'neuf_event'),
'not_found' => __( 'No events found', 'neuf_event'),
'not_found_in_trash' => __( 'No events found in trash', 'neuf_event')
);
register_post_type(
'event',
array(
'labels' => $labels,
'menu_position' => 5,
'public' => true,
'publicly_queryable' => true,
'query_var' => 'event',
'show_ui' => true,
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments',
'revisions',
'administrator',
'custom-fields'
),
'register_meta_box_cb' => 'add_events_metaboxes',
'rewrite' => array(
'slug' => __( 'event', 'neuf_event' ),
),
)
);
}
/* When the post is saved, save our custom data */
function neuf_events_save_post( $post_id, $post ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !array_key_exists('neuf_events_nonce', $_POST) || !wp_verify_nonce( $_POST['neuf_events_nonce'], 'neuf_events_nonce' )) {
return $post_id;
}
// If this is an auto save routine, our form has not been submitted,
// and we do nothing.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
// Check permissions
if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
// Date strings are converted to unix time
$tosave['_neuf_events_starttime'] = strtotime( $_POST['_neuf_events_starttime'] );
$tosave['_neuf_events_endtime'] = strtotime( $_POST['_neuf_events_endtime'] );
$tosave['_neuf_events_price_regular'] = $_POST['_neuf_events_price_regular'];
$tosave['_neuf_events_price_member'] = $_POST['_neuf_events_price_member'];
$tosave['_neuf_events_bs_url'] = $_POST['_neuf_events_bs_url'];
$tosave['_neuf_events_fb_url'] = $_POST['_neuf_events_fb_url'];
$tosave['_neuf_events_venue'] = $_POST['_neuf_events_venue'];
$tosave['_neuf_events_promo_period'] = $_POST['_neuf_events_promo_period'];
// Update or add post meta
foreach($tosave as $key=>$value)
if(!update_post_meta($post_id, $key, $value)) {
add_post_meta($post_id, $key, $value, true);
}
return $post_id;
}
/**
* Set post variables.
*
* Add some handy variables to the global $post. Added to the 'the_post' action hook.
*/
function neuf_events_the_post( &$post ) {
// Only apply to events
if ( 'event' != get_post_type() )
return;
$post->neuf_events_venue = get_post_meta( get_the_ID() , '_neuf_events_venue' , true );
$post->neuf_events_fb_url = get_post_meta( get_the_ID() , '_neuf_events_fb_url' , true );
$post->neuf_events_ticket_url = get_post_meta( get_the_ID() , '_neuf_events_bs_url' , true );
$post->neuf_events_price_regular = get_post_meta( get_the_ID() , '_neuf_events_price_regular' , true );
$post->neuf_events_price_member = get_post_meta( get_the_ID() , '_neuf_events_price_member' , true );
$post->neuf_events_starttime = get_post_meta( get_the_ID() , '_neuf_events_starttime' , true );
$post->neuf_events_endtime = get_post_meta( get_the_ID() , '_neuf_events_endtime' , true );
$post->neuf_events_endtime = $post->neuf_events_endtime ? $post->neuf_events_endtime : $post->neuf_events_starttime + 7200; // No endtime? Assume 2 hours
$post->neuf_events_gcal_url = "http://www.google.com/calendar/event?action=TEMPLATE";
$post->neuf_events_gcal_url .= "&text=" . rawurlencode(get_the_title());
$post->neuf_events_gcal_url .= "&details=" . rawurlencode(get_the_excerpt());
$post->neuf_events_gcal_url .= "&location=Det%20Norske%20Studentersamfund,%20Slemdalsveien%2015,%20Oslo";
$post->neuf_events_gcal_url .= "&trp=true";
$post->neuf_events_gcal_url .= "&sprop=website:" . rawurlencode(get_permalink());
$post->neuf_events_gcal_url .= "&sprop=name:Det%20Norske%20Studentersamfund";
$post->neuf_events_gcal_url .= "&dates=" . date( 'Ymd\THis\Z' , $post->neuf_events_starttime - ( get_option('gmt_offset') * 3600 ) );
$post->neuf_events_gcal_url .= "/" . date( 'Ymd\THis\Z' , $post->neuf_events_starttime - ( get_option('gmt_offset') * 3600 ) );
}
/** Sample table view of events */
function neuf_events_program() {
global $post, $wp_locale;
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => '_neuf_events_starttime',
'orderby' => 'meta_value',
'order' => 'ASC'
) );
if ( $events->have_posts() ) :
$date = "";
ob_start();
echo '<table class="event-table">';
while ( $events->have_posts() ) : $events->the_post();
$venue = get_post_meta( $post->ID, '_neuf_events_venue', true);
$price_regular = get_post_meta( $post->ID, '_neuf_events_price_regular', true);
$price_member = get_post_meta( $post->ID, '_neuf_events_price_member', true);
$time = get_post_meta( $post->ID, '_neuf_events_starttime', true);
?>
<tr>
<td class="day">
<?php echo date('l d. F', $time); ?>
</td>
<td class="time">
kl <?php echo date("H.i", $time); ?>
</td>
<td class="title">
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</td>
<td class="place">
<?php echo $venue;?>
</td>
</tr>
<?php
endwhile;
echo '</table><!-- .event-table -->';
endif;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
?>