Completed
Push — master ( a00b53...3dc4d8 )
by
unknown
21:18
created

Default_Calendar::set_properties()   D

Complexity

Conditions 12
Paths 192

Size

Total Lines 49
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 49
rs 4.8187
cc 12
eloc 21
nc 192
nop 1

How to fix   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
4
 *
5
 * @package SimpleCalendar\Calendars
6
 */
7
namespace SimpleCalendar\Calendars;
8
9
use Carbon\Carbon;
10
use SimpleCalendar\Abstracts\Calendar;
11
use SimpleCalendar\Abstracts\Calendar_View;
12
use SimpleCalendar\Calendars\Admin\Default_Calendar_Admin;
13
use SimpleCalendar\Calendars\Views;
14
use SimpleCalendar\Events\Event;
15
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Default Calendar.
22
 *
23
 * The default calendar view bundled with the plugin.
24
 *
25
 * @since 3.0.0
26
 */
27
class Default_Calendar extends Calendar {
28
29
	/**
30
	 * Limit visibility of daily events.
31
	 *
32
	 * @access public
33
	 * @var int
34
	 */
35
	public $events_limit = -1;
36
37
	/**
38
	 * Trim characters event titles in grid.
39
	 *
40
	 * @access public
41
	 * @var int
42
	 */
43
	public $trim_titles = -1;
44
45
	/**
46
	 * Event bubbles action trigger.
47
	 *
48
	 * @access public
49
	 * @var string
50
	 */
51
	public $event_bubble_trigger = 'click';
52
53
	/**
54
	 * Hide navigation buttons.
55
	 *
56
	 * @access public
57
	 * @var bool
58
	 */
59
	public $compact_list = false;
60
61
	/**
62
	 * Grouped list type.
63
	 *
64
	 * @access public
65
	 * @var string
66
	 */
67
	public $group_type = '';
68
69
	/**
70
	 * Grouped list span.
71
	 *
72
	 * @access public
73
	 * @var int
74
	 */
75
	public $group_span = 1;
76
77
	/**
78
	 * Skin theme.
79
	 *
80
	 * @access public
81
	 * @var string
82
	 */
83
	public $theme = 'light';
84
85
	/**
86
	 * Today color.
87
	 *
88
	 * @access public
89
	 * @var string
90
	 */
91
	public $today_color = '#FF0000';
92
93
	/**
94
	 * Days with events color.
95
	 *
96
	 * @access public
97
	 * @var string
98
	 */
99
	public $days_events_color = '#000000';
100
101
	/**
102
	 * Constructor.
103
	 *
104
	 * @since 3.0.0
105
	 *
106
	 * @param int|object|\WP_Post|Calendar $calendar
107
	 */
108
	public function __construct( $calendar ) {
109
110
		$this->type = 'default-calendar';
111
		$this->name = __( 'Default', 'google-calendar-events' );
112
		$this->views = apply_filters( 'simcal_default_calendar_views', array(
113
			'grid' => __( 'Grid', 'google-calendar-events' ),
114
			'list' => __( 'List', 'google-calendar-events' ),
115
		) );
116
117
		parent::__construct( $calendar );
118
119
		if ( ! is_null( $this->post ) ) {
120
121
			$this->set_properties( $this->view->get_type() );
122
123
			$id = $this->id;
124
			$theme = $this->theme;
125
126
			add_filter( 'simcal_calendar_class', function( $class, $post_id ) use ( $theme, $id ) {
127
				if ( in_array( 'default-calendar', $class ) && $post_id === $id ) {
128
					array_push( $class, 'default-calendar-' . $theme );
129
				}
130
				return $class;
131
			}, 10, 2 );
132
133
		}
134
135
		// Calendar settings handling.
136
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
137
			$admin = new Default_Calendar_Admin();
138
			$this->settings = $admin->settings_fields();
139
		}
140
	}
141
142
	/**
143
	 * Set properties.
144
	 *
145
	 * @since  3.0.0
146
	 * @access private
147
	 *
148
	 * @param  $view
149
	 */
