Google_Sheet_Helpers::api_get_rows( $spreadsheet_id,  $worksheet_id )

Method api_get_rows


Parameters Parameters

$spreadsheet_id

(Required)

$worksheet_id

(Required)


Top ↑

Return Return

(void)


Source Source

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

	public function api_get_rows( $spreadsheet_id, $worksheet_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_rows',
					'access_token'   => $client,
					'spreadsheet_id' => $spreadsheet_id,
					'worksheet_id'   => $worksheet_id,
					'api_ver'        => '2.0',
					'plugin_ver'     => InitializePlugin::PLUGIN_VERSION,
				),
			)
		);

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

			if ( $body && $body->statusCode == 200 ) {
				$rows = $body->data;

				$alphas = range( 'A', 'Z' );

				if ( $rows[0] ) {
					foreach ( $rows[0] as $key => $heading ) {
						if ( empty( $heading ) ) {
							$heading = 'COLUMN:' . $alphas[ $key ];
						}
						$fields[] = array(
							'key'  => $heading,
							'type' => 'text',
							'data' => $heading,
						);
					}
					$response = (object) array(
						'success' => true,
						'samples' => array( $fields ),
					);

					return $response;

				}
			}
		}

		$response = (object) array(
			'success' => false,
			'error'   => 'Couldn\'t fetch rows',
		);

		return $response;

	}