Skip to content

Commit d3d58c7

Browse files
committed
Events: Show upcoming events close to the visitor
1 parent 05a5209 commit d3d58c7

File tree

10 files changed

+332
-15
lines changed

10 files changed

+332
-15
lines changed
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 7 additions & 0 deletions
Loading

public_html/wp-content/themes/wporg-events-2023/patterns/events-list-filters.php renamed to public_html/wp-content/themes/wporg-events-2023/patterns/event-list-filters-with-nearby.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Title: Events List Filters
4-
* Slug: wporg-events-2023/event-list-filters
3+
* Title: Event List Filters
4+
* Slug: wporg-events-2023/event-list-filters-with-nearby
55
* Inserter: no
66
*/
77

@@ -14,6 +14,18 @@
1414
<!-- wp:search {"showLabel":false,"placeholder":"Search events...","width":100,"widthUnit":"%","buttonText":"Search","buttonPosition":"button-inside","buttonUseIcon":true,"className":"is-style-secondary-search-control"} /-->
1515
</div> <!-- /wp:group -->
1616

17+
<!-- wp:html {"className":"wporg-events__nearby-chips"} -->
18+
<div class="wp-block-buttons wporg-events__nearby-chips">
19+
<button id="wporg-events__see-global">
20+
Showing events near me
21+
</button>
22+
23+
<button id="wporg-events__see-nearby" class="wporg-events__hidden">
24+
Showing global events
25+
</button>
26+
</div>
27+
<!-- /wp:html -->
28+
1729
<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"className":"wporg-query-filters","layout":{"type":"flex","flexWrap":"nowrap"}} -->
1830
<div class="wp-block-group wporg-query-filters">
1931
<!-- wp:wporg/query-filter {"key":"format_type","multiple":false} /-->

public_html/wp-content/themes/wporg-events-2023/patterns/front-events.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
<h2 class="wp-block-heading has-inter-font-family has-medium-font-size" style="font-style:normal;font-weight:700">Upcoming events</h2>
1414
<!-- /wp:heading -->
1515

16-
<!-- wp:pattern {"slug":"wporg-events-2023/event-list-filters"} /-->
16+
<!-- wp:pattern {"slug":"wporg-events-2023/event-list-filters-with-nearby"} /-->
1717

18-
<!-- wp:wporg/event-list {"limit": "10"} /-->
18+
<!-- wp:wporg/event-list {"events":"nearby","id":"front-event-list-nearby","limit": "10"} /-->
19+
<!-- wp:wporg/event-list {"events":"all-upcoming","id":"front-event-list-global","className":"wporg-events__hidden","limit": "10"} /-->
1920

2021
<!-- wp:buttons {"style":{"spacing":{"margin":{"top":"var:preset|spacing|30"}}}} -->
2122
<div class="wp-block-buttons" style="margin-top:var(--wp--preset--spacing--30)"><!-- wp:button {"className":"is-style-outline"} -->

public_html/wp-content/themes/wporg-events-2023/postcss/base/layout.pcss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ body {
1010
max-width: var(--wp--custom--layout--wide-size) !important;
1111
}
1212
}
13+
14+
.wporg-events__hidden {
15+
display: none;
16+
}
17+
18+
.wporg-marker-list__loading {
19+
/* The final height could be anywhere between 50px and 750px, so split the difference in order to minimize the
20+
* amount of layout shift. */
21+
height: 350px;
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.wporg-events__nearby-chips {
2+
button::before {
3+
content: "";
4+
width: 24px;
5+
height: 24px;
6+
display: inline-block;
7+
}
8+
9+
#wporg-events__see-global::before {
10+
background-image: url( 'images/close.svg' );
11+
}
12+
13+
#wporg-events__see-nearby::before {
14+
background-image: url( 'images/location.svg' );
15+
}
16+
}

public_html/wp-content/themes/wporg-events-2023/postcss/style.pcss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
@import "base/layout.pcss";
1212
@import "footer/community-callout.pcss";
1313

14+
/* Patterns */
15+
@import "patterns/event-list-filters-with-nearby.pcss";
16+
1417
/* Pages */
1518
@import "page/misc.pcss";
1619
@import "page/front-page/cover.pcss";

public_html/wp-content/themes/wporg-events-2023/src/event-list/block.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"supports": {
2626
"align": true,
27+
"anchor": true,
2728
"color": {
2829
"background": true,
2930
"text": true
@@ -38,5 +39,6 @@
3839
"lineHeight": true
3940
}
4041
},
41-
"editorScript": "file:./index.js"
42+
"editorScript": "file:./index.js",
43+
"viewScript" : "file:./view.js"
4244
}

