Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events: Use a showcase like approach for the events list/archive #1124

Merged
merged 30 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
44371c8
Try using a php block that queries.
StevenDufresne Dec 2, 2023
1a5adca
Example query filtering.
StevenDufresne Dec 2, 2023
a43b837
add block build script.
StevenDufresne Dec 3, 2023
d568afe
Add the patterns
StevenDufresne Dec 4, 2023
cf3a3f4
Hook in another filter.
StevenDufresne Dec 4, 2023
12b7c06
Rename template.
StevenDufresne Dec 4, 2023
29f8205
Some minor updates.
StevenDufresne Dec 4, 2023
c73ed57
Add the month filter
StevenDufresne Dec 4, 2023
9cc0b23
Add in Past Events template
iandunn Dec 4, 2023
5b413d4
update menu links
iandunn Dec 5, 2023
8404ff7
pass filter values to map block
iandunn Dec 5, 2023
5819e91
Add map block. Isn't yet connected to country list.
StevenDufresne Dec 5, 2023
e636d34
Update copy.
StevenDufresne Dec 5, 2023
3e7d9c2
Harmonize spacing.
StevenDufresne Dec 5, 2023
9662871
Make font size smaller for past events.
StevenDufresne Dec 5, 2023
1ee70b1
Update post title spacing.
StevenDufresne Dec 5, 2023
76c027c
Update search layout.
StevenDufresne Dec 5, 2023
55b279c
fix upcoming spacing.
StevenDufresne Dec 5, 2023
55b4e41
Update the filters to show the item count on archives.
StevenDufresne Dec 5, 2023
7b10fbb
Fix the template widths.
StevenDufresne Dec 5, 2023
d3db670
Update event list UI.
StevenDufresne Dec 5, 2023
c26787d
Update the menu text based on designs.
StevenDufresne Dec 5, 2023
855fa57
combine build/watch scripts for convenience
iandunn Dec 5, 2023
0889f6f
use get_events for filtering and caching
iandunn Dec 5, 2023
303e77f
add missing template part
iandunn Dec 5, 2023
0de639a
update upcoming events slug
iandunn Dec 5, 2023
b0c2278
add country filter values
iandunn Dec 5, 2023
9908d41
add all months
iandunn Dec 5, 2023
8796cbb
schedule crons to prime cache
iandunn Dec 6, 2023
47fdebd
apply coding standards
iandunn Dec 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions public_html/wp-content/themes/wporg-events-2023/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@

require_once __DIR__ . '/inc/city-landing-pages.php';

// Block files
require_once __DIR__ . '/src/event-list/index.php';

