Calendar::update()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Calendar Widget
4
 *
5
 * @package SimpleCalendar\Widgets
6
 */
7
namespace SimpleCalendar\Widgets;
8
9
use SimpleCalendar\Abstracts\Calendar_View;
10
use SimpleCalendar\Abstracts\Widget;
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Calendar widget.
18
 *
19
 * Display calendars in a widget area.
20
 *
21
 * @since 3.0.0
22
 */
23
class Calendar extends \WP_Widget implements Widget {
24
25
	/**
26
	 * Calendar feeds.
27
	 *
28
	 * @access public
29
	 * @var array
30
	 */
31
	public $calendars = array();
32
33
	/**
34
	 * Calendar view.
35
	 *
36
	 * @access public
37
	 * @var Calendar_View
38
	 */
39
	public $view = null;
40
41
	/**
42
	 * Constructor.
43
	 *
44
	 * @since 3.0.0
45
	 */
46
	public function __construct() {
47
48
		$id_base        = 'gce_widget'; // old id kept for legacy reasons
49
		$name           = __( 'Simple Calendar', 'google-calendar-events' );
50
		$widget_options = array(
51
			'description' => __( 'Display a calendar of events from one of your calendar feeds.', 'google-calendar-events' )
52
		);
53
54
		parent::__construct( $id_base, $name, $widget_options );
55
56
		if ( is_admin() ) {
57
			if ( ! defined( 'DOING_AJAX' ) ) {
58
				$this->calendars = simcal_get_calendars();
59
			} else {
60
				$this->calendars = get_transient( '_simple-calendar_feed_ids' );
61
			}
62
		}
63
	}
64
65
	/**
66
	 * Print the widget content.
67
	 *
68
	 * @since 3.0.0
69
	 *
70
	 * @param array $args     Display arguments.
71
	 * @param array $instance The settings for the particular instance of the widget.
72
	 */
73
	public function widget( $args, $instance ) {
74
75
		echo $args['before_widget'];
76
77
		if ( ! empty( $instance['title'] ) ) {
78
79
			echo $args['before_title']  . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
80
		}
81
82
		$id = isset( $instance['calendar_id'] ) ? absint( $instance['calendar_id'] ) : 0;
83
		if ( $id > 0 ) {
84
			simcal_print_calendar( $id );
85
		}
86
87
		echo $args['after_widget'];
88
	}
89
90
	/**
91
	 * Update a particular instance of the widget.
92
	 *
93
	 * This function should check that $new_instance is set correctly.
94
	 * The newly-calculated value of `$instance` should be returned.
95
	 * If false is returned, the instance won't be saved/updated.
96
	 *
97
	 * @since  3.0.0
98
	 *
99
	 * @param  array $new_instance New settings for this instance as input by the user via
100
	 * @param  array $old_instance Old settings for this instance.
101
	 *
102
	 * @return array Settings to save or bool false to cancel saving.
103
	 */
104
	public function update( $new_instance, $old_instance ) {
105
106
		$instance = array();
107
108
		$instance['title']          = ( ! empty( $new_instance['title'] ) )        ? sanitize_text_field( $new_instance['title'] ) : '';
109
		$instance['calendar_id']    = ( ! empty( $new_instance['calendar_id'] ) )  ? absint( $new_instance['calendar_id'] ) : '';
110
111
		return $instance;
112
	}
113
114
	/**
115
	 * Print the settings update form.
116
	 *
117
	 * @since  3.0.0
118
	 *
119
	 * @param  array  $instance Current settings.
120
	 *
121
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
122
	 */
123
	public function form( $instance ) {
124
125
		$title          = isset( $instance['title'] )       ? esc_attr( $instance['title'] ) : __( 'Calendar', 'google-calendar-events' );
126
		$calendar_id    = isset( $instance['calendar_id'] ) ? esc_attr( $instance['calendar_id'] ) : '';
127
128
		?>
129
		<div class="simcal-calendar-widget-settings">
130
131
			<p>
132
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'google-calendar-events' ); ?></label>
133
				<br>
134
				<input type="text"
135
				       name="<?php echo $this->get_field_name( 'title' ); ?>"
136
				       id="<?php echo $this->get_field_id( 'title' ); ?>"
137
				       class="widefat simcal-field simcal-field-standard simcal-field-text"
138
				       value="<?php echo $title; ?>">
139
			</p>
140
141
			<p>
142
				<label for="<?php echo $this->get_field_id( 'calendar_id' ); ?>"><?php _e( 'Calendar:', 'google-calendar-events' ); ?></label>
143
				<br>
144
				<?php $multiselect = count( $this->calendars ) > 15 ? ' simcal-field-select-enhanced' : ''; ?>
145
				<select name="<?php echo $this->get_field_name( 'calendar_id' ) ?>"
146
				        id="<?php echo $this->get_field_id( 'calendar_id' ) ?>"
147
						class="simcal-field simcal-field-select<?php echo $multiselect; ?>"
148
						data-noresults="<?php __( 'No calendars found.', 'google-calendar-events' ); ?>">
149
						<?php foreach ( $this->calendars as $id => $name ) : ?>
150
							<option value="<?php echo $id; ?>" <?php selected( $id, $calendar_id, true ); ?>><?php echo $name; ?></option>
151
						<?php endforeach; ?>
152
				</select>
153
			</p>
154
155
		</div>
156
		<?php
157
158
	}
159
160
}
161