Completed
Push — master ( 71159f...86ffa1 )
by
unknown
06:54
created

Default_Calendar_List::html()   B

Complexity

Conditions 9
Paths 49

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 55
rs 7.2446
c 2
b 0
f 1
cc 9
eloc 31
nc 49
nop 0

How to fix   Long Method   

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
	/**
44
	 * Current display end.
45
	 *
46
	 * @access private
47
	 * @var int
48
	 */
49
	private $end = 0;
50
51
	/**
52
	 * Previous display start.
53
	 *
54
	 * @access private
55
	 * @var int
56
	 */
57
	private $prev = 0;
58
59
	/**
60
	 * Next display start.
61
	 *
62
	 * @access private
63
	 * @var int
64
	 */
65
	private $next = 0;
66
67
	/**
68
	 * Constructor.
69
	 *
70
	 * @since 3.0.0
71
	 *
72
	 * @param string|Calendar $calendar
73
	 */
74
	public function __construct( $calendar = '' ) {
75
		$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...
76
	}
77
78
	/**
79
	 * Get the view parent calendar type.
80
	 *
81
	 * @since  3.0.0
82
	 *
83
	 * @return string
84
	 */
85
	public function get_parent() {
86
		return 'default-calendar';
87
	}
88
89
	/**
90
	 * Get the view type.
91
	 *
92
	 * @since  3.0.0
93
	 *
94
	 * @return string
95
	 */
96
	public function get_type() {
97
		return 'list';
98
	}
99
100
	/**
101
	 * Get the view name.
102
	 *
103
	 * @since  3.0.0
104
	 *
105
	 * @return string
106
	 */
107
	public function get_name() {
108
		return __( 'List', 'google-calendar-events' );
109
	}
110
111
	/**
112
	 * Add ajax actions.
113
	 *
114
	 * @since 3.0.0
115
	 */
116
	public function add_ajax_actions() {
117
		add_action( 'wp_ajax_simcal_default_calendar_draw_list', array( $this, 'draw_list_ajax' ) );
118
		add_action( 'wp_ajax_nopriv_simcal_default_calendar_draw_list', array( $this, 'draw_list_ajax' ) );
119
	}
120
121
	/**
122
	 * Default calendar list scripts.
123
	 *
124
	 * Scripts to load when this view is displayed.
125
	 *
126
	 * @since  3.0.0
127
	 *
128
	 * @param  string $min
129
	 *
130
	 * @return array
131
	 */
132
	public function scripts( $min = '' ) {
133
		return array(
134
			'simcal-qtip' => array(
135
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/qtip' . $min . '.js',
136
				'deps'      => array( 'jquery' ),
137
				'ver'       => '2.2.1',
138
				'in_footer' => true,
139
			),
140
			'simcal-default-calendar' => array(
141
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/default-calendar' . $min . '.js',
142
				'deps'      => array(
143
					'jquery',
144
					'simcal-qtip',
145
				),
146
				'var'       => SIMPLE_CALENDAR_VERSION,
147
				'in_footer' => true,
148
				'localize'  => array(
149
					'simcal_default_calendar' => simcal_common_scripts_variables(),
150
				),
151
			),
152
		);
153
	}
154
155
	/**
156
	 * Default calendar list styles.
157
	 *
158
	 * Stylesheets to load when this view is displayed.
159
	 *
160
	 * @since  3.0.0
161
	 *
162
	 * @param  string $min = ''
163
	 *
164
	 * @return array
165
	 */
166
	public function styles( $min = '' ) {
167
		return array(
168
			'simcal-default-calendar-list' => array(
169
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-list' . $min . '.css',
170
				'ver'   => SIMPLE_CALENDAR_VERSION,
171
				'media' => 'all',
172
			),
173
		);
174
	}
175
176
	/**
177
	 * Default calendar list markup.
178
	 *
179
	 * @since 3.0.0
180
	 */
