Completed
Branch TASK-9118-extensions-page (04eaec)
by
unknown
856:31 queued 840:33
created

EEW_Upcoming_Events::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
2
/**
3
 * Event Espresso
4
 *
5
 * Event Registration and Management Plugin for WordPress
6
 *
7
 * @ package			Event Espresso
8
 * @ author			Seth Shoultes
9
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
10
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
11
 * @ link				http://www.eventespresso.com
12
 * @ version		 	4.0
13
 *
14
 * ------------------------------------------------------------------------
15
 *
16
 * Upcoming Events Widget
17
 *
18
 * @package		Event Espresso
19
 * @subpackage	/widgets/upcoming_events/
20
 * @author		Brent Christensen
21
 *
22
 * ------------------------------------------------------------------------
23
 */
24
class EEW_Upcoming_Events  extends WP_Widget {
25
26
27
	/**
28
	 * Register widget with WordPress.
29
	 */
30
	function __construct() {
31
		parent::__construct(
32
			'ee-upcoming-events-widget',
33
			__( 'Event Espresso Upcoming Events', 'event_espresso' ),
34
			 array( 'description' => __( 'A widget to display your upcoming events.', 'event_espresso' )),
35
			array(
36
				'width' => 300,
37
				'height' => 350,
38
				'id_base' => 'ee-upcoming-events-widget'
39
			)
40
		);
41
	}
42
43
44
45
	/**
46
	 * Back-end widget form.
47
	 *
48
	 * @see WP_Widget::form()
49
	 * @param array $instance Previously saved values from database.
50
	 * @return string|void
51
	 */
52
	public function form( $instance ) {
53
54
		EE_Registry::instance()->load_helper( 'Form_Fields' );
55
		EE_Registry::instance()->load_class( 'Question_Option', array(), FALSE, FALSE, TRUE );
56
		// Set up some default widget settings.
57
		$defaults = array(
58
			'title' => __('Upcoming Events', 'event_espresso'),
59
			'category_name' => '',
60
			'show_expired' => FALSE,
61
			'show_desc' => TRUE,
62
			'show_dates' => TRUE,
63
			'show_everywhere' => FALSE,
64
			'date_limit' => 2,
65
			'limit' => 10,
66
			'date_range' => FALSE,
67
			'image_size' => 'medium'
68
		);
69
70
		$instance = wp_parse_args( (array) $instance, $defaults );
71
		// don't add HTML labels for EE_Form_Fields generated inputs
72
		add_filter( 'FHEE__EEH_Form_Fields__label_html', '__return_empty_string' );
73
		$yes_no_values = array(
74
			EE_Question_Option::new_instance( array( 'QSO_value' => FALSE, 'QSO_desc' => __('No', 'event_espresso'))),
75
			EE_Question_Option::new_instance( array( 'QSO_value' => TRUE, 'QSO_desc' => __('Yes', 'event_espresso')))
76
		);
77
78
	?>
79
80
		<!-- Widget Title: Text Input -->
81
82
		<p>
83
			<label for="<?php echo $this->get_field_id('title'); ?>">
84
				<?php _e('Title:', 'event_espresso'); ?>
85
			</label>
86
			<input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" type="text" />
87
		</p>
88
		<p>
89
			<label for="<?php echo $this->get_field_id('category_name'); ?>">
90
				<?php _e('Event Category:', 'event_espresso'); ?>
91
			</label>
92
			<?php
93
			$event_categories = array();
94
			/** @type EEM_Term $EEM_Term */
95
			$EEM_Term = EE_Registry::instance()->load_model( 'Term' );
96
			$categories = $EEM_Term->get_all_ee_categories( TRUE );
97
			if ( $categories ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $categories of type EE_Base_Class[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
98
				foreach ( $categories as $category ) {
99
					if ( $category instanceof EE_Term ) {
100
						$event_categories[] = EE_Question_Option::new_instance( array( 'QSO_value' => $category->get( 'slug' ), 'QSO_desc' => $category->get( 'name' )));
101
					}
102
				}
103
			}
104
			array_unshift( $event_categories, EE_Question_Option::new_instance( array( 'QSO_value' => '', 'QSO_desc' => __(' - display all - ', 'event_espresso'))));
105
			echo EEH_Form_Fields::select(
106
				 __('Event Category:', 'event_espresso'),
107
				$instance['category_name'],
108
				$event_categories,
109
				$this->get_field_name('category_name'),
110
				$this->get_field_id('category_name')
111
			);
112
			?>
113
		</p>
114
		<p>
115
			<label for="<?php echo $this->get_field_id('limit'); ?>">
116
				<?php _e('Number of Events to Display:', 'event_espresso'); ?>
117
			</label>
118
			<input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" value="<?php echo $instance['limit']; ?>" size="3" type="text" />
119
		</p>
120
		<p>
121
			<label for="<?php echo $this->get_field_id('show_expired'); ?>">
122
				<?php _e('Show Expired Events:', 'event_espresso'); ?>
123
			</label>
124
			<?php
125
			echo EEH_Form_Fields::select(
126
				 __('Show Expired Events:', 'event_espresso'),
127
				$instance['show_expired'],
128
				$yes_no_values,
129
				$this->get_field_name('show_expired'),
130
				$this->get_field_id('show_expired')
131
			);
132
			?>
133
		</p>
134
		<p>
135
			<label for="<?php echo $this->get_field_id('image_size'); ?>">
136
				<?php _e('Image Size:', 'event_espresso'); ?>
137
			</label>
138
			<?php
139
			$image_sizes = array();
140
			$sizes = get_intermediate_image_sizes();
141
			if ( $sizes ) {
142
				// loop thru images and create option objects out of them
143
				foreach ( $sizes as $image_size ) {
144
					$image_size = trim( $image_size );
145
					// no big images plz
146
					if ( ! in_array( $image_size, array( 'large', 'post-thumbnail' ))) {
147
						$image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => $image_size, 'QSO_desc' => $image_size ));
148
					}
149
				}
150
				$image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => 'none', 'QSO_desc' =>  __('don\'t show images', 'event_espresso') ));
