Created
February 1, 2013 20:38
-
-
Save rosshanney/4693976 to your computer and use it in GitHub Desktop.
Add month and year parameters to GCE shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin name: Add month and year parameters to GCE shortcode | |
*/ | |
function gce_shortcode_month_year( $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 ); | |
} | |
} | |
function gce_add_updated_shortcode() { | |
add_shortcode( 'google-calendar-events', 'gce_shortcode_month_year' ); | |
} | |
add_action( 'init', 'gce_add_updated_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment