'; } public function description() { return __( 'Send entries to any URL as JSON or form data, with custom headers and a signature.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'Add one feed per URL on the form\'s Integrations tab. With a signing secret, each request carries the header X-FWREN-Signature: sha256=HMAC-SHA256(raw body, secret) and X-FWREN-Timestamp.', 'fieldwren' ) . '

'; } /** @return string[]|null Feed field keys a preset shows (null = all). */ protected function preset_keys() { return null; } /** @return array Settings a preset forces. */ protected function forced() { return array(); } /** @return string Placeholder of the URL. */ protected function url_placeholder() { return 'https://example.com/hook'; } public function feed_fields( $form_id ) { $fields = array( array( 'key' => 'url', 'type' => 'text', 'url' => true, 'label' => __( 'Request URL', 'fieldwren' ), 'required' => true, 'placeholder' => $this->url_placeholder(), 'desc' => __( 'Smartcodes are allowed, e.g. ?email={inputs.email}', 'fieldwren' ) ), array( 'key' => 'method', 'type' => 'select', 'label' => __( 'Method', 'fieldwren' ), 'default' => 'POST', 'no_empty' => true, 'options' => array( 'POST' => 'POST', 'GET' => 'GET', 'PUT' => 'PUT', 'PATCH' => 'PATCH', 'DELETE' => 'DELETE' ) ), array( 'key' => 'format', 'type' => 'select', 'label' => __( 'Body format', 'fieldwren' ), 'default' => 'json', 'no_empty' => true, 'options' => array( 'json' => 'JSON', 'form' => __( 'Form-encoded', 'fieldwren' ) ), 'show_if' => array( 'method' => array( 'POST', 'PUT', 'PATCH', 'DELETE' ) ) ), array( 'key' => 'body', 'type' => 'select', 'label' => __( 'Data to send', 'fieldwren' ), 'default' => 'all', 'no_empty' => true, 'options' => array( 'all' => __( 'All fields (field key → value)', 'fieldwren' ), 'labels' => __( 'All fields (label → value)', 'fieldwren' ), 'raw' => __( 'All fields, raw values (arrays kept)', 'fieldwren' ), 'selected' => __( 'Selected fields', 'fieldwren' ), 'custom' => __( 'Custom keys', 'fieldwren' ), ), ), array( 'key' => 'fields', 'type' => 'fields', 'label' => __( 'Fields', 'fieldwren' ), 'show_if' => array( 'body' => 'selected' ) ), array( 'key' => 'custom', 'type' => 'kv', 'label' => __( 'Custom keys', 'fieldwren' ), 'key_label' => __( 'Key', 'fieldwren' ), 'value_label' => __( 'Value', 'fieldwren' ), 'show_if' => array( 'body' => 'custom' ), 'desc' => __( 'Use dots for nesting, e.g. contact.email.', 'fieldwren' ) ), array( 'key' => 'meta', 'type' => 'toggle', 'label' => __( 'Entry details', 'fieldwren' ), 'text' => __( 'Add form and entry details (form id/title, entry id, date, page URL) under "_meta"', 'fieldwren' ), 'default' => true ), array( 'key' => 'headers', 'type' => 'kv', 'secret' => true, 'label' => __( 'Headers', 'fieldwren' ), 'key_label' => __( 'Header', 'fieldwren' ), 'value_label' => __( 'Value', 'fieldwren' ), 'desc' => __( 'Values are stored with the form and hidden here once saved.', 'fieldwren' ) ), array( 'key' => 'secret', 'type' => 'secret', 'label' => __( 'Signing secret', 'fieldwren' ), 'desc' => __( 'Optional. Adds X-FWREN-Signature: sha256=… (HMAC-SHA256 of the raw body) and X-FWREN-Timestamp.', 'fieldwren' ) ), ); $keys = $this->preset_keys(); if ( null === $keys ) { return $fields; } $out = array(); foreach ( $fields as $f ) { if ( in_array( $f['key'], $keys, true ) ) { $out[] = $f; } } return $out; } /** @return array Payload of the running entry. */ protected function payload( array $s ) { switch ( $s['body'] ?? 'all' ) { case 'labels': $data = array(); foreach ( $this->data_rows() as $r ) { $data[ $r[0] ] = $r[1]; } break; case 'raw': $data = (array) ( $this->ctx['values'] ?? array() ); foreach ( \FWREN\Schema::fields( $this->ctx['form_id'] ) as $k => $f ) { if ( ! empty( $f['sensitive'] ) || ! empty( $f['is_upload'] ) || ! empty( $f['signature'] ) ) { unset( $data[ $k ] ); } } foreach ( $this->data_rows() as $k => $r ) { if ( ! isset( $data[ $k ] ) ) { $data[ $k ] = $r[1]; } } break; case 'selected': $data = array(); foreach ( $this->data_rows( (array) ( $s['fields'] ?? array() ) ) as $k => $r ) { $data[ $k ] = $r[1]; } break; case 'custom': $data = array(); foreach ( $this->kv( 'custom' ) as $k => $v ) { $ref = &$data; $parts = explode( '.', $k ); foreach ( $parts as $i => $p ) { if ( $i === count( $parts ) - 1 ) { $ref[ $p ] = $v; } else { if ( ! isset( $ref[ $p ] ) || ! is_array( $ref[ $p ] ) ) { $ref[ $p ] = array(); } $ref = &$ref[ $p ]; } } unset( $ref ); } break; default: $data = array(); foreach ( $this->data_rows() as $k => $r ) { $data[ $k ] = $r[1]; } } if ( ! empty( $s['meta'] ) ) { $data['_meta'] = $this->entry_meta(); } return apply_filters( 'fwren/webhook/payload', $data, $this->ctx['feed'], $this->ctx['entry'], $this->ctx['values'] ); } public function process( array $feed, array $entry, array $values ) { $s = array_merge( $feed['settings'], $this->forced() ); $url = $this->val( $s['url'] ?? '' ); if ( ! wp_http_validate_url( $url ) && ! preg_match( '#^https?://#i', $url ) ) { return new \WP_Error( 'fwren_bad_url', __( 'The webhook URL is missing or invalid.', 'fieldwren' ) ); } $method = strtoupper( (string) ( $s['method'] ?? 'POST' ) ); $data = $this->payload( $s ); $headers = array(); foreach ( $this->kv( 'headers' ) as $h => $v ) { $h = preg_replace( '/[^A-Za-z0-9\-_]/', '', $h ); if ( $h ) { $headers[ $h ] = str_replace( array( "\r", "\n" ), '', $v ); $this->secrets[] = $headers[ $h ]; } } $body = null; if ( 'GET' === $method ) { $url = add_query_arg( urlencode_deep( self::flatten_query( $data ) ), $url ); $raw = (string) wp_parse_url( $url, PHP_URL_QUERY ); } elseif ( 'form' === ( $s['format'] ?? 'json' ) ) { $body = http_build_query( $data, '', '&' ); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; $raw = $body; } else { $body = wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $headers['Content-Type'] = 'application/json'; $raw = $body; } $secret = (string) ( $feed['settings']['secret'] ?? '' ); if ( '' !== $secret ) { $this->secrets[] = $secret; $headers['X-FWREN-Timestamp'] = (string) time(); $headers['X-FWREN-Signature'] = 'sha256=' . hash_hmac( 'sha256', $raw, $secret ); } $res = $this->http( $method, $url, array( 'headers' => $headers, 'body' => $body ) ); if ( is_wp_error( $res ) ) { return $res; } /* translators: %d status code */ return sprintf( __( 'Delivered (HTTP %d).', 'fieldwren' ), $res['code'] ); } /** @return array Nested arrays flattened for a query string (a[b]=c handled by add_query_arg). */ private static function flatten_query( array $data, $prefix = '' ) { $out = array(); foreach ( $data as $k => $v ) { $key = '' === $prefix ? (string) $k : $prefix . '[' . $k . ']'; if ( is_array( $v ) ) { $out = array_merge( $out, self::flatten_query( $v, $key ) ); } else { $out[ $key ] = is_bool( $v ) ? ( $v ? '1' : '0' ) : (string) $v; } } return $out; } }