| Conditions | 34 |
| Paths | 102 |
| Total Lines | 183 |
| Code Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 148 | public function get_events() { |
||
| 149 | |||
| 150 | $calendar = get_transient( '_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type ); |
||
| 151 | |||
| 152 | if ( empty( $calendar ) && ! empty( $this->google_calendar_id ) ) { |
||
| 153 | |||
| 154 | $error = ''; |
||
| 155 | |||
| 156 | try { |
||
| 157 | $response = $this->make_request( $this->google_calendar_id ); |
||
| 158 | } catch ( \Exception $e ) { |
||
| 159 | $error .= $e->getMessage(); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ( empty( $error ) && isset( $response['events'] ) && isset( $response['timezone'] ) ) { |
||
| 163 | |||
| 164 | $calendar = array_merge( $response, array( 'events' => array() ) ); |
||
| 165 | |||
| 166 | // If no timezone has been set, use calendar feed. |
||
| 167 | if ( 'use_calendar' == $this->timezone_setting ) { |
||
| 168 | $this->timezone = $calendar['timezone']; |
||
| 169 | } |
||
| 170 | |||
| 171 | $source = isset( $response['title'] ) ? sanitize_text_field( $response['title'] ) : ''; |
||
| 172 | |||
| 173 | if ( ! empty( $response['events'] ) && is_array( $response['events'] ) ) { |
||
| 174 | foreach ( $response['events'] as $event ) { |
||
| 175 | if ( $event instanceof \Google_Service_Calendar_Event ) { |
||
| 176 | |||
| 177 | // Visibility and status. |
||
| 178 | // Public calendars may have private events which can't be properly accessed by simple api key method. |
||
| 179 | // Also want to skip cancelled events (single occurences deleted from repeating events) |
||
| 180 | $visibility = $event->getVisibility(); |
||
| 181 | $status = $event->getStatus(); |
||
| 182 | if ( $this->type == 'google' && ( $visibility == 'private' || $visibility == 'confidential' || $status == 'cancelled' ) ) { |
||
| 183 | continue; |
||
| 184 | } |
||
| 185 | |||
| 186 | // Event title & description. |
||
| 187 | $title = strip_tags( $event->getSummary() ); |
||
| 188 | $title = sanitize_text_field( iconv( mb_detect_encoding( $title, mb_detect_order(), true ), 'UTF-8', $title ) ); |
||
| 189 | $description = wp_kses_post( iconv( mb_detect_encoding( $event->getDescription(), mb_detect_order(), true ), 'UTF-8', $event->getDescription() ) ); |
||
| 190 | |||
| 191 | $whole_day = false; |
||
| 192 | |||
| 193 | // Event start properties. |
||
| 194 | if( 'use_calendar' == $this->timezone_setting ) { |
||
| 195 | $start_timezone = ! $event->getStart()->timeZone ? $calendar['timezone'] : $event->getStart()->timeZone; |
||
| 196 | } else { |
||
| 197 | $start_timezone = $this->timezone; |
||
| 198 | } |
||
| 199 | |||
| 200 | if ( is_null( $event->getStart()->dateTime ) ) { |
||
| 201 | // Whole day event. |
||
| 202 | $date = Carbon::parse( $event->getStart()->date ); |
||
| 203 | $google_start = Carbon::createFromDate( $date->year, $date->month, $date->day, $start_timezone )->startOfDay()->addSeconds( 59 ); |
||
| 204 | $google_start_utc = Carbon::createFromDate( $date->year, $date->month, $date->day, 'UTC' )->startOfDay()->addSeconds( 59 ); |
||
| 205 | $whole_day = true; |
||
| 206 | } else { |
||
| 207 | $date = Carbon::parse( $event->getStart()->dateTime ); |
||
| 208 | $google_start = Carbon::create( $date->year, $date->month, $date->day, $date->hour, $date->minute, $date->second, $start_timezone ); |
||
| 209 | $google_start_utc = Carbon::create( $date->year, $date->month, $date->day, $date->hour, $date->minute, $date->second, 'UTC' ); |
||
| 210 | } |
||
| 211 | // Start. |
||
| 212 | $start = $google_start->getTimestamp(); |
||
| 213 | // Start UTC. |
||
| 214 | $start_utc = $google_start_utc->getTimestamp(); |
||
| 215 | |||
| 216 | $end = $end_utc = $end_timezone = ''; |
||
| 217 | $span = 0; |
||
| 218 | if ( false == $event->getEndTimeUnspecified() ) { |
||
| 219 | |||
| 220 | // Event end properties. |
||
| 221 | if( 'use_calendar' == $this->timezone_setting ) { |
||
| 222 | $end_timezone = ! $event->getEnd()->timeZone ? $calendar['timezone'] : $event->getEnd()->timeZone; |
||
| 223 | } else { |
||
| 224 | $end_timezone = $this->timezone; |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( is_null( $event->getEnd()->dateTime ) ) { |
||
| 228 | // Whole day event. |
||
| 229 | $date = Carbon::parse( $event->getEnd()->date ); |
||
| 230 | $google_end = Carbon::createFromDate( $date->year, $date->month, $date->day, $end_timezone )->startOfDay()->subSeconds( 59 ); |
||
| 231 | $google_end_utc = Carbon::createFromDate( $date->year, $date->month, $date->day, 'UTC' )->startOfDay()->subSeconds( 59 ); |
||
| 232 | } else { |
||
| 233 | $date = Carbon::parse( $event->getEnd()->dateTime ); |
||
| 234 | $google_end = Carbon::create( $date->year, $date->month, $date->day, $date->hour, $date->minute, $date->second, $end_timezone ); |
||
| 235 | $google_end_utc = Carbon::create( $date->year, $date->month, $date->day, $date->hour, $date->minute, $date->second, 'UTC' ); |
||
| 236 | } |
||
| 237 | // End. |
||
| 238 | $end = $google_end->getTimestamp(); |
||
| 239 | // End UTC. |
||
| 240 | $end_utc = $google_end_utc->getTimestamp(); |
||
| 241 | |||
| 242 | // Count multiple days. |
||
| 243 | $span = $google_start->setTimezone( $calendar['timezone'] )->diffInDays( $google_end->setTimezone( $calendar['timezone'] ) ); |
||
| 244 | |||
| 245 | if ( $span == 0 ) { |
||
| 246 | if ( $google_start->toDateString() !== $google_end->toDateString() ) { |
||
| 247 | $span = 1; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Multiple days. |
||
| 253 | $multiple_days = $span > 0 ? $span : false; |
||
| 254 | |||
| 255 | // Google cannot have two different locations for start and end time. |
||
| 256 | $start_location = $end_location = $event->getLocation(); |
||
| 257 | |||
| 258 | // Recurring event. |
||
| 259 | $recurrence = $event->getRecurrence(); |
||
| 260 | $recurring_id = $event->getRecurringEventId(); |
||
| 261 | if ( ! $recurrence && $recurring_id ) { |
||
| 262 | $recurrence = true; |
||
| 263 | } |
||
| 264 | |||
| 265 | // Event link. |
||
| 266 | if ( 'use_calendar' == $this->timezone_setting ) { |
||
| 267 | $link = add_query_arg( array( 'ctz' => $this->timezone ), $event->getHtmlLink() ); |
||
| 268 | } else { |
||
| 269 | $link = $event->getHtmlLink(); |
||
| 270 | } |
||
| 271 | |||
| 272 | // Build the event. |
||
| 273 | $calendar['events'][ intval( $start ) ][] = array( |
||
| 274 | 'type' => 'google-calendar', |
||
| 275 | 'source' => $source, |
||
| 276 | 'title' => $title, |
||
| 277 | 'description' => $description, |
||
| 278 | 'link' => $link, |
||
| 279 | 'visibility' => $visibility, |
||
| 280 | 'uid' => $event->getICalUID(), |
||
| 281 | 'calendar' => $this->post_id, |
||
| 282 | 'timezone' => $this->timezone, |
||
| 283 | 'start' => $start, |
||
| 284 | 'start_utc' => $start_utc, |
||
| 285 | 'start_timezone' => $start_timezone, |
||
| 286 | 'start_location' => $start_location, |
||
| 287 | 'end' => $end, |
||
| 288 | 'end_utc' => $end_utc, |
||
| 289 | 'end_timezone' => $end_timezone, |
||
| 290 | 'end_location' => $end_location, |
||
| 291 | 'whole_day' => $whole_day, |
||
| 292 | 'multiple_days' => $multiple_days, |
||
| 293 | 'recurrence' => $recurrence, |
||
| 294 | 'template' => $this->events_template, |
||
| 295 | ); |
||
| 296 | |||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if ( ! empty( $calendar['events'] ) ) { |
||
| 301 | |||
| 302 | ksort( $calendar['events'], SORT_NUMERIC ); |
||
| 303 | |||
| 304 | set_transient( |
||
| 305 | '_simple-calendar_feed_id_' . strval( $this->post_id ) . '_' . $this->type, |
||
| 306 | $calendar, |
||
| 307 | max( absint( $this->cache ), 1 ) // Since a value of 0 means forever we set the minimum here to 1 if the user has set it to be 0 |
||
| 308 | ); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | } else { |
||
| 313 | |||
| 314 | $message = __( 'While trying to retrieve events, Google returned an error:', 'google-calendar-events' ); |
||
| 315 | $message .= '<br><br>' . $error . '<br><br>'; |
||
| 316 | $message .= __( 'Please ensure that both your Google Calendar ID and API Key are valid and that the Google Calendar you want to display is public.', 'google-calendar-events' ) . '<br><br>'; |
||
| 317 | $message .= __( 'Only you can see this notice.', 'google-calendar-events' ); |
||
| 318 | |||
| 319 | return $message; |
||
| 320 | } |
||
| 321 | |||
| 322 | } |
||
| 323 | |||
| 324 | // If no timezone has been set, use calendar feed. |
||
| 325 | if ( 'use_calendar' == $this->timezone_setting && isset( $calendar['timezone'] ) ) { |
||
| 326 | $this->timezone = $calendar['timezone']; |
||
| 327 | } |
||
| 328 | |||
| 329 | return isset( $calendar['events'] ) ? $calendar['events'] : array(); |
||
| 330 | } |
||
| 331 | |||
| 446 |