Completed
Push — master ( f805dd...62b222 )
by
unknown
03:34
created

Default_Calendar_List::get_heading()   D

Complexity

Conditions 20
Paths 48

Size

Total Lines 88
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 57
nc 48
nop 0
dl 0
loc 88
rs 4.7294
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Default Calendar - List View
4
 *
5
 * @package SimpleCalendar/Calendars
6
 */
7
namespace SimpleCalendar\Calendars\Views;
8
9
use Carbon\Carbon;
10
use Mexitek\PHPColors\Color;
11
use SimpleCalendar\Abstracts\Calendar;
12
use SimpleCalendar\Abstracts\Calendar_View;
13
use SimpleCalendar\Calendars\Default_Calendar;
14
use SimpleCalendar\Events\Event;
15
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Default Calendar: List View.
22
 *
23
 * @since  3.0.0
24
 */
25
class Default_Calendar_List implements Calendar_View {
26
27
	/**
28
	 * Calendar.
29
	 *
30
	 * @access public
31
	 * @var Default_Calendar
32
	 */
33
	public $calendar = null;
34
35
	/**
36
	 * Current display start.
37
	 *
38
	 * @access private
39
	 * @var int
40
	 */
41
	private $start = 0;
42
43
	private $first_event = 0;
44
45
	private $last_event = 0;
46
47
	/**
48
	 * Current display end.
49
	 *
50
	 * @access private
51
	 * @var int
52
	 */
53
	private $end = 0;
54
55
	/**
56
	 * Previous display start.
57
	 *
58
	 * @access private
59
	 * @var int
60
	 */
61
	private $prev = 0;
62
63
	/**
64
	 * Next display start.
65
	 *
66
	 * @access private
67
	 * @var int
68
	 */
69
	private $next = 0;
70
71
	/**
72
	 * Constructor.
73
	 *
74
	 * @since 3.0.0
75
	 *
76
	 * @param string|Calendar $calendar
77
	 */
78
	public function __construct( $calendar = '' ) {
79
		$this->calendar = $calendar;
0 ignored issues
show
Documentation Bug introduced by
It seems like $calendar of type string or object<SimpleCalendar\Abstracts\Calendar> is incompatible with the declared type object<SimpleCalendar\Calendars\Default_Calendar> of property $calendar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
	}
81
82
	/**
83
	 * Get the view parent calendar type.
84
	 *
85
	 * @since  3.0.0
86
	 *
87
	 * @return string
88
	 */
89
	public function get_parent() {
90
		return 'default-calendar';
91
	}
92
93
	/**
94
	 * Get the view type.
95
	 *
96
	 * @since  3.0.0
97
	 *
98
	 * @return string
99
	 */
100
	public function get_type() {
101
		return 'list';
102
	}
103
104
	/**
105
	 * Get the view name.
106
	 *
107
	 * @since  3.0.0
108
	 *
109
	 * @return string
110
	 */
111
	public function get_name() {
112
		return __( 'List', 'google-calendar-events' );
113
	}
114
115
	/**
116
	 * Add ajax actions.
117
	 *
118
	 * @since 3.0.0
119
	 */
120
	public function add_ajax_actions() {
121
		add_action( 'wp_ajax_simcal_default_calendar_draw_list', array( $this, 'draw_list_ajax' ) );
122
		add_action( 'wp_ajax_nopriv_simcal_default_calendar_draw_list', array( $this, 'draw_list_ajax' ) );
123
	}
124
125
	/**
126
	 * Default calendar list scripts.
127
	 *
128
	 * Scripts to load when this view is displayed.
129
	 *
130
	 * @since  3.0.0
131
	 *
132
	 * @param  string $min
133
	 *
134
	 * @return array
135
	 */
136
	public function scripts( $min = '' ) {
137
		return array(
138
			'simcal-qtip' => array(
139
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/qtip' . $min . '.js',
140
				'deps'      => array( 'jquery' ),
141
				'ver'       => '2.2.1',
142
				'in_footer' => true,
143
			),
144
			'simcal-default-calendar' => array(
145
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/default-calendar' . $min . '.js',
146
				'deps'      => array(
147
					'jquery',
148
					'simcal-qtip',
149
				),
150
				'var'       => SIMPLE_CALENDAR_VERSION,
151
				'in_footer' => true,
152
				'localize'  => array(
153
					'simcal_default_calendar' => simcal_common_scripts_variables(),
154
				),
155
			),
156
		);
157
	}
158
159
	/**
160
	 * Default calendar list styles.
161
	 *
162
	 * Stylesheets to load when this view is displayed.
163
	 *
164
	 * @since  3.0.0
165
	 *
166
	 * @param  string $min = ''
167
	 *
168
	 * @return array
169
	 */
170
	public function styles( $min = '' ) {
171
		return array(
172
			'simcal-default-calendar-list' => array(
173
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-list' . $min . '.css',
174
				'ver'   => SIMPLE_CALENDAR_VERSION,
175
				'media' => 'all',
176
			),
177
		);
178
	}
179
180
	/**
181
	 * Default calendar list markup.
182
	 *
183
	 * @since 3.0.0
184
	 */
185
	public function html() {
186
187
		$calendar = $this->calendar;
188
189
		if ( $calendar instanceof Default_Calendar ) {
190
191
			$disabled = $calendar->static === true ? ' disabled="disabled"' : '';
192
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
193
194
			$hide_header = get_post_meta( $this->calendar->id, '_default_calendar_list_header', true ) == 'yes' ? true : false;
195
			$static_calendar = get_post_meta( $this->calendar->id, '_calendar_is_static', true ) == 'yes' ? true : false;
196
197
			$header_class = '';
198
			$compact_list_class = $calendar->compact_list ? 'simcal-calendar-list-compact' : '';
199
200
			edit_post_link( __( 'Edit Calendar', 'google-calendar-events' ), '<p class="simcal-align-right"><small>', '</small></p>', $calendar->id );
201
202
			echo '<div class="simcal-calendar-list ' . $compact_list_class . '">';
203
204
			if ( ! $hide_header && ! $static_calendar ) {
205
				echo '<nav class="simcal-calendar-head">' . "\n";
206
207
				echo "\t" . '<div class="simcal-nav">' . "\n";
208
				echo "\t\t" . '<button class="simcal-nav-button simcal-prev" title="' . __('Previous', 'google-calendar-events') . '"' . $disabled . '>' . "\n";
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
209
				echo "\t\t\t" . '<i class="simcal-icon-left"></i>' . "\n";
210
				echo "\t\t" . '</button>' . "\n";
211
				echo "\t" . '</div>' . "\n";
212
213
				if ( $hide_header ) {
214
					$header_class = 'simcal-hide-header';
215
				}
216
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
217
218
				echo "\t" . '<div class="simcal-nav simcal-current ' . $header_class . '" data-calendar-current="' . $calendar->start . '">' . "\n";
219
				echo "\t\t" . '<h3 class="simcal-current-label"> </h3>' . "\n";
220
				echo "\t" . '</div>' . "\n";
221
222
				echo "\t" . '<div class="simcal-nav">';
223
				echo "\t\t" . '<button class="simcal-nav-button simcal-next" title="' . __('Next', 'google-calendar-events') . '"' . $disabled . '>';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
224
				echo "\t\t\t" . '<i class="simcal-icon-right"></i>' . "\n";
225
				echo "\t\t" . '</button>' . "\n";
226
				echo "\t" . '</div>' . "\n";
227
228
				echo '</nav>' . "\n";
229
			}
230
231
			echo $this->draw_list( $calendar->start );
232
233
			echo '<div class="simcal-ajax-loader simcal-spinner-top" style="display: none;"><i class="simcal-icon-spinner simcal-icon-spin"></i></div>';
234
235
			echo '</div>';
236
237
		}
238
239
	}
240
241
	/**
242
	 * Get events for current display.
243
	 *
244
	 * @since  3.0.0
245
	 * @access private
246
	 *
247
	 * @param  int $timestamp
248
	 *
249
	 * @return array
250
	 */
251
	private function get_events( $timestamp ) {
252
253
		$calendar = $this->calendar;
254
		$timezone = $calendar->timezone;
255
256
		if ( ! $calendar->group_type || ! $calendar->group_span ) {
257
			return array();
258
		}
259
260
		$current = Carbon::createFromTimestamp( $timestamp, $timezone );
261
		$prev = clone $current;
262
		$next = clone $current;
263
264
		$this->start = $current->getTimestamp();
265
266
		$interval = $span = max( absint( $calendar->group_span ), 1 );
267
268
		if ( 'monthly' == $calendar->group_type ) {
269
			$this->prev = $prev->subMonths( $span )->getTimestamp();
270
			$this->next = $next->addMonths( $span )->getTimestamp();
271
		} elseif ( 'weekly' == $calendar->group_type ) {
272
			$week = new Carbon( $calendar->timezone );
273
			$week->setTimestamp( $timestamp );
274
			$week->setWeekStartsAt( $calendar->week_starts );
275
			$this->prev = $prev->subWeeks( $span )->getTimestamp();
276
			$this->next = $next->addWeeks( $span )->getTimestamp();
277
		} elseif ( 'daily' == $calendar->group_type ) {
278
			$this->prev = $prev->subDays( $span )->getTimestamp();
279
			$this->next = $next->addDays( $span )->getTimestamp();
280
		}
281
282
		$events = $calendar->events;
283
		$daily_events = $paged_events = $flattened_events = array();
284
285
		if ( 'events' != $calendar->group_type ) {
286
287
			$this->end   = $this->next - 1;
288
289
			$timestamps   = array_keys( $events );
290
			$lower_bound  = array_filter( $timestamps,  array( $this, 'filter_events_before' ) );
291
			$higher_bound = array_filter( $lower_bound, array( $this, 'filter_events_after'  ) );
292
293
			if ( is_array( $higher_bound ) && !empty( $higher_bound ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
294
				$filtered = array_intersect_key( $events, array_combine( $higher_bound, $higher_bound ) );
295
				foreach ( $filtered as $timestamp => $events ) {
296
					$paged_events[ intval( $timestamp ) ] = $events;
297
				}
298
			}
299
300
		} else {
301
302
			foreach ( $events as $timestamp => $e ) {
303
				$second = 0;
304
				foreach ( $e as $event ) {
305
					$flattened_events[ intval( $timestamp + $second ) ][] = $event;
306
					$second++;
307
				}
308
			}
309
			ksort( $flattened_events, SORT_NUMERIC );
310
311
			$keys  = array_keys( $flattened_events );
312
			$current = 0;
313
			foreach ( $keys as $timestamp ) {
314
				if ( $timestamp < $this->start ) {
315
					$current++;
316
				}
317
			}
318
319
			$paged_events = array_slice( $flattened_events, $current, $interval, true );
320
321
			$events_end = isset( $keys[ $current + $interval ] ) ? $keys[ $current + $interval ] : $calendar->end;
322
			$this->end  = $events_end > $calendar->end ? $calendar->end : $events_end;
0 ignored issues
show
Documentation Bug introduced by
It seems like $events_end > $calendar-...ndar->end : $events_end can also be of type string. However, the property $end is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
323
324
			$this->prev = isset( $keys[ $current - $interval ] ) ? $keys[ $current - $interval ] : $calendar->earliest_event;
0 ignored issues
show
Documentation Bug introduced by
It seems like isset($keys[$current - $...alendar->earliest_event can also be of type string. However, the property $prev is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
325
			$this->next = isset( $keys[ $current + $interval ] ) ? $keys[ $current + $interval ] : $this->end;
0 ignored issues
show
Documentation Bug introduced by
It seems like isset($keys[$current + $...$interval] : $this->end can also be of type string. However, the property $next is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
326
327
		}
328
329
		// Put resulting events in an associative array, with Ymd date as key for easy retrieval in calendar days loop.
330
		foreach ( $paged_events as $timestamp => $events ) {
331
			if ( $timestamp <= $this->end ) {
332
				$date = Carbon::createFromTimestamp( $timestamp, $calendar->timezone )->endOfDay()->format( 'Ymd' );
333
				$daily_events[ intval( $date ) ][] = $events;
334
			}
335
		}
336
		ksort( $daily_events, SORT_NUMERIC );
337
338
		$first_event = array_slice( $paged_events, 0, 1, true );
339
		$first_event = array_pop( $first_event );
340
		$this->first_event = $first_event[0]->start;
341
342
		$last_event = array_pop( $paged_events );
343
		$this->last_event = $last_event[0]->start;
344
345
		return $daily_events;
346
	}
347
348
	/**
349
	 * Get calendar list heading.
350
	 *
351
	 * Parses calender date format and adapts to current display range.
352
	 *
353
	 * @since  3.0.0
354
	 * @access private
355
	 *
356
	 * @return array
357
	 */
358
	private function get_heading() {
359
360
		$calendar = $this->calendar;
361
		$start = Carbon::createFromTimestamp( $calendar->start, $calendar->timezone );
362
		$end = Carbon::createFromTimestamp( $this->end, $calendar->timezone );
363
		$date_format = $this->calendar->date_format;
364
		$date_order  = simcal_get_date_format_order( $date_format );
365
366
		$st = $this->start;
367
		$et = $this->end;
368
369
		if ( $this->first_event !== 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
370
			$start = Carbon::createFromTimestamp( $this->first_event, $calendar->timezone );
371
			$st = $this->first_event;
372
		}
373
374
		if ( $this->last_event !== 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
375
			$end = Carbon::createFromTimestamp( $this->last_event, $calendar->timezone );
376
			$et = $this->last_event;
377
		}
378
379
		if ( ( $start->day == $end->day ) && ( $start->month == $end->month ) && ( $start->year == $end->year ) ) {
380
			// Start and end on the same day.
381
			// e.g. 1 February 2020
382
			$large = $small = date_i18n( $calendar->date_format , $st );
383
			if ( ( $date_order['d'] !== false ) && ( $date_order['m'] !== false ) ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
384
				if ( $date_order['m'] > $date_order['d'] ) {
385
					if ( $date_order['y'] !== false && $date_order['y'] > $date_order['m'] ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
386
						$small = date_i18n( 'Y, d M', $st );
387
					} else {
388
						$small = date_i18n( 'd M Y', $st );
389
					}
390
				} else {
391
					if ( $date_order['y'] !== false && $date_order['y'] > $date_order['m'] ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
392
						$small = date_i18n( 'Y, M d', $st );
393
					} else {
394
						$small = date_i18n( 'M d Y', $st );
395
					}
396
				}
397
			}
398
		} elseif ( ( $start->month == $end->month ) && ( $start->year == $end->year ) ) {
399
			// Start and end days on the same month.
400
			// e.g. August 2020
401
			if ( $date_order['y'] === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
402
				// August.
403
				$large = $small = date_i18n( 'F', $st );
404
			} else {
405
				if ( $date_order['y'] < $date_order['m'] ) {
406
					// 2020 August.
407
					$large = date_i18n( 'Y F', $st );
408
					$small = date_i18n( 'Y M', $st );
409
				} else {
410
					// August 2020.
411
					$large = date_i18n( 'F Y', $st );
412
					$small = date_i18n( 'M Y', $st );
413
				}
414
			}
415
		} elseif ( $start->year == $end->year ) {
416
			// Start and end days on months of the same year.
417
			// e.g. August - September 2020
418
			if ( $date_order['y'] === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
419
				// August - September.
420
				$large = date_i18n( 'F', $st ) . ' - ' . date_i18n( 'F', $et );
421
				$small = date_i18n( 'M', $st ) . ' - ' . date_i18n( 'M', $et );
422
			} else {
423
				if ( $date_order['y'] < $date_order['m'] ) {
424
					// 2020, August - September.
425
					$large  = $small = date( 'Y', $st ) . ', ';
426
					$large .= date_i18n( 'F', $st ) . ' - ' . date_i18n( 'F', $et );
427
					$small .= date_i18n( 'M', $st ) . ' - ' . date_i18n( 'M', $et );
428
				} else {
429
					// August - September, 2020.
430
					$large  = date_i18n( 'F', $st ) . ' - ' . date_i18n( 'F', $et ) . ', ';
431
					$small  = date_i18n( 'M', $st ) . ' - ' . date_i18n( 'M', $et ) . ' ';
432
					$year   = date( 'Y', $st );
433
					$large .= $year;
434
					$small .= $year;
435
				}
436
			}
437
		} else {
438
			$large = $small = date( 'Y', $st ) . ' - ' . date( 'Y', $et );
439
		}
440
441
		return array(
442
			'small' => $small,
443
			'large' => $large,
444
		);
445
	}
446
447
	/**
448
	 * Make a calendar list of events.
449
	 *
450
	 * Outputs a list of events according to events for the specified range.
451
	 *
452
	 * @since  3.0.0
453
	 * @access private
454
	 *
455
	 * @param  int $timestamp
456
	 * @param  int $id
457
	 *
458
	 * @return string
459
	 */
460
	private function draw_list( $timestamp, $id = 0 ) {
461
462
		$calendar = $this->calendar;
463
464
		if ( empty( $calendar ) ) {
465
			$calendar = $this->calendar = simcal_get_calendar( intval( $id ) );
466
			if ( ! $calendar instanceof Default_Calendar ) {
467
				return '';
468
			}
469
		}
470
471
		$feed          = simcal_get_feed( $calendar );
472
		$feed_timezone = get_post_meta( $feed->post_id, '_feed_timezone', true );
473
474
		$now = $calendar->now;
475
		$current_events = $this->get_events( $timestamp );
476
		$day_format = explode( ' ', $calendar->date_format );
477
478
		ob_start();
479
480
		// Draw the events.
481
482
		$block_tag = $calendar->compact_list && ! empty( $current_events ) ? 'div' : 'dl';
483
484
		$data_heading = '';
485
		$heading = $this->get_heading();
486
		foreach ( $heading as $k => $v ) {
487
			$data_heading .= ' data-heading-' . $k . '="' . $v . '"';
488
		}
489
490
		echo '<' . $block_tag . ' class="simcal-events-list-container"' .
491
			' data-prev="' . $this->prev . '"' .
492
			' data-next="' . $this->next . '"' .
493
			$data_heading . '>';
494
495
		if ( ! empty( $current_events ) && is_array( $current_events ) ) :
496
497
			foreach ( $current_events as $ymd => $events ) :
498
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
499
500
501
				// This is where we can find out if an event is a multi-day event and if it needs to be shown.
502
				// Since this is for list view we are showing the event on the day viewed if it is part of that day even when
503
				// expand multi-day events are turned off.
504
				if ( isset( $events[0][0]->multiple_days ) && $events[0][0]->multiple_days > 0 ) {
505
					if ( 'current_day_only' == get_post_meta($calendar->id, '_default_calendar_expand_multi_day_events', true ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
506
507
						$year  = substr( $ymd, 0, 4 );
508
						$month = substr( $ymd, 4, 2 );
509
						$day   = substr( $ymd, 6, 2 );
510
511
						$temp_date = Carbon::createFromDate( $year, $month, $day );
512
513
						if( ! ( $temp_date < Carbon::now()->endOfDay() ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
514
							continue;
515
						}
516
					}
517
				}
518
519
				$day_ts = Carbon::createFromFormat( 'Ymd', $ymd, $calendar->timezone )->startOfDay()->getTimestamp();
520
521
				if ( ! $calendar->compact_list ) :
522
523
					$date = new Carbon( 'now', $calendar->timezone );
524
					$date->setLocale( substr( get_locale(), 0, 2 ) );
525
					$date->setTimestamp( $day_ts );
526
527
					if ( $date->isToday() ) {
528
						$the_color = new Color( $calendar->today_color );
529
					} else {
530
						$the_color = new Color( $calendar->days_events_color );
531
					}
532
533
					$bg_color = '#' . $the_color->getHex();
534
					$color = $the_color->isDark() ? '#ffffff' : '#000000';
535
					$border_style = ' style="border-bottom: 1px solid ' . $bg_color . ';" ';
536
					$bg_style = ' style="background-color: ' . $bg_color . '; color: ' . $color . ';"';
537
538
					echo "\t" . '<dt class="simcal-day-label"' . $border_style . '>';
539
					echo '<span' . $bg_style .'>';
540
					foreach ( $day_format as $format ) {
541
						echo $format ? '<span class="simcal-date-format" data-date-format="' . $format . '">' . date_i18n( $format, $day_ts ) . '</span> ' : ' ';
542
					}
543
					echo '</span>';
544
					echo '</dt>' . "\n";
545
546
				endif;
547
548
				$list_events = '<ul class="simcal-events">' . "\n";
549
550
				$calendar_classes = array();
551
				$day_classes = 'simcal-weekday-' . date( 'w', $day_ts );
552
553
				// Is this the present, the past or the future, Doc?
554
				if ( $timestamp <= $now && $timestamp >= $now ) {
555
					$day_classes .= ' simcal-today simcal-present simcal-day';
556
				} elseif ( $timestamp < $now ) {
557
					$day_classes .= ' simcal-past simcal-day';
558
				} elseif ( $this->end > $now ) {
559
					$day_classes .= ' simcal-future simcal-day';
560
				}
561
562
				$count = 0;
563
564
				foreach ( $events as $day_events ) :
565
					foreach ( $day_events as $event ) :
566
567
						if ( $event instanceof Event ) :
568
569
							if ( $feed->type == 'grouped-calendars' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
570
								date_default_timezone_set( $feed_timezone );
0 ignored issues
show
introduced by
Using date_default_timezone_set() and similar isn’t allowed, instead use WP internal timezone support.
Loading history...
571
							} else {
572
								date_default_timezone_set( $event->timezone );
0 ignored issues
show
introduced by
Using date_default_timezone_set() and similar isn’t allowed, instead use WP internal timezone support.
Loading history...
573
							}
574
575
							$event_classes = $event_visibility = '';
576
577
							$calendar_class     = 'simcal-events-calendar-' . strval( $event->calendar );
578
							$calendar_classes[] = $calendar_class;
579
580
							$recurring     = $event->recurrence ? 'simcal-event-recurring ' : '';
581
							$has_location  = $event->venue ? 'simcal-event-has-location ' : '';
582
583
							$event_classes .= 'simcal-event ' . $recurring . $has_location . $calendar_class;
584
585
							// Toggle some events visibility if more than optional limit.
586
							if ( ( $calendar->events_limit > - 1 ) && ( $count >= $calendar->events_limit ) ) :
587
								$event_classes .= ' simcal-event-toggled';
588
								$event_visibility = ' style="display: none"';
589
							endif;
590
591
							$event_color = '';
0 ignored issues
show
Unused Code introduced by
$event_color is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
592
							$bullet = '';
0 ignored issues
show
Unused Code introduced by
$bullet is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
593
							$event_color = $event->get_color();
594
							if ( ! empty( $event_color ) ) {
595
								$side = is_rtl() ? 'right' : 'left';
596
								$event_color = ' style="border-' . $side . ': 4px solid ' . $event_color . '; padding-' . $side . ': 8px;"';
597
							}
598
599
							$list_events .= "\t" . '<li class="' . $event_classes . '"' . $event_visibility . $event_color . ' itemscope itemtype="http://schema.org/Event" data-start="' . esc_attr( $event->start ) . '">' . "\n";
600
							$list_events .= "\t\t" . '<div class="simcal-event-details">' . $calendar->get_event_html( $event ) . '</div>' . "\n";
601
							$list_events .= "\t" . '</li>' . "\n";
602
603
							$count ++;
604
605
							// Event falls within today.
606
							if ( ( $this->end <= $now ) && ( $this->start >= $now ) ) :
607
								$day_classes .= ' simcal-today-has-events';
608
							endif;
609
							$day_classes .= ' simcal-day-has-events simcal-day-has-' . strval( $count ) . '-events';
610
611
							if ( $calendar_classes ) :
612
								$day_classes .= ' ' . trim( implode( ' ', array_unique( $calendar_classes ) ) );
613
							endif;
614
615
						endif;
616
					endforeach;
617
				endforeach;
618
619
				$list_events .= '</ul>' . "\n";
620
621
				// If events visibility is limited, print the button toggle.
622
				if ( ( $calendar->events_limit > -1 ) && ( $count > $calendar->events_limit ) ) :
623
					$list_events .= '<button class="simcal-events-toggle"><i class="simcal-icon-down simcal-icon-animate"></i></button>';
624
				endif;
625
626
				// Print final list of events for the current day.
627
				$tag = $calendar->compact_list ? 'div' : 'dd';
628
				echo '<'  . $tag . ' class="' . $day_classes . '" data-events-count="' . strval( $count ) . '">' . "\n";
629
				echo "\t" . $list_events . "\n";
630
				echo '</' . $tag . '>' . "\n";
631
632
			endforeach;
633
634
		else :
635
636
			echo "\t" . '<p>';
637
638
			$message = get_post_meta( $calendar->id, '_no_events_message', true );
639
640
			if ( 'events' == $calendar->group_type ) {
641
				echo ! empty( $message ) ? $message : __( 'Nothing to show.', 'google-calendar-events' );
642
			} else {
643
				if ( ! empty( $message ) ) {
644
					echo $message;
645
				} else {
646
					$from = Carbon::createFromTimestamp( $this->start, $calendar->timezone )->getTimestamp();
647
					$to = Carbon::createFromTimestamp( $this->end, $calendar->timezone )->getTimestamp();
648
					echo apply_filters( 'simcal_no_events_message', sprintf(
649
						__( 'Nothing from %1$s to %2$s.', 'google-calendar-events' ),
650
						date_i18n( $calendar->date_format, $from ),
651
						date_i18n( $calendar->date_format, $to )
652
					), $calendar->id, $from, $to );
653
				}
654
			}
655
656
			echo "\t" . '</p>' . "\n";
657
658
		endif;
659
660
		echo '</' . $block_tag . '>';
661
662
		date_default_timezone_set( $calendar->site_timezone );
0 ignored issues
show
introduced by
Using date_default_timezone_set() and similar isn’t allowed, instead use WP internal timezone support.
Loading history...
663
664
		return ob_get_clean();
665
	}
666
667
	/**
668
	 * Ajax callback to request a new page.
669
	 *
670
	 * @since 3.0.0
671
	 */
672
	public function draw_list_ajax() {
673
674
		if ( isset( $_POST['ts'] ) && isset( $_POST['id'] ) ) {
675
676
			$ts = absint( $_POST['ts'] );
677
			$id = absint( $_POST['id'] );
678
679
			wp_send_json_success( $this->draw_list( $ts, $id ) );
680
681
		} else {
682
683
			wp_send_json_error( 'Missing arguments in default calendar list ajax request.' );
684
685
		}
686
	}
687
688
	/**
689
	 * Array filter callback.
690
	 *
691
	 * @since  3.0.0
692
	 * @access private
693
	 *
694
	 * @param  int $event Timestamp.
695
	 *
696
	 * @return bool
697
	 */
698
	private function filter_events_before( $event ) {
699
		return intval( $event ) >= intval( $this->start );
700
	}
701
702
	/**
703
	 * Array filter callback.
704
	 *
705
	 * @since  3.0.0
706
	 * @access private
707
	 *
708
	 * @param  int $event Timestamp.
709
	 *
710
	 * @return bool
711
	 */
712
	private function filter_events_after( $event ) {
713
		return intval( $event ) < intval( $this->end );
714
	}
715
716
}
717