150
	private function set_properties( $view ) {
151
152
		// Set styles.
153
		if ( 'dark' == get_post_meta( $this->id, '_default_calendar_style_theme', true ) ) {
154
			$this->theme = 'dark';
155
		}
156
		if ( $today_color = get_post_meta( $this->id, '_default_calendar_style_today', true ) ) {
157
			$this->today_color = esc_attr( $today_color );
158
		}
159
		if ( $day_events_color = get_post_meta( $this->id, '_default_calendar_style_days_events', true ) ) {
160
			$this->days_events_color = esc_attr( $day_events_color );
161
		}
162
163
		// Hide too many events.
164
		if ( 'yes' == get_post_meta( $this->id, '_default_calendar_limit_visible_events', true ) ) {
165
			$this->events_limit = absint( get_post_meta( $this->id, '_default_calendar_visible_events', true ) );
166
		}
167
168
		// Expand multiple day events.
169
		if ( 'yes' == get_post_meta( $this->id, '_default_calendar_expand_multi_day_events', true ) || ( 'list' == $view && 'current_day_only' == get_post_meta( $this->id, '_default_calendar_expand_multi_day_events', true ) ) ) {
170
			$this->events = $this->expand_multiple_days_events();
171
		}
172
173
		if ( 'grid' == $view ) {
174
175
			// Use hover to open event bubbles.
176
			if ( 'hover' == get_post_meta( $this->id, '_default_calendar_event_bubble_trigger', true ) ) {
177
				$this->event_bubble_trigger = 'hover';
178
			}
179
180
			// Trim long event titles.
181
			if ( 'yes' == get_post_meta( $this->id, '_default_calendar_trim_titles', true ) ) {
182
				$this->trim_titles = max( absint( get_post_meta( $this->id, '_default_calendar_trim_titles_chars', true ) ), 1 );
183
			}
184
185
		} else {
186
187
			// List range.
188
			$this->group_type = esc_attr( get_post_meta( $this->id, '_default_calendar_list_range_type', true ) );
189
			$this->group_span = max( absint( get_post_meta( $this->id, '_default_calendar_list_range_span', true ) ), 1 );
190
191
			// Make the list look more compact.
192
			if ( 'yes' == get_post_meta( $this->id, '_default_calendar_compact_list', true ) ) {
193
				$this->compact_list = true;
194
			}
195
196
		}
197
198
	}
199
200
	/**
201
	 * Expand multiple day events.
202
	 *
203
	 * @since  3.0.0
204
	 * @access private
205
	 *
206
	 * @return array
207
	 */
208
	private function expand_multiple_days_events() {
209
210
		$old_events = $this->events;
211
		$new_events = array();
212
213
		if ( ! empty( $old_events ) ) {
214
215
			foreach ( $old_events as $events ) {
216
				foreach ( $events as $event ) {
217
					if ( $event instanceof Event ) {
218
						if ( false !== $event->multiple_days ) {
219
							$days = $event->multiple_days;
220
221
							if ( $days == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
222
								$new_events[ intval( $event->start + ( DAY_IN_SECONDS ) - 1 ) ][] = $event;
223
							} else {
224
225
								/*if ( ! empty( $event->whole_day ) ) {
226
									$days--;
227
								}*/
228
229
								for ( $d = 1; $d <= $days; $d++ ) {
230
									$new_events[ intval( $event->start + ( $d * DAY_IN_SECONDS ) - 1 ) ][] = $event;
231
								}
232
							}
233
						}
234
					}
235
				}
236
237
			}
238
239
		}
240
241
		$events = $old_events + $new_events;
242
		ksort( $events, SORT_NUMERIC );
243
		return $events;
244
	}
245
246
	/**
247
	 * Get a view.
248
	 *
249
	 * Returns one of this calendar's views.
250
	 *
251
	 * @since  3.0.0
252
	 *
253
	 * @param  string $view
254
	 *
255
	 * @return null|Calendar_View
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Views\Default_Calendar_G...ault_Calendar_List|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
256
	 */
257
	public function get_view( $view = '' ) {
258
259
		$view = ! empty( $view ) ? $view : 'grid';
260
261
		do_action( 'simcal_calendar_get_view', $this->type, $view );
262
263
		if ( 'grid' == $view ) {
264
			return new Views\Default_Calendar_Grid( $this );
265
		} elseif ( 'list' == $view ) {
266
			return new Views\Default_Calendar_List( $this );
267
		}
268
269
		return null;
270
	}
271
272
}
273