mirror of
https://0xacab.org/radar/radar-wp.git
synced 2025-04-22 07:46:31 +02:00
WIP New v2 plugin with widget and shortcode, and more WP friendly markup.
This commit is contained in:
parent
5fd32fb63b
commit
649f84ad8c
557 changed files with 648 additions and 98567 deletions
73
includes/squat-radar-connector.php
Normal file
73
includes/squat-radar-connector.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?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) ) {
|
||||
$query['fields'] = urlencode(implode(',', $fields));
|
||||
}
|
||||
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 ) {
|
||||
$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 { }
|
Loading…
Add table
Add a link
Reference in a new issue