PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.8
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.8
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / integrations / slack / class-integration-slack.php

class-integration-slack.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.8, at includes/integrations/slack/class-integration-slack.php

109 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Slack Integrations
5 *
6 * @package weForms\Integrations
7 */
8 class WeForms_Integration_Slack extends WeForms_Abstract_Integration {
9
10 /**
11 * Initialize the plugin
12 */
13 function __construct() {
14 $this->id = 'slack';
15 $this->title = __( 'Slack', 'weforms' );
16 $this->icon = WEFORMS_ASSET_URI . '/images/icon-slack.png';
17
18 $this->settings_fields = array(
19 'enabled' => false,
20 'url' => '',
21 );
22
23 add_action( 'weforms_entry_submission', array( $this, 'send_notification' ), 10, 4 );
24 }
25
26 /**
27 * Subscribe a user when a form is submitted
28 *
29 * @param int $entry_id
30 * @param int $form_id
31 * @param int $page_id
32 * @param array $form_settings
33 *
34 * @return void
35 */
36 public function send_notification( $entry_id, $form_id, $page_id, $form_settings ) {
37
38 $integration = weforms_is_integration_active( $form_id, $this->id );
39
40 if ( false === $integration ) {
41 return;
42 }
43
44 if ( empty( $integration->url ) ) {
45 return;
46 }
47
48 $attachment_fields = array();
49 $title = sprintf( __( "#%d New submission on %s", 'weforms' ), $entry_id, get_post_field( 'post_title', $form_id ) );
50 $form_data = weforms_get_entry_data( $entry_id );
51
52 if ( false === $form_data ) {
53 return;
54 }
55
56 // prepare the attachment data
57 foreach ( $form_data['fields'] as $meta_key => $value ) {
58 $is_short = true;
59 $_value = $form_data['data'][ $meta_key ];
60
61 if ( 'textarea' == $value['type'] ) {
62 $is_short = false;
63 $_value = html_entity_decode( $_value );
64
65 // replace paragrphs and breaks
66 $_value = str_replace( '<p>', '', $_value );
67 $_value = str_replace( '<br />', '', $_value );
68 $_value = str_replace( '</p>', "\n", $_value );
69 }
70
71 $attachment_fields[] = array(
72 'title' => $value['label'],
73 'value' => $_value,
74 'short' => $is_short
75 );
76 }
77
78 $data = array(
79 'payload' => json_encode( array(
80 'username' => 'weForms',
81 'icon_url' => '',
82 'attachments' => array(
83 array(
84 'fallback' => $title,
85 'color' => '#36a64f',
86 'title' => $title,
87 'title_link' => sprintf( '%s#/form/%d/entries/%d', admin_url( 'admin.php?page=weforms' ), $form_id, $entry_id ),
88 'fields' => $attachment_fields,
89 'footer' => 'weForms',
90 'ts' => current_time( 'timestamp' )
91 )
92 )
93 ) ) );
94
95 $posting_to_slack = wp_remote_post( $integration->url, array(
96 'method' => 'POST',
97 'timeout' => 30,
98 'redirection' => 5,
99 'httpversion' => '1.0',
100 'blocking' => false,
101 'headers' => array(),
102 'body' => $data,
103 'cookies' => array()
104 ) );
105
106 }
107
108 }
109