Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

Upcoming_Events_Widget   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A css() 0 12 1
B form() 0 31 2
A update() 0 6 1
B widget() 0 24 3
1
<?php
2
3
class Upcoming_Events_Widget extends WP_Widget {
4
	function __construct() {
5
		parent::__construct(
6
			'upcoming_events_widget',
7
			/** This filter is documented in modules/widgets/facebook-likebox.php */
8
			apply_filters( 'jetpack_widget_name', __( 'Upcoming Events', 'jetpack' ) ),
9
			array(
10
				'description' => __( 'Display upcoming events from an iCalendar feed.', 'jetpack' ),
11
				'customize_selective_refresh' => true,
12
			)
13
		);
14
		if ( is_active_widget( false, false, $this->id_base ) ) {
15
			add_action( 'wp_head', array( $this, 'css' ) );
16
		}
17
	}
18
19
	function css() {
20
?>
21
<style type="text/css">
22
.upcoming-events li {
23
	margin-bottom: 10px;
24
}
25
.upcoming-events li span {
26
	display: block;
27
}
28
</style>
29
<?php
30
	}
31
32
	function form( $instance ) {
33
		$defaults = array(
34
			'title' => __( 'Upcoming Events', 'jetpack' ),
35
			'feed-url' => '',
36
			'count' => 3
37
		);
38
		$instance = array_merge( $defaults, (array) $instance );
39
?>
40
41
		<p>
42
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label>
43
		<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'] ); ?>" />
44
		</p>
45
46
		<p>
47
		<label for="<?php echo $this->get_field_id( 'feed-url' ); ?>"><?php _e( 'iCalendar Feed URL:', 'jetpack' ); ?></label>
48
		<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'] ); ?>" />
49
		</p>
50
51
		<p>
52
		<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Items to show:', 'jetpack' ); ?></label>
53
		<select id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>">
54
			<?php $i = 1;
55
			while ( $i <= 10 ) { ?>
56
				<option <?php selected( $instance['count'], $i ) ?>><?php echo $i; ?></option>
57
			<?php $i++; } ?>
58
			<option value="0" <?php selected( $instance['count'], 0 ) ?>><?php _e( 'All' , 'jetpack' ) ?></option>
59
		</select>
60
		</p>
61
<?php
62
	}
63
64
	function update( $new_instance, $old_instance ) {
65
		$instance['title'] = strip_tags( $new_instance['title'] );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$instance was never initialized. Although not strictly required by PHP, it is generally a good practice to add $instance = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
66
		$instance['feed-url'] = strip_tags( $new_instance['feed-url'] );
67
		$instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less
68
		return $instance;
69
	}
70
71
	function widget( $args, $instance ) {
72
		jetpack_require_lib( 'icalendar-reader' );
73
74
		$events = icalendar_render_events( $instance['feed-url'], array(
75
			'context' => 'widget',
76
			'number' => $instance['count']
77
		) );
78
79
		// nothing to display?
80
		if ( ! $events )
0 ignored issues
show
Bug Best Practice introduced by
The expression $events of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
			$events = sprintf( '<p>%s</p>', __( 'No upcoming events', 'jetpack' ) );
82
83
		echo $args['before_widget'];
84
		if ( ! empty( $instance['title'] ) ) {
85
			echo $args['before_title'];
86
			echo esc_html( $instance['title'] );
87
			echo $args['after_title'];
88
		}
89
		echo $events;
90
		echo $args['after_widget'];
91
92
		/** This action is documented in modules/widgets/gravatar-profile.php */
93
		do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' );
94
	}
95
}
96
97
function upcoming_events_register_widgets() {
98
	register_widget( 'Upcoming_Events_Widget' );
99
}
100
101
add_action( 'widgets_init', 'upcoming_events_register_widgets' );
102