181
	public function html() {
182
183
		$calendar = $this->calendar;
184
185
		if ( $calendar instanceof Default_Calendar ) {
186
187
			$disabled = $calendar->static === true ? ' disabled="disabled"' : '';
188
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
189
190
			$hide_header = get_post_meta( $this->calendar->id, '_default_calendar_list_header', true ) == 'yes' ? true : false;
191
			$static_calendar = get_post_meta( $this->calendar->id, '_calendar_is_static', true ) == 'yes' ? true : false;
192
193
			$header_class = '';
194
			$compact_list_class = $calendar->compact_list ? 'simcal-calendar-list-compact' : '';
195
196
			edit_post_link( __( 'Edit Calendar', 'google-calendar-events' ), '<p class="simcal-align-right"><small>', '</small></p>', $calendar->id );
197
198
			echo '<div class="simcal-calendar-list ' . $compact_list_class . '">';
199
200
			if ( ! $hide_header && ! $static_calendar ) {
201
				echo '<nav class="simcal-calendar-head">' . "\n";
202
203
				echo "\t" . '<div class="simcal-nav">' . "\n";
204
				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...
205
				echo "\t\t\t" . '<i class="simcal-icon-left"></i>' . "\n";
206
				echo "\t\t" . '</button>' . "\n";
207
				echo "\t" . '</div>' . "\n";
208
209
				if ( $hide_header ) {
210
					$header_class = 'simcal-hide-header';
211
				}
212
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
213
214
				echo "\t" . '<div class="simcal-nav simcal-current ' . $header_class . '" data-calendar-current="' . $calendar->start . '">' . "\n";
215
				echo "\t\t" . '<h3 class="simcal-current-label"> </h3>' . "\n";
216
				echo "\t" . '</div>' . "\n";
217
218
				echo "\t" . '<div class="simcal-nav">';
219
				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...
220
				echo "\t\t\t" . '<i class="simcal-icon-right"></i>' . "\n";
221
				echo "\t\t" . '</button>' . "\n";
222
				echo "\t" . '</div>' . "\n";
223
224
				echo '</nav>' . "\n";
225
			}
226
227
			echo $this->draw_list( $calendar->start );
228
229
			echo '<div class="simcal-ajax-loader simcal-spinner-top" style="display: none;"><i class="simcal-icon-spinner simcal-icon-spin"></i></div>';
230
231
			echo '</div>';
232
233
		}
234
235
	}
236
237
	/**
238
	 * Get events for current display.
239
	 *
240
	 * @since  3.0.0
241
	 * @access private
242
	 *
243
	 * @param  int $timestamp
244
	 *
245
	 * @return array
246
	 */
