From 5a4315befac4cc8507bccf9757eade8c8b51b155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 2 Aug 2021 15:21:40 +0200 Subject: [PATCH] use wordpress http api --- src/init.php | 86 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/src/init.php b/src/init.php index 2c28fa1..7bac3ef 100644 --- a/src/init.php +++ b/src/init.php @@ -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,48 +111,52 @@ 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 ''; return ob_get_clean();