Default_Calendar_Grid::add_ajax_actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Default Calendar - Grid 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\Events\Event;
14
use SimpleCalendar\Calendars\Default_Calendar;
15
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Default Calendar: Grid View.
22
 *
23
 * @since  3.0.0
24
 */
25
class Default_Calendar_Grid 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
	 * Constructor.
53
	 *
54
	 * @since 3.0.0
55
	 *
56
	 * @param string|Calendar $calendar
57
	 */
58
	public function __construct( $calendar = '' ) {
59
		$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...
60
	}
61
62
	/**
63
	 * Get the view parent calendar type.
64
	 *
65
	 * @since  3.0.0
66
	 *
67
	 * @return string
68
	 */
69
	public function get_parent() {
70
		return 'default-calendar';
71
	}
72
73
	/**
74
	 * Get the view type.
75
	 *
76
	 * @since  3.0.0
77
	 *
78
	 * @return string
79
	 */
80
	public function get_type() {
81
		return 'grid';
82
	}
83
84
	/**
85
	 * Get the view name.
86
	 *
87
	 * @since  3.0.0
88
	 *
89
	 * @return string
90
	 */
91
	public function get_name() {
92
		return __( 'Grid', 'google-calendar-events' );
93
	}
94
95
	/**
96
	 * Add ajax actions.
97
	 *
98
	 * @since 3.0.0
99
	 */
100
	public function add_ajax_actions() {
101
		add_action( 'wp_ajax_simcal_default_calendar_draw_grid', array( $this, 'draw_grid_ajax' ) );
102
		add_action( 'wp_ajax_nopriv_simcal_default_calendar_draw_grid', array( $this, 'draw_grid_ajax' ) );
103
	}
104
105
	/**
106
	 * Default calendar grid scripts.
107
	 *
108
	 * Scripts to load when this view is displayed.
109
	 *
110
	 * @since  3.0.0
111
	 *
112
	 * @param  string $min
113
	 *
114
	 * @return array
115
	 */
116
	public function scripts( $min = '' ) {
117
		return array(
118
			'simcal-qtip' => array(
119
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/jquery.qtip' . $min . '.js',
120
				'deps'      => array( 'jquery' ),
121
				'in_footer' => true,
122
			),
123
			'simcal-fullcal-moment' => array(
124
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/moment' . $min . '.js',
125
				'deps'      => array( 'jquery' ),
126
				'in_footer' => true,
127
			),
128
			'simcal-moment-timezone' => array(
129
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/moment-timezone-with-data' . $min . '.js',
130
				'deps'      => array( 'jquery' ),
131
				'in_footer' => true,
132
			),
133
			'simcal-default-calendar' => array(
134
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/default-calendar' . $min . '.js',
135
				'deps'      => array(
136
					'jquery',
137
					'simcal-qtip',
138
					'simcal-fullcal-moment',
139
					'simcal-moment-timezone',
140
				),
141
				'in_footer' => true,
142
				'localize'  => array(
143
					'simcal_default_calendar' => simcal_common_scripts_variables(),
144
				),
145
			),
146
		);
147
	}
148
149
	/**
150
	 * Default calendar grid styles.
151
	 *
152
	 * Stylesheets to load when this view is displayed.
153
	 *
154
	 * @since  3.0.0
155
	 *
156
	 * @param  string $min = ''
157
	 *
158
	 * @return array
159
	 */
160
	public function styles( $min = '' ) {
161
		return array(
162
			'simcal-qtip' => array(
163
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/vendor/jquery.qtip' . $min . '.css',
164
				'media' => 'all',
165
			),
166
			'simcal-default-calendar-grid' => array(
167
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-grid' . $min . '.css',
168
				'deps'  => array(
169
					'simcal-qtip',
170
				),
171
				'media' => 'all',
172
			),
173
			'simcal-default-calendar-list' => array(
174
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-list' . $min . '.css',
175
				'deps'  => array(
176
					'simcal-qtip',
177
				),
178
				'media' => 'all',
179
			),
180
		);
181
	}
182
183
	/**
184
	 * Default calendar grid markup.
185
	 *
186
	 * @since  3.0.0
187
	 */
