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