add_action( 'after_setup_theme', __NAMESPACE__ . '\theme_support' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
add_filter( 'wporg_query_filter_options_event_type', __NAMESPACE__ . '\get_event_type_options' );
add_filter( 'wporg_query_filter_options_format_type', __NAMESPACE__ . '\get_format_type_options' );
add_filter( 'wporg_query_filter_options_month', __NAMESPACE__ . '\get_month_options' );
add_action( 'wporg_query_filter_in_form', __NAMESPACE__ . '\inject_other_filters' );

add_filter( 'query_vars', __NAMESPACE__ . '\add_query_vars' );

/**
* Register theme supports.
Expand Down Expand Up @@ -46,3 +54,130 @@ function add_site_navigation_menus( $menus ) {
),
);
}

/**
* Sets up our Query filter for event_type.
*
* @return array
*/
function get_event_type_options( array $options ): array {
global $wp_query;
$selected = isset( $wp_query->query['event_type'] ) ? (array) $wp_query->query['event_type'] : array();
$count = count( $selected );
$label = sprintf(
/* translators: The dropdown label for filtering, %s is the selected term count. */
_n( 'Type <span>%s</span>', 'Type <span>%s</span>', $count, 'wporg' ),
$count
);

return array(
'label' => $label,
'title' => __( 'Type', 'wporg' ),
'key' => 'event_type',
'action' => home_url( '/upcoming-events/' ),
'options' => array(
'meetup' => 'Meetup',
'wordcamp' => 'WordCamp',
),
'selected' => $selected,
);
}

/**
* Sets up our Query filter for format_type.
*
* @return array
*/
function get_format_type_options( array $options ): array {
global $wp_query;
$selected = isset( $wp_query->query['format_type'] ) ? (array) $wp_query->query['format_type'] : array();
$count = count( $selected );
$label = sprintf(
/* translators: The dropdown label for filtering, %s is the selected term count. */
_n( 'Format <span>%s</span>', 'Format <span>%s</span>', $count, 'wporg' ),
$count
);

return array(
'label' => $label,
'title' => __( 'Format', 'wporg' ),
'key' => 'format_type',
'action' => home_url( '/upcoming-events/' ),
'options' => array(
'meetup' => 'Online',
'wordcamp' => 'In Person',
),
'selected' => $selected,
);
}

/**
* Sets up our Query filter for month.
*
* @return array
*/
function get_month_options( array $options ): array {
global $wp_query;
$selected = isset( $wp_query->query['month'] ) ? (array) $wp_query->query['month'] : array();
$count = count( $selected );
$label = sprintf(
/* translators: The dropdown label for filtering, %s is the selected term count. */
_n( 'Month <span>%s</span>', 'Month <span>%s</span>', $count, 'wporg' ),
$count
);

return array(
'label' => $label,
'title' => __( 'Month', 'wporg' ),
'key' => 'month',
'action' => home_url( '/upcoming-events/' ),
'options' => array(
'01' => 'January',
'02' => 'February',
),
'selected' => $selected,
);
}

/**
* Add in our custom query vars.
*/
function add_query_vars( $query_vars ) {
$query_vars[] = 'event_type';
$query_vars[] = 'format_type';
$query_vars[] = 'month';
return $query_vars;
}


/**
* Add in the other existing filters as hidden inputs in the filter form.
*
* Enables combining filters by building up the correct URL on submit,
* for example sites using a tag, a category, and matching a search term:
* ?tag[]=cuisine&cat[]=3&s=wordpress`
*
* @param string $key The key for the current filter.
*/
function inject_other_filters( $key ) {
global $wp_query;

$query_vars = array( 'event_type', 'format_type', 'month' );
foreach ( $query_vars as $query_var ) {
if ( ! isset( $wp_query->query[ $query_var ] ) ) {
continue;
}
if ( $key === $query_var ) {
continue;
}
$values = (array) $wp_query->query[ $query_var ];
foreach ( $values as $value ) {
printf( '<input type="hidden" name="%s[]" value="%s" />', esc_attr( $query_var ), esc_attr( $value ) );
}
}

// Pass through search query.
if ( isset( $wp_query->query['s'] ) ) {
printf( '<input type="hidden" name="s" value="%s" />', esc_attr( $wp_query->query['s'] ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"postcss-preset-env": "^9.3.0"
},
"scripts": {
"build:block": "wp-scripts build",
iandunn marked this conversation as resolved.
Show resolved Hide resolved
"start": "npm run watch",
"watch": "npm run build -- --watch",
"build": "postcss postcss/style.pcss postcss/editor.pcss --dir . --ext css",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Title: Events List Filters
* Slug: wporg-events-2023/event-list-filters
* Inserter: no
*/

?>

<!-- wp:group {"align":"wide","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between"}} -->
<div class="wp-block-group alignwide">
<!-- wp:group {"layout":{"type":"flex","flexWrap":"wrap"}} -->
<div class="wp-block-group">
<!-- wp:search {"showLabel":false,"placeholder":"<?php esc_html_e( 'Search events...', 'wporg' ); ?>","width":100,"widthUnit":"%","buttonText":"<?php esc_html_e( 'Search', 'wporg' ); ?>","buttonPosition":"button-inside","buttonUseIcon":true,"className":"is-style-secondary-search-control"} /-->
</div>
<!-- /wp:group -->

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"format_type","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"event_type","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"month","multiple":false} /-->
</div>
<!-- /wp:group -->
</div>
<!-- /wp:group -->
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<h2 class="wp-block-heading has-inter-font-family has-medium-font-size" style="font-style:normal;font-weight:700">Upcoming events</h2>
<!-- /wp:heading -->

<!-- wp:pattern {"slug":"wporg-events-2023/event-list-filters"} /-->

<!-- wp:wporg/google-map {"id":"all-upcoming-list","apiKey":"WORDCAMP_DEV_GOOGLE_MAPS_API_KEY","filterSlug":"all-upcoming","showMap":false,"showSearch":false,"listDisplayLimit":10} /-->

<!-- wp:buttons {"style":{"spacing":{"margin":{"top":"var:preset|spacing|40"}}}} -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wporg/event-list",
"version": "0.1.0",
"title": "WordPress Events List",
"category": "design",
"icon": "list-view",
"description": "List of WordPress Events",
"textdomain": "wporg",
"supports": {
"align": true,
"color": {
"background": true,
"text": true
},
"spacing": {
"margin": [ "top", "bottom" ],
"padding": true,
"blockGap": false
},
"typography": {
"fontSize": true,
"lineHeight": true
}
},
"editorScript": "file:./index.js",
"style": "file:./style-index.css"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/

import { Disabled } from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';

export default function Edit( { attributes, name } ) {
return (
<div { ...useBlockProps() }>
<InspectorControls>

</InspectorControls>
<Disabled>
<ServerSideRender block={ name } attributes={ attributes } />
</Disabled>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import Edit from './edit';
import metadata from './block.json';
import './style.scss';

registerBlockType( metadata.name, {
edit: Edit,
save: () => null,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Block Name: WordPress Event List
* Description: List of WordPress Events.
*
* @package wporg
*/

namespace WordPressdotorg\Theme\Events_2023\WordPress_Event_List;

use function WordPressdotorg\MU_Plugins\Google_Map\{get_all_upcoming_events};

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

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function init() {
register_block_type(
dirname( dirname( __DIR__ ) ) . '/build/event-list',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

/**
* Render the block content.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the block markup.
*/
function render( $attributes, $content, $block ) {

// Get all the events
$events = get_event_list();

// Get all the filters that are currently applied.
$filtered_events = array_slice( filter_events( $events ), 0, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cutting the list off at 10, but we only want that for the homepage. Maybe the block should accept a limit attribute that defaults to 50 or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good idea. I was thinking about the grouping as well. Should we pass in a groupyByMonth property? Feels too specific.

Alternatively, we can pass a "heading" property to this list block and create a wrapper block where we use the query results to loop and output multiple instances of the event list block. I'll look at that today.


// loop through events output markup using gutenberg blocks
$content = '<ul class="wporg-marker-list__container">';

foreach ( $filtered_events as $event ) {
$content .= '<li class="wporg-marker-list-item">';
$content .= '<h3 class="wporg-marker-list-item__title">' . $event->title . '</h3>';
$content .= '<div class="wporg-marker-list-item__location">' . $event->location . '</div>';
$content .= '<div class="wporg-marker-list-item__date-time">' . $event->timestamp . '</div>';
$content .= '</li>';
}

$content .= '</ul>';

$wrapper_attributes = get_block_wrapper_attributes();
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
do_blocks( $content )
);
}

/**
* Get a list of the currently-applied filters.
*
* @param boolean $include_search Whether the result should include the search term.
*
* @return array
*/
function filter_events( $events ) {
global $wp_query;

$taxes = array(
'map_type' => 'map_type',
);
$terms = array();

// Get the terms
foreach ( $taxes as $query_var => $taxonomy ) {

if ( ! isset( $wp_query->query[ $query_var ] ) ) {
continue;
}

$values = (array) $wp_query->query[ $query_var ];
foreach ( $values as $value ) {
$terms[] = $value;
}
}

if ( empty( $terms ) ) {
return $events;
}

$filtered_events = array();
foreach ( $events as $event ) {
// Assuming each event has a 'type' property
if ( isset($event->type) && in_array($event->type, $terms) ) {
$filtered_events[] = $event;
}
}

return $filtered_events;
}

/**
* Retrieves event list based on the filters.
*
* @return array
*/
function get_event_list(): array {
return get_all_upcoming_events();
iandunn marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- wp:template-part {"slug":"header"} /-->
iandunn marked this conversation as resolved.
Show resolved Hide resolved

<!-- wp:group {"tagName":"main","style":{"spacing":{"blockGap":"0px"}},"className":"entry-content","layout":{"type":"constrained"}} -->
<main class="wp-block-group entry-content">
<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"var:preset|spacing|40","left":"var:preset|spacing|edge-space","right":"var:preset|spacing|edge-space","bottom":"var:preset|spacing|40"}}},"layout":{"type":"default"}} -->
<div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--edge-space);padding-right:var(--wp--preset--spacing--edge-space);padding-bottom:var(--wp--preset--spacing--40)">
<!-- wp:post-title /-->
<!-- wp:pattern {"slug":"wporg-events-2023/event-list-filters"} /-->
<!-- wp:wporg/event-list /-->
</div>
<!-- /wp:group -->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer"} /-->
Loading
Loading