Wp_Helpers::sendtest_webhook()


Parameters Parameters

$_POST

(Required)


Source Source

File: src/integrations/wp/helpers/wp-helpers.php

	public function sendtest_webhook() {



		Automator()->utilities->ajax_auth_check( $_POST );

		$key_values   = array();
		$values       = (array) Automator()->uap_sanitize( $_POST['values'], 'mixed' );
		$request_type = 'POST';
		if ( isset( $values['WEBHOOKURL'] ) ) {
			$webhook_url = esc_url_raw( $values['WEBHOOKURL'] );

			if ( empty( $webhook_url ) ) {
				wp_send_json(
					array(
						'type'    => 'error',
						'message' => esc_attr__( 'Please enter a valid webhook URL.', 'uncanny-automator' ),
					)
				);
			}

			for ( $i = 1; $i <= WP_SENDWEBHOOK::$number_of_keys; $i ++ ) {
				$key                = sanitize_text_field( $values[ 'KEY' . $i ] );
				$value              = sanitize_text_field( $values[ 'VALUE' . $i ] );
				$key_values[ $key ] = $value;
			}
		} elseif ( isset( $values['WEBHOOK_URL'] ) ) {
			$webhook_url = esc_url_raw( $values['WEBHOOK_URL'] );

			if ( empty( $webhook_url ) ) {
				wp_send_json(
					array(
						'type'    => 'error',
						'message' => esc_attr__( 'Please enter a valid webhook URL.', 'uncanny-automator' ),
					)
				);
			}

			if ( ! isset( $values['WEBHOOK_FIELDS'] ) || empty( $values['WEBHOOK_FIELDS'] ) ) {
				wp_send_json(
					array(
						'type'    => 'error',
						'message' => esc_attr__( 'Please enter valid fields.', 'uncanny-automator' ),
					)
				);
			}

			$fields = $values['WEBHOOK_FIELDS'];

			$fields_count = count( $fields );

			for ( $i = 0; $i <= $fields_count; $i ++ ) {
				$key   = isset( $fields[ $i ]['KEY'] ) ? sanitize_text_field( $fields[ $i ]['KEY'] ) : null;
				$value = isset( $fields[ $i ]['VALUE'] ) ? sanitize_text_field( $fields[ $i ]['VALUE'] ) : null;
				if ( ! is_null( $key ) && ! is_null( $value ) ) {
					$key_values[ $key ] = $value;
				}
			}

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

		if ( $key_values && ! is_null( $webhook_url ) ) {

			$args = array(
				'method'   => $request_type,
				'body'     => $key_values,
				'timeout'  => '30',
				'blocking' => false,
			);

			$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 );
				wp_send_json(
					array(
						'type'    => 'error',
						'message' => $error_message,
					)
				);
			}

			/* translators: 1. Webhook URL */
			$success_message = sprintf( esc_attr__( 'Successfully sent data on %1$s.', 'uncanny-automator' ), $webhook_url );

			wp_send_json(
				array(
					'type'    => 'success',
					'message' => $success_message,
				)
			);
		}
	}