Newer
Older
<?php
/**
* Url controller class.
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Controller;
class Url extends Base {
/**
* The base route.
*
* @since 0.1
* @var string
*/
protected $rest_base = 'url';
/**
* 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' ],
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
'args' => $this->get_item_args()
],
'schema' => [ $this, 'get_item_schema' ]
] );
}
/**
* Get items.
*
* @since 0.1
* @param WP_REST_Request $request
*/
public function get_item( $request ) {
/**
* Filter formatted api params.
*
* @since 0.1
* @param array $params
* @param WP_REST_Request $request
*/
$params = apply_filters( 'civi_wp_rest/controller/url/params', $this->get_formatted_params( $request ), $request );
// track url
$url = \CRM_Mailing_Event_BAO_TrackableURLOpen::track( $params['queue_id'], $params['url_id'] );
/**
* Filter url.
*
* @param string $url
* @param array $params
* @param WP_REST_Request $request
*/
$url = apply_filters( 'civi_wp_rest/controller/url/before_parse_url', $url, $params, $request );
// parse url
$url = $this->parse_url( $url, $params );
$this->do_redirect( $url );
}
/**
* Get formatted api params.
*
* @since 0.1
* @param WP_REST_Resquest $request
* @return array $params
*/
protected function get_formatted_params( $request ) {
$args = $request->get_params();
$params = [
'queue_id' => isset( $args['qid'] ) ? $args['qid'] ?? '' : $args['q'] ?? '',
'url_id' => $args['u']
];
// unset unnecessary args
unset( $args['qid'], $args['u'], $args['q'] );
if ( ! empty( $args ) ) {
$params['query'] = http_build_query( $args );
}
return $params;
}
/**
* Parses the url.
*
* @since 0.1
* @param string $url
* @param array $params
* @return string $url
*/
protected function parse_url( $url, $params ) {
// CRM-18320 - Fix encoded ampersands
$url = str_replace( '&', '&', $url );
// CRM-7103 - Look for additional query variables and append them
if ( isset( $params['query'] ) && strpos( $url, '?' ) ) {
$url .= '&' . $params['query'];
} elseif ( isset( $params['query'] ) ) {
$url .= '?' . $params['query'];
}
if ( strpos( $url, 'mailto' ) ) $url = strstr( $url, 'mailto' );
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
return apply_filters( 'civi_wp_rest/controller/url/parsed_url', $url, $params );
}
/**
* Do redirect.
*
* @since 0.1
* @param string $url
*/
protected function do_redirect( $url ) {
wp_redirect( $url );
exit;
}
/**
* Item schema.
*
* @since 0.1
* @return array $schema
*/
public function get_item_schema() {
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'civicrm_api3/v3/url',
'description' => __( 'CiviCRM API3 wrapper', 'civicrm' ),
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'type' => 'object',
'required' => [ 'qid', 'u' ],
'properties' => [
'qid' => [
'type' => 'integer'
],
'q' => [
'type' => 'integer'
],
'u' => [
'type' => 'integer'
]
]
];
}
/**
* Item arguments.
*
* @since 0.1
* @return array $arguments
*/
public function get_item_args() {
return [
'qid' => [
'required' => false,
'validate_callback' => function( $value, $request, $key ) {
'required' => false,
'validate_callback' => function( $value, $request, $key ) {