Google_Sheet_Helpers::api_get_worksheets_from_spreadsheet( $spreadsheet_id )

Method api_get_worksheets_from_spreadsheet


Parameters Parameters

$spreadsheet_id

(Required)


Top ↑

Return Return

(void)


Source Source

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

	public function api_get_worksheets_from_spreadsheet( $spreadsheet_id ) {

		$client = $this->get_google_client();

		if ( ! $client || empty( $spreadsheet_id ) ) {
			return;
		}

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

		$fields[] = array(
			'value' => '-1',
			'text'  => __( 'Select a worksheet', '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 $worksheet ) {

					$sheet_id    = $worksheet->properties->sheetId;
					$sheet_title = $worksheet->properties->title;
					if ( 0 === (int) $sheet_id ) {
						$hashed   = sha1( self::$hash_string );
						$sheet_id = substr( $hashed, 0, 9 );
					}
					$fields[] = array(
						'value' => $sheet_id,
						'text'  => $sheet_title,
					);
				}

				return $fields;
			} else {
				return array(
					'text'  => 'Error communicating with the Google',
					'value' => 0,
				);
			}
		}

		return array(
			'text'  => 'Error communicating with the API',
			'value' => 0,
		);

	}