Completed
Branch BUG/upcoming-widget-sort (2cad59)
by
unknown
06:49 queued 04:40
created
widgets/upcoming_events/EEW_Upcoming_Events.widget.php 1 patch
Indentation   +594 added lines, -594 removed lines patch added patch discarded remove patch
@@ -12,109 +12,109 @@  discard block
 block discarded – undo
12 12
 class EEW_Upcoming_Events extends EspressoWidget
13 13
 {
14 14
 
15
-    /**
16
-     * @var string
17
-     */
18
-    private $title;
19
-    /**
20
-     * @var string
21
-     */
22
-    private $events_category;
23
-
24
-    /**
25
-     * @var bool
26
-     */
27
-    private $show_expired;
28
-
29
-    /**
30
-     * @var string
31
-     */
32
-    private $image_size;
33
-
34
-    /**
35
-     * @var bool
36
-     */
37
-    private $show_desc;
38
-
39
-    /**
40
-     * @var bool
41
-     */
42
-    private $show_dates;
43
-
44
-    /**
45
-     * @var string
46
-     */
47
-    private $date_limit;
48
-
49
-    /**
50
-     * @var string
51
-     */
52
-    private $date_range;
53
-
54
-    /**
55
-     * @var string
56
-     */
57
-    private $limit;
58
-
59
-    /**
60
-     * @var string
61
-     */
62
-    private $order;
63
-
64
-
65
-    /**
66
-     * Register widget with WordPress.
67
-     */
68
-    public function __construct()
69
-    {
70
-        parent::__construct(
71
-            esc_html__('Event Espresso Upcoming Events', 'event_espresso'),
72
-            ['description' => esc_html__('A widget to display your upcoming events.', 'event_espresso')]
73
-        );
74
-    }
75
-
76
-
77
-    /**
78
-     * Back-end widget form.
79
-     *
80
-     * @param array $instance Previously saved values from database.
81
-     * @return void
82
-     * @throws EE_Error
83
-     * @throws ReflectionException
84
-     * @see WP_Widget::form()
85
-     */
86
-    public function form($instance)
87
-    {
88
-
89
-        EE_Registry::instance()->load_class('Question_Option', [], false, false, true);
90
-        // Set up some default widget settings.
91
-        $defaults = [
92
-            'title'           => esc_html__('Upcoming Events', 'event_espresso'),
93
-            'category_name'   => '',
94
-            'show_expired'    => 0,
95
-            'show_desc'       => true,
96
-            'show_dates'      => true,
97
-            'show_everywhere' => false,
98
-            'date_limit'      => 2,
99
-            'limit'           => 10,
100
-            'sort'            => 'ASC',
101
-            'date_range'      => false,
102
-            'image_size'      => 'medium',
103
-        ];
104
-
105
-        $instance = wp_parse_args((array) $instance, $defaults);
106
-        // don't add HTML labels for EE_Form_Fields generated inputs
107
-        add_filter('FHEE__EEH_Form_Fields__label_html', '__return_empty_string');
108
-        $yes_no_values = [
109
-            EE_Question_Option::new_instance(['QSO_value' => false, 'QSO_desc' => esc_html__('No', 'event_espresso')]),
110
-            EE_Question_Option::new_instance(['QSO_value' => true, 'QSO_desc' => esc_html__('Yes', 'event_espresso')]),
111
-        ];
112
-        $sort_values   = [
113
-            EE_Question_Option::new_instance(['QSO_value' => 'ASC', 'QSO_desc' => esc_html__('ASC', 'event_espresso')]),
114
-            EE_Question_Option::new_instance(['QSO_value' => 'DESC', 'QSO_desc' => esc_html__('DESC', 'event_espresso')]),
115
-        ];
116
-
117
-        ?>
15
+	/**
16
+	 * @var string
17
+	 */
18
+	private $title;
19
+	/**
20
+	 * @var string
21
+	 */
22
+	private $events_category;
23
+
24
+	/**
25
+	 * @var bool
26
+	 */
27
+	private $show_expired;
28
+
29
+	/**
30
+	 * @var string
31
+	 */
32
+	private $image_size;
33
+
34
+	/**
35
+	 * @var bool
36
+	 */
37
+	private $show_desc;
38
+
39
+	/**
40
+	 * @var bool
41
+	 */
42
+	private $show_dates;
43
+
44
+	/**
45
+	 * @var string
46
+	 */
47
+	private $date_limit;
48
+
49
+	/**
50
+	 * @var string
51
+	 */
52
+	private $date_range;
53
+
54
+	/**
55
+	 * @var string
56
+	 */
57
+	private $limit;
58
+
59
+	/**
60
+	 * @var string
61
+	 */
62
+	private $order;
63
+
64
+
65
+	/**
66
+	 * Register widget with WordPress.
67
+	 */
68
+	public function __construct()
69
+	{
70
+		parent::__construct(
71
+			esc_html__('Event Espresso Upcoming Events', 'event_espresso'),
72
+			['description' => esc_html__('A widget to display your upcoming events.', 'event_espresso')]
73
+		);
74
+	}
75
+
76
+
77
+	/**
78
+	 * Back-end widget form.
79
+	 *
80
+	 * @param array $instance Previously saved values from database.
81
+	 * @return void
82
+	 * @throws EE_Error
83
+	 * @throws ReflectionException
84
+	 * @see WP_Widget::form()
85
+	 */
86
+	public function form($instance)
87
+	{
88
+
89
+		EE_Registry::instance()->load_class('Question_Option', [], false, false, true);
90
+		// Set up some default widget settings.
91
+		$defaults = [
92
+			'title'           => esc_html__('Upcoming Events', 'event_espresso'),
93
+			'category_name'   => '',
94
+			'show_expired'    => 0,
95
+			'show_desc'       => true,
96
+			'show_dates'      => true,
97
+			'show_everywhere' => false,
98
+			'date_limit'      => 2,
99
+			'limit'           => 10,
100
+			'sort'            => 'ASC',
101
+			'date_range'      => false,
102
+			'image_size'      => 'medium',
103
+		];
104
+
105
+		$instance = wp_parse_args((array) $instance, $defaults);
106
+		// don't add HTML labels for EE_Form_Fields generated inputs
107
+		add_filter('FHEE__EEH_Form_Fields__label_html', '__return_empty_string');
108
+		$yes_no_values = [
109
+			EE_Question_Option::new_instance(['QSO_value' => false, 'QSO_desc' => esc_html__('No', 'event_espresso')]),
110
+			EE_Question_Option::new_instance(['QSO_value' => true, 'QSO_desc' => esc_html__('Yes', 'event_espresso')]),
111
+		];
112
+		$sort_values   = [
113
+			EE_Question_Option::new_instance(['QSO_value' => 'ASC', 'QSO_desc' => esc_html__('ASC', 'event_espresso')]),
114
+			EE_Question_Option::new_instance(['QSO_value' => 'DESC', 'QSO_desc' => esc_html__('DESC', 'event_espresso')]),
115
+		];
116
+
117
+		?>
118 118
 
119 119
         <!-- Widget Title: Text Input -->
120 120
 
@@ -151,32 +151,32 @@  discard block
 block discarded – undo
151 151
                 <?php esc_html_e('Show Expired Events:', 'event_espresso'); ?>
152 152
             </label>
153 153
             <?php
154
-            $show_expired_options   = $yes_no_values;
155
-            $show_expired_options[] = EE_Question_Option::new_instance(
156
-                ['QSO_value' => 2, 'QSO_desc' => esc_html__('Show Only Expired', 'event_espresso')]
157
-            );
158
-            echo EEH_Form_Fields::select(
159
-                esc_html__('Show Expired Events:', 'event_espresso'),
160
-                $instance['show_expired'],
161
-                $show_expired_options,
162
-                $this->fieldName('show_expired'),
163
-                $this->fieldID('show_expired')
164
-            );
165
-            ?>
154
+			$show_expired_options   = $yes_no_values;
155
+			$show_expired_options[] = EE_Question_Option::new_instance(
156
+				['QSO_value' => 2, 'QSO_desc' => esc_html__('Show Only Expired', 'event_espresso')]
157
+			);
158
+			echo EEH_Form_Fields::select(
159
+				esc_html__('Show Expired Events:', 'event_espresso'),
160
+				$instance['show_expired'],
161
+				$show_expired_options,
162
+				$this->fieldName('show_expired'),
163
+				$this->fieldID('show_expired')
164
+			);
165
+			?>
166 166
         </p>
167 167
         <p>
168 168
             <label for="<?php echo $this->fieldID('sort'); ?>">
169 169
                 <?php esc_html_e('Sort Events:', 'event_espresso'); ?>
170 170
             </label>
171 171
             <?php
172
-            echo EEH_Form_Fields::select(
173
-                esc_html__('Sort Events:', 'event_espresso'),
174
-                $instance['sort'],
175
-                $sort_values,
176
-                $this->fieldName('sort'),
177
-                $this->fieldID('sort')
178
-            );
179
-            ?>
172
+			echo EEH_Form_Fields::select(
173
+				esc_html__('Sort Events:', 'event_espresso'),
174
+				$instance['sort'],
175
+				$sort_values,
176
+				$this->fieldName('sort'),
177
+				$this->fieldID('sort')
178
+			);
179
+			?>
180 180
         </p>
181 181
         <p>
182 182
             <label for="<?php echo $this->fieldID('image_size'); ?>">
@@ -190,42 +190,42 @@  discard block
 block discarded – undo
190 190
                 <?php esc_html_e('Show Description:', 'event_espresso'); ?>
191 191
             </label>
192 192
             <?php
193
-            echo EEH_Form_Fields::select(
194
-                esc_html__('Show Description:', 'event_espresso'),
195
-                $instance['show_desc'],
196
-                $yes_no_values,
197
-                $this->fieldName('show_desc'),
198
-                $this->fieldID('show_desc')
199
-            );
200
-            ?>
193
+			echo EEH_Form_Fields::select(
194
+				esc_html__('Show Description:', 'event_espresso'),
195
+				$instance['show_desc'],
196
+				$yes_no_values,
197
+				$this->fieldName('show_desc'),
198
+				$this->fieldID('show_desc')
199
+			);
200
+			?>
201 201
         </p>
202 202
         <p>
203 203
             <label for="<?php echo $this->fieldID('show_dates'); ?>">
204 204
                 <?php esc_html_e('Show Dates:', 'event_espresso'); ?>
205 205
             </label>
206 206
             <?php
207
-            echo EEH_Form_Fields::select(
208
-                esc_html__('Show Dates:', 'event_espresso'),
209
-                $instance['show_dates'],
210
-                $yes_no_values,
211
-                $this->fieldName('show_dates'),
212
-                $this->fieldID('show_dates')
213
-            );
214
-            ?>
207
+			echo EEH_Form_Fields::select(
208
+				esc_html__('Show Dates:', 'event_espresso'),
209
+				$instance['show_dates'],
210
+				$yes_no_values,
211
+				$this->fieldName('show_dates'),
212
+				$this->fieldID('show_dates')
213
+			);
214
+			?>
215 215
         </p>
216 216
         <p>
217 217
             <label for="<?php echo $this->fieldID('show_everywhere'); ?>">
218 218
                 <?php esc_html_e('Show on all Pages:', 'event_espresso'); ?>
219 219
             </label>
220 220
             <?php
221
-            echo EEH_Form_Fields::select(
222
-                esc_html__('Show on all Pages:', 'event_espresso'),
223
-                $instance['show_everywhere'],
224
-                $yes_no_values,
225
-                $this->fieldName('show_everywhere'),
226
-                $this->fieldID('show_everywhere')
227
-            );
228
-            ?>
221
+			echo EEH_Form_Fields::select(
222
+				esc_html__('Show on all Pages:', 'event_espresso'),
223
+				$instance['show_everywhere'],
224
+				$yes_no_values,
225
+				$this->fieldName('show_everywhere'),
226
+				$this->fieldID('show_everywhere')
227
+			);
228
+			?>
229 229
         </p>
230 230
         <p>
231 231
             <label for="<?php echo $this->fieldID('date_limit'); ?>">
@@ -243,278 +243,278 @@  discard block
 block discarded – undo
243 243
                 <?php esc_html_e('Show Date Range:', 'event_espresso'); ?>
244 244
             </label>
245 245
             <?php
246
-            echo EEH_Form_Fields::select(
247
-                esc_html__('Show Date Range:', 'event_espresso'),
248
-                $instance['date_range'],
249
-                $yes_no_values,
250
-                $this->fieldName('date_range'),
251
-                $this->fieldID('date_range')
252
-            );
253
-            ?>
246
+			echo EEH_Form_Fields::select(
247
+				esc_html__('Show Date Range:', 'event_espresso'),
248
+				$instance['date_range'],
249
+				$yes_no_values,
250
+				$this->fieldName('date_range'),
251
+				$this->fieldID('date_range')
252
+			);
253
+			?>
254 254
             <span class="description">
255 255
                 <br />
256 256
                 <?php esc_html_e(
257
-                    'This setting will replace the list of dates in the widget.',
258
-                    'event_espresso'
259
-                ); ?>
257
+					'This setting will replace the list of dates in the widget.',
258
+					'event_espresso'
259
+				); ?>
260 260
             </span>
261 261
         </p>
262 262
 
263 263
         <?php
264
-    }
265
-
266
-
267
-    /**
268
-     * Sanitize widget form values as they are saved.
269
-     *
270
-     * @param array $new_instance Values just sent to be saved.
271
-     * @param array $old_instance Previously saved values from database.
272
-     *
273
-     * @return array Updated safe values to be saved.
274
-     * @see WP_Widget::update()
275
-     *
276
-     */
277
-    public function update($new_instance, $old_instance)
278
-    {
279
-        $instance                    = $old_instance;
280
-        $instance['title']           = ! empty($new_instance['title']) ? strip_tags($new_instance['title']) : '';
281
-        $instance['category_name']   = $new_instance['category_name'];
282
-        $instance['show_expired']    = $new_instance['show_expired'];
283
-        $instance['limit']           = $new_instance['limit'];
284
-        $instance['sort']            = $new_instance['sort'];
285
-        $instance['image_size']      = $new_instance['image_size'];
286
-        $instance['show_desc']       = $new_instance['show_desc'];
287
-        $instance['show_dates']      = $new_instance['show_dates'];
288
-        $instance['show_everywhere'] = $new_instance['show_everywhere'];
289
-        $instance['date_limit']      = $new_instance['date_limit'];
290
-        $instance['date_range']      = $new_instance['date_range'];
291
-        return $instance;
292
-    }
293
-
294
-
295
-    /**
296
-     * Front-end display of widget.
297
-     *
298
-     * @param array $args     Widget arguments.
299
-     * @param array $instance Saved values from database.
300
-     * @throws EE_Error
301
-     * @throws ReflectionException
302
-     * @see WP_Widget::widget()
303
-     *
304
-     */
305
-    public function widget($args, $instance)
306
-    {
307
-
308
-        global $post;
309
-        // make sure there is some kinda post object
310
-        if ($post instanceof WP_Post) {
311
-            $before_widget = '';
312
-            $before_title  = '';
313
-            $after_title   = '';
314
-            $after_widget  = '';
315
-            // but NOT an events archives page, cuz that would be like two event lists on the same page
316
-            $show_everywhere = ! isset($instance['show_everywhere']) || absint($instance['show_everywhere']);
317
-            if ($show_everywhere || ! ($post->post_type == 'espresso_events' && is_archive())) {
318
-                // let's use some of the event helper functions'
319
-                // make separate vars out of attributes
320
-                extract($args);
321
-
322
-                // grab widget settings
323
-                $this->parseWidgetSettings($instance);
324
-                $title = $this->widgetTitle();
325
-
326
-                // Before widget (defined by themes).
327
-                echo $before_widget;
328
-                // Display the widget title if one was input (before and after defined by themes).
329
-                if (! empty($title)) {
330
-                    echo $before_title . $title . $after_title;
331
-                }
332
-                echo $this->widgetContent($post);
333
-                // After widget (defined by themes).
334
-                echo $after_widget;
335
-            }
336
-        }
337
-    }
338
-
339
-
340
-    /**
341
-     * make_the_title_a_link
342
-     * callback for widget_title filter
343
-     *
344
-     * @param $title
345
-     * @return string
346
-     */
347
-    public function make_the_title_a_link($title)
348
-    {
349
-        return '<a href="' . EEH_Event_View::event_archive_url() . '">' . $title . '</a>';
350
-    }
351
-
352
-
353
-    /**
354
-     * @param string $field_name
355
-     * @return string
356
-     * @since   4.10.14.p
357
-     */
358
-    public function fieldID($field_name)
359
-    {
360
-        return esc_attr(parent::get_field_id($field_name));
361
-    }
362
-
363
-
364
-    /**
365
-     * @param string $field_name
366
-     * @return string
367
-     * @since   4.10.14.p
368
-     */
369
-    public function fieldName($field_name)
370
-    {
371
-        return esc_attr(parent::get_field_name($field_name));
372
-    }
373
-
374
-
375
-    /**
376
-     * @param array $instance
377
-     * @throws EE_Error
378
-     * @throws ReflectionException
379
-     * @since   4.10.14.p
380
-     */
381
-    private function eventCategoriesSelector(array $instance)
382
-    {
383
-        $event_categories = [];
384
-        $categories       = EEM_Term::instance()->get_all_ee_categories(true);
385
-        if ($categories) {
386
-            foreach ($categories as $category) {
387
-                if ($category instanceof EE_Term) {
388
-                    $event_categories[] =
389
-                        EE_Question_Option::new_instance(
390
-                            [
391
-                                'QSO_value' => $category->get('slug'),
392
-                                'QSO_desc'  => $category->get('name'),
393
-                            ]
394
-                        );
395
-                }
396
-            }
397
-        }
398
-        array_unshift(
399
-            $event_categories,
400
-            EE_Question_Option::new_instance(
401
-                [
402
-                    'QSO_value' => '',
403
-                    'QSO_desc'  => esc_html__(' - display all - ', 'event_espresso'),
404
-                ]
405
-            )
406
-        );
407
-        echo EEH_Form_Fields::select(
408
-            esc_html__('Event Category:', 'event_espresso'),
409
-            $instance['category_name'],
410
-            $event_categories,
411
-            $this->fieldName('category_name'),
412
-            $this->fieldID('category_name')
413
-        );
414
-    }
415
-
416
-
417
-    /**
418
-     * @param array $instance
419
-     * @since   4.10.14.p
420
-     */
421
-    private function imageSizeSelector(array $instance)
422
-    {
423
-        $image_sizes = [];
424
-        $sizes       = get_intermediate_image_sizes();
425
-        if ($sizes) {
426
-            // loop thru images and create option objects out of them
427
-            foreach ($sizes as $image_size) {
428
-                $image_size = trim($image_size);
429
-                // no big images plz
430
-                if (! in_array($image_size, ['large', 'post-thumbnail'])) {
431
-                    $image_sizes[] =
432
-                        EE_Question_Option::new_instance(['QSO_value' => $image_size, 'QSO_desc' => $image_size]);
433
-                }
434
-            }
435
-            $image_sizes[] =
436
-                EE_Question_Option::new_instance(
437
-                    ['QSO_value' => 'none', 'QSO_desc' => esc_html__('don\'t show images', 'event_espresso')]
438
-                );
439
-        }
440
-        echo EEH_Form_Fields::select(
441
-            esc_html__('Image Size:', 'event_espresso'),
442
-            $instance['image_size'],
443
-            $image_sizes,
444
-            $this->fieldName('image_size'),
445
-            $this->fieldID('image_size')
446
-        );
447
-    }
448
-
449
-
450
-    /**
451
-     * @param array $instance
452
-     * @since   4.10.14.p
453
-     */
454
-    private function parseWidgetSettings(array $instance)
455
-    {
456
-        $this->title = isset($instance['title']) && ! empty($instance['title']) ? $instance['title'] : '';
457
-        $this->events_category     = isset($instance['category_name']) && ! empty($instance['category_name'])
458
-            ? $instance['category_name']
459
-            : false;
460
-        $this->show_expired = isset($instance['show_expired'])
461
-            ? filter_var($instance['show_expired'], FILTER_VALIDATE_BOOLEAN)
462
-            : 0;
463
-        $this->image_size   = isset($instance['image_size']) && ! empty($instance['image_size'])
464
-            ? $instance['image_size']
465
-            : 'medium';
466
-        $this->show_desc    = ! isset($instance['show_desc'])
467
-                              || filter_var($instance['show_desc'], FILTER_VALIDATE_BOOLEAN);
468
-        $this->show_dates   = ! isset($instance['show_dates'])
469
-                              || filter_var($instance['show_dates'], FILTER_VALIDATE_BOOLEAN);
470
-        $this->date_limit   = isset($instance['date_limit']) && ! empty($instance['date_limit'])
471
-            ? $instance['date_limit']
472
-            : null;
473
-        $this->date_range   = isset($instance['date_range']) && ! empty($instance['date_range'])
474
-            ? $instance['date_range']
475
-            : false;
476
-        $this->limit        = isset($instance['limit']) ? absint($instance['limit']) : 10;
477
-        $this->order        = isset($instance['sort']) && $instance['sort'] === 'DESC'
478
-            ? 'DESC'
479
-            : 'ASC';
480
-    }
481
-
482
-
483
-    /**
484
-     * @return mixed|void
485
-     * @since   4.10.14.p
486
-     */
487
-    private function widgetTitle()
488
-    {
489
-        // add function to make the title a link
490
-        add_filter('widget_title', [$this, 'make_the_title_a_link'], 15);
491
-        // filter the title
492
-        $title = apply_filters('widget_title', $this->title);
493
-        // remove the function from the filter, so it does not affect other widgets
494
-        remove_filter('widget_title', [$this, 'make_the_title_a_link'], 15);
495
-        return $title;
496
-    }
497
-
498
-
499
-    /**
500
-     * @param WP_Post $post
501
-     * @return string
502
-     * @throws EE_Error
503
-     * @throws ReflectionException
504
-     * @since   4.10.14.p
505
-     */
506
-    private function widgetContent(WP_Post $post)
507
-    {
508
-        // run the query
509
-        $events = $this->getUpcomingEvents();
510
-        if (empty($events)) {
511
-            return '';
512
-        }
513
-        $list_items = '';
514
-        foreach ($events as $event) {
515
-            if ($event instanceof EE_Event && (! is_single() || $post->ID != $event->ID())) {
516
-                $event_url = $this->eventUrl($event);
517
-                $list_items .= '
264
+	}
265
+
266
+
267
+	/**
268
+	 * Sanitize widget form values as they are saved.
269
+	 *
270
+	 * @param array $new_instance Values just sent to be saved.
271
+	 * @param array $old_instance Previously saved values from database.
272
+	 *
273
+	 * @return array Updated safe values to be saved.
274
+	 * @see WP_Widget::update()
275
+	 *
276
+	 */
277
+	public function update($new_instance, $old_instance)
278
+	{
279
+		$instance                    = $old_instance;
280
+		$instance['title']           = ! empty($new_instance['title']) ? strip_tags($new_instance['title']) : '';
281
+		$instance['category_name']   = $new_instance['category_name'];
282
+		$instance['show_expired']    = $new_instance['show_expired'];
283
+		$instance['limit']           = $new_instance['limit'];
284
+		$instance['sort']            = $new_instance['sort'];
285
+		$instance['image_size']      = $new_instance['image_size'];
286
+		$instance['show_desc']       = $new_instance['show_desc'];
287
+		$instance['show_dates']      = $new_instance['show_dates'];
288
+		$instance['show_everywhere'] = $new_instance['show_everywhere'];
289
+		$instance['date_limit']      = $new_instance['date_limit'];
290
+		$instance['date_range']      = $new_instance['date_range'];
291
+		return $instance;
292
+	}
293
+
294
+
295
+	/**
296
+	 * Front-end display of widget.
297
+	 *
298
+	 * @param array $args     Widget arguments.
299
+	 * @param array $instance Saved values from database.
300
+	 * @throws EE_Error
301
+	 * @throws ReflectionException
302
+	 * @see WP_Widget::widget()
303
+	 *
304
+	 */
305
+	public function widget($args, $instance)
306
+	{
307
+
308
+		global $post;
309
+		// make sure there is some kinda post object
310
+		if ($post instanceof WP_Post) {
311
+			$before_widget = '';
312
+			$before_title  = '';
313
+			$after_title   = '';
314
+			$after_widget  = '';
315
+			// but NOT an events archives page, cuz that would be like two event lists on the same page
316
+			$show_everywhere = ! isset($instance['show_everywhere']) || absint($instance['show_everywhere']);
317
+			if ($show_everywhere || ! ($post->post_type == 'espresso_events' && is_archive())) {
318
+				// let's use some of the event helper functions'
319
+				// make separate vars out of attributes
320
+				extract($args);
321
+
322
+				// grab widget settings
323
+				$this->parseWidgetSettings($instance);
324
+				$title = $this->widgetTitle();
325
+
326
+				// Before widget (defined by themes).
327
+				echo $before_widget;
328
+				// Display the widget title if one was input (before and after defined by themes).
329
+				if (! empty($title)) {
330
+					echo $before_title . $title . $after_title;
331
+				}
332
+				echo $this->widgetContent($post);
333
+				// After widget (defined by themes).
334
+				echo $after_widget;
335
+			}
336
+		}
337
+	}
338
+
339
+
340
+	/**
341
+	 * make_the_title_a_link
342
+	 * callback for widget_title filter
343
+	 *
344
+	 * @param $title
345
+	 * @return string
346
+	 */
347
+	public function make_the_title_a_link($title)
348
+	{
349
+		return '<a href="' . EEH_Event_View::event_archive_url() . '">' . $title . '</a>';
350
+	}
351
+
352
+
353
+	/**
354
+	 * @param string $field_name
355
+	 * @return string
356
+	 * @since   4.10.14.p
357
+	 */
358
+	public function fieldID($field_name)
359
+	{
360
+		return esc_attr(parent::get_field_id($field_name));
361
+	}
362
+
363
+
364
+	/**
365
+	 * @param string $field_name
366
+	 * @return string
367
+	 * @since   4.10.14.p
368
+	 */
369
+	public function fieldName($field_name)
370
+	{
371
+		return esc_attr(parent::get_field_name($field_name));
372
+	}
373
+
374
+
375
+	/**
376
+	 * @param array $instance
377
+	 * @throws EE_Error
378
+	 * @throws ReflectionException
379
+	 * @since   4.10.14.p
380
+	 */
381
+	private function eventCategoriesSelector(array $instance)
382
+	{
383
+		$event_categories = [];
384
+		$categories       = EEM_Term::instance()->get_all_ee_categories(true);
385
+		if ($categories) {
386
+			foreach ($categories as $category) {
387
+				if ($category instanceof EE_Term) {
388
+					$event_categories[] =
389
+						EE_Question_Option::new_instance(
390
+							[
391
+								'QSO_value' => $category->get('slug'),
392
+								'QSO_desc'  => $category->get('name'),
393
+							]
394
+						);
395
+				}
396
+			}
397
+		}
398
+		array_unshift(
399
+			$event_categories,
400
+			EE_Question_Option::new_instance(
401
+				[
402
+					'QSO_value' => '',
403
+					'QSO_desc'  => esc_html__(' - display all - ', 'event_espresso'),
404
+				]
405
+			)
406
+		);
407
+		echo EEH_Form_Fields::select(
408
+			esc_html__('Event Category:', 'event_espresso'),
409
+			$instance['category_name'],
410
+			$event_categories,
411
+			$this->fieldName('category_name'),
412
+			$this->fieldID('category_name')
413
+		);
414
+	}
415
+
416
+
417
+	/**
418
+	 * @param array $instance
419
+	 * @since   4.10.14.p
420
+	 */
421
+	private function imageSizeSelector(array $instance)
422
+	{
423
+		$image_sizes = [];
424
+		$sizes       = get_intermediate_image_sizes();
425
+		if ($sizes) {
426
+			// loop thru images and create option objects out of them
427
+			foreach ($sizes as $image_size) {
428
+				$image_size = trim($image_size);
429
+				// no big images plz
430
+				if (! in_array($image_size, ['large', 'post-thumbnail'])) {
431
+					$image_sizes[] =
432
+						EE_Question_Option::new_instance(['QSO_value' => $image_size, 'QSO_desc' => $image_size]);
433
+				}
434
+			}
435
+			$image_sizes[] =
436
+				EE_Question_Option::new_instance(
437
+					['QSO_value' => 'none', 'QSO_desc' => esc_html__('don\'t show images', 'event_espresso')]
438
+				);
439
+		}
440
+		echo EEH_Form_Fields::select(
441
+			esc_html__('Image Size:', 'event_espresso'),
442
+			$instance['image_size'],
443
+			$image_sizes,
444
+			$this->fieldName('image_size'),
445
+			$this->fieldID('image_size')
446
+		);
447
+	}
448
+
449
+
450
+	/**
451
+	 * @param array $instance
452
+	 * @since   4.10.14.p
453
+	 */
454
+	private function parseWidgetSettings(array $instance)
455
+	{
456
+		$this->title = isset($instance['title']) && ! empty($instance['title']) ? $instance['title'] : '';
457
+		$this->events_category     = isset($instance['category_name']) && ! empty($instance['category_name'])
458
+			? $instance['category_name']
459
+			: false;
460
+		$this->show_expired = isset($instance['show_expired'])
461
+			? filter_var($instance['show_expired'], FILTER_VALIDATE_BOOLEAN)
462
+			: 0;
463
+		$this->image_size   = isset($instance['image_size']) && ! empty($instance['image_size'])
464
+			? $instance['image_size']
465
+			: 'medium';
466
+		$this->show_desc    = ! isset($instance['show_desc'])
467
+							  || filter_var($instance['show_desc'], FILTER_VALIDATE_BOOLEAN);
468
+		$this->show_dates   = ! isset($instance['show_dates'])
469
+							  || filter_var($instance['show_dates'], FILTER_VALIDATE_BOOLEAN);
470
+		$this->date_limit   = isset($instance['date_limit']) && ! empty($instance['date_limit'])
471
+			? $instance['date_limit']
472
+			: null;
473
+		$this->date_range   = isset($instance['date_range']) && ! empty($instance['date_range'])
474
+			? $instance['date_range']
475
+			: false;
476
+		$this->limit        = isset($instance['limit']) ? absint($instance['limit']) : 10;
477
+		$this->order        = isset($instance['sort']) && $instance['sort'] === 'DESC'
478
+			? 'DESC'
479
+			: 'ASC';
480
+	}
481
+
482
+
483
+	/**
484
+	 * @return mixed|void
485
+	 * @since   4.10.14.p
486
+	 */
487
+	private function widgetTitle()
488
+	{
489
+		// add function to make the title a link
490
+		add_filter('widget_title', [$this, 'make_the_title_a_link'], 15);
491
+		// filter the title
492
+		$title = apply_filters('widget_title', $this->title);
493
+		// remove the function from the filter, so it does not affect other widgets
494
+		remove_filter('widget_title', [$this, 'make_the_title_a_link'], 15);
495
+		return $title;
496
+	}
497
+
498
+
499
+	/**
500
+	 * @param WP_Post $post
501
+	 * @return string
502
+	 * @throws EE_Error
503
+	 * @throws ReflectionException
504
+	 * @since   4.10.14.p
505
+	 */
506
+	private function widgetContent(WP_Post $post)
507
+	{
508
+		// run the query
509
+		$events = $this->getUpcomingEvents();
510
+		if (empty($events)) {
511
+			return '';
512
+		}
513
+		$list_items = '';
514
+		foreach ($events as $event) {
515
+			if ($event instanceof EE_Event && (! is_single() || $post->ID != $event->ID())) {
516
+				$event_url = $this->eventUrl($event);
517
+				$list_items .= '
518 518
                 <li id="ee-upcoming-events-widget-li-' . esc_attr($event->ID()) . '" 
519 519
                     class="ee-upcoming-events-widget-li"
520 520
                 >
@@ -525,194 +525,194 @@  discard block
 block discarded – undo
525 525
                     </h5>
526 526
                     ' . $this->eventWidgetContent($event, $event_url) . '
527 527
                 </li>';
528
-            }
529
-        }
530
-        return '
528
+			}
529
+		}
530
+		return '
531 531
             <ul class="ee-upcoming-events-widget-ul">
532 532
                 ' . $list_items . '
533 533
             </ul>';
534
-    }
535
-
536
-
537
-    /**
538
-     * @param EE_Event $event
539
-     * @return string|null
540
-     * @throws EE_Error
541
-     * @since   4.10.14.p
542
-     */
543
-    private function eventUrl(EE_Event $event)
544
-    {
545
-        return esc_url_raw(
546
-            apply_filters(
547
-                'FHEE_EEW_Upcoming_Events__widget__event_url',
548
-                $event->get_permalink(),
549
-                $event
550
-            )
551
-        );
552
-    }
553
-
554
-
555
-    /**
556
-     * @return EE_Base_Class[]
557
-     * @throws EE_Error
558
-     */
559
-    private function getUpcomingEvents()
560
-    {
561
-        return EEM_Event::instance()->get_all(
562
-            [
563
-                $this->queryWhereParams(),
564
-                'limit'    => '0,' . $this->limit,
565
-                'order_by' => 'Datetime.DTT_EVT_start',
566
-                'order'    => $this->order,
567
-                'group_by' => 'EVT_ID',
568
-            ]
569
-        );
570
-    }
571
-
572
-
573
-    /**
574
-     * @return mixed|void
575
-     * @throws EE_Error
576
-     * @since   4.10.14.p
577
-     */
578
-    private function queryWhereParams()
579
-    {
580
-        // start to build our where clause
581
-        $where = [
582
-            'status' => ['IN', ['publish', 'sold_out']],
583
-        ];
584
-        // add category
585
-        if ($this->events_category) {
586
-            $where['Term_Taxonomy.taxonomy']  = 'espresso_event_categories';
587
-            $where['Term_Taxonomy.Term.slug'] = $this->events_category;
588
-        }
589
-        // if NOT expired then we want events that start today or in the future
590
-        // if NOT show expired then we want events that start today or in the future
591
-        if ($this->show_expired == 0) {
592
-            $where['Datetime.DTT_EVT_end'] = [
593
-                '>=',
594
-                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
595
-            ];
596
-        }
597
-        // if show ONLY expired we want events that ended prior to today
598
-        if ($this->show_expired == 2) {
599
-            $where['Datetime.DTT_EVT_end'] = [
600
-                '<=',
601
-                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
602
-            ];
603
-        }
604
-        // allow $where to be filtered
605
-        return apply_filters('FHEE__EEW_Upcoming_Events__widget__where', $where, $this->events_category, $this->show_expired);
606
-    }
607
-
608
-
609
-    /**
610
-     * @param EE_Event $event
611
-     * @return string
612
-     * @throws EE_Error
613
-     * @throws ReflectionException
614
-     * @since   4.10.14.p
615
-     */
616
-    private function linkClass(EE_Event $event)
617
-    {
618
-        // how big is the event name ?
619
-        $name_length = strlen($event->name());
620
-        switch ($name_length) {
621
-            case $name_length > 70:
622
-                return ' three-line';
623
-            case $name_length > 35:
624
-                return ' two-line';
625
-        }
626
-        return ' one-line';
627
-    }
628
-
629
-
630
-    /**
631
-     * @param EE_Event $event
632
-     * @param string   $event_url
633
-     * @return mixed|string|void
634
-     * @throws EE_Error
635
-     * @throws ReflectionException
636
-     * @since   4.10.14.p
637
-     */
638
-    private function eventWidgetContent(EE_Event $event, $event_url = '')
639
-    {
640
-        if (post_password_required($event->ID())) {
641
-            return apply_filters(
642
-                'FHEE_EEW_Upcoming_Events__widget__password_form',
643
-                get_the_password_form($event->ID()),
644
-                $event
645
-            );
646
-        }
647
-
648
-        $content = '';
649
-        if (has_post_thumbnail($event->ID()) && $this->image_size != 'none') {
650
-            $content .= '
534
+	}
535
+
536
+
537
+	/**
538
+	 * @param EE_Event $event
539
+	 * @return string|null
540
+	 * @throws EE_Error
541
+	 * @since   4.10.14.p
542
+	 */
543
+	private function eventUrl(EE_Event $event)
544
+	{
545
+		return esc_url_raw(
546
+			apply_filters(
547
+				'FHEE_EEW_Upcoming_Events__widget__event_url',
548
+				$event->get_permalink(),
549
+				$event
550
+			)
551
+		);
552
+	}
553
+
554
+
555
+	/**
556
+	 * @return EE_Base_Class[]
557
+	 * @throws EE_Error
558
+	 */
559
+	private function getUpcomingEvents()
560
+	{
561
+		return EEM_Event::instance()->get_all(
562
+			[
563
+				$this->queryWhereParams(),
564
+				'limit'    => '0,' . $this->limit,
565
+				'order_by' => 'Datetime.DTT_EVT_start',
566
+				'order'    => $this->order,
567
+				'group_by' => 'EVT_ID',
568
+			]
569
+		);
570
+	}
571
+
572
+
573
+	/**
574
+	 * @return mixed|void
575
+	 * @throws EE_Error
576
+	 * @since   4.10.14.p
577
+	 */
578
+	private function queryWhereParams()
579
+	{
580
+		// start to build our where clause
581
+		$where = [
582
+			'status' => ['IN', ['publish', 'sold_out']],
583
+		];
584
+		// add category
585
+		if ($this->events_category) {
586
+			$where['Term_Taxonomy.taxonomy']  = 'espresso_event_categories';
587
+			$where['Term_Taxonomy.Term.slug'] = $this->events_category;
588
+		}
589
+		// if NOT expired then we want events that start today or in the future
590
+		// if NOT show expired then we want events that start today or in the future
591
+		if ($this->show_expired == 0) {
592
+			$where['Datetime.DTT_EVT_end'] = [
593
+				'>=',
594
+				EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
595
+			];
596
+		}
597
+		// if show ONLY expired we want events that ended prior to today
598
+		if ($this->show_expired == 2) {
599
+			$where['Datetime.DTT_EVT_end'] = [
600
+				'<=',
601
+				EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
602
+			];
603
+		}
604
+		// allow $where to be filtered
605
+		return apply_filters('FHEE__EEW_Upcoming_Events__widget__where', $where, $this->events_category, $this->show_expired);
606
+	}
607
+
608
+
609
+	/**
610
+	 * @param EE_Event $event
611
+	 * @return string
612
+	 * @throws EE_Error
613
+	 * @throws ReflectionException
614
+	 * @since   4.10.14.p
615
+	 */
616
+	private function linkClass(EE_Event $event)
617
+	{
618
+		// how big is the event name ?
619
+		$name_length = strlen($event->name());
620
+		switch ($name_length) {
621
+			case $name_length > 70:
622
+				return ' three-line';
623
+			case $name_length > 35:
624
+				return ' two-line';
625
+		}
626
+		return ' one-line';
627
+	}
628
+
629
+
630
+	/**
631
+	 * @param EE_Event $event
632
+	 * @param string   $event_url
633
+	 * @return mixed|string|void
634
+	 * @throws EE_Error
635
+	 * @throws ReflectionException
636
+	 * @since   4.10.14.p
637
+	 */
638
+	private function eventWidgetContent(EE_Event $event, $event_url = '')
639
+	{
640
+		if (post_password_required($event->ID())) {
641
+			return apply_filters(
642
+				'FHEE_EEW_Upcoming_Events__widget__password_form',
643
+				get_the_password_form($event->ID()),
644
+				$event
645
+			);
646
+		}
647
+
648
+		$content = '';
649
+		if (has_post_thumbnail($event->ID()) && $this->image_size != 'none') {
650
+			$content .= '
651 651
                 <div class="ee-upcoming-events-widget-img-dv">
652 652
                     <a class="ee-upcoming-events-widget-img" href="' . $event_url . '">
653 653
                         ' . get_the_post_thumbnail($event->ID(), $this->image_size) . '
654 654
                     </a>
655 655
                 </div>';
656
-        }
657
-
658
-        if ($this->show_dates) {
659
-            $content .= $this->eventDates($event);
660
-        }
661
-
662
-        if ($this->show_desc) {
663
-            global $allowedtags;
664
-            $desc    = $event->short_description(25);
665
-            $content .= $desc ? '<p style="margin-top: .5em">' . wp_kses($desc, $allowedtags) . '</p>' : '';
666
-        }
667
-
668
-        return $content;
669
-    }
670
-
671
-
672
-    /**
673
-     * @param EE_Event $event
674
-     * @return string
675
-     * @throws EE_Error
676
-     * @throws ReflectionException
677
-     * @since   4.10.14.p
678
-     */
679
-    private function eventDates(EE_Event $event)
680
-    {
681
-        $date_format        = apply_filters(
682
-            'FHEE__espresso_event_date_range__date_format',
683
-            get_option('date_format')
684
-        );
685
-        $time_format        = apply_filters(
686
-            'FHEE__espresso_event_date_range__time_format',
687
-            get_option('time_format')
688
-        );
689
-        $single_date_format = apply_filters(
690
-            'FHEE__espresso_event_date_range__single_date_format',
691
-            get_option('date_format')
692
-        );
693
-        $single_time_format = apply_filters(
694
-            'FHEE__espresso_event_date_range__single_time_format',
695
-            get_option('time_format')
696
-        );
697
-        if ($this->date_range == true) {
698
-            return espresso_event_date_range(
699
-                $date_format,
700
-                $time_format,
701
-                $single_date_format,
702
-                $single_time_format,
703
-                $event->ID(),
704
-                false
705
-            );
706
-        }
707
-        return espresso_list_of_event_dates(
708
-            $event->ID(),
709
-            $date_format,
710
-            $time_format,
711
-            false,
712
-            null,
713
-            true,
714
-            true,
715
-            $this->date_limit
716
-        );
717
-    }
656
+		}
657
+
658
+		if ($this->show_dates) {
659
+			$content .= $this->eventDates($event);
660
+		}
661
+
662
+		if ($this->show_desc) {
663
+			global $allowedtags;
664
+			$desc    = $event->short_description(25);
665
+			$content .= $desc ? '<p style="margin-top: .5em">' . wp_kses($desc, $allowedtags) . '</p>' : '';
666
+		}
667
+
668
+		return $content;
669
+	}
670
+
671
+
672
+	/**
673
+	 * @param EE_Event $event
674
+	 * @return string
675
+	 * @throws EE_Error
676
+	 * @throws ReflectionException
677
+	 * @since   4.10.14.p
678
+	 */
679
+	private function eventDates(EE_Event $event)
680
+	{
681
+		$date_format        = apply_filters(
682
+			'FHEE__espresso_event_date_range__date_format',
683
+			get_option('date_format')
684
+		);
685
+		$time_format        = apply_filters(
686
+			'FHEE__espresso_event_date_range__time_format',
687
+			get_option('time_format')
688
+		);
689
+		$single_date_format = apply_filters(
690
+			'FHEE__espresso_event_date_range__single_date_format',
691
+			get_option('date_format')
692
+		);
693
+		$single_time_format = apply_filters(
694
+			'FHEE__espresso_event_date_range__single_time_format',
695
+			get_option('time_format')
696
+		);
697
+		if ($this->date_range == true) {
698
+			return espresso_event_date_range(
699
+				$date_format,
700
+				$time_format,
701
+				$single_date_format,
702
+				$single_time_format,
703
+				$event->ID(),
704
+				false
705
+			);
706
+		}
707
+		return espresso_list_of_event_dates(
708
+			$event->ID(),
709
+			$date_format,
710
+			$time_format,
711
+			false,
712
+			null,
713
+			true,
714
+			true,
715
+			$this->date_limit
716
+		);
717
+	}
718 718
 }
Please login to merge, or discard this patch.