Feed::set_earliest_event()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 26
loc 26
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Feed
4
 *
5
 * @package SimpleCalendar/Feeds
6
 */
7
namespace SimpleCalendar\Abstracts;
8
9
use Carbon\Carbon;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * The Feed.
17
 *
18
 * Source of events supplied to calendars.
19
 *
20
 * @since 3.0.0
21
 */
22
abstract class Feed {
23
24
	/**
25
	 * Feed type.
26
	 *
27
	 * @access public
28
	 * @var string
29
	 */
30
	public $type = '';
31
32
	/**
33
	 * Feed name.
34
	 *
35
	 * @access public
36
	 * @var string
37
	 */
38
	public $name = '';
39
40
	/**
41
	 * Calendar post id.
42
	 *
43
	 * @access public
44
	 * @var int
45
	 */
46
	public $post_id = 0;
47
48
	/**
49
	 * Calendar opening.
50
	 *
51
	 * @access protected
52
	 * @var int
53
	 */
54
	protected $calendar_start = 0;
55
56
	/**
57
	 * Start of week.
58
	 *
59
	 * @access protected
60
	 * @var int
61
	 */
62
	protected $week_starts = 0;
63
64
	/**
65
	 * Events.
66
	 *
67
	 * @access public
68
	 * @var array
69
	 */
70
	public $events = array();
71
72
	/**
73
	 * Events template.
74
	 *
75
	 * @access protected
76
	 * @var string
77
	 */
78
	protected $events_template = '';
79
80
	/**
81
	 * Timezone setting.
82
	 *
83
	 * @access protected
84
	 * @var string
85
	 */
86
	protected  $timezone_setting = '';
87
88
	/**
89
	 * Timezone.
90
	 *
91
	 * @access public
92
	 * @var string
93
	 */
94
	public $timezone = '';
95
96
	/**
97
	 * Earliest possible event.
98
	 *
99
	 * @access public
100
	 * @var int
101
	 */
102
	public $time_min = 0;
103
104
	/**
105
	 * Latest possible event.
106
	 *
107
	 * @access public
108
	 * @var int
109
	 */
110
	public $time_max = 0;
111
112
	/**
113
	 * Feed cache interval.
114
	 *
115
	 * @access protected
116
	 * @var int
117
	 */
118
	protected $cache = 7200;
119
120
	/**
121
	 * Feed settings.
122
	 *
123
	 * @access protected
124
	 * @var array
125
	 */
126
	protected $settings = array();
127
128
	/**
129
	 * Constructor.
130
	 *
131
	 * @since 3.0.0
132
	 *
133
	 * @param string|Calendar $calendar
134
	 */
135
	public function __construct( $calendar = '' ) {
136
137
		if ( $calendar instanceof Calendar ) {
138
139
			if ( isset( $calendar->id ) ) {
140
				$this->post_id = $calendar->id;
141
			}
142
			if ( isset( $calendar->start ) ) {
143
				$this->calendar_start = $calendar->start;
144
			}
145
			$this->week_starts      = isset( $calendar->week_starts ) ? $calendar->week_starts : get_option( 'start_of_week' );
146
			$this->events_template  = ! empty( $calendar->events_template ) ? $calendar->events_template : simcal_default_event_template();
147
148
			if ( $this->post_id > 0 ) {
149
				$this->set_cache();
150
				$this->timezone_setting = get_post_meta( $this->post_id, '_feed_timezone_setting', true );
151
				$this->timezone = $calendar->timezone;
152
				$this->set_earliest_event();
153
				$this->set_latest_event();
154
			}
155
		}
156
	}
157
158
	/**
159
	 * Input fields for settings page.
160
	 *
161
	 * @since  3.0.0
162
	 *
163
	 * @return false|array
164
	 */
165
	public function settings_fields() {
166
		return $this->settings;
167
	}
168
169
	/**
170
	 * Set earliest event.
171
	 *
172
	 * @since 3.0.0
173
	 *
174
	 * @param int $timestamp
175
	 */
176 View Code Duplication
	public function set_earliest_event( $timestamp = 0 ) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
177
178
		$earliest = intval( $timestamp );
179
180
		if ( $earliest === 0 ) {
181
182
			$start = Carbon::createFromTimestamp( $this->calendar_start, $this->timezone );
183
184
			$earliest_date  = esc_attr( get_post_meta( $this->post_id, '_feed_earliest_event_date', true ) );
185
			$earliest_range = max( absint( get_post_meta( $this->post_id, '_feed_earliest_event_date_range', true ) ), 1 );
186
187
			if ( 'days_before' == $earliest_date ) {
188
				$earliest = $start->subDays( $earliest_range )->getTimestamp();
189
			} elseif ( 'weeks_before' == $earliest_date ) {
190
				$earliest = $start->subWeeks( $earliest_range )->addDay()->getTimestamp();
191
			} elseif ( 'months_before' == $earliest_date ) {
192
				$earliest = $start->subMonths( $earliest_range )->addDay()->getTimestamp();
193
			} elseif ( 'years_before' == $earliest_date ) {
194
				$earliest = $start->subYears( $earliest_range )->addDay()->getTimestamp();
195
			} else {
196
				$earliest = $start->getTimestamp();
197
			}
198
		}
199
200
		$this->time_min = $earliest;
201
	}
202
203
	/**
204
	 * Set latest event.
205
	 *
206
	 * @since 3.0.0
207
	 *
208
	 * @param int $timestamp
209
	 */
210 View Code Duplication
	public function set_latest_event( $timestamp = 0 ) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
211
212
		$latest = intval( $timestamp );
213
214
		if ( $latest === 0 ) {
215
216
			$start = Carbon::createFromTimestamp( $this->calendar_start, $this->timezone )->endOfDay();
217
218
			$latest_date  = esc_attr( get_post_meta( $this->post_id, '_feed_latest_event_date', true ) );
219
			$latest_range = max( absint( get_post_meta( $this->post_id, '_feed_latest_event_date_range', true ) ), 1 );
220
221
			if ( 'days_after' == $latest_date ) {
222
				$latest = $start->addDays( $latest_range )->getTimestamp();
223
			} elseif ( 'weeks_after' == $latest_date ) {
224
				$latest = $start->addWeeks( $latest_range )->subDay()->getTimestamp();
225
			} elseif ( 'months_after' == $latest_date ) {
226
				$latest = $start->addMonths( $latest_range )->subDay()->getTimestamp();
227
			} elseif ( 'years_after' == $latest_date ) {
228
				$latest = $start->addYears( $latest_range )->subDay()->getTimestamp();
229
			} else {
230
				$latest = $start->getTimestamp();
231
			}
232
233
		}
234
235
		$this->time_max = $latest;
236
	}
237
238
	/**
239
	 * Set cache.
240
	 *
241
	 * @since 3.0.0
242
	 *
243
	 * @param int $time
244
	 */
245
	public function set_cache( $time = 0 ) {
246
		if ( $time === 0 || ! is_numeric( $time ) ) {
247
			$cache = get_post_meta( $this->post_id, '_feed_cache', true );
248
			$time  = is_numeric( $cache ) && $cache >= 0 ? absint( $cache ) : $this->cache;
249
		}
250
		$this->cache = absint( $time );
251
	}
252
253
	/**
254
	 * Get events feed.
255
	 *
256
	 * @since 3.0.0
257
	 *
258
	 * @return array
259
	 */
260
	abstract public function get_events();
261
262
}
263