summaryrefslogtreecommitdiff
path: root/Postman/Postman-Mail/PostmanContactForm7.php
blob: 8792b08e88d01f7e9069cd4a1ade7fc1c7410a6e (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
41
42
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
class Postsmtp_ContactForm7 {

    private $result_error;

    public function __construct() {
        add_action( 'wpcf7_mail_failed', array( $this, 'save_error' ) );
        add_filter( 'wpcf7_ajax_json_echo', array( $this, 'change_rest_response' ), 10, 2 );
    }

    public function save_error($contact_form) {
        $this->result_error = apply_filters( 'postman_wp_mail_result', null );
    }

    public function change_rest_response( $response ) {
        if ( array_key_exists('status', $response) && $response['status'] == 'mail_failed' ) {
            $message = $this->result_error ['exception']->getMessage();

            if ( ! $message || $message == '' ) {
                return $response;
            }

            $currentTransport = PostmanOptions::getInstance()->getTransportType();
            $result = json_decode($message);
            $is_json = (json_last_error() == JSON_ERROR_NONE);

            switch ($currentTransport) {
                case 'gmail_api':
                    $response['message'] = $is_json ? $result->error->message : $message;
                    break;
                default:
                    $response['message'] = $is_json ? json_encode(json_decode($message), JSON_PRETTY_PRINT) : $message;
            }
        }

        return $response;
    }

}