PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.1
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.0.1, at includes/integrations/slack/class-integration-slack.php

110 lines 3.3 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 WPUF_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_filter( 'weforms_form_builder_integrations', array( $this, 'register_integration_settings' ) );
24 add_action( 'weforms_entry_submission', array( $this, 'send_notification' ), 10, 4 );
25 }
26
27 /**
28 * Subscribe a user when a form is submitted
29 *
30 * @param int $entry_id
31 * @param int $form_id
32 * @param int $page_id
33 * @param array $form_settings
34 *
35 * @return void
36 */
37 public function send_notification( $entry_id, $form_id, $page_id, $form_settings ) {
38
39 $integration = wpuf_is_integration_active( $form_id, $this->id );
40
41 if ( false === $integration ) {
42 return;
43 }
44
45 if ( empty( $integration->url ) ) {
46 return;
47 }
48
49 $attachment_fields = array();
50 $title = sprintf( __( "#%d New submission on %s", 'weforms' ), $entry_id, get_post_field( 'post_title', $form_id ) );
51 $form_data = weforms_get_entry_data( $entry_id );
52
53 if ( false === $form_data ) {
54 return;
55 }
56
57 // prepare the attachment data
58 foreach ( $form_data['fields'] as $meta_key => $value ) {
59 $is_short = true;
60 $_value = $form_data['data'][ $meta_key ];
61
62 if ( 'textarea' == $value['type'] ) {
63 $is_short = false;
64 $_value = html_entity_decode( $_value );
65
66 // replace paragrphs and breaks
67 $_value = str_replace( '<p>', '', $_value );
68 $_value = str_replace( '<br />', '', $_value );
69 $_value = str_replace( '</p>', "\n", $_value );
70 }
71
72 $attachment_fields[] = array(
73 'title' => $value['label'],
74 'value' => $_value,
75 'short' => $is_short
76 );
77 }
78
79 $data = array(
80 'payload' => json_encode( array(
81 'username' => 'weForms',
82 'icon_url' => '',
83 'attachments' => array(
84 array(
85 'fallback' => $title,
86 'color' => '#36a64f',
87 'title' => $title,
88 'title_link' => sprintf( '%s#/form/%d/entries/%d', admin_url( 'admin.php?page=weforms' ), $form_id, $entry_id ),
89 'fields' => $attachment_fields,
90 'footer' => 'weForms',
91 'ts' => current_time( 'timestamp' )
92 )
93 )
94 ) ) );
95
96 $posting_to_slack = wp_remote_post( $integration->url, array(
97 'method' => 'POST',
98 'timeout' => 30,
99 'redirection' => 5,
100 'httpversion' => '1.0',
101 'blocking' => false,
102 'headers' => array(),
103 'body' => $data,
104 'cookies' => array()
105 ) );
106
107 }
108
109 }
110