' . esc_html__( 'Create a Slack app → Incoming Webhooks → "Add New Webhook to Workspace", pick a channel and paste the URL. A feed can use its own webhook to post to another channel.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'webhook_url' => array( 'label' => __( 'Default webhook URL', 'fieldwren' ), 'type' => 'password', 'placeholder' => 'https://hooks.slack.com/services/…' ), ); } public function can_verify() { return true; } public function verify( array $creds ) { if ( empty( $creds['webhook_url'] ) ) { return new \WP_Error( 'fwren_missing', __( 'Add a webhook URL first.', 'fieldwren' ) ); } // A valid hook rejects an empty payload with "no_text" / "invalid_payload"; a bad one answers 403/404. $res = $this->http( 'POST', $creds['webhook_url'], array( 'body' => array( 'x' => '' ), 'ok' => array( 400 ) ) ); if ( is_wp_error( $res ) ) { return $res; } return ( 400 === $res['code'] && preg_match( '/no_text|invalid_payload|missing_text/', (string) $res['raw'] ) ) ? true : new \WP_Error( 'fwren_slack', substr( (string) $res['raw'], 0, 200 ) ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'webhook', 'type' => 'secret', 'url' => true, 'label' => __( 'Webhook URL', 'fieldwren' ), 'placeholder' => 'https://hooks.slack.com/services/…', 'desc' => __( 'Leave empty to use the default webhook from Settings → Integrations.', 'fieldwren' ) ) ), $this->message_fields(), array( array( 'key' => 'mention', 'type' => 'select', 'label' => __( 'Notify', 'fieldwren' ), 'default' => '', 'no_empty' => true, 'options' => array( '' => __( 'Nobody', 'fieldwren' ), 'here' => '@here', 'channel' => '@channel' ) ) ) ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $url = trim( (string) ( $s['webhook'] ?? '' ) ); $url = '' !== $url ? $url : $this->cred( 'webhook_url' ); if ( ! $url ) { return new \WP_Error( 'fwren_missing', __( 'No Slack webhook URL.', 'fieldwren' ) ); } $this->secrets[] = $url; $esc = static function ( $t ) { return str_replace( array( '&', '<', '>' ), array( '&', '<', '>' ), (string) $t ); }; $title = self::cut( $this->sval( 'title' ) ? $this->sval( 'title' ) : $this->default_title(), 150 ); $text = $this->sval( 'message' ); $mention = (string) ( $s['mention'] ?? '' ); $blocks = array( array( 'type' => 'header', 'text' => array( 'type' => 'plain_text', 'text' => $title, 'emoji' => true ) ) ); if ( $text || $mention ) { $blocks[] = array( 'type' => 'section', 'text' => array( 'type' => 'mrkdwn', 'text' => self::cut( trim( ( $mention ? ' ' : '' ) . $esc( $text ) ), 3000 ) ) ); } $fields = array(); foreach ( $this->message_rows() as $r ) { $fields[] = array( 'type' => 'mrkdwn', 'text' => self::cut( '*' . $esc( $r[0] ) . "*\n" . $esc( $r[1] ), 2000 ) ); } foreach ( array_slice( array_chunk( $fields, 10 ), 0, 40 ) as $chunk ) { $blocks[] = array( 'type' => 'section', 'fields' => $chunk ); } $meta = $this->entry_meta(); $blocks[] = array( 'type' => 'context', 'elements' => array( array( 'type' => 'mrkdwn', 'text' => $esc( $meta['form_title'] ) . ( $meta['entry_id'] ? ' · #' . $meta['entry_id'] : '' ) . ' · ' . wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ) ), ); if ( ! empty( $s['link'] ) && $meta['entry_url'] ) { $blocks[] = array( 'type' => 'actions', 'elements' => array( array( 'type' => 'button', 'text' => array( 'type' => 'plain_text', 'text' => __( 'View entry', 'fieldwren' ) ), 'url' => $meta['entry_url'] ) ) ); } $res = $this->http( 'POST', $url, array( 'body' => array( 'text' => $title, 'blocks' => $blocks ) ) ); return is_wp_error( $res ) ? $res : __( 'Posted to Slack.', 'fieldwren' ); } }