188
	public function html() {
189
190
		$calendar = $this->calendar;
191
192
		if ( $calendar instanceof Default_Calendar ) {
193
194
			?>
195
196
			<?php edit_post_link( __( 'Edit Calendar', 'google-calendar-events' ), '<p class="simcal-align-right"><small>', '</small></p>', $calendar->id ); ?>
197
198
			<table class="simcal-calendar-grid"
199
			       data-event-bubble-trigger="<?php echo $calendar->event_bubble_trigger; ?>">
200
				<thead class="simcal-calendar-head">
201
					<tr>
202 View Code Duplication
						<?php if ( ! $calendar->static ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
							<th class="simcal-nav simcal-prev-wrapper" colspan="<?php echo apply_filters( 'simcal_prev_cols', '1' ); ?>">
204
								<button class="simcal-nav-button simcal-month-nav simcal-prev" title="<?php _e( 'Previous Month', 'google-calendar-events' ); ?>"><i class="simcal-icon-left"></i></button>
205
							</th>
206
						<?php } ?>
207
						<th colspan="<?php echo apply_filters( 'simcal_current_cols', $calendar->static ? '7' : '5' ); ?>"
208
						    class="simcal-nav simcal-current"
209
						    data-calendar-current="<?php echo $calendar->start; ?>">
210
							<?php
211
212
							echo '<h3>';
213
214
							// Display month and year according to user date format preference.
215
216
							$year_pos  = strcspn( $calendar->date_format, 'Y y' );
217
							$month_pos = strcspn( $calendar->date_format, 'F M m n' );
218
219
							$current = array( 'month' => 'F', 'year' => 'Y' );
220
221
							if ( $year_pos < $month_pos ) {
222
								$current = array_reverse( $current );
223
							}
224
225
							foreach ( $current as $k => $v ) {
226
								echo ' <span class="simcal-current-' . $k , '">' . date_i18n( $v, $calendar->start ) . '</span> ';
227
							}
228
229
							echo '</h3>';
230
231
							?>
232
						</th>
233 View Code Duplication
						<?php if ( ! $calendar->static ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
							<th class="simcal-nav simcal-next-wrapper" colspan="<?php echo apply_filters( 'simcal_next_cols', '1' ); ?>">
235
								<button class="simcal-nav-button simcal-month-nav simcal-next" title="<?php _e( 'Next Month', 'google-calendar-events' ); ?>"><i class="simcal-icon-right"></i></button>
236
							</th>
237
						<?php } ?>
238
					</tr>
239
					<tr>
240
						<?php
241
242
						// Print day names in short or long form for different viewport sizes.
243
244
						$week_starts     = $calendar->week_starts;
245
						$week_days_short = simcal_get_calendar_names_i18n( 'day', 'short' );
246
						$week_days_full  = simcal_get_calendar_names_i18n( 'day', 'full' );
247
248 View Code Duplication
						for ( $i = $week_starts; $i <= 6; $i ++ ) :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
250
							?>
251
							<th class="simcal-week-day simcal-week-day-<?php echo $i ?>"
252
								data-screen-small="<?php echo mb_substr( $week_days_short[ $i ], 0, 1, 'UTF-8' ); ?>"
253
							    data-screen-medium="<?php echo $week_days_short[ $i ]; ?>"
254
							    data-screen-large="<?php echo $week_days_full[ $i ]; ?>"><?php echo $week_days_short[ $i ]; ?></th>
255
							<?php
256
257
						endfor;
258
259
						if ( $week_starts !== 0 ) :
260 View Code Duplication
							for ( $i = 0; $i < $week_starts; $i ++ ) :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
262
								?>
263
								<th class="simcal-week-day simcal-week-day-<?php echo $i ?>"
264
								    data-screen-small="<?php echo mb_substr( $week_days_short[ $i ], 0, 1, 'UTF-8' ); ?>"
265
								    data-screen-medium="<?php echo $week_days_short[ $i ]; ?>"
266
								    data-screen-large="<?php echo $week_days_full[ $i ]; ?>"><?php echo $week_days_short[ $i ]; ?></th>
267
								<?php
268
269
							endfor;
270
						endif;
271
272
						?>
273
					</tr>
274
				</thead>
275
276
				<?php echo $this->draw_month( date( 'n', $calendar->start ), date( 'Y', $calendar->start ) ); ?>
277
278
			</table>
279
280
			<?php
281
282
			echo '<div class="simcal-ajax-loader simcal-spinner-top" style="display: none;"><i class="simcal-icon-spinner simcal-icon-spin"></i></div>';
283
		}
284
	}
