216 lines
5.9 KiB
PHP
216 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* Blocks Initializer
|
|
*
|
|
* Enqueue CSS/JS of all the blocks.
|
|
*
|
|
* @since 1.0.0
|
|
* @package CGB
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Enqueue Gutenberg block assets for both frontend + backend.
|
|
*
|
|
* Assets enqueued:
|
|
* 1. blocks.style.build.css - Frontend + Backend.
|
|
* 2. blocks.build.js - Backend.
|
|
* 3. blocks.editor.build.css - Backend.
|
|
*
|
|
* @uses {wp-blocks} for block type registration & related functions.
|
|
* @uses {wp-element} for WP Element abstraction — structure of blocks.
|
|
* @uses {wp-i18n} to internationalize the block's text.
|
|
* @uses {wp-editor} for WP editor styles.
|
|
* @since 1.0.0
|
|
*/
|
|
function stripTrailingSlash(&$component) {
|
|
$component = rtrim($component, '/');
|
|
}
|
|
function addhttp($url) {
|
|
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
|
|
$url = "https://" . $url;
|
|
}
|
|
return $url;
|
|
}
|
|
function block_render_callback($attributes, $content) {
|
|
//print_r($attributes);
|
|
$base = $attributes['mobilizonBaseURL'];
|
|
if (isset($attributes['mobilizonEventLimit']) && ($limit != 0)) {
|
|
$limit = $attributes['mobilizonEventLimit'];
|
|
$limit = "(limit: ${limit})";
|
|
} else {
|
|
$limit = "";
|
|
}
|
|
$url_array = array($base, "api");
|
|
array_walk_recursive($url_array, 'stripTrailingSlash');
|
|
$endpoint = implode('/', $url_array);
|
|
|
|
$endpoint = addhttp($endpoint);
|
|
|
|
if (isset($attributes['mobilizonGroupName']) && $attributes['mobilizonGroupName'] != '' ) {
|
|
$filter_by_group = True;
|
|
} else {
|
|
$filter_by_group = False;
|
|
}
|
|
if ($filter_by_group) {
|
|
$groupName = $attributes['mobilizonGroupName'];
|
|
$query = "query {
|
|
group (preferredUsername: \"${groupName}\") {
|
|
organizedEvents ${limit} {
|
|
elements {
|
|
id,
|
|
title,
|
|
url,
|
|
beginsOn,
|
|
endsOn,
|
|
physicalAddress {
|
|
description,
|
|
locality
|
|
}
|
|
},
|
|
total
|
|
}
|
|
}
|
|
}
|
|
";
|
|
}
|
|
else {
|
|
$query = "query {
|
|
events ${limit} {
|
|
elements {
|
|
id,
|
|
url,
|
|
title,
|
|
beginsOn,
|
|
endsOn,
|
|
status,
|
|
picture {
|
|
url
|
|
},
|
|
physicalAddress {
|
|
id,
|
|
description,
|
|
locality
|
|
}
|
|
},
|
|
total
|
|
}
|
|
}
|
|
";
|
|
}
|
|
|
|
|
|
$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
|
|
)
|
|
);
|
|
|
|
$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);
|
|
if ($filter_by_group) {
|
|
$events = $data->data->group->organizedEvents->elements;
|
|
} else {
|
|
$events = $data->data->events->elements;
|
|
}
|
|
|
|
echo '<ul class="wp-block-cgb-block-mobilizon">';
|
|
foreach ($events as $event) {
|
|
echo '<li>';
|
|
echo '<time>';
|
|
echo date_i18n( 'D, d. M. Y, G:i', strtotime($event->beginsOn) );
|
|
echo '</time>';
|
|
echo '<h2>';
|
|
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));
|
|
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';
|
|
}
|
|
}
|
|
echo '</li>';
|
|
}
|
|
echo '</ul>';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
function mobilizon_cgb_block_assets() { // phpcs:ignore
|
|
// Register block styles for both frontend + backend.
|
|
wp_register_style(
|
|
'mobilizon-cgb-style-css', // Handle.
|
|
plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ), // Block style CSS.
|
|
is_admin() ? array( 'wp-editor' ) : null, // Dependency to include the CSS after it.
|
|
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
|
|
);
|
|
|
|
// Register block editor script for backend.
|
|
wp_register_script(
|
|
'mobilizon-cgb-block-js', // Handle.
|
|
plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack.
|
|
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above.
|
|
null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time.
|
|
true // Enqueue the script in the footer.
|
|
);
|
|
|
|
// Register block editor styles for backend.
|
|
wp_register_style(
|
|
'mobilizon-cgb-block-editor-css', // Handle.
|
|
plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS.
|
|
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
|
|
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
|
|
);
|
|
|
|
// WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object.
|
|
wp_localize_script(
|
|
'mobilizon-cgb-block-js',
|
|
'cgbGlobal', // Array containing dynamic data for a JS Global.
|
|
[
|
|
'pluginDirPath' => plugin_dir_path( __DIR__ ),
|
|
'pluginDirUrl' => plugin_dir_url( __DIR__ ),
|
|
// Add more data here that you want to access from `cgbGlobal` object.
|
|
]
|
|
);
|
|
|
|
/**
|
|
* Register Gutenberg block on server-side.
|
|
*
|
|
* Register the block on server-side to ensure that the block
|
|
* scripts and styles for both frontend and backend are
|
|
* enqueued when the editor loads.
|
|
*
|
|
* @link https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type#enqueuing-block-scripts
|
|
* @since 1.16.0
|
|
*/
|
|
register_block_type(
|
|
'cgb/block-mobilizon', array(
|
|
// Enqueue blocks.style.build.css on both frontend & backend.
|
|
'style' => 'mobilizon-cgb-style-css',
|
|
// Enqueue blocks.build.js in the editor only.
|
|
'editor_script' => 'mobilizon-cgb-block-js',
|
|
// Enqueue blocks.editor.build.css in the editor only.
|
|
'editor_style' => 'mobilizon-cgb-block-editor-css',
|
|
'render_callback' => 'block_render_callback',
|
|
)
|
|
);
|
|
|
|
}
|
|
|
|
// Hook: Block assets.
|
|
add_action( 'init', 'mobilizon_cgb_block_assets' );
|