Google_Sheet_Helpers::api_get_google_drives()

Method api_get_google_drives


Return Return

(void)


Source Source

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

	public function api_get_google_drives() {

		$gs_client = $this->get_google_client();

		if ( ! $gs_client ) {
			return;
		}

		$response = wp_remote_post(
			$this->automator_api,
			array(
				'method' => 'POST',
				'body'   => array(
					'action'       => 'list_drives',
					'access_token' => $gs_client,
					'api_ver'      => '2.0',
					'plugin_ver'   => InitializePlugin::PLUGIN_VERSION,
				),
			)
		);

		$body = null;

		$options = array();

		$options['-1'] = __( 'My google drive', 'uncanny-automator' );

		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
			$body = json_decode( wp_remote_retrieve_body( $response ) );

			if ( $body && $body->statusCode == 200 ) {
				foreach ( $body->data as $drive ) {
					$options[ $drive->id ] = $drive->name;
				}
			} else {
				$options['-1'] = __( 'API returned an error: ', 'uncanny-automator' ) . $body->error->description;
			}
		} else {
			$options['-1'] = __( 'Could not connect to the API. Please try again in a few minutes.', 'uncanny-automator' );
		}

		set_transient( 'automator_api_get_google_drives', $options, 60 );

		return $options;

	}