forked from stephenharris/Event-Organiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-organiser-venues.php
455 lines (370 loc) · 14.8 KB
/
event-organiser-venues.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/****** VENUE PAGE ******/
if ( !class_exists( 'EventOrganiser_Admin_Page' ) ){
require_once( EVENT_ORGANISER_DIR.'classes/class-eventorganiser-admin-page.php' );
}
$supports = eventorganiser_get_option( 'supports' );
if( !in_array( 'event-venue', $supports ) ){
return;
}
/**
* @ignore
*/
class EventOrganiser_Venues_Page extends EventOrganiser_Admin_Page
{
function set_constants(){
$this->hook = 'edit.php?post_type=event';
$this->title = __( 'Venues', 'eventorganiser' );
$this->menu = __( 'Venues', 'eventorganiser' );
$this->permissions = 'manage_venues';
$this->slug = 'venues';
//Workaround for bug http://core.trac.wordpress.org/ticket/18958
add_filter( 'set-screen-option', array( $this, 'set_per_page' ),10,3 );
}
/*
* Actions to be taken prior to page loading. Hooked on to load-{page}
*/
function page_actions(){
global $EO_Errors;
add_action( 'admin_notices',array( $this, 'admin_notices' ) );
//Determine action if any
$action = $this->current_action();
$venue = ( isset( $_REQUEST['event-venue'] ) ? $_REQUEST['event-venue'] : false );
if ( ( $action && $venue ) || $action == 'add' ):
if ( !current_user_can( 'manage_venues' ) )
wp_die( __( 'You do not have permission to manage venues', 'eventorganiser' ) );
switch( $action ):
case 'update':
if( !check_admin_referer( 'eventorganiser_update_venue_'.$venue ) )
wp_die( __( 'You do not have permission to edit this venue.', 'eventorganiser' ) );
$venue = get_term_by( 'slug', esc_attr( $venue ), 'event-venue' );
$return = eo_update_venue( $venue->term_id, $_POST['eo_venue'] );
if ( is_wp_error( $return ) ){
$EO_Errors->add( 'eo_error', __( 'Venue <strong>was not</strong> updated', 'eventorganiser' ).': '.$return->get_error_message() );
} else{
$term_id = (int) $return['term_id'];
$venue = get_term( $term_id, 'event-venue' );
$url = add_query_arg(
array(
'page' => 'venues',
'action' => 'edit',
'event-venue' => $venue->slug,
'message' => 2,
),
admin_url( 'edit.php?post_type=event' )
);
wp_redirect( $url );
exit();
}
break;
case 'add':
if( !check_admin_referer( 'eventorganiser_add_venue' ) )
wp_die( __( 'You do not have permission to edit this venue.', 'eventorganiser' ) );
$args = $_POST['eo_venue'];
$name = isset( $args['name'] ) ? $args['name'] : '';
//Venue may already exist in database since it may have been added via ajax (by pro plug-in);
if ( !empty( $args['venue_id'] ) && $_venue = eo_get_venue_by( 'id', $args['venue_id'] ) ){
//Since we're updating, need to explicitly provide slug to update it. Slug will be 'new-venue'.
$args['slug'] = sanitize_title( $args['name'] );
$return = eo_update_venue( $_venue->term_id, $args );
} else{
$return = eo_insert_venue( $name, $args );
}
if ( is_wp_error( $return ) ){
$EO_Errors->add( 'eo_error', __( 'Venue <strong>was not</strong> created', 'eventorganiser' ).': '.$return->get_error_message() );
$_REQUEST['action'] = 'create';
} else{
$term_id = (int) $return['term_id'];
$venue = get_term( $term_id, 'event-venue' );
$url = add_query_arg(
array(
'page' => 'venues',
'action' => 'edit',
'event-venue' => $venue->slug,
'message' => 1,
),
admin_url( 'edit.php?post_type=event' )
);
wp_redirect( $url );
exit();
}
break;
case 'delete':
if( is_array( $_REQUEST['event-venue'] ) )
$nonce = 'bulk-venues';
else
$nonce = 'eventorganiser_delete_venue_'.$venue;
if( !check_admin_referer( $nonce ) )
wp_die( __( 'You do not have permission to delete this venue', 'eventorganiser' ) );
$venues = (array) $venue;
//Count the number of deleted venues
$deleted = 0;
foreach ( $venues as $venue ):
$venue = get_term_by( 'slug',esc_attr( $venue ), 'event-venue' );
$resp = eo_delete_venue( $venue->term_id );
if ( !is_wp_error( $resp) && true === $resp ){
$deleted++;
}
endforeach;
if ( $deleted > 0 ){
$url = add_query_arg(
array(
'page' => 'venues',
'message' => 3,
),
admin_url( 'edit.php?post_type=event' )
);
wp_redirect( $url );
exit();
} else{
$EO_Errors = new WP_Error( 'eo_error', __( 'Venue(s) <strong>were not </strong> deleted', 'eventorganiser' ) );
}
break;
endswitch;
endif;
$action = $this->current_action();
if ( in_array( $action, array( 'edit', 'update', 'create' ) ) ){
$venue = ( isset( $_REQUEST['event-venue'] ) ? $_REQUEST['event-venue'] : false );
//Venued edit page
add_meta_box( 'submitdiv', __( 'Save', 'eventorganiser' ), 'eventorganiser_venue_submit', 'event_page_venues', 'side', 'high' );
/**
* Fires after all built-in meta boxes for venues have been added.
*
* @param Object $venue Venue (term object)
*/
do_action( 'add_meta_boxes_event_page_venues', $venue );
/**
* @ignore
*/
do_action( 'add_meta_boxes', 'event_page_venues', $venue );
add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2 ) );
} else{
//Venue admin list
require_once( 'classes/class-eo-venue-list-table.php' );
add_filter( 'manage_event_page_venues_columns', 'eventorganiser_venue_admin_columns' ) ;
add_screen_option( 'per_page', array( 'option' => 'edit_event_venue_per_page', 'label' => __( 'Venues', 'eventorganiser' ), 'default' => 20 ) );
}
}
function admin_notices(){
$m = isset( $_GET['message'] ) ? (int) $_GET['message'] : 0;
$messages = array(
1 => __( 'Venue <strong>created</strong>', 'eventorganiser' ),
2 => __( 'Venue <strong>updated</strong>', 'eventorganiser' ),
3 => __( 'Venue(s) <strong>deleted</strong>', 'eventorganiser' )
);
if( isset( $messages[$m]) )
printf( '<div class="updated"><p>%s</p></div>', $messages[$m] );
}
function page_scripts(){
$action = $this->current_action();
$screen = get_current_screen();
if ( in_array( $action,array( 'create', 'edit', 'add', 'update' ) ) ):
wp_enqueue_script( 'eo_venue' );
wp_localize_script( 'eo_venue', 'EO_Venue', array( 'location' => get_option( 'timezone_string' ), 'draggable' => true, 'screen_id' => $screen->id) );
wp_enqueue_style( 'eventorganiser-style' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'postbox' );
add_thickbox();
endif;
}
function set_per_page( $validated_value, $option, $value ){
//Workaround for bug http://core.trac.wordpress.org/ticket/18958
if ( 'edit_event_venue_per_page' != $option )
return $validated_value;
$value = (int) $value;
if ( $value < 1 || $value > 999 )
return false;
return $value;
}
function display(){
$action = $this->current_action();
$venue = ( isset( $_REQUEST['event-venue'] ) ? $_REQUEST['event-venue'] : false );
?>
<div class="wrap">
<?php screen_icon( 'edit' );?>
<?php
if ( ( ( $action == 'edit' || $action == 'update' ) && $venue ) || $action == 'create' ):
$this->edit_form( $venue );
else:
//Else we are not creating or editing. Display table
$venue_table = new EO_Venue_List_Table();
$venue_table->prepare_items();
//Check if we have searched the venues
$search_term = ( isset( $_REQUEST['s'] ) ? esc_attr( $_REQUEST['s']) : '' );?>
<h2>
<?php _e( 'Venues', 'eventorganiser' );?>
<a href="edit.php?post_type=event&page=venues&action=create" class="add-new-h2"><?php _ex( 'Add New', 'post' ); ?></a>
<?php
if ( $search_term)
printf( '<span class="subtitle">' . __( 'Search results for “%s”' ) . '</span>', $search_term )
?>
</h2>
<form id="eo-venue-table" method="get">
<!-- Ensure that the form posts back to our current page -->
<input type="hidden" name="page" value="venues" />
<input type="hidden" name="post_type" value="event" />
<!-- Now we can render the completed list table -->
<?php $venue_table->search_box( __( 'Search Venues', 'eventorganiser' ), 's' ); ?>
<?php $venue_table->display(); ?>
</form>
<?php endif;?>
</div><!--End .wrap -->
<?php
}
/**
* Display form for creating / editing venues
*
* @since 1.0.0
*/
function edit_form( $venue = false ){
$venue = get_term_by( 'slug', $venue, 'event-venue' );
$term_id = isset( $venue->term_id ) ? (int) $venue->term_id : 0;
$do = ( $this->current_action() == 'edit' ? 'update' : 'add' );
$nonce = ( $do == 'update' ? 'eventorganiser_update_venue_'.$venue->slug : 'eventorganiser_add_venue' );
if ( $this->current_action() == 'edit' ) : ?>
<h2>
<?php _e( 'Edit Venue', 'eventorganiser' ); ?>
<a href="edit.php?post_type=event&page=venues&action=create" class="add-new-h2"><?php _ex( 'Add New', 'post' ); ?></a>
</h2>
<?php else: ?>
<h2>
<?php _e( 'Add New Venue', 'eventorganiser' );?>
</h2>
<?php endif; ?>
<form name="venuedetails" id="eo_venue_form" method="post" action="<?php echo admin_url( 'edit.php?post_type=event&page=venues' ); ?>">
<input type="hidden" name="action" value="<?php echo $do; ?>">
<input type="hidden" id="eo_venue_id" name="eo_venue[venue_id]" value="<?php echo $term_id;?>">
<input type="hidden" name="event-venue" value="<?php echo ( isset( $venue->slug ) ? $venue->slug : '' ) ;?>">
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field( $nonce ); ?>
<?php
//WP3.3-3.3.1 backwards compabt
if( version_compare( get_bloginfo('version'), 3.4 ) < 0 )
$columns = '1';
else
$columns = (1 == get_current_screen()->get_columns() ? '1' : '2' );
?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo $columns;?>">
<div id="post-body-content">
<div id="titlediv"><?php eventorganiser_venue_title( $venue ); ?></div>
<div class="postbox " id="venue_address">
<div class="handlediv" title="Click to toggle"><br></div>
<h3 class="hndle"><span><?php _e( 'Venue Location', 'eventorganiser' );?></span></h3>
<div class="inside"><?php eventorganiser_venue_location( $venue ); ?></div>
</div><!-- .postbox -->
<div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="venue_description postarea">
<?php wp_editor(eo_get_venue_meta($term_id,'_description'), 'content', array( 'textarea_name' => 'eo_venue[description]', 'dfw' => false, 'tabindex' => 7 ) ); ?>
</div>
</div><!-- #post-body-content -->
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes( '', 'side', $venue ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes( '', 'normal', $venue ); ?>
<?php do_meta_boxes( '', 'advanced', $venue ); ?>
</div>
</div><!-- #post-body -->
<br class="clear">
</div><!-- #poststuff -->
</form>
<?php
}
}
$venues_page = new EventOrganiser_Venues_Page();
//Helper function to get the 'structure' of the event-venue taxonomy
function eo_get_venue_permastructure(){
global $wp_rewrite;
$termlink = $wp_rewrite->get_extra_permastruct( 'event-venue' );
if ( empty( $termlink ) ){
$t = get_taxonomy( 'event-venue' );
$termlink = "?$t->query_var=";
} else{
$termlink = preg_replace( '/%event-venue%/', '', $termlink );
}
$termlink = home_url( $termlink );
return $termlink;
}
//Submit metabox
function eventorganiser_venue_submit( $venue ){
$value = $venue ? __( 'Update Venue', 'eventorganiser' ) : __( 'Add Venue', 'eventorganiser' ); ?>
<div id="minor-publishing-actions">
<div id="save-action">
<p>
<input type="submit" class="button button-primary" id="save-venue" name="eo_venue[Submit]" value="<?php echo esc_attr( $value ); ?>" tabindex="10" />
</p>
</div>
<div class="clear"></div>
</div>
<?php
}
//Location metabox - called directly. Is not movable.
function eventorganiser_venue_location( $venue ){
$term_id = isset( $venue->term_id ) ? (int) $venue->term_id : 0;
$address = eo_get_venue_address( $term_id );?>
<table class ="address">
<tbody>
<?php
$address_fields = _eventorganiser_get_venue_address_fields();
$tabindex = 2;
foreach ( $address_fields as $key => $label ){
//Keys are prefixed by '_'.
$key = trim( $key, '_' );
printf(
'<tr>
<th><label>%1$s:</label></th>
<td><input type="text" name="eo_venue[%2$s]" class="eo_addressInput" id="eo-venue-%2$s" value="%3$s" tabindex="%4$s"/></td>
</tr>',
$label,
esc_attr( $key ),
esc_attr( $address[$key] ),
$tabindex++
);
}
?>
</tbody>
</table>
<div id="venuemap" class="gmap3"></div>
<div class="clear"></div>
<input type="hidden" name="eo_venue[latitude]" id="eo_venue_Lat" value="<?php echo esc_attr( eo_get_venue_lat( $term_id ) ); ?>"/>
<input type="hidden" name="eo_venue[longtitude]" id="eo_venue_Lng" value="<?php echo esc_attr( eo_get_venue_lng( $term_id ) ); ?>"/>
<?php
}
//Venue title input
function eventorganiser_venue_title( $venue ){
$term_id = isset( $venue->term_id) ? (int) $venue->term_id : 0;
$name = isset( $venue->name ) ? $venue->name : ''; ?>
<div id="titlewrap">
<input type="text" placeholder="<?php esc_attr_e( 'Venue name', 'eventorganiser' );?>" autocomplete="off" id="title" value="<?php echo esc_attr( $name );?>" tabindex="1" size="30" name="eo_venue[name]">
</div>
<div class="inside">
<div id="edit-slug-box">
<?php if ( $venue ): ?>
<strong><?php _e( 'Permalink:' );?></strong>
<span id="sample-permalink">
<?php echo eo_get_venue_permastructure();?>
<input type="text" name="eo_venue[slug]"value="<?php echo ( isset( $venue->slug ) ? esc_attr( $venue->slug ) : '' ) ;?>" id="<?php echo $term_id; ?>-slug">
</span>
<input type="hidden" value="<?php echo get_term_link( $venue, 'event-venue' ); ?>" id="shortlink">
<a onclick="prompt( 'URL:', jQuery( '#shortlink' ).val() ); return false;" class="button" href=""><?php _e( 'Get Link', 'eventorganiser' );?></a>
<span id='view-post-btn'><a href="<?php echo get_term_link( $venue, 'event-venue' ); ?>" class='button' target='_blank'><?php _e( 'View Venue', 'eventorganiser' );?></a></span>
<?php endif;?>
</div><!-- #edit-slug-box -->
</div> <!-- .inside -->
<?php
}
function eventorganiser_venue_admin_columns( $columns ){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'name' => __( 'Venue', 'eventorganiser' ),
);
$address_columns = _eventorganiser_get_venue_address_fields();
foreach( $address_columns as $key => $label )
$columns['venue'.$key] = $label;
$columns += array(
'venue_slug' => __( 'Slug' ),
'posts' => __( 'Events', 'eventorganiser' ),
);
return $columns;
}