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; return $url;
} }
function block_render_callback($attributes, $content) { function block_render_callback($attributes, $content) {
//print_r($attributes); // Get maximum number of events to be returned
$base = $attributes['mobilizonBaseURL']; if (isset($attributes['mobilizonEventLimit']) && ($attributes['mobilizonEventLimit'] != 0)) {
if (isset($attributes['mobilizonEventLimit']) && ($limit != 0)) {
$limit = $attributes['mobilizonEventLimit']; $limit = $attributes['mobilizonEventLimit'];
$limit = "(limit: ${limit})"; $limit = "(limit: ${limit})";
} else { } else {
$limit = ""; $limit = "";
} }
// Get API-URL from Instance URL
$base = $attributes['mobilizonBaseURL'];
$url_array = array($base, "api"); $url_array = array($base, "api");
array_walk_recursive($url_array, 'stripTrailingSlash'); array_walk_recursive($url_array, 'stripTrailingSlash');
$endpoint = implode('/', $url_array); $endpoint = implode('/', $url_array);
$endpoint = addhttp($endpoint); $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'] != '' ) { if (isset($attributes['mobilizonGroupName']) && $attributes['mobilizonGroupName'] != '' ) {
$filter_by_group = True; $filter_by_group = True;
} else { } else {
$filter_by_group = False; $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) { if ($filter_by_group) {
$groupName = $attributes['mobilizonGroupName']; $groupName = $attributes['mobilizonGroupName'];
$query = "query { $query = "query {
@ -63,6 +69,7 @@ function block_render_callback($attributes, $content) {
organizedEvents ${limit} { organizedEvents ${limit} {
elements { elements {
id, id,
updatedAt,
title, title,
url, url,
beginsOn, beginsOn,
@ -83,6 +90,7 @@ function block_render_callback($attributes, $content) {
events ${limit} { events ${limit} {
elements { elements {
id, id,
updatedAt,
url, url,
title, title,
beginsOn, 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']; $headers = ['Content-Type: application/json', 'User-Agent: Minimal GraphQL client'];
$data = array ('query' => $query); $body = array ('query' => $query);
$data = http_build_query($data); $args = array(
'body' => $body,
$options = array( 'headers' => $headers,
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $data
)
); );
$context = stream_context_create($options); // Send HTTP-Query
$result = file_get_contents(sprintf($endpoint), false, $context); $response = wp_remote_post($endpoint, $args);
if ($result === FALSE) { /* Handle error */ }
ob_start(); // Check if the HTTP-Query was successful
$data = json_decode($result); if ( wp_remote_retrieve_response_code( $response ) != 200 ) { /* Handle error */ }
//print_r($data);
// 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) { if ($filter_by_group) {
$events = $data->data->group->organizedEvents->elements; $events = $body['data']['group']['organizedEvents']['elements'];
} else { } 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">'; echo '<ul class="wp-block-cgb-block-mobilizon">';
// Loop through each event
foreach ($events as $event) { foreach ($events as $event) {
print_r ($event);
echo '<li>'; echo '<li>';
echo '<time>'; 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 '</time>';
echo '<h2>'; echo '<h2>';
echo $event->title; echo $event['title'];
echo '</h2>'; echo '</h2>';
//print_r($attributes);
if ( !isset( $attributes['mobilizonShowHeaderImage'] ) ) { if ( !isset( $attributes['mobilizonShowHeaderImage'] ) ) {
if (isset($event->picture->url) ){ if ( isset( $event['picture']['url'] ) ){
$imageData = base64_encode(file_get_contents($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 '<img src="data:image/jpeg;base64,'.$imageData.'">';
} }
} }
}
echo '</li>'; echo '</li>';
} }
echo '</ul>'; echo '</ul>';