This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Calendar |
||
4 | * |
||
5 | * @package SimpleCalendar/Calendars |
||
6 | */ |
||
7 | namespace SimpleCalendar\Abstracts; |
||
8 | |||
9 | use Carbon\Carbon; |
||
10 | use SimpleCalendar\Events\Event; |
||
11 | use SimpleCalendar\Events\Event_Builder; |
||
12 | use SimpleCalendar\Events\Events; |
||
13 | |||
14 | if ( ! defined( 'ABSPATH' ) ) { |
||
15 | exit; |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * The Calendar. |
||
20 | * |
||
21 | * Displays events from events feed. |
||
22 | */ |
||
23 | abstract class Calendar { |
||
24 | |||
25 | /** |
||
26 | * Calendar Id. |
||
27 | * |
||
28 | * @access public |
||
29 | * @var int |
||
30 | */ |
||
31 | public $id = 0; |
||
32 | |||
33 | /** |
||
34 | * Calendar post object. |
||
35 | * |
||
36 | * @access public |
||
37 | * @var \WP_Post |
||
38 | */ |
||
39 | public $post = null; |
||
40 | |||
41 | /** |
||
42 | * Calendar Type. |
||
43 | * |
||
44 | * @access public |
||
45 | * @var string |
||
46 | */ |
||
47 | public $type = ''; |
||
48 | |||
49 | /** |
||
50 | * Calendar Name. |
||
51 | * |
||
52 | * @access public |
||
53 | * @var string |
||
54 | */ |
||
55 | public $name = ''; |
||
56 | |||
57 | /** |
||
58 | * Feed type. |
||
59 | * |
||
60 | * @access public |
||
61 | * @var string |
||
62 | */ |
||
63 | public $feed = ''; |
||
64 | |||
65 | /** |
||
66 | * Calendar start. |
||
67 | * |
||
68 | * @access public |
||
69 | * @var int |
||
70 | */ |
||
71 | public $start = 0; |
||
72 | |||
73 | /** |
||
74 | * Calendar end. |
||
75 | * |
||
76 | * @access public |
||
77 | * @var int |
||
78 | */ |
||
79 | public $end = 0; |
||
80 | |||
81 | /** |
||
82 | * Static calendar. |
||
83 | * |
||
84 | * @access public |
||
85 | * @var bool |
||
86 | */ |
||
87 | public $static = false; |
||
88 | |||
89 | /** |
||
90 | * Today. |
||
91 | * |
||
92 | * @access public |
||
93 | * @var int |
||
94 | */ |
||
95 | public $today = 0; |
||
96 | |||
97 | /** |
||
98 | * Time now. |
||
99 | * |
||
100 | * @access public |
||
101 | * @var int |
||
102 | */ |
||
103 | public $now = 0; |
||
104 | |||
105 | /** |
||
106 | * Timezone offset. |
||
107 | * |
||
108 | * @access public |
||
109 | * @var int |
||
110 | */ |
||
111 | public $offset = 0; |
||
112 | |||
113 | /** |
||
114 | * Timezone |
||
115 | * |
||
116 | * @access public |
||
117 | * @var string |
||
118 | */ |
||
119 | public $timezone = 'UTC'; |
||
120 | |||
121 | /** |
||
122 | * Site timezone. |
||
123 | * |
||
124 | * @access public |
||
125 | * @var string |
||
126 | */ |
||
127 | public $site_timezone = 'UTC'; |
||
128 | |||
129 | /** |
||
130 | * Date format. |
||
131 | * |
||
132 | * @access public |
||
133 | * @var string |
||
134 | */ |
||
135 | public $date_format = ''; |
||
136 | |||
137 | /** |
||
138 | * Time format. |
||
139 | * |
||
140 | * @access public |
||
141 | * @var string |
||
142 | */ |
||
143 | public $time_format = ''; |
||
144 | |||
145 | /** |
||
146 | * Date-time separator. |
||
147 | * |
||
148 | * @access public |
||
149 | * @var string |
||
150 | */ |
||
151 | public $datetime_separator = '@'; |
||
152 | |||
153 | /** |
||
154 | * First day of the week. |
||
155 | * |
||
156 | * @access public |
||
157 | * @var int |
||
158 | */ |
||
159 | public $week_starts = 0; |
||
160 | |||
161 | /** |
||
162 | * Events to display. |
||
163 | * |
||
164 | * @access public |
||
165 | * @var array |
||
166 | */ |
||
167 | public $events = array(); |
||
168 | |||
169 | /** |
||
170 | * Errors. |
||
171 | * |
||
172 | * @access public |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $errors = array(); |
||
176 | |||
177 | /** |
||
178 | * Earliest event. |
||
179 | * |
||
180 | * @access public |
||
181 | * @var int |
||
182 | */ |
||
183 | public $earliest_event = 0; |
||
184 | |||
185 | /** |
||
186 | * Latest event. |
||
187 | * |
||
188 | * @access public |
||
189 | * @var int |
||
190 | */ |
||
191 | public $latest_event = 0; |
||
192 | |||
193 | /** |
||
194 | * Event builder content. |
||
195 | * |
||
196 | * @access public |
||
197 | * @var string |
||
198 | */ |
||
199 | public $events_template = ''; |
||
200 | |||
201 | /** |
||
202 | * Calendar Views. |
||
203 | * |
||
204 | * The calendar available views. |
||
205 | * |
||
206 | * @access public |
||
207 | * @var array |
||
208 | */ |
||
209 | public $views = array(); |
||
210 | |||
211 | /** |
||
212 | * Calendar View. |
||
213 | * |
||
214 | * The current view. |
||
215 | * |
||
216 | * @access public |
||
217 | * @var Calendar_View |
||
218 | */ |
||
219 | public $view = null; |
||
220 | |||
221 | /** |
||
222 | * Calendar settings. |
||
223 | * |
||
224 | * @access protected |
||
225 | * @var array |
||
226 | */ |
||
227 | protected $settings = array(); |
||
228 | |||
229 | /** |
||
230 | * Constructor. |
||
231 | * |
||
232 | * @since 3.0.0 |
||
233 | * |
||
234 | * @param int|object|\WP_Post|Calendar $calendar |
||
235 | * @param string $view |
||
236 | */ |
||
237 | public function __construct( $calendar, $view = '' ) { |
||
238 | |||
239 | // Set the post object. |
||
240 | $this->set_post_object( $calendar ); |
||
241 | |||
242 | if ( ! is_null( $this->post ) ) { |
||
243 | |||
244 | // Set calendar type and events source. |
||
245 | $this->set_taxonomies(); |
||
246 | |||
247 | // Set calendar default datetime properties. |
||
248 | $this->set_timezone(); |
||
249 | $this->set_start_of_week(); |
||
250 | $this->set_static(); |
||
251 | |||
252 | // Set calendar start. |
||
253 | $this->set_start(); |
||
254 | |||
255 | // Set the events template. |
||
256 | $this->set_events_template(); |
||
257 | |||
258 | // Get events source data. |
||
259 | $feed = simcal_get_feed( $this ); |
||
260 | if ( $feed instanceof Feed ) { |
||
261 | if ( ! empty( $feed->events ) ) { |
||
262 | if ( is_array( $feed->events ) ) { |
||
263 | $this->set_events( $feed->events ); |
||
264 | if ( 'use_calendar' == get_post_meta( $this->id, '_feed_timezone_setting', true ) ) { |
||
265 | $this->timezone = $feed->timezone; |
||
266 | $this->set_start(); |
||
267 | } |
||
268 | } elseif ( is_string( $feed->events ) ) { |
||
269 | $this->errors[] = $feed->events; |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | |||
274 | // Set general purpose timestamps. |
||
275 | $now = Carbon::now( $this->timezone ); |
||
276 | $this->now = $now->getTimestamp(); |
||
277 | $this->today = $now->startOfDay()->getTimestamp(); |
||
278 | $this->offset = $now->getOffset(); |
||
279 | |||
280 | // Set date time formatting. |
||
281 | $this->set_date_format(); |
||
282 | $this->set_time_format(); |
||
283 | $this->set_datetime_separator(); |
||
284 | |||
285 | // Set earliest and latest event timestamps. |
||
286 | if ( $this->events && is_array( $this->events ) ) { |
||
0 ignored issues
–
show
|
|||
287 | $this->earliest_event = intval( current( array_keys( $this->events ) ) ); |
||
288 | $this->latest_event = intval( key( array_slice( $this->events, -1, 1, true ) ) ); |
||
289 | } |
||
290 | |||
291 | // Set calendar end. |
||
292 | $this->set_end(); |
||
293 | |||
294 | // Set view. |
||
295 | if ( ! $view ) { |
||
296 | |||
297 | $calendar_view = get_post_meta( $this->id, '_calendar_view', true ); |
||
298 | $calendar_view = isset( $calendar_view[ $this->type ] ) ? $calendar_view[ $this->type ] : ''; |
||
299 | |||
300 | $view = esc_attr( $calendar_view ); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | // Get view. |
||
305 | $this->view = $this->get_view( $view ); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Overloading __isset function with post meta. |
||
310 | * |
||
311 | * @since 3.0.0 |
||
312 | * |
||
313 | * @param mixed $key Post meta key. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function __isset( $key ) { |
||
318 | return metadata_exists( 'post', $this->id, '_' . $key ); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Overloading __get function with post meta. |
||
323 | * |
||
324 | * @since 3.0.0 |
||
325 | * |
||
326 | * @param string $key Post meta key. |
||
327 | * |
||
328 | * @return mixed |
||
329 | */ |
||
330 | public function __get( $key ) { |
||
331 | $value = get_post_meta( $this->id, '_' . $key, true ); |
||
332 | if ( ! empty( $value ) ) { |
||
333 | $this->$key = $value; |
||
334 | } |
||
335 | return $value; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Set post object and id. |
||
340 | * |
||
341 | * @since 3.0.0 |
||
342 | * |
||
343 | * @param int|object|\WP_Post|Calendar $calendar |
||
344 | */ |
||
345 | public function set_post_object( $calendar ) { |
||
346 | if ( is_numeric( $calendar ) ) { |
||
347 | $this->id = absint( $calendar ); |
||
348 | $this->post = get_post( $this->id ); |
||
349 | } elseif ( $calendar instanceof Calendar ) { |
||
350 | $this->id = absint( $calendar->id ); |
||
351 | $this->post = $calendar->post; |
||
352 | } elseif ( $calendar instanceof \WP_Post ) { |
||
0 ignored issues
–
show
The class
WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
353 | $this->id = absint( $calendar->ID ); |
||
354 | $this->post = $calendar; |
||
355 | } elseif ( isset( $calendar->id ) && isset( $calendar->post ) ) { |
||
356 | $this->id = $calendar->id; |
||
357 | $this->post = $calendar->post; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Return the calendar title. |
||
363 | * |
||
364 | * @since 3.0.0 |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function get_title() { |
||
369 | $title = isset( $this->post->post_title ) ? $this->post->post_title : ''; |
||
370 | return apply_filters( 'simcal_calendar_title', $title ); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Get the calendar post data. |
||
375 | * |
||
376 | * @since 3.0.0 |
||
377 | * |
||
378 | * @return \WP_Post |
||
379 | */ |
||
380 | public function get_post_data() { |
||
381 | return $this->post; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Set taxonomies. |
||
386 | * |
||
387 | * @since 3.0.0 |
||
388 | * @access protected |
||
389 | */ |
||
390 | protected function set_taxonomies() { |
||
391 | // Set calendar type. |
||
392 | if ( $type = wp_get_object_terms( $this->id, 'calendar_type' ) ) { |
||
393 | $this->type = sanitize_title( current( $type )->name ); |
||
394 | } else { |
||
395 | $this->type = apply_filters( 'simcal_calendar_default_type', 'default-calendar' ); |
||
396 | } |
||
397 | // Set feed type. |
||
398 | if ( $feed_type = wp_get_object_terms( $this->id, 'calendar_feed' ) ) { |
||
399 | $this->feed = sanitize_title( current( $feed_type )->name ); |
||
400 | } else { |
||
401 | $this->feed = apply_filters( 'simcal_calendar_default_feed', 'google' ); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Get events. |
||
407 | * |
||
408 | * @since 3.0.0 |
||
409 | * |
||
410 | * @return Events |
||
411 | */ |
||
412 | public function get_events() { |
||
413 | return new Events( $this->events, $this->timezone ); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Set events. |
||
418 | * |
||
419 | * @since 3.0.0 |
||
420 | * |
||
421 | * @param array $array |
||
422 | */ |
||
423 | public function set_events( array $array ) { |
||
424 | |||
425 | $events = array(); |
||
426 | |||
427 | if ( ! empty( $array ) ) { |
||
428 | foreach ( $array as $tz => $e ) { |
||
429 | foreach ( $e as $event ) { |
||
430 | $events[ $tz ][] = $event instanceof Event ? $event : new Event( $event ); |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | |||
435 | $this->events = $events; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Get the event builder template. |
||
440 | * |
||
441 | * @since 3.0.0 |
||
442 | * |
||
443 | * @param string $template |
||
444 | * |
||
445 | * @return string |
||
0 ignored issues
–
show
|
|||
446 | */ |
||
447 | public function set_events_template( $template = '' ) { |
||
448 | if ( empty( $template ) ) { |
||
449 | $template = isset( $this->post->post_content ) ? $this->post->post_content : ''; |
||
450 | } |
||
451 | |||
452 | // TODO: Removed wpautop() call. |
||
453 | |||
454 | $event_formatting = get_post_meta( $this->id, '_event_formatting', true ); |
||
455 | |||
456 | switch( $event_formatting ) { |
||
457 | case 'none': |
||
458 | $this->events_template = wp_kses_post( trim( $template ) ); |
||
459 | break; |
||
460 | case 'no_linebreaks': |
||
461 | $this->events_template = wpautop( wp_kses_post( trim( $template ) ), false ); |
||
462 | break; |
||
463 | default: |
||
464 | $this->events_template = wpautop( wp_kses_post( trim( $template ) ), true ); |
||
465 | } |
||
466 | |||
467 | //$this->events_template = wpautop( wp_kses_post( trim( $template ) ), true ); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
47% 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. ![]() |
|||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Set the timezone. |
||
472 | * |
||
473 | * @since 3.0.0 |
||
474 | * |
||
475 | * @param string $tz Timezone. |
||
476 | */ |
||
477 | public function set_timezone( $tz = '' ) { |
||
478 | |||
479 | $site_tz = esc_attr( simcal_get_wp_timezone() ); |
||
480 | |||
481 | if ( $this->feed === 'grouped-calendars' ) { |
||
482 | $this->timezone = $site_tz; |
||
483 | return; |
||
484 | } |
||
485 | |||
486 | if ( empty( $tz ) ) { |
||
487 | |||
488 | $timezone_setting = get_post_meta( $this->id, '_feed_timezone_setting', true ); |
||
489 | |||
490 | if ( 'use_site' == $timezone_setting ) { |
||
491 | $tz = $site_tz; |
||
492 | } elseif ( 'use_custom' == $timezone_setting ) { |
||
493 | $custom_timezone = esc_attr( get_post_meta( $this->id, '_feed_timezone', true ) ); |
||
494 | // One may be using a non standard timezone in GMT (UTC) offset format. |
||
495 | if ( ( strpos( $custom_timezone, 'UTC+' ) === 0 ) || ( strpos( $custom_timezone, 'UTC-' ) === 0 ) ) { |
||
496 | $tz = simcal_get_timezone_from_gmt_offset( substr( $custom_timezone, 3 ) ); |
||
497 | } else { |
||
498 | $tz = ! empty( $custom_timezone ) ? $custom_timezone : 'UTC'; |
||
499 | } |
||
500 | } |
||
501 | |||
502 | $this->timezone = empty( $tz ) ? 'UTC' : $tz; |
||
503 | return; |
||
504 | } |
||
505 | |||
506 | $this->site_timezone = $site_tz; |
||
507 | $this->timezone = simcal_esc_timezone( $tz, $this->timezone ); |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Set date format. |
||
512 | * |
||
513 | * @since 3.0.0 |
||
514 | * |
||
515 | * @param string $format PHP datetime format. |
||
516 | */ |
||
517 | View Code Duplication | public function set_date_format( $format = '' ) { |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
518 | |||
519 | $date_format_custom = $date_format_default = $format; |
||
520 | |||
521 | if ( empty( $date_format_custom ) ) { |
||
522 | |||
523 | $date_format_option = esc_attr( get_post_meta( $this->id, '_calendar_date_format_setting', true ) ); |
||
524 | $date_format_default = esc_attr( get_option( 'date_format' ) ); |
||
525 | $date_format_custom = ''; |
||
526 | |||
527 | if ( 'use_custom' == $date_format_option ) { |
||
528 | $date_format_custom = esc_attr( get_post_meta( $this->id, '_calendar_date_format', true ) ); |
||
529 | } elseif ( 'use_custom_php' == $date_format_option ) { |
||
530 | $date_format_custom = esc_attr( get_post_meta( $this->id, '_calendar_date_format_php', true ) ); |
||
531 | } |
||
532 | } |
||
533 | |||
534 | $this->date_format = $date_format_custom ? $date_format_custom : $date_format_default; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Set time format. |
||
539 | * |
||
540 | * @since 3.0.0 |
||
541 | * |
||
542 | * @param string $format PHP datetime format. |
||
543 | */ |
||
544 | View Code Duplication | public function set_time_format( $format = '' ) { |
|
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
545 | |||
546 | $time_format_custom = $time_format_default = $format; |
||
547 | |||
548 | if ( empty( $time_format_custom ) ) { |
||
549 | |||
550 | $time_format_option = esc_attr( get_post_meta( $this->id, '_calendar_time_format_setting', true ) ); |
||
551 | $time_format_default = esc_attr( get_option( 'time_format' ) ); |
||
552 | $time_format_custom = ''; |
||
553 | |||
554 | if ( 'use_custom' == $time_format_option ) { |
||
555 | $time_format_custom = esc_attr( get_post_meta( $this->id, '_calendar_time_format', true ) ); |
||
556 | } elseif ( 'use_custom_php' == $time_format_option ) { |
||
557 | $time_format_custom = esc_attr( get_post_meta( $this->id, '_calendar_time_format_php', true ) ); |
||
558 | } |
||
559 | } |
||
560 | |||
561 | $this->time_format = $time_format_custom ? $time_format_custom : $time_format_default; |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Set date-time separator. |
||
566 | * |
||
567 | * @since 3.0.0 |
||
568 | * |
||
569 | * @param string $separator A UTF8 character used as separator. |
||
570 | */ |
||
571 | public function set_datetime_separator( $separator = '' ) { |
||
572 | |||
573 | if ( empty( $separator ) ) { |
||
574 | $separator = get_post_meta( $this->id, '_calendar_datetime_separator', true ); |
||
575 | } |
||
576 | |||
577 | $this->datetime_separator = esc_attr( $separator ); |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Set start of week. |
||
582 | * |
||
583 | * @since 3.0.0 |
||
584 | * |
||
585 | * @param int $weekday From 0 (Sunday) to 6 (Friday). |
||
586 | */ |
||
587 | public function set_start_of_week( $weekday = -1 ) { |
||
588 | |||
589 | $week_starts = is_int( $weekday ) ? $weekday : -1; |
||
590 | |||
591 | if ( $week_starts < 0 || $week_starts > 6 ) { |
||
592 | |||
593 | $week_starts_setting = get_post_meta( $this->id, '_calendar_week_starts_on_setting', true ); |
||
594 | $week_starts = intval( get_option( 'start_of_week' ) ); |
||
595 | |||
596 | if ( 'use_custom' == $week_starts_setting ) { |
||
597 | $week_starts_on = get_post_meta( $this->id, '_calendar_week_starts_on', true ); |
||
598 | $week_starts = is_numeric( $week_starts_on ) ? intval( $week_starts_on ) : $week_starts; |
||
599 | } |
||
600 | } |
||
601 | |||
602 | $this->week_starts = $week_starts; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Set calendar start. |
||
607 | * |
||
608 | * @since 3.0.0 |
||
609 | * |
||
610 | * @param int $timestamp |
||
611 | */ |
||
612 | public function set_start( $timestamp = 0 ) { |
||
613 | |||
614 | if ( is_int( $timestamp ) && $timestamp !== 0 ) { |
||
615 | $this->start = $timestamp; |
||
616 | return; |
||
617 | } |
||
618 | |||
619 | $start_dt = Carbon::now( $this->timezone ); |
||
620 | |||
621 | $calendar_begins = esc_attr( get_post_meta( $this->id, '_calendar_begins', true ) ); |
||
622 | $nth = max( absint( get_post_meta( $this->id, '_calendar_begins_nth', true ) ), 1 ); |
||
623 | |||
624 | if ( 'today' == $calendar_begins ) { |
||
625 | $start_dt = Carbon::today( $this->timezone ); |
||
626 | } elseif ( 'days_before' == $calendar_begins ) { |
||
627 | $start_dt = Carbon::today( $this->timezone )->subDays( $nth ); |
||
628 | } elseif ( 'days_after' == $calendar_begins ) { |
||
629 | $start_dt = Carbon::today( $this->timezone )->addDays( $nth ); |
||
630 | View Code Duplication | } elseif ( 'this_week' == $calendar_begins ) { |
|
0 ignored issues
–
show
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. ![]() |
|||
631 | $week = new Carbon( 'now', $this->timezone ); |
||
632 | $week->setWeekStartsAt( $this->week_starts ); |
||
633 | $start_dt = $week->startOfWeek(); |
||
634 | } elseif ( 'weeks_before' == $calendar_begins ) { |
||
635 | $week = new Carbon( 'now', $this->timezone ); |
||
636 | $week->setWeekStartsAt( $this->week_starts ); |
||
637 | $start_dt = $week->startOfWeek()->subWeeks( $nth ); |
||
638 | View Code Duplication | } elseif ( 'weeks_after' == $calendar_begins ) { |
|
0 ignored issues
–
show
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. ![]() |
|||
639 | $week = new Carbon( 'now', $this->timezone ); |
||
640 | $week->setWeekStartsAt( $this->week_starts ); |
||
641 | $start_dt = $week->startOfWeek()->addWeeks( $nth ); |
||
642 | } elseif ( 'this_month' == $calendar_begins ) { |
||
643 | $start_dt = Carbon::today( $this->timezone )->startOfMonth(); |
||
644 | } elseif ( 'months_before' == $calendar_begins ) { |
||
645 | $start_dt = Carbon::today( $this->timezone )->subMonths( $nth )->startOfMonth(); |
||
646 | } elseif ( 'months_after' == $calendar_begins ) { |
||
647 | $start_dt = Carbon::today( $this->timezone )->addMonths( $nth )->startOfMonth(); |
||
648 | } elseif ( 'this_year' == $calendar_begins ) { |
||
649 | $start_dt = Carbon::today( $this->timezone )->startOfYear()->addHour(); |
||
650 | } elseif ( 'years_before' == $calendar_begins ) { |
||
651 | $start_dt = Carbon::today( $this->timezone )->subYears( $nth )->startOfYear(); |
||
652 | } elseif ( 'years_after' == $calendar_begins ) { |
||
653 | $start_dt = Carbon::today( $this->timezone )->addYears( $nth )->startOfYear(); |
||
654 | } elseif ( 'custom_date' == $calendar_begins ) { |
||
655 | if ( $date = get_post_meta( $this->id, '_calendar_begins_custom_date', true ) ) { |
||
656 | $start_dt = Carbon::createFromFormat( 'Y-m-d', esc_attr( $date ), $this->timezone )->setTimezone( $this->timezone )->startOfDay(); |
||
657 | } |
||
658 | } |
||
659 | |||
660 | $this->start = $start_dt->timestamp; |
||
661 | } |
||
662 | |||
663 | /** |
||
664 | * Set calendar end. |
||
665 | * |
||
666 | * @since 3.0.0 |
||
667 | * |
||
668 | * @param int $timestamp |
||
669 | */ |
||
670 | public function set_end( $timestamp = 0 ) { |
||
671 | $latest = is_int( $timestamp ) && $timestamp !== 0 ? $timestamp : $this->latest_event; |
||
672 | $this->end = $latest > $this->start ? $latest : $this->start; |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * Set the calendar to static. |
||
677 | * |
||
678 | * @since 3.0.0 |
||
679 | * |
||
680 | * @param string|bool $static |
||
681 | */ |
||
682 | public function set_static( $static = '' ) { |
||
683 | |||
684 | if ( ! empty( $static ) && is_bool( $static ) ) { |
||
685 | $this->static = $static; |
||
686 | return; |
||
687 | } |
||
688 | |||
689 | if ( 'yes' == get_post_meta( $this->id, '_calendar_is_static', true ) ) { |
||
690 | $this->static = true; |
||
691 | return; |
||
692 | } |
||
693 | |||
694 | $this->static = false; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Input fields for settings page. |
||
699 | * |
||
700 | * @since 3.0.0 |
||
701 | * |
||
702 | * @return array |
||
703 | */ |
||
704 | public function settings_fields() { |
||
705 | return $this->settings; |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Get a calendar view. |
||
710 | * |
||
711 | * @since 3.0.0 |
||
712 | * |
||
713 | * @param string $view |
||
714 | * |
||
715 | * @return Calendar_View |
||
716 | */ |
||
717 | abstract public function get_view( $view = '' ); |
||
718 | |||
719 | /** |
||
720 | * Get event HTML parsed by template. |
||
721 | * |
||
722 | * @since 3.0.0 |
||
723 | * |
||
724 | * @param Event $event Event object to be parsed. |
||
725 | * @param string $template (optional) To use another template or a partial. |
||
726 | * |
||
727 | * @return string |
||
728 | */ |
||
729 | public function get_event_html( Event $event, $template = '' ) { |
||
730 | $event_builder = new Event_Builder( $event, $this ); |
||
731 | // Use the event template to parse tags; if empty, fallback to calendar post content. |
||
732 | $template = empty( $template ) ? ( empty( $event->template ) ? $this->events_template : $event->template ) : $template; |
||
733 | return $event_builder->parse_event_template_tags( $template ); |
||
734 | } |
||
735 | |||
736 | /** |
||
737 | * Get "Add to Google Calendar" link. |
||
738 | * |
||
739 | * @since 3.1.3 |
||
740 | * |
||
741 | * @param Event $event Event object to be parsed. |
||
742 | * |
||
743 | * @return string |
||
744 | */ |
||
745 | public function get_add_to_gcal_url( Event $event ) { |
||
746 | $base_url = 'https://calendar.google.com/calendar/render'; |
||
747 | // Was https://www.google.com/calendar/render |
||
748 | |||
749 | // Start & end date/time in specific format for GCal. |
||
750 | // &dates=20160504T110000/20160504T170000 |
||
751 | // No "Z"s tacked on to preserve source timezone. |
||
752 | // All day events remove time component, but need to add a full day to show up correctly. |
||
753 | $is_all_day = ( true == $event->whole_day ); |
||
0 ignored issues
–
show
|
|||
754 | $gcal_dt_format = $is_all_day ? 'Ymd' : 'Ymd\THi00'; |
||
755 | $gcal_begin_dt = $event->start_dt->format( $gcal_dt_format ); |
||
756 | $end_dt_raw = $is_all_day ? $event->end_dt->addDay() : $event->end_dt; |
||
757 | $gcal_end_dt = $end_dt_raw->format( $gcal_dt_format ); |
||
758 | $gcal_dt_string = $gcal_begin_dt . '/' . $gcal_end_dt; |
||
759 | |||
760 | // "details" (description) should work even when blank. |
||
761 | // "location" (address) should work with an address, just a name or blank. |
||
762 | $params = array( |
||
763 | 'action' => 'TEMPLATE', |
||
764 | 'text' => urlencode( strip_tags( $event->title ) ), |
||
765 | 'dates' => $gcal_dt_string, |
||
766 | 'details' => urlencode( $event->description ), |
||
767 | 'location' => urlencode( $event->start_location['address'] ), |
||
768 | 'trp' => 'false', |
||
769 | ); |
||
770 | |||
771 | // "ctz" (timezone) arg should be included unless all-day OR 'UTC'. |
||
772 | if ( ! $is_all_day && ( 'UTC' !== $event->timezone ) ) { |
||
773 | $params['ctz'] = urlencode( $event->timezone ); |
||
774 | } |
||
775 | |||
776 | $url = add_query_arg( $params, $base_url ); |
||
777 | |||
778 | return $url; |
||
779 | } |
||
780 | |||
781 | /** |
||
782 | * Output the calendar markup. |
||
783 | * |
||
784 | * @since 3.0.0 |
||
785 | * |
||
786 | * @param string $view The calendar view to display. |
||
787 | */ |
||
788 | public function html( $view = '' ) { |
||
789 | |||
790 | $view = empty( $view ) ? $this->view : $this->get_view( $view ); |
||
791 | |||
792 | if ( $view instanceof Calendar_View ) { |
||
793 | |||
794 | if ( ! empty( $this->errors ) ) { |
||
795 | |||
796 | if ( current_user_can( 'manage_options' ) ) { |
||
797 | echo '<pre><code>'; |
||
798 | foreach ( $this->errors as $error ) { echo $error; } |
||
799 | echo '</code></pre>'; |
||
800 | } |
||
801 | |||
802 | } else { |
||
803 | |||
804 | // Get a CSS class from the class name of the calendar view (minus namespace part). |
||
805 | $view_name = implode( '-', array_map( 'lcfirst', explode( '_', strtolower( get_class( $view ) ) ) ) ); |
||
806 | $view_class = substr( $view_name, strrpos( $view_name, '\\' ) + 1 ); |
||
807 | |||
808 | $calendar_class = trim( implode( ' simcal-', apply_filters( 'simcal_calendar_class', array( |
||
809 | 'simcal-calendar', |
||
810 | $this->type, |
||
811 | $view_class, |
||
812 | ), $this->id ) ) ); |
||
813 | |||
814 | echo '<div class="' . $calendar_class . '" ' |
||
815 | . 'data-calendar-id="' . $this->id . '" ' |
||
816 | . 'data-timezone="' . $this->timezone . '" ' |
||
817 | . 'data-offset="' . $this->offset . '" ' |
||
818 | . 'data-week-start="' . $this->week_starts . '" ' |
||
819 | . 'data-calendar-start="' . $this->start .'" ' |
||
820 | . 'data-calendar-end="' . $this->end . '" ' |
||
821 | . 'data-events-first="' . $this->earliest_event .'" ' |
||
822 | . 'data-events-last="' . $this->latest_event . '"' |
||
823 | . '>'; |
||
824 | |||
825 | do_action( 'simcal_calendar_html_before', $this->id ); |
||
826 | |||
827 | $view->html(); |
||
828 | |||
829 | do_action( 'simcal_calendar_html_after', $this->id ); |
||
830 | |||
831 | //$settings = get_option( 'simple-calendar_settings_calendars' ); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
46% 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. ![]() |
|||
832 | $poweredby = get_post_meta( $this->id, '_poweredby', true ); |
||
833 | |||
834 | if ( 'yes' == $poweredby ) { |
||
835 | $align = is_rtl() ? 'left' : 'right'; |
||
836 | echo '<small class="simcal-powered simcal-align-' . $align .'">' . |
||
837 | sprintf( __( 'Powered by <a href="%s" target="_blank">Simple Calendar</a>', 'google-calendar-events' ), simcal_get_url( 'home' ) ) . |
||
838 | '</small>'; |
||
839 | } |
||
840 | |||
841 | echo '</div>'; |
||
842 | |||
843 | } |
||
844 | |||
845 | } |
||
846 | |||
847 | } |
||
848 | |||
849 | } |
||
850 |
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.