Completed
Push — master ( afae5e...fce50c )
by
unknown
03:06
created

Grouped_Calendars::get_events()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 57
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 57
rs 6.62
cc 12
eloc 26
nc 6
nop 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Grouped Calendars Feed
4
 *
5
 * @package SimpleCalendar/Feeds
6
 */
7
namespace SimpleCalendar\Feeds;
8
9
use SimpleCalendar\Abstracts\Calendar;
10
use SimpleCalendar\Abstracts\Feed;
11
use SimpleCalendar\Feeds\Admin\Grouped_Calendars_Admin;
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Grouped calendars feed.
19
 *
20
 * Feed made of multiple calendar feeds combined together.
21
 *
22
 * @since  3.0.0
23
 */
24
class Grouped_Calendars extends Feed {
25
26
	/**
27
	 * Feed ids to get events from.
28
	 *
29
	 * @access public
30
	 * @var array
31
	 */
32
	public $calendars_ids = array();
33
34
	/**
35
	 * Set properties.
36
	 *
37
	 * @since 3.0.0
38
	 *
39
	 * @param string|Calendar $calendar
40
	 */
41
	public function __construct( $calendar = '' ) {
42
43
		parent::__construct( $calendar );
44
45
		$this->type = 'grouped-calendars';
46
		$this->name = __( 'Grouped Calendar', 'google-calendar-events' );
47
48
		if ( $this->post_id > 0 ) {
49
			$this->set_source();
50
			if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
51
				$this->events = $this->get_events();
52
			}
53
		}
54
55
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
56
			new Grouped_Calendars_Admin( $this );
57
		}
58
	}
59
60
	/**
61
	 * Set source.
62
	 *
63
	 * @since 3.0.0
64
	 *
65
	 * @param array $ids Array of calendar ids.
66
	 */
67
	public function set_source( $ids = array() ) {
68
69
		$source = get_post_meta( $this->post_id, '_grouped_calendars_source', true );
70
71
		if ( 'ids' == $source ) {
72
73
			if ( empty( $ids ) ) {
74
				$ids = get_post_meta( $this->post_id, '_grouped_calendars_ids', true );
75
			}
76
77
			$this->calendars_ids = ! empty( $ids ) && is_array( $ids ) ? array_map( 'absint', $ids ) : array();
78
79
		} elseif ( 'category' == $source ) {
80
81
			$categories = get_post_meta( $this->post_id, '_grouped_calendars_category', true );
82
83
			if ( $categories && is_array( $categories ) ) {
84
85
				$tax_query = array(
86
					'taxonomy' => 'calendar_category',
87
					'field'    => 'term_id',
88
					'terms'    => array_map( 'absint', $categories ),
89
				);
90
91
				$calendars = get_posts( array(
92
					'post_type' => 'calendar',
93
					'tax_query' => array( $tax_query ),
94
					'nopaging'  => true,
1 ignored issue
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
95
					'fields'    => 'ids',
96
				) );
97
98
				$this->calendars_ids = ! empty( $calendars ) && is_array( $calendars ) ? $calendars : array();
99
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
100
101
		}
102
	}
103
104
	/**
105
	 * Get events from multiple calendars.
106
	 *
107
	 * @since  3.0.0
108
	 *
109
	 * @return array
110
	 */
111
	public function get_events() {
112
113
		$ids    = $this->calendars_ids;
114
		$events = get_transient( '_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type );
115
116
		if ( empty( $events ) && ! empty( $ids ) && is_array( $ids ) ) {
117
118
			$events = array();
119
120
			foreach ( $ids as $cal_id ) {
121
122
				$calendar = simcal_get_calendar( intval( $cal_id ) );
123
124
				simcal_delete_feed_transients( $cal_id );
125
126
				if ( $calendar instanceof Calendar ) {
127
128
					// Sometimes the calendars might have events at the same time from different calendars
129
					// When merging the arrays together some of the events will be lost because the keys are the same and one will overwrite the other
130
					// This snippet checks if the key already exists in the master events array and if it does it subtracts 1 from it to make the key unique and then unsets the original key.
131
					foreach( $calendar->events as $k => $v ) {
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...
132
						if ( array_key_exists( $k, $events ) ) {
133
							$calendar->events[ $k - 1 ] = $v;
134
							unset( $k );
135
						}
136
					}
137
138
					$events = is_array( $calendar->events ) ? $events + $calendar->events : $events;
139
				}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
140
141
			}
142
143
			if ( ! empty( $events ) ) {
144
145
				// Trim events to set the earliest one as specified in feed settings.
146
				$earliest_event = intval( $this->time_min );
147
				if ( $earliest_event > 0 ) {
148
					$events = $this->array_filter_key( $events, array( $this, 'filter_events_before' ) );
149
				}
150
151
				// Trim events to set the latest one as specified in feed settings.
152
				$latest_event = intval( $this->time_max );
153
				if ( $latest_event > 0 ) {
154
					$events = $this->array_filter_key( $events, array( $this, 'filter_events_after' ) );
155
				}
156
157
				set_transient(
158
					'_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type,
159
					$events,
160
					absint( $this->cache )
161
				);
162
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
163
164
		}
165
166
		return $events;
167
	}
168
169
	/**
170
	 * Array filter key.
171
	 *
172
	 * `array_filter` does not allow to parse an associative array keys before PHP 5.6.
173
	 *
174
	 * @since  3.0.0
175
	 * @access private
176
	 *
177
	 * @param  array        $array
178
	 * @param  array|string $callback
179
	 *
180
	 * @return array
181
	 */
182
	private function array_filter_key( array $array, $callback ) {
183
		$matched_keys = array_filter( array_keys( $array ), $callback );
184
		return array_intersect_key( $array, array_flip( $matched_keys ) );
185
	}
186
187
	/**
188
	 * Array filter callback.
189
	 *
190
	 * @since  3.0.0
191
	 * @access private
192
	 *
193
	 * @param  int $event Timestamp.
194
	 *
195
	 * @return bool
196
	 */
197
	private function filter_events_before( $event ) {
198
		if ( $this->time_min !== 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
199
			return intval( $event ) > intval( $this->time_min );
200
		}
201
		return true;
202
	}
203
204
	/**
205
	 * Array filter callback.
206
	 *
207
	 * @since  3.0.0
208
	 * @access private
209
	 *
210
	 * @param  int $event Timestamp.
211
	 *
212
	 * @return bool
213
	 */
214
	private function filter_events_after( $event ) {
215
		if ( $this->time_max !== 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
216
			return intval( $event ) < intval( $this->time_max );
217
		}
218
		return true;
219
	}
220
221
}
222