-
Notifications
You must be signed in to change notification settings - Fork 15
/
templates.class.php
59 lines (46 loc) · 1.67 KB
/
templates.class.php
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace WPSparkPost;
// If ABSPATH is defined, we assume WP is calling us.
// Otherwise, this could be an illicit direct request.
if (!defined('ABSPATH')) exit();
class SparkPostTemplates
{
public $endpoint;
public function __construct($mailer)
{
$this->mailer = $mailer;
$this->endpoint = apply_filters('sp_hostname', 'api') . '/api/v1/templates';
}
public function preview($id, $substitution_data)
{
$endpoint = apply_filters('sp_api_location', $this->endpoint);
$url = "{$endpoint}/{$id}/preview?draft=false";
$body = array(
'substitution_data' => $substitution_data
);
$data = array(
'method' => 'POST',
'timeout' => 15,
'headers' => $this->mailer->get_request_headers(),
'body' => json_encode($body)
);
$this->mailer->debug('Making template API request');
$this->mailer->debug(print_r($data, true));
$response = $this->mailer->request($url, $data);
$this->mailer->debug('Template API request completed');
$this->mailer->check_permission_error($response, 'Templates: Read/Write');
$body = json_decode($response['body']);
if (property_exists($body, 'errors')) {
$this->mailer->debug('Error in getting template data');
$this->mailer->error($body->errors);
return false;
}
if (property_exists($body, 'results')) {
return $body->results;
} else {
$this->mailer->debug('API response is unknown');
$this->mailer->error('Unknown response');
return false;
}
}
}