ZAPIER_SENDWEBHOOK::send_webhook( $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/zapier/actions/zapier-sendwebhook.php

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


		$key_values   = array();
		$headers      = array();
		$request_type = 'POST';
		if ( isset( $action_data['meta']['WEBHOOKURL'] ) ) {
			$webhook_url = Automator()->parse->text( $action_data['meta']['WEBHOOKURL'], $recipe_id, $user_id, $args );

			for ( $i = 1; $i <= ZAPIER_SENDWEBHOOK::$number_of_keys; $i ++ ) {
				$key                = Automator()->parse->text( $action_data['meta'][ 'KEY' . $i ], $recipe_id, $user_id, $args );
				$value              = Automator()->parse->text( $action_data['meta'][ 'VALUE' . $i ], $recipe_id, $user_id, $args );
				$key_values[ $key ] = $value;
			}

		} elseif ( isset( $action_data['meta']['WEBHOOK_URL'] ) ) {
			$webhook_url = Automator()->parse->text( $action_data['meta']['WEBHOOK_URL'], $recipe_id, $user_id, $args );

			$fields = json_decode( $action_data['meta']['WEBHOOK_FIELDS'], true );

			for ( $i = 0; $i < count( $fields ); $i ++ ) {
				$key                = Automator()->parse->text( $fields[ $i ]['KEY'], $recipe_id, $user_id, $args );
				$value              = Automator()->parse->text( $fields[ $i ]['VALUE'], $recipe_id, $user_id, $args );
				$key_values[ $key ] = $value;
			}

			if ( isset( $action_data['meta']['WEBHOOK_HEADERS'] ) ) {
				$header_meta = json_decode( $action_data['meta']['WEBHOOK_HEADERS'], true );
				if ( ! empty( $header_meta ) ) {
					for ( $i = 0; $i <= count( $header_meta ); $i ++ ) {
						$key = isset( $header_meta[ $i ]['NAME'] ) ? Automator()->parse->text( $header_meta[ $i ]['NAME'], $recipe_id, $user_id, $args ) : null;
						// remove colon if user added in NAME
						$key   = str_replace( ':', '', $key );
						$value = isset( $header_meta[ $i ]['VALUE'] ) ? Automator()->parse->text( $header_meta[ $i ]['VALUE'], $recipe_id, $user_id, $args ) : null;
						if ( ! is_null( $key ) && ! is_null( $value ) ) {
							$headers[ $key ] = $value;
						}
					}
				}
			}

			if ( 'POST' === (string) $action_data['meta']['ACTION_EVENT'] || 'CUSTOM' === (string) $action_data['meta']['ACTION_EVENT'] ) {
				$request_type = 'POST';
			} elseif ( 'GET' === (string) $action_data['meta']['ACTION_EVENT'] ) {
				$request_type = 'GET';
			} elseif ( 'PUT' === (string) $action_data['meta']['ACTION_EVENT'] ) {
				$request_type = 'PUT';
			}
		}

		if ( $key_values && ! is_null( $webhook_url ) ) {
			$args = [
				'method'   => $request_type,
				'body'     => $key_values,
				'timeout'  => '30',
				'blocking' => false,
			];

			if ( ! empty( $headers ) ) {
				$args['headers'] = $headers;
			}

			$response = wp_remote_request( $webhook_url, $args );

			if ( $response instanceof WP_Error ) {
				/* translators: 1. Webhook URL */
				$error_message = sprintf( esc_attr__( 'An error was found in the webhook (%1$s) response.', 'uncanny-automator' ), $webhook_url );
				Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

				return;
			}

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

	}