285
286
	/**
287
	 * Make a calendar grid.
288
	 *
289
	 * Outputs an html calendar according to month and year passed in arguments.
290
	 * Loosely inspired by: http://davidwalsh.name/php-calendar
291
	 * Adjusted by timezone and with an arbitrary week start day.
292
	 *
293
	 * @since  3.0.0
294
	 * @access private
295
	 *
296
	 * @param  int $month The month to print (two digits).
297
	 * @param  int $year  The corresponding year (four digits).
298
	 * @param  int $id    The calendar id.
299
	 *
300
	 * @return string
301
	 */
302
	private function draw_month( $month, $year, $id = 0 ) {
303
304
		$calendar = $this->calendar;
305
		if ( empty( $calendar ) ) {
306
			$calendar = simcal_get_calendar( intval( $id ) );
307
			if ( ! $calendar ) {
308
				return '';
309
			}
310
		}
311
312
		$events = $calendar->events;
313
314
		// Variables to cycle days in current month and find today in calendar.
315
		$now         = $calendar->now;
316
		$current     = Carbon::create( $year, $month, 1, 0, 0, 0, $calendar->timezone );
317
		$current_min = $current->getTimestamp();
318
		$current_max = $current->endOfDay()->getTimestamp();
319
320
		// Calendar grid variables.
321
		$week_starts   = $calendar->week_starts;
322
		$week_of_year  = $current->weekOfYear;   // Relative count of the week number of the year.
323
		$month_starts  = $current->dayOfWeek;    // Day upon which the month starts.
324
		$days_in_month = $current->daysInMonth;  // Number of days in the given month.
325
326
		// Set current month events timestamp boundaries.
327
		$this->start = $current_min;
328
		$this->end   = $current->endOfMonth()->getTimestamp();
329
330
		// Get daily events for this month.
331
		if ( $events && is_array( $events ) ) {
332
333
			// Filter events within the boundaries previously set above.
334
			$timestamps   = array_keys( $events );
335
			$lower_bound  = array_filter( $timestamps, array( $this, 'filter_events_before' ) );
336
			$higher_bound = array_filter( $lower_bound, array( $this, 'filter_events_after' ) );
337
			$filtered     = ( is_array( $events ) && is_array( $higher_bound) ) && ! empty( $events ) && ! empty( $higher_bound ) ? array_intersect_key( $events, array_combine( $higher_bound, $higher_bound ) ) : array();
338
339
			// Put resulting events in an associative array, with day of the month as key for easy retrieval in calendar days loop.
340
			$day_events = array();
341
			foreach ( $filtered as $timestamp => $events_in_day ) {
342
				foreach ( $events_in_day as $event ) {
343
					if ( $event instanceof Event ){
344
						$day = intval( Carbon::createFromTimestamp( $timestamp, $calendar->timezone )->endOfDay()->day );
345
						$day_events[ $day ][] = $event;
346
					}
347
				}
348
			}
349
350
			ksort( $day_events, SORT_NUMERIC );
351
		}
352
353
		ob_start();
354
355
		echo '<tbody class="simcal-month simcal-month-' . $month . '">' . "\n";
356
		echo "\t" . '<tr class="simcal-week simcal-week-' . $week_of_year . '">';
357
358
		$days_in_row = 0;
359
		// Week may start on an arbitrary day (sun, 0 - sat, 6).
360
		$week_day = $week_starts;
361
362
		// This fixes a possible bug when a month starts by Sunday (0).
363
		if ( 0 !== $week_starts ) {
364
			$b = $month_starts === 0 ? 7 : $month_starts;
365
		} else {
366
			$b = $month_starts;
367
		}
368
369
		// Void days in first week.
370
		for ( $a = $week_starts; $a < $b; $a++ ) :
371
372
			$last_void_day_class = ( $a === ( $b - 1 ) ) ? 'simcal-day-void-last' : '';
373
374
			echo '<td class="simcal-day simcal-day-void ' . $last_void_day_class . '"></td>' . "\n";
375
376
			// Reset day of the week count (sun, 0 - sat, 6).
377
			if ( $week_day === 6 ) {
378
				$week_day = -1;
379
			}
380
			$week_day++;
381
382
			$days_in_row++;
383
384
		endfor;
385
386
		// Actual days of the month.
387
		for ( $day = 1; $day <= $days_in_month; $day++ ) :
388
389
			$count = 0;
390
			$calendar_classes = array();
391
			$day_classes = 'simcal-day-' . $day . ' simcal-weekday-' . $week_day;
392
393
			$border_style = $bg_color = $color = '';
394
395
			// Is this the present, the past or the future, Doc?
396
			if ( $current_min <= $now && $current_max >= $now ) {
397
				$day_classes .= ' simcal-today simcal-present simcal-day';
398
				$the_color = new Color( $calendar->today_color );
399
				$bg_color = '#' . $the_color->getHex();
400
				$color = $the_color->isDark() ? '#ffffff' : '#000000';
401
				$border_style = ' style="border: 1px solid ' . $bg_color . ';"';
402
			} elseif ( $current_max < $now ) {
403
				$day_classes .= ' simcal-past simcal-day';
404
			} elseif ( $current_min > $now ) {
405
				$day_classes .= ' simcal-future simcal-day';
406
			}
407
408
			// Print events for the current day in loop, if found any.
409
			if ( isset( $day_events[ $day ] ) ) :
410
411
				$bullet_colors = array();
412
413
				$list_events = '<ul class="simcal-events">';
414
415
				foreach ( $day_events[ $day ] as $event ) :
416
417
					$event_classes = $event_visibility = '';
418
419
					if ( $event instanceof Event ) :
420
421
						// Store the calendar id where the event belongs (useful in grouped calendar feeds)
422
						$calendar_class  = 'simcal-events-calendar-' . strval( $event->calendar );
423
						$calendar_classes[] = $calendar_class ;
424
425
						$recurring     = $event->recurrence ? 'simcal-event-recurring ' : '';
426
						$has_location  = $event->venue ? 'simcal-event-has-location ' : '';
427
428
						$event_classes  .= 'simcal-event ' . $recurring . $has_location . $calendar_class . ' simcal-tooltip';
429
430
						// Toggle some events visibility if more than optional limit.
431 View Code Duplication
						if ( ( $calendar->events_limit > -1 )  && ( $count >= $calendar->events_limit ) ) :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
432
							$event_classes    .= ' simcal-event-toggled';
433
							$event_visibility  = ' style="display: none"';
434
						endif;
435
436
						// Event title in list.
437
						$title = ! empty( $event->title ) ? trim( $event->title ) : __( 'Event', 'google-calendar-events' );
438
						if ( $calendar->trim_titles >= 1 ) {
439
							$title = strlen( $title ) > $calendar->trim_titles ? mb_substr( $title, 0, $calendar->trim_titles ) . '&hellip;' : $title;
440
						}
441
442
						// Event color.
443
						$bullet = '';
444
						//$bullet_color = '#000';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
445
						$event_color = $event->get_color();
446
						if ( ! empty( $event_color ) ) {
447
							$bullet = '<span style="color: ' . $event_color . ';">&#9632;</span> ';
448
							$bullet_colors[] = $event_color;
449
						} else {
450
							$bullet_colors[] = '#000';
451
						}
452
453
						// Event contents.
454
						$list_events .= "\t" . '<li class="' . $event_classes . '"' . $event_visibility . ' itemscope itemtype="http://schema.org/Event">' . "\n";
455
						$list_events .= "\t\t" . '<span class="simcal-event-title">' . $bullet . $title . '</span>' . "\n";
456
						$list_events .= "\t\t" . '<div class="simcal-event-details simcal-tooltip-content" style="display: none;">' . $calendar->get_event_html( $event ) . '</div>' . "\n";
457
						$list_events .= "\t" . '</li>' . "\n";
458
459
						$count ++;
460
461
					endif;
462
463
				endforeach;
464
465
				if ( ( $current_min <= $now ) && ( $current_max >= $now ) ) {
466
					$day_classes .= ' simcal-today-has-events';
467
				}
468
				$day_classes .= ' simcal-day-has-events simcal-day-has-' . strval( $count ) . '-events';
469
470
				if ( $calendar_classes ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $calendar_classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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

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

Loading history...
471
					$day_classes .= ' ' . trim( implode( ' ', array_unique( $calendar_classes ) ) );
472
				}
473
474
				$list_events .= '</ul>' . "\n";
475
476
				// Optional button to toggle hidden events in list.
477
				if ( ( $calendar->events_limit > -1 ) && ( $count > $calendar->events_limit ) ) :
478
					$list_events .= '<button class="simcal-events-toggle"><i class="simcal-icon-down simcal-icon-animate"></i></button>';
479
				endif;
480
481
			else :
482
483
				// Empty cell for day with no events.
484
				$list_events = '<span class="simcal-no-events"></span>';
485
486
			endif;
487
488
			// The actual days with numbers and events in each row cell.
489
			echo '<td class="' . $day_classes . '" data-events-count="' . strval( $count ) . '">' . "\n";
490
491
			if ( $color ) {
492
				$day_style = ' style="background-color: ' . $bg_color . '; color: ' . $color .'"';
493
			} elseif ( $count > 0 ) {
494
				$the_color = new Color( $calendar->days_events_color );
495
				$color = ! $color ? ( $the_color->isDark() ? '#ffffff' : '#000000' ) : $color;
496
				$bg_color = ! $bg_color ? '#' . $the_color->getHex() : $bg_color;
497
				$day_style = ' style="background-color: ' . $bg_color . '; color: ' . $color .'"';
498
			} else {
499
				$day_style = '';
500
			}
501
502
			echo "\t" . '<div' . $border_style . '>' . "\n";
503
			echo "\t\t" . '<span class="simcal-day-label simcal-day-number"' . $day_style . '>' . $day . '</span>' . "\n";
504
			echo "\t\t" . $list_events . "\n";
505
			echo "\t\t";
506
			echo '<span class="simcal-events-dots" style="display: none;">';
507
508
			// Event bullets for calendar mobile mode.
509
			for( $i = 0; $i < $count; $i++ ) {
510
				echo '<b style="color: ' . $bullet_colors[ $i ] . ';"> &bull; </b>';
0 ignored issues
show
Bug introduced by
The variable $bullet_colors does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
511
			}
512
513
			echo '</span>' . "\n";
514
			echo "\t" . '</div>' . "\n";
515
			echo '</td>' . "\n";
516
517
			// Reset day of the week count (sun, 0 - sat, 6).
518
			if ( $week_day === 6 ) {
519
				$week_day = - 1;
520
			}
521
			$week_day++;
522
523
			// Reset count of days for this row (0-6).
524
			if ( $days_in_row === 6 ) :
525
526
				// Close the week row.
527
				echo '</tr>';
528
529
				// Open a new week row.
530
				if ( $day < $days_in_month ) {
531
					echo '<tr class="simcal-week simcal-week-' . $week_of_year++ . '">' . "\n";
532
				}
533
534
				$days_in_row = -1;
535
536
			endif;
537
538
			$days_in_row++;
539
540
			$current_min = Carbon::createFromTimestamp( $current_min, $calendar->timezone )->addDay()->getTimestamp();
541
			$current_max = Carbon::createFromTimestamp( $current_max, $calendar->timezone )->addDay()->getTimestamp();
542
543
		endfor;
544
545
		// Void days at the end of the month.
546
		$remainder_days = ( 6 - $days_in_row );
547
548
		for ( $i = 0; $i <= $remainder_days; $i ++ ) {
549
550
			$last_void_day_class = ( $i == $remainder_days ) ? 'simcal-day-void-last' : '';
551
552
			echo '<td class="simcal-day simcal-day-void ' . $last_void_day_class . '"></td>' . "\n";
553
554
			$week_day++;
555
		}
556
557
		echo "\t" . '</tr>' . "\n";
558
		echo '</tbody>' . "\n";
559
560
		return ob_get_clean();
561
	}
