Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Automator_Send_Webhook::format_outgoing_data( $fields,  $data_type, bool $is_check_sample = false )

Format outgoing data in to appropriate data type


Parameters Parameters

$fields

(Required)

$data_type

(Required)

$is_check_sample

(Optional)

Default value: false


Top ↑

Return Return

(false|mixed|string)


Source Source

File: src/core/lib/webhooks/class-automator-send-webhook.php

	private function format_outgoing_data( $fields, $data_type, $is_check_sample = false ) {
		$original = $fields;
		switch ( $data_type ) {
			case 'json':
			case 'graph':
				if ( $is_check_sample ) {
					$fields = wp_json_encode( $fields, JSON_PRETTY_PRINT );
				} else {
					$fields = wp_json_encode( $fields );
				}
				break;
			case 'form-data':
				$fields = http_build_query( $fields );
				if ( $is_check_sample ) {
					$fields = html_entity_decode(
						str_replace(
							array(
								'%2F',
								'%7B',
								'%7D',
								'%3A',
								'&',
							),
							array(
								'/',
								'{',
								'}',
								':',
								"\n" . '&',
							),
							$fields
						)
					);
				}
				break;
			case 'plain':
				$fields = implode( apply_filters( 'automator_send_webhook_plain_text_separator', ',', $fields ), $fields );
				break;
			case 'binary':
				$fields = implode( apply_filters( 'automator_send_webhook_binary_separator', ',', $fields ), $fields );
				$fields = $this->string_to_binary_conversion( $fields );
				break;
			case 'html':
				$fields = $this->build_html_table( $fields, $is_check_sample );
				break;
			case 'xml':
				try {
					$xml_body_wrapper = apply_filters( 'automator_send_webhook_xml_body', '<body></body>', $fields );
					if ( empty( $xml_body_wrapper ) ) {
						$fields = __( 'XML body wrapper cannot be empty. Please use `automator_send_webhook_xml_body` filter to define a wrapper.', 'uncanny-automator' );
						break;
					}
					$xml_data = new SimpleXMLElement( $xml_body_wrapper );
					$this->array_to_xml( $fields, $xml_data );
					$fields = $xml_data->asXML();

					if ( $is_check_sample ) {
						$dom                     = new DOMDocument( '1.0' );
						$dom->preserveWhiteSpace = true; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
						$dom->formatOutput       = true; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
						$dom->loadXML( trim( $fields ) );
						$fields = $dom->saveXML();
					} else {
						$fields = str_replace( PHP_EOL, '', $fields );
					}
				} catch ( \Exception $e ) {
					$fields = $e->getMessage();
				}
				break;
			case 'x-www-form-urlencoded':
				if ( $is_check_sample ) {
					$fields = wp_json_encode( $fields, JSON_PRETTY_PRINT );
				} else {
					$fields = $original;
				}
				break;
		}

		return apply_filters( 'automator_send_webhook_data_format', $fields, $original, $data_type );
	}