Google_Calendar_Helpers::list_events()

Method list_events.


Description Description

A wp_ajax callback method.


Top ↑

Return Return

(Uncanny_Automatorvoid.)


Source Source

File: src/integrations/google-calendar/helpers/google-calendar-helpers.php

	public function list_events() {

		Automator()->utilities->ajax_auth_check();

		$items = array();

		try {

			$response = $this->api_call(
				array(
					'action'      => 'list_events',
					'calendar_id' => automator_filter_input( 'value', INPUT_POST ),
					'timezone'    => wp_timezone_string(),
				)
			);

			if ( is_array( $response['data'] ) && ! empty( $response['data'] ) ) {

				foreach ( $response['data'] as $event ) {

					if ( ! empty( $event['summary'] ) ) {

						// Date start can either be from [date_start] or from [datetime_start].
						// Google API returns date_start if its wholeday event. Otherwise, datetime_start.
						$date_start = isset( $event['datetime_start'] ) ? $event['datetime_start'] : '';

						$type = 'datetime';

						if ( empty( $date_start ) ) {
							// Try the [date_start]. The [date_start] AND the [datetime_start] cannot be both nulled at the same time.
							$date_start = isset( $event['date_start'] ) ? $event['date_start'] : '';
							$type       = 'date';
						}

						$date = new \DateTime( $date_start, new \DateTimeZone( wp_timezone_string() ) );

						$date_string = $date->format( 'F j, Y' );

						if ( 'datetime' === $type ) {
							$date_string = $date->format( 'F j, Y g:i A' );
						}

						$items[] = array(
							'text'  => sprintf( '%1$s (%2$s)', $event['summary'], $date_string ),
							'value' => $event['id'],
						);

					}
				}
			}
		} catch ( \Exception $e ) {

			$items = array(
				array(
					/* translators: Error message */
					'text'  => sprintf( esc_html__( '%1$s: %2$s Please try again later.', 'uncanny-automator' ), $e->getCode(), $e->getMessage() ),
					'value' => '',
				),
			);

		}

		if ( ! empty( $events ) ) {
			foreach ( $events as $event ) {
				$items[] = array(
					'text'  => $event['summary'],
					'value' => $event['id'],
				);
			}
		}

		wp_send_json( $items );

	}