Gotowebinar_Helpers::get_webinars()


Source

File: src/integrations/gotowebinar/helpers/gotowebinar-helpers.php

	public function get_webinars() {

		$webinars = array();

		try {
			list( $access_token, $organizer_key ) = $this->get_webinar_token();

			$current_time = current_time( 'Y-m-d\TH:i:s\Z' );
	
			$current_time_plus_years = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '+2 year', strtotime( $current_time ) ) );
	
			$params['headers'] = array(
				'Authorization' => $access_token,
			);
	
			$params['method'] = 'GET';
			$params['url'] = 'https://api.getgo.com/G2W/rest/v2/organizers/' . $organizer_key . '/webinars?fromTime=' . $current_time . '&toTime=' . $current_time_plus_years . '&page=0&size=200';
	
			$response = $this->remote_request( $params );

			$code = wp_remote_retrieve_response_code( $response );
	
			// Prepare webinar list.
			if ( 200 !== $code ) {
				throw new \Exception( __( 'Unable to fetch webinars from this account', 'uncanny-automator' ) );
			}
	
			$jsondata = json_decode( preg_replace( '/("\w+"):(\d+(\.\d+)?)/', '\\1:"\\2"', wp_remote_retrieve_body( $response ) ), true );

			$jsondata = isset( $jsondata['_embedded']['webinars'] ) ? $jsondata['_embedded']['webinars'] : array();

			if ( count( $jsondata ) < 1 ) {
				throw new \Exception( __( 'No webinars were found in this account', 'uncanny-automator' ) );
			}

			foreach ( $jsondata as $key1 => $webinar ) {

				$webinars[] = array(
					'text'  => $webinar['subject'],
					'value' => (string) $webinar['webinarKey'] . '-objectkey',
				);

			}
		
		} catch ( \Exception $e ) {
			
			$webinars[] = array(
				'text'  => $e->getMessage(),
				'value' => ''
			);
		}

		return $webinars;
	}