$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 ''; 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' );