authors
8 years ago
contact-info
5 years ago
eu-cookie-law
1 year ago
facebook-likebox
11 years ago
flickr
2 years ago
gallery
1 year ago
goodreads
2 years ago
google-translate
1 year ago
image-widget
8 years ago
instagram
6 years ago
internet-defense-league
2 years ago
milestone
1 year ago
my-community
8 years ago
simple-payments
1 year ago
social-icons
1 year ago
social-media-icons
5 years ago
top-posts
8 years ago
wordpress-post-widget
2 years ago
authors.php
1 year ago
blog-stats.php
1 year ago
class-jetpack-eu-cookie-law-widget.php
1 year ago
class-jetpack-instagram-widget.php
1 year ago
contact-info.php
1 year ago
customizer-controls.css
9 years ago
customizer-utils.js
1 year ago
facebook-likebox.php
2 years ago
flickr.php
1 year ago
gallery.php
1 year ago
goodreads.php
1 year ago
google-translate.php
1 year ago
gravatar-profile.css
1 year ago
gravatar-profile.php
1 year ago
image-widget.php
1 year ago
internet-defense-league.php
3 years ago
mailchimp.php
3 years ago
milestone.php
5 years ago
my-community.php
1 year ago
rsslinks-widget.php
3 years ago
simple-payments.php
1 year ago
social-icons.php
1 year ago
social-media-icons.php
1 year ago
top-posts.php
1 year ago
twitter-timeline-admin.js
1 year ago
twitter-timeline.php
3 years ago
upcoming-events.php
2 years ago
wordpress-post-widget.php
1 year ago
upcoming-events.php
197 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Upcoming Events widget |
| 4 | * |
| 5 | * It relies on the icalendar-reader library. |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 11 | |
| 12 | /** |
| 13 | * Register the widget. |
| 14 | */ |
| 15 | function upcoming_events_register_widgets() { |
| 16 | register_widget( Jetpack_Upcoming_Events_Widget::class ); |
| 17 | } |
| 18 | add_action( 'widgets_init', 'upcoming_events_register_widgets' ); |
| 19 | |
| 20 | /** |
| 21 | * Widget class. |
| 22 | */ |
| 23 | class Jetpack_Upcoming_Events_Widget extends WP_Widget { |
| 24 | /** |
| 25 | * Constructor |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | parent::__construct( |
| 29 | 'upcoming_events_widget', |
| 30 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 31 | apply_filters( 'jetpack_widget_name', __( 'Upcoming Events', 'jetpack' ) ), |
| 32 | array( |
| 33 | 'description' => __( 'Display upcoming events from an iCalendar feed.', 'jetpack' ), |
| 34 | 'customize_selective_refresh' => true, |
| 35 | ) |
| 36 | ); |
| 37 | if ( is_active_widget( false, false, $this->id_base ) ) { |
| 38 | add_action( 'wp_head', array( $this, 'css' ) ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Output CSS in the header everywhere where the widget is active. |
| 44 | */ |
| 45 | public function css() { |
| 46 | ?> |
| 47 | <style type="text/css"> |
| 48 | .upcoming-events li { |
| 49 | margin-bottom: 10px; |
| 50 | } |
| 51 | .upcoming-events li span { |
| 52 | display: block; |
| 53 | } |
| 54 | </style> |
| 55 | <?php |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
| 60 | * |
| 61 | * @param array $instance Instance configuration. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function form( $instance ) { |
| 66 | $defaults = array( |
| 67 | 'title' => __( 'Upcoming Events', 'jetpack' ), |
| 68 | 'feed-url' => '', |
| 69 | 'count' => 3, |
| 70 | ); |
| 71 | $instance = array_merge( $defaults, (array) $instance ); |
| 72 | ?> |
| 73 | |
| 74 | <p> |
| 75 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
| 76 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| 77 | </p> |
| 78 | |
| 79 | <p> |
| 80 | <label for="<?php echo esc_attr( $this->get_field_id( 'feed-url' ) ); ?>"><?php esc_html_e( 'iCalendar Feed URL:', 'jetpack' ); ?></label> |
| 81 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'feed-url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'feed-url' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['feed-url'] ); ?>" /> |
| 82 | </p> |
| 83 | |
| 84 | <p> |
| 85 | <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_html_e( 'Items to show:', 'jetpack' ); ?></label> |
| 86 | <select id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>"> |
| 87 | <?php for ( $i = 1; $i <= 10; $i++ ) { ?> |
| 88 | <option <?php selected( $instance['count'], $i ); ?>><?php echo (int) $i; ?></option> |
| 89 | <?php } ?> |
| 90 | <option value="0" <?php selected( $instance['count'], 0 ); ?>><?php esc_html_e( 'All', 'jetpack' ); ?></option> |
| 91 | </select> |
| 92 | </p> |
| 93 | <?php |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Deals with the settings when they are saved by the admin. |
| 98 | * |
| 99 | * @param array $new_instance New configuration values. |
| 100 | * @param array $old_instance Old configuration values. |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 105 | $instance = array(); |
| 106 | $instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 107 | $instance['feed-url'] = wp_strip_all_tags( $new_instance['feed-url'] ); |
| 108 | $instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less |
| 109 | return $instance; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Outputs the HTML for this widget. |
| 114 | * |
| 115 | * @param array $args An array of standard parameters for widgets in this theme. |
| 116 | * @param array $instance An array of settings for this widget instance. |
| 117 | * |
| 118 | * @return void Echoes it's output |
| 119 | */ |
| 120 | public function widget( $args, $instance ) { |
| 121 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/icalendar-reader.php'; |
| 122 | |
| 123 | $ical = new iCalendarReader(); |
| 124 | $events = array(); |
| 125 | if ( ! empty( $instance['feed-url'] ) ) { |
| 126 | $events = $ical->get_events( $instance['feed-url'], $instance['count'] ); |
| 127 | $events = $this->apply_timezone_offset( $events ); |
| 128 | } |
| 129 | $ical->timezone = null; |
| 130 | |
| 131 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 132 | if ( ! empty( $instance['title'] ) ) { |
| 133 | echo $args['before_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 134 | echo esc_html( $instance['title'] ); |
| 135 | echo $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 136 | } |
| 137 | |
| 138 | if ( empty( $instance['feed-url'] ) ) { |
| 139 | if ( current_user_can( 'manage_options' ) ) { |
| 140 | echo '<div class="error-message">'; |
| 141 | esc_html_e( 'The events feed URL is not properly set up in this widget.', 'jetpack' ); |
| 142 | echo '</div>'; |
| 143 | } |
| 144 | } elseif ( ! $events ) { |
| 145 | echo '<p>'; |
| 146 | esc_html_e( 'No upcoming events', 'jetpack' ); |
| 147 | echo '</p>'; |
| 148 | } else { |
| 149 | ?> |
| 150 | <ul class="upcoming-events"> |
| 151 | <?php foreach ( $events as $event ) : ?> |
| 152 | <li> |
| 153 | <strong class="event-summary"> |
| 154 | <?php |
| 155 | echo $ical->escape( stripslashes( $event['SUMMARY'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this method is built to escape. |
| 156 | ?> |
| 157 | </strong> |
| 158 | <span class="event-when"><?php echo esc_html( $ical->formatted_date( $event ) ); ?></span> |
| 159 | <?php if ( ! empty( $event['LOCATION'] ) ) : ?> |
| 160 | <span class="event-location"> |
| 161 | <?php |
| 162 | echo $ical->escape( stripslashes( $event['LOCATION'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this method is built to escape. |
| 163 | ?> |
| 164 | </span> |
| 165 | <?php endif; ?> |
| 166 | <?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?> |
| 167 | <span class="event-description"> |
| 168 | <?php |
| 169 | echo wp_trim_words( $ical->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this method is built to escape. |
| 170 | ?> |
| 171 | </span> |
| 172 | <?php endif; ?> |
| 173 | </li> |
| 174 | <?php endforeach; ?> |
| 175 | </ul> |
| 176 | <?php |
| 177 | } |
| 178 | |
| 179 | echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 180 | |
| 181 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 182 | do_action( 'jetpack_stats_extra', 'widget_view', 'upcoming_events' ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Left this function here for backward compatibility |
| 187 | * just incase a site using jetpack is also using this function |
| 188 | * |
| 189 | * @param array|false $events Array of events, false on failure. |
| 190 | */ |
| 191 | private function apply_timezone_offset( $events ) { |
| 192 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/icalendar-reader.php'; |
| 193 | |
| 194 | return ( new iCalendarReader() )->apply_timezone_offset( $events ); |
| 195 | } |
| 196 | } |
| 197 |