151
			}
152
			echo EEH_Form_Fields::select(
153
				 __('Image Size:', 'event_espresso'),
154
				$instance['image_size'],
155
				$image_sizes,
156
				$this->get_field_name('image_size'),
157
				$this->get_field_id('image_size')
158
			);
159
			?>
160
161
		</p>
162
		<p>
163
			<label for="<?php echo $this->get_field_id('show_desc'); ?>">
164
				<?php _e('Show Description:', 'event_espresso'); ?>
165
			</label>
166
			<?php
167
			echo EEH_Form_Fields::select(
168
				 __('Show Description:', 'event_espresso'),
169
				$instance['show_desc'],
170
				$yes_no_values,
171
				$this->get_field_name('show_desc'),
172
				$this->get_field_id('show_desc')
173
			);
174
			?>
175
		</p>
176
		<p>
177
			<label for="<?php echo $this->get_field_id('show_dates'); ?>">
178
				<?php _e('Show Dates:', 'event_espresso'); ?>
179
			</label>
180
			<?php
181
			echo EEH_Form_Fields::select(
182
				 __('Show Dates:', 'event_espresso'),
183
				$instance['show_dates'],
184
				$yes_no_values,
185
				$this->get_field_name('show_dates'),
186
				$this->get_field_id('show_dates')
187
			);
188
			?>
189
		</p>
190
		<p>
191
			<label for="<?php echo $this->get_field_id('show_everywhere'); ?>">
192
		        <?php _e('Show on all Pages:', 'event_espresso'); ?>
193
			</label>
194
	 	    <?php
195
	 	    echo EEH_Form_Fields::select(
196
	 	        __('Show on all Pages:', 'event_espresso'),
197
	 	        $instance['show_everywhere'],
198
	 	        $yes_no_values,
199
	 	        $this->get_field_name('show_everywhere'),
200
	 	        $this->get_field_id('show_everywhere')
201
	 	    );
202
	 	    ?>
203
	 	</p>
204
		<p>
205
			<label for="<?php echo $this->get_field_id('date_limit'); ?>">
206
				<?php _e('Number of Dates to Display:', 'event_espresso'); ?>
207
			</label>
208
			<input id="<?php echo $this->get_field_id('date_limit'); ?>" name="<?php echo $this->get_field_name('date_limit'); ?>" value="<?php echo esc_attr( $instance['date_limit'] ); ?>" size="3" type="text" />
209
		</p>
210
		<p>
