Newer
Older
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Open controller class.
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Controller;
class Open extends Base {
/**
* The base route.
*
* @since 0.1
* @var string
*/
protected $rest_base = 'open';
/**
* Registers routes.
*
* @since 0.1
*/
public function register_routes() {
register_rest_route( $this->get_namespace(), $this->get_rest_base(), [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_item' ],
'args' => $this->get_item_args()
],
'schema' => [ $this, 'get_item_schema' ]
] );
}
/**
* Get item.
*
* @since 0.1
* @param WP_REST_Request $request
*/
public function get_item( $request ) {
$queue_id = $request->get_param( 'q' );
// track open
\CRM_Mailing_Event_BAO_Opened::open( $queue_id );
// serve tracker file
add_filter( 'rest_pre_serve_request', [ $this, 'serve_tracker_file' ], 10, 4 );
}
/**
* Serves the tracker gif file.
*
* @since 0.1
* @param bool $served Whether the request has been served
* @param WP_REST_Response $result
* @param WP_REST_Request $request
* @param WP_REST_Server $server
* @return bool $served Whether the request has been served
*/
public function serve_tracker_file( $served, $result, $request, $server ) {
// tracker file path
$file = CIVICRM_PLUGIN_DIR . 'civicrm/i/tracker.gif';
// set headers
$server->send_header( 'Content-type', 'image/gif' );
$server->send_header( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0' );
$server->send_header( 'Content-Description', 'File Transfer' );
$server->send_header( 'Content-Disposition', 'inline; filename=tracker.gif' );
$server->send_header( 'Content-Length', filesize( $file ) );
$buffer = readfile( $file );
return true;
}
/**
* Item schema.
*
* @since 0.1
* @return array $schema
*/
public function get_item_schema() {
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'civicrm/v3/open',
'description' => 'CiviCRM Open endpoint',
'type' => 'object',
'required' => [ 'q' ],
'properties' => [
'q' => [
'type' => 'integer'
]
]
];
}
/**
* Item arguments.
*
* @since 0.1
* @return array $arguments
*/
public function get_item_args() {
return [
'q' => [
'type' => 'integer',
'required' => true,
'validate_callback' => function( $value, $request, $key ) {
return is_numeric( $value );
}
]
];
}
}