Newer
Older
<?php
/**
* Url controller class.
*
*/
namespace CiviCRM_WP_REST\Controller;
class Url extends Base {
*/
protected $rest_base = 'url';
/**
* Registers routes.
*
*/
public function register_routes() {
register_rest_route($this->get_namespace(), $this->get_rest_base(), [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'get_item'],
'permission_callback' => '__return_true',
'args' => $this->get_item_args(),
],
'schema' => [$this, 'get_item_schema'],
]);
}
/**
* Get items.
*
* @param WP_REST_Request $request
*/
public function get_item($request) {
/**
* Filter formatted API params.
*
* @since 5.25
*
* @param array $params
* @param WP_REST_Request $request
*/
$params = apply_filters('civi_wp_rest/controller/url/params', $this->get_formatted_params($request), $request);
$url = \CRM_Mailing_Event_BAO_MailingEventTrackableURLOpen::track($params['queue_id'], $params['url_id']);
*
* @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 5.25
*
* @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;
}
/**
*
* @param string $url
* @param array $params
* @return string $url
*/
protected function parse_url($url, $params) {
// 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');
}
return apply_filters('civi_wp_rest/controller/url/parsed_url', $url, $params);
}
/**
* Do redirect.
*
* @param string $url
*/
protected function do_redirect($url) {
wp_redirect($url);
exit;
}
/**
* Item schema.
*
158
159
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
* @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'),
'type' => 'object',
'required' => ['qid', 'u'],
'properties' => [
'qid' => [
'type' => 'integer',
],
'q' => [
'type' => 'integer',
],
'u' => [
'type' => 'integer',
],
],
];
}
/**
* Item arguments.
*
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
* @return array $arguments
*/
public function get_item_args() {
return [
'qid' => [
'type' => ['integer', 'string'],
'required' => FALSE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value) || empty($value);
},
],
'q' => [
'type' => ['integer', 'string'],
'required' => FALSE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value) || empty($value);
},
],
'u' => [
'type' => 'integer',
'required' => TRUE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value);
},
],
];
}