247
	private function get_events( $timestamp ) {
248
249
		$calendar = $this->calendar;
250
		$timezone = $calendar->timezone;
251
252
		if ( ! $calendar->group_type || ! $calendar->group_span ) {
253
			return array();
254
		}
255
256
		$current = Carbon::createFromTimestamp( $timestamp, $timezone );
257
		$prev = clone $current;
258
		$next = clone $current;
259
260
		$this->start = $current->getTimestamp();
261
262
		$interval = $span = max( absint( $calendar->group_span ), 1 );
263
264
		if ( 'monthly' == $calendar->group_type ) {
265
			$this->prev = $prev->subMonths( $span )->getTimestamp();
266
			$this->next = $next->addMonths( $span )->getTimestamp();
267
		} elseif ( 'weekly' == $calendar->group_type ) {
268
			$week = new Carbon( $calendar->timezone );
269
			$week->setTimestamp( $timestamp );
270
			$week->setWeekStartsAt( $calendar->week_starts );
271
			$this->prev = $prev->subWeeks( $span )->getTimestamp();
272
			$this->next = $next->addWeeks( $span )->getTimestamp();
273
		} elseif ( 'daily' == $calendar->group_type ) {
274
			$this->prev = $prev->subDays( $span )->getTimestamp();
275
			$this->next = $next->addDays( $span )->getTimestamp();
276
		}
277
278
		$events = $calendar->events;
279
		$daily_events = $paged_events = $flattened_events = array();
280
281
		if ( 'events' != $calendar->group_type ) {
282
283
			$this->end   = $this->next - 1;
284
285
			$timestamps   = array_keys( $events );
286
			$lower_bound  = array_filter( $timestamps,  array( $this, 'filter_events_before' ) );
287
			$higher_bound = array_filter( $lower_bound, array( $this, 'filter_events_after'  ) );
288
289
			if ( is_array( $higher_bound ) && !empty( $higher_bound ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
290
				$filtered = array_intersect_key( $events, array_combine( $higher_bound, $higher_bound ) );
291
				foreach ( $filtered as $timestamp => $events ) {
292
					$paged_events[ intval( $timestamp ) ] = $events;
293
				}
294
			}
295
296
		} else {
297
298
			foreach ( $events as $timestamp => $e ) {
299
				$second = 0;
300
				foreach ( $e as $event ) {
301
					$flattened_events[ intval( $timestamp + $second ) ][] = $event;
302
					$second++;
303
				}
304
			}
305
			ksort( $flattened_events, SORT_NUMERIC );
306
307
			$keys  = array_keys( $flattened_events );
308
			$current = 0;
309
			foreach ( $keys as $timestamp ) {
310
				if ( $timestamp <= $this->start ) {
311
					$current++;
312
				}
313
			}
314
315
			$paged_events = array_slice( $flattened_events, $current, $interval, true );
316
317
			$events_end = isset( $keys[ $current + $interval ] ) ? $keys[ $current + $interval ] : $calendar->end;
318
			$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...
319
			// -1 adjusts the interval count to index count, which starts at 0.
320
			$this->prev = isset( $keys[ $current - $interval - 1 ] ) ? $keys[ $current - $interval - 1 ] : $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...
321
			$this->next = isset( $keys[ $current + $interval - 1 ] ) ? $keys[ $current + $interval - 1 ] : $this->end;
0 ignored issues
show
Documentation Bug introduced by
It seems like isset($keys[$current + $...erval - 1] : $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...
322
323
		}
324
325
		// Put resulting events in an associative array, with Ymd date as key for easy retrieval in calendar days loop.
326
		foreach ( $paged_events as $timestamp => $events ) {
327
			if ( $timestamp <= $this->end ) {
328
				$date = Carbon::createFromTimestamp( $timestamp, $calendar->timezone )->endOfDay()->format( 'Ymd' );
329
				$daily_events[ intval( $date ) ][] = $events;
330
			}
331
		}
332
		ksort( $daily_events, SORT_NUMERIC );
333
334
		return $daily_events;
335
	}
336
337
	/**
338
	 * Get calendar list heading.
339
	 *
340
	 * Parses calender date format and adapts to current display range.
341
	 *
342
	 * @since  3.0.0
343
	 * @access private
344
	 *
345
	 * @return array
346
	 */
347
	private function get_heading() {
348
349
		$calendar = $this->calendar;
350
		$start = Carbon::createFromTimestamp( $this->start, $calendar->timezone );
351
		$end = Carbon::createFromTimestamp( $this->end, $calendar->timezone );
352
		$date_format = $this->calendar->date_format;
353
		$date_order  = simcal_get_date_format_order( $date_format );
354
355
		if ( ( $start->day == $end->day ) && ( $start->month == $end->month ) && ( $start->year == $end->year ) ) {
356
			// Start and end on the same day.
357
			// e.g. 1 February 2020
358
			$large = $small = date_i18n( $calendar->date_format , $this->start );
359
			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...
360
				if ( $date_order['m'] > $date_order['d'] ) {
361
					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...
362
						$small = date_i18n( 'Y, d M', $this->start );
363
					} else {
364
						$small = date_i18n( 'd M Y', $this->start );
365
					}
366
				} else {
367
					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...
368
						$small = date_i18n( 'Y, M d', $this->start );
369
					} else {
370
						$small = date_i18n( 'M d Y', $this->start );
371
					}
372
				}
373
			}
374
		} elseif ( ( $start->month == $end->month ) && ( $start->year == $end->year ) ) {
375
			// Start and end days on the same month.
376
			// e.g. August 2020
377
			if ( $date_order['y'] === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
378
				// August.
379
				$large = $small = date_i18n( 'F', $this->start );
380
			} else {
381
				if ( $date_order['y'] < $date_order['m'] ) {
382
					// 2020 August.
383
					$large = date_i18n( 'Y F', $this->start );
384
					$small = date_i18n( 'Y M', $this->start );
385
				} else {
386
					// August 2020.
387
					$large = date_i18n( 'F Y', $this->start );
388
					$small = date_i18n( 'M Y', $this->start );
389
				}
390
			}
391
		} elseif ( $start->year == $end->year ) {
392
			// Start and end days on months of the same year.
393
			// e.g. August - September 2020
394
			if ( $date_order['y'] === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
395
				// August - September.
396
				$large = date_i18n( 'F', $this->start ) . ' - ' . date_i18n( 'F', $this->end );
397
				$small = date_i18n( 'M', $this->start ) . ' - ' . date_i18n( 'M', $this->end );
398
			} else {
399
				if ( $date_order['y'] < $date_order['m'] ) {
400
					// 2020, August - September.
401
					$large  = $small = date( 'Y', $this->start ) . ', ';
402
					$large .= date_i18n( 'F', $this->start ) . ' - ' . date_i18n( 'F', $this->end );
403
					$small .= date_i18n( 'M', $this->start ) . ' - ' . date_i18n( 'M', $this->end );
404
				} else {
405
					// August - September, 2020.
406
					$large  = date_i18n( 'F', $this->start ) . ' - ' . date_i18n( 'F', $this->end ) . ', ';
407
					$small  = date_i18n( 'M', $this->start ) . ' - ' . date_i18n( 'M', $this->end ) . ' ';
408
					$year   = date( 'Y', $this->start );
409
					$large .= $year;
410
					$small .= $year;
411
				}
412
			}
413
		} else {
414
			$large = $small = date( 'Y', $this->start ) . ' - ' . date( 'Y', $this->end );
415
		}
