-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpt-events.php
executable file
·302 lines (238 loc) · 10.4 KB
/
cpt-events.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
// 0. Base
add_action('admin_init', 'tf_functions_css');
function tf_functions_css() {
wp_enqueue_style('tf-functions-css', get_bloginfo('template_directory') . '/css/tf-functions.css');
}
// 1. Custom Post Type Registration (Events)
add_action( 'init', 'create_event_postype' );
function create_event_postype() {
$labels = array(
'name' => _x('Eventos', 'post type general name'),
'singular_name' => _x('Evento', 'post type singular name'),
'add_new' => _x('Adicionar evento', 'events'),
'add_new_item' => __('Adicionar novo evento'),
'edit_item' => __('Editar evento'),
'new_item' => __('Novo evento'),
'view_item' => __('Ver evento'),
'search_items' => __('Procurar eventos'),
'not_found' => __('Eventos não encontrados'),
'not_found_in_trash' => __('Nada encontrado no lixo'),
'parent_item_colon' => '',
);
$args = array(
'label' => __('Events'),
'labels' => $labels,
'public' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d', // ?
'capability_type' => 'post',
'menu_icon' => 'dashicons-calendar-alt',
'hierarchical' => false,
'rewrite' => array( "slug" => "events" ),
'supports'=> array('title', 'thumbnail', 'excerpt', 'editor') ,
'show_in_nav_menus' => true,
'taxonomies' => array( 'tf_eventcategory', 'post_tag')
);
register_post_type( 'tf_events', $args);
}
// 2. Custom Taxonomy Registration (Event Types)
function create_eventcategory_taxonomy() {
$labels = array(
'name' => _x( 'Categorias', 'taxonomy general name' ),
'singular_name' => _x( 'Categoria', 'taxonomy singular name' ),
'search_items' => __( 'Procurar categorias' ),
'popular_items' => __( 'Categorias populares' ),
'all_items' => __( 'Todas categorias' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Editar categorias' ),
'update_item' => __( 'Atualizar categorias' ),
'add_new_item' => __( 'Adicionar nova categoria' ),
'new_item_name' => __( 'Nome nova categoria' ),
'separate_items_with_commas' => __( 'Separate categorias por virgulas' ),
'add_or_remove_items' => __( 'Adicionar ou remover categorias' ),
'choose_from_most_used' => __( 'Escolher entre as categorias mais utilizadas' ),
);
register_taxonomy('tf_eventcategory','tf_events', array(
'label' => __('Evento Categoria'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'event-category' ),
));
}
add_action( 'init', 'create_eventcategory_taxonomy', 0 );
// 3. Show Columns
add_filter ("manage_edit-tf_events_columns", "tf_events_edit_columns");
add_action ("manage_posts_custom_column", "tf_events_custom_columns");
function tf_events_edit_columns($columns) {
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"tf_col_ev_cat" => "Categoria",
"tf_col_ev_date" => "Datas",
"tf_col_ev_times" => "Horário",
"tf_col_ev_thumb" => "Thumbnail",
"title" => "Evento",
"tf_col_ev_desc" => "Descrição",
);
return $columns;
}
function tf_events_custom_columns($column) {
global $post;
$custom = get_post_custom();
switch ($column)
{
case "tf_col_ev_cat":
// - show taxonomy terms -
$eventcats = get_the_terms($post->ID, "tf_eventcategory");
$eventcats_html = array();
if ($eventcats) {
foreach ($eventcats as $eventcat)
array_push($eventcats_html, $eventcat->name);
echo implode($eventcats_html, ", ");
} else {
_e('None', 'themeforce');;
}
break;
case "tf_col_ev_date":
// - show dates -
$startd = $custom["tf_events_startdate"][0];
$endd = $custom["tf_events_enddate"][0];
$startdate = date("F j, Y", $startd);
$enddate = date("F j, Y", $endd);
echo $startdate . '<br /><em>' . $enddate . '</em>';
break;
case "tf_col_ev_times":
// - show times -
$startt = $custom["tf_events_startdate"][0];
$endt = $custom["tf_events_enddate"][0];
$time_format = get_option('time_format');
$starttime = date($time_format, $startt);
$endtime = date($time_format, $endt);
echo $starttime . ' - ' .$endtime;
break;
case "tf_col_ev_thumb":
// - show thumb -
$post_image_id = get_post_thumbnail_id(get_the_ID());
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
echo '<img src="';
echo bloginfo('template_url');
echo '/timthumb/timthumb.php?src=';
echo $thumbnail;
echo '&h=60&w=60&zc=1" alt="" />';
}
break;
case "tf_col_ev_desc";
the_excerpt();
break;
}
}
// 4. Show Meta-Box
add_action( 'admin_init', 'tf_events_create' );
function tf_events_create() {
add_meta_box('tf_events_meta', 'Events', 'tf_events_meta', 'tf_events');
}
function tf_events_meta () {
// - grab data -
global $post;
$custom = get_post_custom($post->ID);
$meta_sd = $custom["tf_events_startdate"][0];
$meta_ed = $custom["tf_events_enddate"][0];
$meta_st = $meta_sd;
$meta_et = $meta_ed;
// - grab wp time format -
$date_format = get_option('date_format'); // Not required in my code
$time_format = get_option('time_format');
// - populate today if empty, 00:00 for time -
if ($meta_sd == null) { $meta_sd = time(); $meta_ed = $meta_sd; $meta_st = 0; $meta_et = 0;}
// - convert to pretty formats -
$clean_sd = date("D, M d, Y", $meta_sd);
$clean_ed = date("D, M d, Y", $meta_ed);
$clean_st = date($time_format, $meta_st);
$clean_et = date($time_format, $meta_et);
// - security -
echo '<input type="hidden" name="tf-events-nonce" id="tf-events-nonce" value="' .
wp_create_nonce( 'tf-events-nonce' ) . '" />';
// - output -
?>
<div class="tf-meta">
<ul>
<li><label>Start Date</label><input name="tf_events_startdate" class="tfdate" value="<?php echo $clean_sd; ?>" /></li>
<li><label>Start Time</label><input name="tf_events_starttime" value="<?php echo $clean_st; ?>" /><em>Usar o formato de 24h (7pm = 19:00)</em></li>
<li><label>End Date</label><input name="tf_events_enddate" class="tfdate" value="<?php echo $clean_ed; ?>" /></li>
<li><label>End Time</label><input name="tf_events_endtime" value="<?php echo $clean_et; ?>" /><em>Usar o formato de 24h (7pm = 19:00)</em></li>
</ul>
</div>
<?php
}
// 5. Save Data
add_action ('save_post', 'save_tf_events');
function save_tf_events(){
global $post;
// - still require nonce
if ( !wp_verify_nonce( $_POST['tf-events-nonce'], 'tf-events-nonce' )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// - convert back to unix & update post
if(!isset($_POST["tf_events_startdate"])):
return $post;
endif;
$updatestartd = strtotime ( $_POST["tf_events_startdate"] . $_POST["tf_events_starttime"] );
update_post_meta($post->ID, "tf_events_startdate", $updatestartd );
if(!isset($_POST["tf_events_enddate"])):
return $post;
endif;
$updateendd = strtotime ( $_POST["tf_events_enddate"] . $_POST["tf_events_endtime"]);
update_post_meta($post->ID, "tf_events_enddate", $updateendd );
}
// 6. Customize Update Messages
add_filter('post_updated_messages', 'events_updated_messages');
function events_updated_messages( $messages ) {
global $post, $post_ID;
$messages['tf_events'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('Evento Atualizado. <a href="%s">View item</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Campo Atualizado.'),
3 => __('Campo Atualizado.'),
4 => __('Evento Atualizado.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Evento restaurado para revisão por: %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Evento publicado. <a href="%s">View event</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Evento salvo.'),
8 => sprintf( __('Evento Criado. <a target="_blank" href="%s">Preview event</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Evento programado para: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Ver previa</a>'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Event draft updated. <a target="_blank" href="%s">Preview event</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
// 7. JS Datepicker UI
function events_styles() {
global $post_type;
if( 'tf_events' != $post_type )
return;
wp_enqueue_style('ui-datepicker', get_bloginfo('template_url') . '/css/jquery-ui-1.8.9.custom.css');
}
function events_scripts() {
global $post_type;
if( 'tf_events' != $post_type )
return;
wp_enqueue_script('jquery-ui', get_bloginfo('template_url') . '/js/jquery-ui-1.8.9.custom.min.js', array('jquery'));
wp_enqueue_script('ui-datepicker', get_bloginfo('template_url') . '/js/jquery.ui.datepicker.min.js');
wp_enqueue_script('custom_script', get_bloginfo('template_url').'/js/pubforce-admin.js', array('jquery'));
}
add_action( 'admin_print_styles-post.php', 'events_styles', 1000 );
add_action( 'admin_print_styles-post-new.php', 'events_styles', 1000 );
add_action( 'admin_print_scripts-post.php', 'events_scripts', 1000 );
add_action( 'admin_print_scripts-post-new.php', 'events_scripts', 1000 );
}
?>