211
			<label for="<?php echo $this->get_field_id('date_range'); ?>">
212
				<?php _e('Show Date Range:', 'event_espresso'); ?>
213
			</label>
214
			<?php
215
			echo EEH_Form_Fields::select(
216
				 __('Show Date Range:', 'event_espresso'),
217
				$instance['date_range'],
218
				$yes_no_values,
219
				$this->get_field_name('date_range'),
220
				$this->get_field_id('date_range')
221
			);
222
			?><span class="description"><br /><?php _e('This setting will replace the list of dates in the widget.', 'event_espresso'); ?></span>
223
		</p>
224
225
		<?php
226
	}
227
228
229
230
	/**
231
	 * Sanitize widget form values as they are saved.
232
	 *
233
	 * @see WP_Widget::update()
234
	 *
235
	 * @param array $new_instance Values just sent to be saved.
236
	 * @param array $old_instance Previously saved values from database.
237
	 *
238
	 * @return array Updated safe values to be saved.
239
	 */
240
	public function update( $new_instance, $old_instance ) {
241
		$instance = $old_instance;
242
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
243
		$instance['category_name'] = $new_instance['category_name'];
244
		$instance['show_expired'] = $new_instance['show_expired'];
245
		$instance['limit'] = $new_instance['limit'];
246
		$instance['image_size'] = $new_instance['image_size'];
247
		$instance['show_desc'] = $new_instance['show_desc'];
248
		$instance['show_dates'] = $new_instance['show_dates'];
249
		$instance['show_everywhere'] = $new_instance['show_everywhere'];
250
		$instance['date_limit'] = $new_instance['date_limit'];
251
		$instance['date_range'] = $new_instance['date_range'];
252
		return $instance;
253
	}
254
255
256
257
	/**
258
	 * Front-end display of widget.
259
	 *
260
	 * @see WP_Widget::widget()
261
	 *
262
	 * @param array $args     Widget arguments.
263
	 * @param array $instance Saved values from database.
264
	 */
