Skip to content

Commit

Permalink
Events: Convert landing page patterns to a block (#1077)
Browse files Browse the repository at this point in the history
This prevents them from being loaded on pages where they aren't used.
  • Loading branch information
iandunn authored Oct 3, 2023
1 parent 923023f commit 7113107
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace WordPressdotorg\Events_2023;
defined( 'WPINC' ) || die();

require_once __DIR__ . '/event-getters.php';

add_action( 'init', __NAMESPACE__ . '\register_blocks' );


/**
* Register blocks.
*/
function register_blocks(): void {
register_block_type(
__DIR__ . '/build/block.json',
array(
'render_callback' => __NAMESPACE__ . '\render_events_landing_page',
)
);
}

/**
* Render the output of the Events Landing Page block.
*/
function render_events_landing_page( array $block_attributes, string $content ): string {
switch ( $block_attributes['events'] ) {
case 'all-upcoming':
$map_options = array(
'id' => 'all-upcoming-events',
'markers' => get_all_upcoming_events(),
);
break;

case 'city':
// Can't use `$wp->request` because Gutenberg calls this on `init`, before `parse_request`.
// @todo That was true when this was part of a pattern, but it isn't now that this is a block. We can
// maybe pass `$wp->request` to `normalize_request_uri()` now, or maybe remove that function entirely.
$request_uri = normalize_request_uri( $_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING'] );

$map_options = array(
'id' => 'city-landing-page',
'markers' => get_city_landing_page_events( $request_uri ),
);
break;

default:
return 'No events available';
}

return do_blocks( '<!-- wp:wporg/google-map '. wp_json_encode( $map_options ) .' /-->' );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "events-landing-page",
"version": "0.0.1",
"description": "Gather events for the different landing pages, and pass them to the Google Map block to be displayed.",
"main": "index.js",
"scripts": {
"start": "wp-scripts start",
"build": "wp-scripts build"
},
"devDependencies": {
"@wordpress/scripts": "^26.13.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wporg/events-landing-page",
"title": "Events Landing Page",
"icon": "admin-site-alt",
"category": "widgets",
"description": "Gather events for the different landing pages, and pass them to the Google Map block to be displayed.",
"textdomain": "wporg",
"attributes": {
"events": {
"type": "string",
"default": ""
}
},
"editorScript": "file:./index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { Placeholder } from '@wordpress/components';

/**
* Internal dependencies
*/
import metadata from './block.json';

function Edit() {
return (
<Placeholder
instructions={ __(
'This is a placeholder for the editor. Data is supplied to this block via the template that includes it.',
'wordcamporg'
) }
label={ __( 'Events Landing Page', 'wordcamporg' ) }
/>
);
}

registerBlockType( metadata.name, {
edit: Edit,
} );
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

defined( 'WPINC' ) || die();

require_once __DIR__ . '/inc/event-getters.php';
require_once __DIR__ . '/blocks/events-landing-page/events-landing-page.php';

add_filter( 'parse_request', __NAMESPACE__ . '\add_city_landing_page_query_vars' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<!-- wp:group {"tagName":"main","className":"entry-content"} -->
<main class="wp-block-group entry-content">
<!-- wp:pattern {"slug":"wporg-events-2023/front-page-map"} /-->
<!-- wp:wporg/events-landing-page {"events":"all-upcoming"} /-->
</main>
<!-- /wp:group -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<!-- wp:group {"tagName":"main","className":"entry-content"} -->
<main class="wp-block-group entry-content">
<!-- wp:pattern {"slug":"wporg-events-2023/city-landing-page-map"} /-->
<!-- wp:wporg/events-landing-page {"events":"city"} /-->
</main>
<!-- /wp:group -->

Expand Down

0 comments on commit 7113107

Please sign in to comment.