416
417
		return array(
418
			'small' => $small,
419
			'large' => $large,
420
		);
421
	}
422
423
	/**
424
	 * Make a calendar list of events.
425
	 *
426
	 * Outputs a list of events according to events for the specified range.
427
	 *
428
	 * @since  3.0.0
429
	 * @access private
430
	 *
431
	 * @param  int $timestamp
432
	 * @param  int $id
433
	 *
434
	 * @return string
435
	 */
436
	private function draw_list( $timestamp, $id = 0 ) {
437
438
		$calendar = $this->calendar;
439
440
		if ( empty( $calendar ) ) {
441
			$calendar = $this->calendar = simcal_get_calendar( intval( $id ) );
442
			if ( ! $calendar instanceof Default_Calendar ) {
443
				return '';
444
			}
445
		}
446
447
		date_default_timezone_set( $calendar->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...
448
449
		$now = $calendar->now;
450
		$current_events = $this->get_events( $timestamp );
451
		$day_format = explode( ' ', $calendar->date_format );
452
453
		ob_start();
454
455
		// Draw the events.
456
457
		$block_tag = $calendar->compact_list && ! empty( $current_events ) ? 'div' : 'dl';
458
459
		$data_heading = '';
460
		$heading = $this->get_heading();
461
		foreach ( $heading as $k => $v ) {
462
			$data_heading .= ' data-heading-' . $k . '="' . $v . '"';
463
		}
464
465
		echo '<' . $block_tag . ' class="simcal-events-list-container"' .
466
			' data-prev="' . $this->prev . '"' .
467
			' data-next="' . $this->next . '"' .
468
			$data_heading . '>';
469
470
		if ( ! empty( $current_events ) && is_array( $current_events ) ) :
471
472
			foreach ( $current_events as $ymd => $events ) :
473
474
				// This is where we can find out if an event is a multi-day event and if it needs to be shown.
475
				// Since this is for list view we are showing the event on the day viewed if it is part of that day even when
476
				// expand multi-day events are turned off.
477
				if ( isset( $events[0][0]->multiple_days ) && $events[0][0]->multiple_days > 0 ) {
478
					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...
479
480
						$year  = substr( $ymd, 0, 4 );
481
						$month = substr( $ymd, 4, 2 );
482
						$day   = substr( $ymd, 6, 2 );
483
484
						$temp_date = Carbon::createFromDate( $year, $month, $day );
485
486
						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...
487
							continue;
488
						}
489
					}
490
				}
