SHEET_ADDARECORD::add_row_google_sheet( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)


Source Source

File: src/integrations/google-sheet/actions/sheet-addarecord.php

	public function add_row_google_sheet( $user_id, $action_data, $recipe_id, $args ) {

		$gs_drive        = $action_data['meta']['GSDRIVE'];
		$gs_spreadsheet  = $action_data['meta']['GSSPREADSHEET'];
		$gs_worksheet    = $action_data['meta']['GSWORKSHEET'];
		$worksheet_field = $action_data['meta']['WORKSHEET_FIELDS'];
		$fields          = json_decode( $worksheet_field, true );
		$key_values      = array();
		$check_all_empty = true;
		$hashed          = sha1( Google_Sheet_Helpers::$hash_string );
		$sheet_id        = substr( $hashed, 0, 9 );

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

		for ( $i = 0; $i < count( $fields ); $i ++ ) {
			$key                = $fields[ $i ]['COLUMN_NAME'];
			$value              = Automator()->parse->text( $fields[ $i ]['COLUMN_VALUE'], $recipe_id, $user_id, $args );
			$key_values[ $key ] = $value;
			if ( ! empty( $value ) ) {
				$check_all_empty = false;
			}
		}

		if ( $check_all_empty ) {
			// log error no heading found.
			$error_msg                           = __( 'Trying to add an empty row.', 'uncanny-automator' );
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

			return;
		}

		try {
			$response = Automator()->helpers->recipe->google_sheet->api_append_row( $gs_spreadsheet, $gs_worksheet, $key_values );

			if ( is_wp_error( $response ) ) {
				$error_msg                           = implode( "\n", $response->get_error_messages() );
				$action_data['do-nothing']           = true;
				$action_data['complete_with_errors'] = true;
				Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

				return;
			}

			$body = json_decode( wp_remote_retrieve_body( $response ) );

			if ( isset( $body->error ) ) {
				$error_msg                           = $body->error->description;
				$action_data['do-nothing']           = true;
				$action_data['complete_with_errors'] = true;
				Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

				return;
			}

			Automator()->complete_action( $user_id, $action_data, $recipe_id );

			return;

		} catch ( \Exception $e ) {
			$error_msg = $e->getMessage();
			if ( $json = json_decode( $error_msg ) ) {
				if ( isset( $json->error ) && isset( $json->error->message ) ) {
					$error_msg = $json->error->message;
				}
			}
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

			return;
		}
	}