暂无描述

upcoming-events.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Display a list of upcoming events from a calendar.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. /**
  8. * Register a upcomingevents shortcode.
  9. * Most of the heavy lifting done in iCalendarReader class,
  10. * where the icalendar_render_events() function controls the display.
  11. */
  12. class Upcoming_Events_Shortcode {
  13. /**
  14. * Register things.
  15. */
  16. public static function init() {
  17. add_shortcode( 'upcomingevents', array( __CLASS__, 'shortcode' ) );
  18. }
  19. /**
  20. * Register the shortcode.
  21. *
  22. * @param array $atts Shortcode attributes.
  23. */
  24. public static function shortcode( $atts = array() ) {
  25. jetpack_require_lib( 'icalendar-reader' );
  26. $atts = shortcode_atts(
  27. array(
  28. 'url' => '',
  29. 'number' => 0,
  30. ),
  31. $atts,
  32. 'upcomingevents'
  33. );
  34. $args = array(
  35. 'context' => 'shortcode',
  36. 'number' => absint( $atts['number'] ),
  37. );
  38. $events = icalendar_render_events( $atts['url'], $args );
  39. if ( ! $events ) {
  40. $events = sprintf( '<p>%s</p>', __( 'No upcoming events', 'jetpack' ) );
  41. }
  42. return $events;
  43. }
  44. }
  45. add_action( 'plugins_loaded', array( 'Upcoming_Events_Shortcode', 'init' ), 101 );