491
492
				$day_ts = Carbon::createFromFormat( 'Ymd', $ymd, $calendar->timezone )->getTimestamp();
493
494
				if ( ! $calendar->compact_list ) :
495
496
					$date = new Carbon( 'now', $calendar->timezone );
497
					$date->setLocale( substr( get_locale(), 0, 2 ) );
498
					$date->setTimestamp( $day_ts );
499
500
					if ( $date->isToday() ) {
501
						$the_color = new Color( $calendar->today_color );
502
					} else {
503
						$the_color = new Color( $calendar->days_events_color );
504
					}
505
506
					$bg_color = '#' . $the_color->getHex();
507
					$color = $the_color->isDark() ? '#ffffff' : '#000000';
508
					$border_style = ' style="border-bottom: 1px solid ' . $bg_color . ';" ';
509
					$bg_style = ' style="background-color: ' . $bg_color . '; color: ' . $color . ';"';
510
511
					echo "\t" . '<dt class="simcal-day-label"' . $border_style . '>';
512
					echo '<span' . $bg_style .'>';
513
					foreach ( $day_format as $format ) {
514
						echo $format ? '<span class="simcal-date-format" data-date-format="' . $format . '">' . date_i18n( $format, $day_ts ) . '</span> ' : ' ';
515
					}
516
					echo '</span>';
517
					echo '</dt>' . "\n";
518
519
				endif;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 5 tabs, found 4
Loading history...
520
521
				$list_events = '<ul class="simcal-events">' . "\n";
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 5 tabs, found 4
Loading history...
522
523
				$calendar_classes = array();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 5 tabs, found 4
Loading history...
524
				$day_classes = 'simcal-weekday-' . date( 'w', $day_ts );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 5 tabs, found 4
Loading history...
525
526
				// Is this the present, the past or the future, Doc?
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 5 tabs, found 4
Loading history...
527
				if ( $timestamp <= $now && $timestamp >= $now ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 5 tabs, found 4
Loading history...
528
					$day_classes .= ' simcal-today simcal-present simcal-day';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 6 tabs, found 5
Loading history...
529
				} elseif ( $timestamp < $now ) {
530
					$day_classes .= ' simcal-past simcal-day';
531
				} elseif ( $this->end > $now ) {
532
					$day_classes .= ' simcal-future simcal-day';
533
				}
534
535
				$count = 0;
536
537
				foreach ( $events as $day_events ) :
538
					foreach ( $day_events as $event ) :
539
						if ( $event instanceof Event ) :
540
541
							$event_classes = $event_visibility = '';
542
543
							$calendar_class     = 'simcal-events-calendar-' . strval( $event->calendar );
544
							$calendar_classes[] = $calendar_class;
545
546
							$recurring     = $event->recurrence ? 'simcal-event-recurring ' : '';
547
							$has_location  = $event->venue ? 'simcal-event-has-location ' : '';
548
549
							$event_classes .= 'simcal-event ' . $recurring . $has_location . $calendar_class;
550
551
							// Toggle some events visibility if more than optional limit.
552
							if ( ( $calendar->events_limit > - 1 ) && ( $count >= $calendar->events_limit ) ) :
553
								$event_classes .= ' simcal-event-toggled';
554
								$event_visibility = ' style="display: none"';
555
							endif;
556
557
							$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...
558
							$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...
559
							$event_color = $event->get_color();
560
							if ( ! empty( $event_color ) ) {
561
								$side = is_rtl() ? 'right' : 'left';
562
								$event_color = ' style="border-' . $side . ': 4px solid ' . $event_color . '; padding-' . $side . ': 8px;"';
563
							}
564
565
							$list_events .= "\t" . '<li class="' . $event_classes . '"' . $event_visibility . $event_color . ' itemscope itemtype="http://schema.org/Event">' . "\n";
