summaryrefslogtreecommitdiff
blob: f78bf9d1480928e611239504ad871467673d430b (plain)
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
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

/**
 * Display a list of upcoming events from a calendar.
 *
 * @package automattic/jetpack
 */

/**
 * Register a upcomingevents shortcode.
 * Most of the heavy lifting done in iCalendarReader class,
 * where the icalendar_render_events() function controls the display.
 */
class Upcoming_Events_Shortcode {

	/**
	 * Register things.
	 */
	public static function init() {
		add_shortcode( 'upcomingevents', array( __CLASS__, 'shortcode' ) );
	}

	/**
	 * Register the shortcode.
	 *
	 * @param array $atts Shortcode attributes.
	 */
	public static function shortcode( $atts = array() ) {
		jetpack_require_lib( 'icalendar-reader' );
		$atts   = shortcode_atts(
			array(
				'url'    => '',
				'number' => 0,
			),
			$atts,
			'upcomingevents'
		);
		$args   = array(
			'context' => 'shortcode',
			'number'  => absint( $atts['number'] ),
		);
		$events = icalendar_render_events( $atts['url'], $args );

		if ( ! $events ) {
			$events = sprintf( '<p>%s</p>', __( 'No upcoming events', 'jetpack' ) );
		}

		return $events;
	}
}
add_action( 'plugins_loaded', array( 'Upcoming_Events_Shortcode', 'init' ), 101 );