Google_Sheet_Helpers::select_gsworksheet_from_gsspreadsheet_columns()


Source Source

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

	public function select_gsworksheet_from_gsspreadsheet_columns() {

		// Nonce and post object validation
		Automator()->utilities->ajax_auth_check();
		$fields = array();
		$values = automator_filter_input_array( 'values', INPUT_POST );
		if ( ! isset( $values['GSSPREADSHEET'] ) ) {
			echo wp_json_encode( $fields );
			die();
		}
		$gs_spreadsheet_id = sanitize_text_field( $values['GSSPREADSHEET'] );

		if ( empty( $values['GSWORKSHEET'] ) ) {
			echo wp_json_encode( $fields );
			die();
		}

		$worksheet_id = sanitize_text_field( $values['GSWORKSHEET'] );
		$hashed       = sha1( self::$hash_string );
		$sheet_id     = substr( $hashed, 0, 9 );

		if ( (string) $worksheet_id === (string) $sheet_id || intval( '-1' ) === intval( $worksheet_id ) ) {
			$worksheet_id = 0;
		}

		$response = $this->api_get_rows( $gs_spreadsheet_id, $worksheet_id );

		$fields = array();

		if ( ! empty( $response ) && ! empty( $response->samples ) ) {
			$rows = array_shift( $response->samples );
			foreach ( $rows as $index => $r ) {
				$num2alpha = sprintf( '1-%1$s2:%1$s', $this->num2alpha( $index ) );
				$fields[]  = array(
					'value' => $num2alpha,
					'text'  => $r['key'],
				);
			}
		}

		echo wp_json_encode( $fields );

		die();

	}