566
							$list_events .= "\t\t" . '<div class="simcal-event-details">' . $calendar->get_event_html( $event ) . '</div>' . "\n";
567
							$list_events .= "\t" . '</li>' . "\n";
568
569
							$count ++;
570
571
							// Event falls within today.
572
							if ( ( $this->end <= $now ) && ( $this->start >= $now ) ) :
573
								$day_classes .= ' simcal-today-has-events';
574
							endif;
575
							$day_classes .= ' simcal-day-has-events simcal-day-has-' . strval( $count ) . '-events';
576
577
							if ( $calendar_classes ) :
578
								$day_classes .= ' ' . trim( implode( ' ', array_unique( $calendar_classes ) ) );
579
							endif;
580
581
						endif;
582
					endforeach;
583
				endforeach;
584
585
				$list_events .= '</ul>' . "\n";
586
587
				// If events visibility is limited, print the button toggle.
588
				if ( ( $calendar->events_limit > -1 ) && ( $count > $calendar->events_limit ) ) :
589
					$list_events .= '<button class="simcal-events-toggle"><i class="simcal-icon-down simcal-icon-animate"></i></button>';
590
				endif;
591
592
				// Print final list of events for the current day.
593
				$tag = $calendar->compact_list ? 'div' : 'dd';
594
				echo '<'  . $tag . ' class="' . $day_classes . '" data-events-count="' . strval( $count ) . '">' . "\n";
595
				echo "\t" . $list_events . "\n";
596
				echo '</' . $tag . '>' . "\n";
597
598
			endforeach;
599
600
		else :
601
602
			echo "\t" . '<p>';
603
604
			$message = get_post_meta( $calendar->id, '_no_events_message', true );
605
606
			if ( 'events' == $calendar->group_type ) {
607
				echo ! empty( $message ) ? $message : __( 'Nothing to show.', 'google-calendar-events' );
608
			} else {
609
				if ( ! empty( $message ) ) {
610
					echo $message;
611
				} else {
612
					$from = Carbon::createFromTimestamp( $this->start, $calendar->timezone )->getTimestamp();
613
					$to = Carbon::createFromTimestamp( $this->end, $calendar->timezone )->getTimestamp();
614
					echo apply_filters( 'simcal_no_events_message', sprintf(
615
						__( 'Nothing from %1$s to %2$s.', 'google-calendar-events' ),
616
						date_i18n( $calendar->date_format, $from ),
617
						date_i18n( $calendar->date_format, $to )
618
					), $calendar->id, $from, $to );
619
				}
620
			}
621
622
			echo "\t" . '</p>' . "\n";
623
624
		endif;
625
626
		echo '</' . $block_tag . '>';
627
628
		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...
629
630
		return ob_get_clean();
631
	}
632
633
	/**
634
	 * Ajax callback to request a new page.
635
	 *
636
	 * @since 3.0.0
637
	 */
638
	public function draw_list_ajax() {
639
640
		if ( isset( $_POST['ts'] ) && isset( $_POST['id'] ) ) {
641
642
			$ts = absint( $_POST['ts'] );
643
			$id = absint( $_POST['id'] );
644
645
			wp_send_json_success( $this->draw_list( $ts, $id ) );
646
647
		} else {
648
649
			wp_send_json_error( 'Missing arguments in default calendar list ajax request.' );
650
651
		}
652
	}
653
654
	/**
655
	 * Array filter callback.
656
	 *
657
	 * @since  3.0.0
658
	 * @access private
659
	 *
660
	 * @param  int $event Timestamp.
661
	 *
662
	 * @return bool
663
	 */
664
	private function filter_events_before( $event ) {
665
		return intval( $event ) >= intval( $this->start );
666
	}
667
668
	/**
669
	 * Array filter callback.
670
	 *
671
	 * @since  3.0.0
672
	 * @access private
673
	 *
674
	 * @param  int $event Timestamp.
675
	 *
676
	 * @return bool
677
	 */
678
	private function filter_events_after( $event ) {
679
		return intval( $event ) < intval( $this->end );
680
	}
681
682
}
683