Açıklama Yok

upcoming-events.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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
  49. $i = 1;
  50. while ( $i <= 10 ) {
  51. ?>
  52. <option <?php selected( $instance['count'], $i ); ?>><?php echo $i; ?></option>
  53. <?php $i++; } ?>
  54. <option value="0" <?php selected( $instance['count'], 0 ); ?>><?php _e( 'All', 'jetpack' ); ?></option>
  55. </select>
  56. </p>
  57. <?php
  58. }
  59. function update( $new_instance, $old_instance ) {
  60. $instance['title'] = strip_tags( $new_instance['title'] );
  61. $instance['feed-url'] = strip_tags( $new_instance['feed-url'] );
  62. $instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less
  63. return $instance;
  64. }
  65. function widget( $args, $instance ) {
  66. jetpack_require_lib( 'icalendar-reader' );
  67. $ical = new iCalendarReader();
  68. $events = $ical->get_events( $instance['feed-url'], $instance['count'] );
  69. $events = $this->apply_timezone_offset( $events );
  70. $ical->timezone = null;
  71. echo $args['before_widget'];
  72. if ( ! empty( $instance['title'] ) ) {
  73. echo $args['before_title'];
  74. echo esc_html( $instance['title'] );
  75. echo $args['after_title'];
  76. }
  77. if ( ! $events ) : // nothing to display?
  78. ?>
  79. <p><?php echo __( 'No upcoming events', 'jetpack' ); ?></p>
  80. <?php
  81. else :
  82. ?>
  83. <ul class="upcoming-events">
  84. <?php foreach ( $events as $event ) : ?>
  85. <li>
  86. <strong class="event-summary"><?php echo $ical->escape( stripslashes( $event['SUMMARY'] ) ); ?></strong>
  87. <span class="event-when"><?php echo $ical->formatted_date( $event ); ?></span>
  88. <?php if ( ! empty( $event['LOCATION'] ) ) : ?>
  89. <span class="event-location"><?php echo $ical->escape( stripslashes( $event['LOCATION'] ) ); ?></span>
  90. <?php endif; ?>
  91. <?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?>
  92. <span class="event-description"><?php echo wp_trim_words( $ical->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></span>
  93. <?php endif; ?>
  94. </li>
  95. <?php endforeach; ?>
  96. </ul>
  97. <?php
  98. endif;
  99. echo $args['after_widget'];
  100. /** This action is documented in modules/widgets/gravatar-profile.php */
  101. do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' );
  102. }
  103. // Left this function here for backward compatibility
  104. // just incase a site using jetpack is also using this function
  105. function apply_timezone_offset( $events ) {
  106. jetpack_require_lib( 'icalendar-reader' );
  107. $ical = new iCalendarReader();
  108. return $ical->apply_timezone_offset( $events );
  109. }
  110. }
  111. function upcoming_events_register_widgets() {
  112. register_widget( 'Jetpack_Upcoming_Events_Widget' );
  113. }
  114. add_action( 'widgets_init', 'upcoming_events_register_widgets' );