2019-04-27 18:36:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
class Squat_Radar_Connector {
|
|
|
|
|
|
|
|
const BASE_URL = 'https://radar.squat.net';
|
|
|
|
const API_EVENTS = '/api/1.2/search/events.json';
|
|
|
|
|
|
|
|
function get_events( $query ) {
|
|
|
|
|
|
|
|
$url = self::BASE_URL . self::API_EVENTS . '?' . build_query( $query );
|
|
|
|
$response = wp_remote_get( $url );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
throw new Squat_Radar_Connector_Exception( $response->get_error_message() );
|
|
|
|
}
|
|
|
|
$code = wp_remote_retrieve_response_code( $response );
|
|
|
|
if ( $code != 200) {
|
|
|
|
throw new Squat_Radar_Connector_Exception( wp_remote_retrieve_body( $response ), $code );
|
|
|
|
}
|
|
|
|
return json_decode( wp_remote_retrieve_body( $response ), true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode_search_url( $url ) {
|
|
|
|
$matches = [];
|
|
|
|
$result = [];
|
|
|
|
// Urldecode not required here because of the regex match.
|
|
|
|
// Radar paramaters here are transcoded so will match.
|
|
|
|
if (preg_match('|//radar.squat.net/([a-z]{2})/events/([a-zA-Z0-9/]*)|', $url, $matches)) {
|
|
|
|
$result['language'] = $matches[1];
|
|
|
|
foreach (array_chunk(explode('/', $matches[2]), 2) as $key_value_pair) {
|
|
|
|
$result['facets'][$key_value_pair[0]] = $key_value_pair[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function encode_api_query( $facets = [], $fields = [], $language = '', $limit = 10 ) {
|
|
|
|
$query = [];
|
|
|
|
|
|
|
|
// Urlencode should do nothing here @see comment in decode_search_url.
|
|
|
|
// If someone has snuck something in it will however help.
|
|
|
|
foreach ( $facets as $key => $value ) {
|
|
|
|
$query[] = ['facets[' . urlencode($key) . '][]' => urlencode($value)];
|
|
|
|
}
|
|
|
|
if ( ! empty($fields) ) {
|
2019-04-29 20:07:19 +02:00
|
|
|
// {raw}urlencode is encoding : and , both of which are valid pchar.
|
|
|
|
$query['fields'] = preg_replace('/[^a-z_:,]/', '', implode(',', $fields));
|
2019-04-27 18:36:32 +02:00
|
|
|
}
|
|
|
|
if ( ! empty($language) ) {
|
|
|
|
$query['language'] = urlencode($language);
|
|
|
|
}
|
|
|
|
if ( ! empty($limit) ) {
|
|
|
|
$query['limit'] = urlencode($limit);
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
function events( $facets, $fields = [], $language = NULL, $limit = 10, $expiration = 10800, $reset = FALSE ) {
|
2019-04-29 20:07:19 +02:00
|
|
|
$fields = array_merge($fields, ['uuid', 'title', 'url', 'event_status']);
|
2019-04-27 18:36:32 +02:00
|
|
|
$transient_key = 'squat_radar_events_' . sha1(implode($facets) . implode($fields) . $language . $limit);
|
|
|
|
if (! $reset && $data = get_transient( $transient_key )) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
$query = $this->encode_api_query( $facets, $fields, $language, $limit );
|
|
|
|
$events = $this->get_events($query);
|
|
|
|
|
|
|
|
set_transient( $transient_key, $events, $expiration );
|
|
|
|
return $events;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Squat_Radar_Connector_Exception extends Exception { }
|