$query);
$args = array(
'body' => $body,
'headers' => $headers,
);
// Send HTTP-Query and return the response
return(wp_remote_post($endpoint, $args));
}
ob_start();
function block_render_callback($attributes, $content) {
// var_dump($attributes); // Debugging only
// Check if we are supposed to render anything at all
if (array_key_exists("mobilizonInputFieldIsValid", $attributes) && $attributes["mobilizonInputFieldIsValid"]) {
// Set the string which limits how many events are queriend at maximum
if (isset($attributes['mobilizonEventLimit']) && ($attributes['mobilizonEventLimit'] != 0)) {
$limit = $attributes['mobilizonEventLimit'];
$limit = "(limit: ${limit})";
} else {
$limit = "";
}
}
// Get if we are getting the events of a group only, or not
// This is changes the way we query the events, as well as we parse the reponse array
if (isset($attributes['mobilizonGroupName']) && $attributes['mobilizonGroupName'] != '' ) {
$filter_by_group = True;
} else {
$filter_by_group = False;
}
// Define query string
// The query quite differs, if we query only the events of a certain group
if ($filter_by_group) {
$groupName = $attributes['mobilizonGroupName'];
$query = "query {
group (preferredUsername: \"${groupName}\") {
organizedEvents ${limit} {
elements {
id,
updatedAt,
title,
url,
beginsOn,
endsOn,
physicalAddress {
description,
locality
}
},
total
}
}
}
";
}
else {
$query = "query {
events ${limit} {
elements {
id,
updatedAt,
url,
title,
beginsOn,
endsOn,
status,
picture {
url
},
physicalAddress {
id,
description,
locality
}
},
total
}
}
";
}
// Execute the event query to the mobilizon instance
$response = mobilizon_query($attributes['mobilizonBaseURL'], $query);
// Check if the HTTP-Query was successful, if not do nothing?
if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
return ob_get_clean();;
}
// Extract the events as an array from the query's response body
$body = json_decode(wp_remote_retrieve_body( $response ), true);
if ($filter_by_group) {
$events = $body['data']['group']['organizedEvents']['elements'];
} else {
$events = $body['data']['events']['elements'];
}
// Display the event-array in as html list
echo '
';
// Loop through each event
foreach ($events as $event) {
echo '