Skip to content

Instantly share code, notes, and snippets.

@rosshanney
Created October 17, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rosshanney/3908350 to your computer and use it in GitHub Desktop.
Save rosshanney/3908350 to your computer and use it in GitHub Desktop.
Adds year / month params to [google-calendar-events] shortcode
//Handles the shortcode stuff
function shortcode_handler( $atts ) {
$options = get_option( GCE_OPTIONS_NAME );
//Check that any feeds have been added
if ( is_array( $options ) && ! empty( $options ) ) {
extract( shortcode_atts( array(
'id' => '',
'type' => 'grid',
'title' => null,
'max' => 0,
'order' => 'asc',
'year' => '',
'month' => ''
), $atts ) );
$no_feeds_exist = true;
$feed_ids = array();
if ( '' != $id ) {
//Break comma delimited list of feed ids into array
$feed_ids = explode( ',', str_replace( ' ', '', $id ) );
//Check each id is an integer, if not, remove it from the array
foreach ( $feed_ids as $key => $feed_id ) {
if ( 0 == absint( $feed_id ) )
unset( $feed_ids[$key] );
}
//If at least one of the feed ids entered exists, set no_feeds_exist to false
foreach ( $feed_ids as $feed_id ) {
if ( isset($options[$feed_id] ) )
$no_feeds_exist = false;
}
} else {
foreach ( $options as $feed ) {
$feed_ids[] = $feed['id'];
}
$no_feeds_exist = false;
}
//Ensure max events is a positive integer
$max_events = absint( $max );
//Ensure sort order is asc or desc
$sort_order = ( 'desc' == $order ) ? 'desc' : 'asc';
$y = ( '' == $year ) ? null : absint( $year );
$m = ( '' == $month ) ? null : absint( $month );
//Check that at least one valid feed id has been entered
if ( empty( $feed_ids ) || $no_feeds_exist ) {
return __( 'No valid Feed IDs have been entered for this shortcode. Please check that you have entered the IDs correctly and that the Feeds have not been deleted.', GCE_TEXT_DOMAIN );
} else {
//Turns feed_ids back into string of feed ids delimited by '-' ('1-2-3-4' for example)
$feed_ids = implode( '-', $feed_ids );
//If title has been omitted from shortcode, set title_text to null, otherwise set to title (even if empty string)
$title_text = ( false === $title ) ? null : $title;
switch ( $type ) {
case 'grid':
return gce_print_grid( $feed_ids, $title_text, $max_events, false, $m, $y );
case 'ajax':
return gce_print_grid( $feed_ids, $title_text, $max_events, true, $m, $y );
case 'list':
return gce_print_list( $feed_ids, $title_text, $max_events, $sort_order );
case 'list-grouped':
return gce_print_list( $feed_ids, $title_text, $max_events, $sort_order, true );
}
}
} else {
return __( 'No feeds have been added yet. You can add a feed in the Google Calendar Events settings.', GCE_TEXT_DOMAIN );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment