| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if ( ! function_exists( 'sc_snippets_get_upcoming_events' ) ) :
|
| function sc_snippets_get_upcoming_events( $number = 5, $calendar_ids = array(), $order = 'asc' ) {
|
|
|
| if ( ! class_exists( '\Sugar_Calendar\Helpers' )
|
| || ! method_exists( '\Sugar_Calendar\Helpers', 'get_upcoming_events_list_with_recurring' )
|
| ) {
|
| return array();
|
| }
|
|
|
| $number = (int) $number;
|
|
|
| if ( $number < 1 ) {
|
| $number = 5;
|
| }
|
|
|
| $events = \Sugar_Calendar\Helpers::get_upcoming_events_list_with_recurring(
|
| array(
|
| 'number' => $number,
|
| 'calendar_ids' => $calendar_ids,
|
| 'event_order' => ( strtolower( $order ) === 'desc' ) ? 'desc' : 'asc',
|
| ),
|
| array()
|
| );
|
|
|
|
|
| return array_slice( $events, 0, $number );
|
| }
|
| endif;
|
|
|
|
|
|
|
|
|
| add_shortcode( 'sc_upcoming_events', function ( $atts ) {
|
|
|
| $atts = shortcode_atts(
|
| array(
|
| 'number' => 5,
|
| 'calendars' => '',
|
| 'order' => 'asc',
|
| 'separator' => ' · ',
|
| 'venue' => 'no',
|
| 'wrap' => 'li',
|
| ),
|
| $atts,
|
| 'sc_upcoming_events'
|
| );
|
|
|
| $calendar_ids = array_filter(
|
| array_map( 'absint', explode( ',', (string) $atts['calendars'] ) )
|
| );
|
|
|
| $events = sc_snippets_get_upcoming_events( $atts['number'], $calendar_ids, $atts['order'] );
|
|
|
| if ( empty( $events ) ) {
|
| return '';
|
| }
|
|
|
|
|
| $tz = wp_timezone();
|
| $date_format = get_option( 'date_format' );
|
| $time_format = get_option( 'time_format' );
|
| $separator = $atts['separator'];
|
|
|
|
|
| $tag = preg_replace( '/[^a-z0-9]/', '', strtolower( (string) $atts['wrap'] ) );
|
|
|
| if ( ! in_array( $tag, array( '', 'li', 'div', 'span', 'p' ), true ) ) {
|
| $tag = 'li';
|
| }
|
|
|
| $out = '';
|
|
|
| foreach ( $events as $event ) {
|
|
|
| $parts = array();
|
|
|
|
|
| $parts[] = esc_html( $event->start_date( $date_format, $tz ) );
|
|
|
|
|
| if ( $event->is_all_day() ) {
|
| $parts[] = esc_html__( 'All-day', 'sugar-snippets' );
|
| } else {
|
| $parts[] = esc_html( $event->start_date( $time_format, $tz ) );
|
| }
|
|
|
|
|
| $parts[] = esc_html( get_the_title( $event->object_id ) );
|
|
|
|
|
| if ( strtolower( $atts['venue'] ) === 'yes' && ! empty( $event->venue_id ) ) {
|
| $venue_name = get_the_title( $event->venue_id );
|
|
|
| if ( $venue_name !== '' ) {
|
| $parts[] = esc_html( $venue_name );
|
| }
|
| }
|
|
|
| $line = implode( $separator, $parts );
|
|
|
| if ( $tag !== '' ) {
|
| $out .= '<' . $tag . ' class="sc-upcoming-event">' . $line . '</' . $tag . '>';
|
| } else {
|
| $out .= $line . '<br />';
|
| }
|
| }
|
|
|
| return $out;
|
| } );
|
| |
| |
Comments