265
	public function widget( $args, $instance ) {
266
267
		global $post;
268
		// make sure there is some kinda post object
269
		if ( $post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
270
			$before_widget = '';
271
			$before_title = '';
272
			$after_title = '';
273
			$after_widget = '';
274
			// but NOT an events archives page, cuz that would be like two event lists on the same page
275
			$show_everywhere = isset( $instance['show_everywhere'] ) ? (bool) absint( $instance['show_everywhere'] ) : TRUE;
276
			if ( $show_everywhere || ! ( $post->post_type == 'espresso_events' && is_archive() )) {	
277
				// let's use some of the event helper functions'
278
				EE_Registry::instance()->load_helper( 'Event_View' );
279
				// make separate vars out of attributes
280
281
282
				extract($args);
283
284
				// add function to make the title a link
285
	            add_filter('widget_title', array($this, 'make_the_title_a_link'), 15);
286
287
				// filter the title
288
				$title = apply_filters('widget_title', $instance['title']);
289
290
				// remove the function from the filter, so it does not affect other widgets
291
	            remove_filter('widget_title', array($this, 'make_the_title_a_link'), 15);
292
293
				// Before widget (defined by themes).
294
				echo $before_widget;
295
				// Display the widget title if one was input (before and after defined by themes).
296
				if ( ! empty( $title )) {
297
					echo $before_title . $title . $after_title;
298
				}
299
				// grab widget settings
300
				$category = isset( $instance['category_name'] ) && ! empty( $instance['category_name'] ) ? $instance['category_name'] : FALSE;
301
				$show_expired = isset( $instance['show_expired'] ) ? (bool) absint( $instance['show_expired'] ) : FALSE;
302
				$image_size = isset( $instance['image_size'] ) && ! empty( $instance['image_size'] ) ? $instance['image_size'] : 'medium';
303
				$show_desc = isset( $instance['show_desc'] ) ? (bool) absint( $instance['show_desc'] ) : TRUE;
304
				$show_dates = isset( $instance['show_dates'] ) ? (bool) absint( $instance['show_dates'] ) : TRUE;
305
				$date_limit = isset( $instance['date_limit'] ) && ! empty( $instance['date_limit'] ) ? $instance['date_limit'] : NULL;
306
				$date_range = isset( $instance['date_range'] ) && ! empty( $instance['date_range'] ) ? $instance['date_range'] : FALSE;
307
				// start to build our where clause
308
				$where = array(
309
//					'Datetime.DTT_is_primary' => 1,
310
					'status' => 'publish'
311
				);
312
				// add category
313
				if ( $category ) {
314
					$where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
315
					$where['Term_Taxonomy.Term.slug'] = $category;
316
				}
317
				// if NOT expired then we want events that start today or in the future
318
				if ( ! $show_expired ) {
319
					$where['Datetime.DTT_EVT_end'] = array( '>=', EEM_Datetime::instance()->current_time_for_query( 'DTT_EVT_end' ) );
320
				}
321
				// run the query
322
				$events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
0 ignored issues
show
Bug introduced by
The method get_all cannot be called on \EE_Registry::instance()->load_model('Event') (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
323
					$where,
324
					'limit' => $instance['limit'] > 0 ? '0,' . $instance['limit'] : '0,10',
325
					'order_by' => 'Datetime.DTT_EVT_start',
326
					'order' => 'ASC',
327
					'group_by' => 'EVT_ID'
328
				));
329
330
				if ( ! empty( $events )) {
331
					echo '<ul class="ee-upcoming-events-widget-ul">';
332
					foreach ( $events as $event ) {
333
						if ( $event instanceof EE_Event && ( !is_single() || $post->ID != $event->ID() ) ) {
334
							//printr( $event, '$event  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
335
							echo '<li id="ee-upcoming-events-widget-li-' . $event->ID() . '" class="ee-upcoming-events-widget-li">';
336
							// how big is the event name ?
337
							$name_length = strlen( $event->name() );
338
							switch( $name_length ) {
339
								case $name_length > 70 :
340
									$len_class =  ' three-line';
341
									break;
342
								case $name_length > 35 :
343
									$len_class =  ' two-line';
344
									break;
345
								default :
346
									$len_class =  ' one-line';
347
							}
348
							echo '<h5 class="ee-upcoming-events-widget-title-h5"><a class="ee-widget-event-name-a' . $len_class . '" href="' . get_permalink( $event->ID() ) . '">' . $event->name() . '</a></h5>';
349
							if ( has_post_thumbnail( $event->ID() ) && $image_size != 'none' ) {
350
								echo '<div class="ee-upcoming-events-widget-img-dv"><a class="ee-upcoming-events-widget-img" href="' . get_permalink( $event->ID() ) . '">' . get_the_post_thumbnail( $event->ID(), $image_size ) . '</a></div>';
351
							}
352
							$desc = $event->short_description( 25 );
353
							if ( $show_dates ) {
354
								$date_format = apply_filters( 'FHEE__espresso_event_date_range__date_format', get_option( 'date_format' ));
355
								$time_format = apply_filters( 'FHEE__espresso_event_date_range__time_format', get_option( 'time_format' ));
356
								$single_date_format = apply_filters( 'FHEE__espresso_event_date_range__single_date_format', get_option( 'date_format' ));
357
								$single_time_format = apply_filters( 'FHEE__espresso_event_date_range__single_time_format', get_option( 'time_format' ));
358
								if ( $date_range == TRUE ) {
359
									echo espresso_event_date_range( $date_format, $time_format, $single_date_format, $single_time_format, $event->ID() );
360
								}else{
361
									echo espresso_list_of_event_dates( $event->ID(), $date_format, $time_format, FALSE, NULL, TRUE, TRUE, $date_limit );
362
								}
363
							}
364
							if ( $show_desc && $desc ) {
365
								echo '<p style="margin-top: .5em">' . $desc . '</p>';
366
							}
367
							echo '</li>';
368
						}
369
					}
370
					echo '</ul>';
371
				}
372
				// After widget (defined by themes).
373
				echo $after_widget;
374
			}
375
		}
376
	}
377
378
379
380
	/**
381
	 * make_the_title_a_link
382
	 * callback for widget_title filter
383
	 *
384
	 * @param $title
385
	 * @return string
386
	 */
387
	public function make_the_title_a_link($title) {
388
	    return '<a href="' . EEH_Event_View::event_archive_url() . '">' . $title . '</a>';
389
	}
390
391
}
392
// End of file EEW_Upcoming_Events.widget.php
393
// Location: /widgets/upcoming_events/EEW_Upcoming_Events.widget.php
394