562
563
	/**
564
	 * Ajax callback to request a new month.
565
	 *
566
	 * @since 3.0.0
567
	 */
568
	public function draw_grid_ajax() {
0 ignored issues
show
Coding Style introduced by
draw_grid_ajax uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
569
570
		if ( isset( $_POST['month'] ) && isset( $_POST['year'] ) && isset( $_POST['id'] ) ) {
571
572
			$month = absint( $_POST['month'] );
573
			$year  = absint( $_POST['year'] );
574
			$id    = absint( $_POST['id'] );
575
576
			wp_send_json_success( $this->draw_month( $month, $year, $id ) );
577
578
		} else {
579
580
			wp_send_json_error( 'Missing arguments in default calendar grid ajax request.' );
581
582
		}
583
584
	}
585
586
	/**
587
	 * Array filter callback.
588
	 *
589
	 * @since  3.0.0
590
	 * @access private
591
	 *
592
	 * @param  int $event Timestamp.
593
	 *
594
	 * @return bool
595
	 */
596
	private function filter_events_before( $event ) {
597
		return intval( $event ) >= intval( $this->start );
598
	}
599
600
	/**
601
	 * Array filter callback.
602
	 *
603
	 * @since  3.0.0
604
	 * @access private
605
	 *
606
	 * @param  int $event Timestamp.
607
	 *
608
	 * @return bool
609
	 */
610
	private function filter_events_after( $event ) {
611
		return intval( $event ) < intval( $this->end );
612
	}
613
614
}
615