public_html/wp-content/themes/wporg-events-2023/src/event-list/index.php

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
use WordPressdotorg\Events_2023;
1111
use WP_Block;
1212
use WordPressdotorg\MU_Plugins\Google_Map;
13+
use WP_Community_Events;
1314

1415
add_action( 'init', __NAMESPACE__ . '\init' );
15-
16+
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\enqueue_assets' );
1617

1718
/**
1819
* Registers the block using the metadata loaded from the `block.json` file.
@@ -30,6 +31,28 @@ function init() {
3031
);
3132
}
3233

34+
/**
35+
* Enqueue scripts and styles.
36+
*/
37+
function enqueue_assets() {
38+
require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
39+
40+
$payload = array(
41+
'ip' => WP_Community_Events::get_unsafe_client_ip(),
42+
'number' => 10,
43+
);
44+
45+
if ( is_user_logged_in() ) {
46+
$payload['locale'] = get_user_locale( get_current_user_id() );
47+
}
48+
49+
wp_add_inline_script(
50+
'wporg-event-list-view-script',
51+
'localEventsPayload = ' . wp_json_encode( $payload ) . ';',
52+
'before'
53+
);
54+
}
55+
3356
/**
3457
* Render the block content.
3558
*
@@ -40,11 +63,30 @@ function init() {
4063
* @return string Returns the block markup.
4164
*/
4265
function render( $attributes, $content, $block ) {
66+
if ( 'nearby' === $attributes['events'] ) {
67+
$content = get_nearby_events_markup();
68+
} else {
69+
$content = get_global_events_markup( $attributes['events'], $attributes['limit'], $attributes['groupByMonth'] );
70+
}
71+
72+
$wrapper_attributes = get_block_wrapper_attributes( array( 'id' => $attributes['id'] ?? '' ) );
73+
74+
return sprintf(
75+
'<div %1$s>%2$s</div>',
76+
$wrapper_attributes,
77+
do_blocks( $content )
78+
);
79+
}
80+
81+
/**
82+
* Get markup for a list of global events.
83+
*/
84+
function get_global_events_markup( string $filter, int $limit, bool $group_by_month ): string {
4385
$facets = Events_2023\get_query_var_facets();
44-
$events = Google_Map\get_events( $attributes['events'], 0, 0, $facets );
86+
$events = Google_Map\get_events( $filter, 0, 0, $facets );
4587

4688
// Get all the filters that are currently applied.
47-
$filtered_events = array_slice( filter_events( $events ), 0, (int) $attributes['limit'] );
89+
$filtered_events = array_slice( filter_events( $events ), 0, $limit );
4890

4991
// The results are not guaranteed to be in order, so sort them.
5092
usort( $filtered_events,
@@ -57,7 +99,7 @@ function ( $a, $b ) {
5799
return get_no_result_view();
58100
}
59101

60-
if ( (bool) $attributes['groupByMonth'] ) {
102+
if ( $group_by_month ) {
61103
// Group events by month year.
62104
$grouped_events = array();
63105
foreach ( $filtered_events as $event ) {
@@ -74,12 +116,46 @@ function ( $a, $b ) {
74116
$content = get_list_markup( $filtered_events );
75117
}
76118

77-
$wrapper_attributes = get_block_wrapper_attributes();
78-
return sprintf(
79-
'<div %1$s>%2$s</div>',
80-
$wrapper_attributes,
81-
do_blocks( $content )
82-
);
119+
return $content;
120+
}
121+
122+
/**
123+
* Get markup for a list of nearby events.
124+
*
125+
* The events themselves are populated by an XHR, to avoid blocking the TTFB with an external HTTP request. See `view.js`.
126+
*/
127+
function get_nearby_events_markup(): string {
128+
ob_start();
129+
130+
?>
131+
132+
<p class="wporg-marker-list__loading">
133+
Loading nearby events...
134+
<img
135+
src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>"
136+
width="20"
137+
height="20"
138+
alt=""
139+
/>
140+
</p>
141+
142+
<?php
143+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The called function escapes it.
144+
echo get_list_markup( array() );
145+
?>
146+
147+
<!-- wp:wporg/notice {"type":"warning","className":"wporg-marker-list__no-results wporg-events__hidden"} -->
148+
<div class="wp-block-wporg-notice is-warning-notice wporg-marker-list__no-results wporg-events__hidden">
149+
<p>
150+
There are no events scheduled near you at the moment. You can <a href="<?php echo esc_url( home_url( 'upcoming-events/' ) ); ?>">browse global events</a>, or
151+
<a href="https://make.wordpress.org/community/handbook/meetup-organizer/welcome/" class="external-link">learn how to organize an event in your area</a>.
152+
</p>
153+
</div>
154+
<!-- /wp:wporg/notice -->
155+
156+
<?php
157+
158+
return ob_get_clean();
83159
}
84160

85161
/**

0 commit comments

Comments
 (0)