summaryrefslogtreecommitdiff
path: root/Postman/Extensions/Core/Notifications/PostmanSlackNotify.php
blob: 1da009af3ad0b265a0c3de2a56b8e5a2d4827ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

class PostmanSlackNotify implements Postman_Notify {

    public function send_message($message)
    {
        $options = PostmanNotifyOptions::getInstance();

        $api_url = $options->getSlackToken();

        $headers = array(
            'content-type' => 'application/json'
        );

        $body = array(
            'text' => $message
        );

        $args = array(
            'headers' => $headers,
            'body' => json_encode($body)
        );

        $result = wp_remote_post( $api_url, $args );

        if ( is_wp_error($result) ) {
            error_log( __CLASS__ . ': ' . $result->get_error_message() );
        }

        $code = wp_remote_retrieve_response_code( $result );
        $message = wp_remote_retrieve_response_message( $result );

        if ( $code != 200 && $message !== 'OK' ) {
            error_log( __CLASS__ . ': ' . $message );
        }
    }
}