Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active February 5, 2016 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuadavidnelson/5c0e446b69c5321fb040 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/5c0e446b69c5321fb040 to your computer and use it in GitHub Desktop.
Helper functions for compiling date and ical outputs with event calendar
<?php
/**
* Get the "Add to Calendar" button output, used with addevent.com service.
*
* @see https://www.addevent.com/buttons/add-to-calendar
*
* @uses event_is_multiday()
* @uses wp_trim_characters()
*
* @param int $post_id
* @param array $args
*
* @return string|boolean $output The add event output. False on failure.
*/
function get_event_add_to_calendar( $post_id, $args = array() ) {
// Bail if the post id is invalid
if( ! absint( $post_id ) )
return false;
// Get the meta
$all_day_event = esc_attr( get_post_meta( $post_id, 'event_all_day', true ) );
$event_start = intval( get_post_meta( $post_id, 'event_start', true ) );
$event_end = intval( get_post_meta( $post_id, 'event_end', true ) );
$desciption = wp_kses_post( get_post_meta( $post_id, 'event_description', true ) );
// Verify the start and end times are good to go
if( !is_timestamp( $event_start ) || !is_timestamp( $event_end ) )
return false;
// Parse the arguments
$defaults = array(
'host' => '',
'host_email' => '',
'location' => '',
);
$args = wp_parse_args( $args, $defaults );
$date_format = 'd/m/Y';
$time_format = 'h:i A';
// If it's an all day event or if there are issues with the event end meta
if( 'on' == $all_day_event ) {
// Multiple day event
if( event_is_multiday( $post_id ) ) {
$output_start = date( $date_format . ' ' . $time_format, $event_start );
$output_end = date( $date_format . ' ' . $time_format, $event_end );
// Single day event
} else {
$output_start = date( $date_format . ' ', $event_start ) . ' 12:01 AM';
$output_end = date( $date_format . ' ', $event_start ) . ' 23:59 PM';
}
// Not an all-day event
} else {
$output_start = date( $date_format . ' ' . $time_format, $event_start );
$output_end = date( $date_format . ' ' . $time_format, $event_end );
}
// Mandatory: Start and end times
$output = '<span class="start">' . $output_start . '</span>';
$output .= '<span class="end">' . $output_end . '</span>';
// Mandatory: Event Title
$output .= '<span class="title">' . get_the_title( $post_id ) . '</span>';
// Mandatory: date format
$output .= '<span class="date_format">MM/DD/YYYY</span>';
// Mandatory: Timezone
$output .= '<span class="timezone">' . esc_attr( get_option( 'timezone_string' ) ) . '</span>';
// Optional Information
// Description
$output .= !empty( $desciption ) ? '<span class="description">' . wp_trim_characters( wp_strip_all_tags( $desciption ), 500 ) . '</span>' : '';
// host information
$output .= !empty( $args['host'] ) ? '<span class="organizer">' . esc_attr( $args['host'] ) . '</span>' : '';
$output .= !empty( $args['host_email'] ) ? '<span class="organizer_email">' . esc_attr( $args['host_email'] ) . '</span>' : '';
// Event Location
$output .= !empty( $args['location'] ) ? '<span class="location">' . esc_attr( $args['location'] ) . '</span>' : '';
// All day event
$output .= ( 'on' == $all_day_event ) ? '<span class="all_day_event ">true</span>' : '<span class="all_day_event">false</span>';
return '<div title="Add to Calendar" class="addevetatc">Add to Calendar' . $output . '</div>';
}
/**
* Trims a string of words to a specified number of characters, gracefully stopping at white spaces.
* Also strips HTML tags, to prevent breaking in the middle of a tag.
*
* @link http://bavotasan.com/2012/trim-characters-using-php/
*
* @param string $text The string of words to be trimmed.
* @param int $length Maximum number of characters; defaults to 45.
* @param string $append String to append to end, when trimmed; defaults to ellipsis.
*
* @return String of words trimmed at specified character length.
*
* @author c.bavota
*/
if( !function_exists( 'wp_trim_characters' ) ) {
function wp_trim_characters( $text, $length = 45, $append = '&hellip;' ) {
$length = (int) $length;
$text = trim( strip_tags( $text ) );
if ( strlen( $text ) > $length ) {
$text = substr( $text, 0, $length + 1 );
$words = preg_split( "/[\s]|&nbsp;/", $text, -1, PREG_SPLIT_NO_EMPTY );
preg_match( "/[\s]|&nbsp;/", $text, $lastchar, 0, $length );
if ( empty( $lastchar ) )
array_pop( $words );
$text = implode( ' ', $words ) . $append;
}
return $text;
}
}
<?php
/**
* Get the event details for the ical.
*
* @uses event_is_multiday()
* @uses is_timestamp()
*
* @param int $post_id
* @param array $args
*
* @return string $date The date output or empty string.
*/
function get_event_date( $post_id, $args = array() ) {
// Bail if the post id isn't valid
if( ! absint( $post_id ) )
return '';
// Get the post meta.
$all_day_event = esc_attr( get_post_meta( $post_id, 'event_all_day', true ) );
$event_start = intval( get_post_meta( $post_id, 'event_start', true ) );
$event_end = intval( get_post_meta( $post_id, 'event_end', true ) );
// Bail if these aren't valid
if( ! is_timestamp( $event_start ) || ! is_timestamp( $event_end ) )
return '';
// Parse the arguments
$defaults = array(
'date_format' => 'M j, Y',
'time_format' => 'g:i A',
'wrap' => 'div', // the html element wrapping the dates
'date_wrap' => 'span' // the html element wrapping each individual date (start and end)
);
$args = wp_parse_args( $args, $defaults );
// Setup variables
$date_format = esc_attr( $args['date_format'] );
$time_format = esc_attr( $args['time_format'] );
$wrap = empty( $args['wrap'] ) ? 'div' : esc_attr( $args['wrap'] );
$date_wrap = empty( $args['date_wrap'] ) ? 'span' : esc_attr( $args['date_wrap'] );
$multiday = event_is_multiday( $post_id );
$all_day = ( 'on' == $all_day_event ) ? true : false; // setup for CMB2, which gives checkboxes an 'on' value
$print_date_format = $all_day ? $date_format : $date_format . ' ' . $time_format;
// Build the dates
$start = date( $print_date_format, $event_start );
$end = ( $multiday ) ? date( $print_date_format, $event_end ) : date( $time_format, $event_end );
$iso_start = date( 'c', $event_start );
$iso_end = date( 'c', $event_end );
// Add the values to the output
$date = !empty( $start ) ? "<{$date_wrap} class=\"start-date\" itemprop=\"startDate\" content=\"{$iso_start}\">{$start}</{$date_wrap}>" : '';
// only show the end date/time if it's not an all day event, or if it's a multiday event (which could also be an all day event)
if( ! $all_day || $multiday )
$date .= empty( $end ) ?: " - <{$date_wrap} class=\"end-date\" itemprop=\"endDate\" content=\"{$iso_end}\">{$end}</{$date_wrap}>";
// If we have an output, wrap it up
return empty( $date ) ?: "<{$wrap} class=\"event-date\">{$date}</{$wrap}>";
}
/**
* Determine if the event occurs over multiple days.
*
* @uses is_timestamp()
*
* @param int $post_id
*
* @return boolean
*/
function event_is_multiday( $post_id ) {
$start = intval( get_post_meta( $post_id, 'event_start', true ) );
$end = intval( get_post_meta( $post_id, 'event_end', true ) );
if( is_timestamp( $start ) && is_timestamp( $end ) && date( 'Ymd', $start ) < date( 'Ymd', $end ) ) {
return true;
}
return false;
}
/**
* Checks if a string is a valid timestamp.
*
* @link https://gist.github.com/sepehr/6351385
*
* @param string $timestamp Timestamp to validate.
*
* @return bool
*/
function is_timestamp( $timestamp ) {
$check = ( is_int( $timestamp ) OR is_float( $timestamp ) )
? $timestamp
: (string) (int) $timestamp;
return ( $check === $timestamp )
AND ( (int) $timestamp <= PHP_INT_MAX)
AND ( (int) $timestamp >= ~PHP_INT_MAX);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment