upcoming-events.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class Jetpack_Upcoming_Events_Widget extends WP_Widget {
  3. function __construct() {
  4. parent::__construct(
  5. 'upcoming_events_widget',
  6. /** This filter is documented in modules/widgets/facebook-likebox.php */
  7. apply_filters( 'jetpack_widget_name', __( 'Upcoming Events', 'jetpack' ) ),
  8. array(
  9. 'description' => __( 'Display upcoming events from an iCalendar feed.', 'jetpack' ),
  10. 'customize_selective_refresh' => true,
  11. )
  12. );
  13. if ( is_active_widget( false, false, $this->id_base ) ) {
  14. add_action( 'wp_head', array( $this, 'css' ) );
  15. }
  16. }
  17. function css() {
  18. ?>
  19. <style type="text/css">
  20. .upcoming-events li {
  21. margin-bottom: 10px;
  22. }
  23. .upcoming-events li span {
  24. display: block;
  25. }
  26. </style>
  27. <?php
  28. }
  29. function form( $instance ) {
  30. $defaults = array(
  31. 'title' => __( 'Upcoming Events', 'jetpack' ),
  32. 'feed-url' => '',
  33. 'count' => 3
  34. );
  35. $instance = array_merge( $defaults, (array) $instance );
  36. ?>
  37. <p>
  38. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label>
  39. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  40. </p>
  41. <p>
  42. <label for="<?php echo $this->get_field_id( 'feed-url' ); ?>"><?php _e( 'iCalendar Feed URL:', 'jetpack' ); ?></label>
  43. <input class="widefat" id="<?php echo $this->get_field_id( 'feed-url' ); ?>" name="<?php echo $this->get_field_name( 'feed-url' ); ?>" type="text" value="<?php echo esc_attr( $instance['feed-url'] ); ?>" />
  44. </p>
  45. <p>
  46. <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Items to show:', 'jetpack' ); ?></label>
  47. <select id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>">
  48. <?php $i = 1;
  49. while ( $i <= 10 ) { ?>
  50. <option <?php selected( $instance['count'], $i ) ?>><?php echo $i; ?></option>
  51. <?php $i++; } ?>
  52. <option value="0" <?php selected( $instance['count'], 0 ) ?>><?php _e( 'All' , 'jetpack' ) ?></option>
  53. </select>
  54. </p>
  55. <?php
  56. }
  57. function update( $new_instance, $old_instance ) {
  58. $instance['title'] = strip_tags( $new_instance['title'] );
  59. $instance['feed-url'] = strip_tags( $new_instance['feed-url'] );
  60. $instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less
  61. return $instance;
  62. }
  63. function widget( $args, $instance ) {
  64. jetpack_require_lib( 'icalendar-reader' );
  65. $ical = new iCalendarReader();
  66. $events = $ical->get_events( $instance['feed-url'], $instance['count'] );
  67. $events = $this->apply_timezone_offset( $events );
  68. $ical->timezone = null;
  69. echo $args['before_widget'];
  70. if ( ! empty( $instance['title'] ) ) {
  71. echo $args['before_title'];
  72. echo esc_html( $instance['title'] );
  73. echo $args['after_title'];
  74. }
  75. if ( ! $events ) : // nothing to display?
  76. ?>
  77. <p><?php echo __( 'No upcoming events', 'jetpack' ) ?></p>
  78. <?php
  79. else :
  80. ?>
  81. <ul class="upcoming-events">
  82. <?php foreach ( $events as $event ) : ?>
  83. <li>
  84. <strong class="event-summary"><?php echo $ical->escape( stripslashes( $event['SUMMARY'] ) ); ?></strong>
  85. <span class="event-when"><?php echo $ical->formatted_date( $event ); ?></span>
  86. <?php if ( ! empty( $event['LOCATION'] ) ) : ?>
  87. <span class="event-location"><?php echo $ical->escape( stripslashes( $event['LOCATION'] ) ); ?></span>
  88. <?php endif; ?>
  89. <?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?>
  90. <span class="event-description"><?php echo wp_trim_words( $ical->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></span>
  91. <?php endif; ?>
  92. </li>
  93. <?php endforeach; ?>
  94. </ul>
  95. <?php
  96. endif;
  97. echo $args['after_widget'];
  98. /** This action is documented in modules/widgets/gravatar-profile.php */
  99. do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' );
  100. }
  101. // Left this function here for backward compatibility
  102. // just incase a site using jetpack is also using this function
  103. function apply_timezone_offset( $events ) {
  104. jetpack_require_lib( 'icalendar-reader' );
  105. $ical = new iCalendarReader();
  106. return $ical->apply_timezone_offset( $events );
  107. }
  108. }
  109. function upcoming_events_register_widgets() {
  110. register_widget( 'Jetpack_Upcoming_Events_Widget' );
  111. }
  112. add_action( 'widgets_init', 'upcoming_events_register_widgets' );