| @@ -1,0 +1,70 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Display a list of upcoming events from a calendar. | |
| 5 | + * | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit( 0 ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Register a upcomingevents shortcode. | |
| 15 | + * Most of the heavy lifting done in iCalendarReader class, | |
| 16 | + * where the icalendar_render_events() function controls the display. | |
| 17 | + */ | |
| 18 | +class Upcoming_Events_Shortcode { | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Register things. | |
| 22 | + */ | |
| 23 | + public static function init() { | |
| 24 | + add_shortcode( 'upcomingevents', array( __CLASS__, 'shortcode' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Register the shortcode. | |
| 29 | + * | |
| 30 | + * @param array $atts Shortcode attributes. | |
| 31 | + */ | |
| 32 | + public static function shortcode( $atts = array() ) { | |
| 33 | + require_once JETPACK__PLUGIN_DIR . '/_inc/lib/icalendar-reader.php'; | |
| 34 | + $atts = shortcode_atts( | |
| 35 | + array( | |
| 36 | + 'url' => '', | |
| 37 | + 'number' => 0, | |
| 38 | + ), | |
| 39 | + $atts, | |
| 40 | + 'upcomingevents' | |
| 41 | + ); | |
| 42 | + $args = array( | |
| 43 | + 'context' => 'shortcode', | |
| 44 | + 'number' => absint( $atts['number'] ), | |
| 45 | + ); | |
| 46 | + | |
| 47 | + if ( empty( $atts['url'] ) ) { | |
| 48 | + // If the current user can access the Appearance->Widgets page. | |
| 49 | + if ( current_user_can( 'edit_theme_options' ) ) { | |
| 50 | + return sprintf( '<p>%s</p>', __( 'You must specify a URL to an iCalendar feed in the shortcode. This notice is only displayed to administrators.', 'jetpack' ) ); | |
| 51 | + } | |
| 52 | + return self::no_upcoming_event_text(); | |
| 53 | + } | |
| 54 | + $events = icalendar_render_events( $atts['url'], $args ); | |
| 55 | + | |
| 56 | + if ( ! $events ) { | |
| 57 | + $events = self::no_upcoming_event_text(); | |
| 58 | + } | |
| 59 | + | |
| 60 | + return $events; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Returns No Upcoming Event text. | |
| 65 | + */ | |
| 66 | + private static function no_upcoming_event_text() { | |
| 67 | + return sprintf( '<p>%s</p>', __( 'No upcoming events', 'jetpack' ) ); | |
| 68 | + } | |
| 69 | +} | |
| 70 | +add_action( 'after_setup_theme', array( 'Upcoming_Events_Shortcode', 'init' ), 2 ); | |