use wordpress http api

This commit is contained in:
André Menrath 2021-08-02 15:21:40 +02:00
parent 1a3b254fe4
commit 5a4315befa
1 changed files with 49 additions and 37 deletions

View File

@ -37,25 +37,31 @@ function addhttp($url) {
return $url;
}
function block_render_callback($attributes, $content) {
//print_r($attributes);
$base = $attributes['mobilizonBaseURL'];
if (isset($attributes['mobilizonEventLimit']) && ($limit != 0)) {
// Get maximum number of events to be returned
if (isset($attributes['mobilizonEventLimit']) && ($attributes['mobilizonEventLimit'] != 0)) {
$limit = $attributes['mobilizonEventLimit'];
$limit = "(limit: ${limit})";
} else {
$limit = "";
}
// Get API-URL from Instance URL
$base = $attributes['mobilizonBaseURL'];
$url_array = array($base, "api");
array_walk_recursive($url_array, 'stripTrailingSlash');
$endpoint = implode('/', $url_array);
$endpoint = addhttp($endpoint);
// Get the user-setting, if we are getting the events of a group only, or not
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 {
@ -63,6 +69,7 @@ function block_render_callback($attributes, $content) {
organizedEvents ${limit} {
elements {
id,
updatedAt,
title,
url,
beginsOn,
@ -83,6 +90,7 @@ function block_render_callback($attributes, $content) {
events ${limit} {
elements {
id,
updatedAt,
url,
title,
beginsOn,
@ -103,47 +111,51 @@ function block_render_callback($attributes, $content) {
";
}
// Define default GraphQL headers
$headers = ['Content-Type: application/json', 'User-Agent: Minimal GraphQL client'];
$data = array ('query' => $query);
$data = http_build_query($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $data
)
$body = array ('query' => $query);
$args = array(
'body' => $body,
'headers' => $headers,
);
$context = stream_context_create($options);
$result = file_get_contents(sprintf($endpoint), false, $context);
if ($result === FALSE) { /* Handle error */ }
ob_start();
$data = json_decode($result);
//print_r($data);
// Send HTTP-Query
$response = wp_remote_post($endpoint, $args);
// Check if the HTTP-Query was successful
if ( wp_remote_retrieve_response_code( $response ) != 200 ) { /* Handle error */ }
// 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 = $data->data->group->organizedEvents->elements;
$events = $body['data']['group']['organizedEvents']['elements'];
} else {
$events = $data->data->events->elements;
$events = $body['data']['events']['elements'];
}
// Display the event-array in as html list
ob_start();
echo '<ul class="wp-block-cgb-block-mobilizon">';
// Loop through each event
foreach ($events as $event) {
print_r ($event);
echo '<li>';
echo '<time>';
echo date_i18n( 'D, d. M. Y, G:i', strtotime($event->beginsOn) );
echo date_i18n( 'D, d. M. Y, G:i', strtotime($event['beginsOn']) );
echo '</time>';
echo '<h2>';
echo $event->title;
echo $event['title'];
echo '</h2>';
//print_r($attributes);
if (!isset($attributes['mobilizonShowHeaderImage']) ) {
if (isset($event->picture->url) ){
$imageData = base64_encode(file_get_contents($event->picture->url));
if ( !isset( $attributes['mobilizonShowHeaderImage'] ) ) {
if ( isset( $event['picture']['url'] ) ){
$response = wp_remote_get( $event['picture']['url'] );
$http_code = wp_remote_retrieve_response_code( $response );
if ($http_code == 200) {
$imageData = base64_encode( wp_remote_retrieve_body( $response ) );
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';
}
}
}
echo '</li>';
}
echo '</ul>';