Skip to content

Commit

Permalink
Redo RSVP Empty feature as a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 25, 2024
1 parent 1cc969b commit 4df96fa
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 109 deletions.
12 changes: 1 addition & 11 deletions build/blocks/rsvp-response-v2/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
"icon": "groups",
"example": {},
"description": "Displays a list of members who have confirmed their attendance for an event.",
"attributes": {
"emptyRsvpMessage": {
"type": "string",
"default": "No one is attending this event yet."
}
},
"attributes": {},
"supports": {
"align": [
"wide",
Expand All @@ -22,11 +17,6 @@
"interactivity": true,
"html": false
},
"allowedBlocks": [
"core/group",
"core/comment-author-name",
"core/avatar"
],
"providesContext": {
"commentId": "commentId"
},
Expand Down
2 changes: 1 addition & 1 deletion build/blocks/rsvp-response-v2/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '2201207c7bbc3cfad47e');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'a1a7e7ae20945bbd7d12');
2 changes: 1 addition & 1 deletion build/blocks/rsvp-response-v2/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/blocks/rsvp-template/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('@wordpress/interactivity'), 'version' => '1691210014ab76aac638', 'type' => 'module');
<?php return array('dependencies' => array('@wordpress/interactivity'), 'version' => '7b668610c20f0dddfa3b', 'type' => 'module');
2 changes: 1 addition & 1 deletion build/blocks/rsvp-template/view.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/utility_style-rtl.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/utility_style.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => 'eaf74a388a7076b33c99');
<?php return array('dependencies' => array(), 'version' => '5b6cb9b271535a842021');
2 changes: 1 addition & 1 deletion build/utility_style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 31 additions & 18 deletions includes/core/classes/blocks/class-rsvp-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,43 @@ protected function setup_hooks(): void {
*/
public function transform_block_content( string $block_content, array $block ): string {
if ( self::BLOCK_NAME === $block['blockName'] ) {
$event = new Event( get_the_ID() );
$tag = new WP_HTML_Tag_Processor( $block_content );
$empty_rsvp_message = $block['attrs']['emptyRsvpMessage'] ?? __( 'No one is attending this event yet.', 'gatherpress' );
$event = new Event( get_the_ID() );
$tag = new WP_HTML_Tag_Processor( $block_content );

if ( ! $event->rsvp ) {
return $block_content;
}

if ( $tag->next_tag() ) {
$tag->set_attribute(
'data-wp-interactive',
'gatherpress/rsvp'
);
$responses = (int) $event->rsvp->responses()['attending']['count'];
$visibility_class = empty( $responses ) ? 'gatherpress--is-visible' : '';
$block_content = $tag->get_updated_html();

// @todo: Replace this workaround with a method to properly update inner blocks
// when https://github.com/WordPress/gutenberg/issues/60397 is resolved.
$block_content = preg_replace(
'/(<\/div>)\s*$/',
sprintf( '<p class="gatherpress--empty-rsvp-message %s">' . wp_kses_post( $empty_rsvp_message ) . '</p>$1', esc_attr( $visibility_class ) ),
$block_content
);
$tag->set_attribute( 'data-wp-interactive', 'gatherpress/rsvp' );

$responses = (int) $event->rsvp->responses()['attending']['count'];

while ( $tag->next_tag() ) {

Check failure on line 97 in includes/core/classes/blocks/class-rsvp-response.php

View workflow job for this annotation

GitHub Actions / PHPStan for WordPress

While loop condition is always true.
$class_attr = $tag->get_attribute( 'class' );

if ( $class_attr && false !== strpos( $class_attr, 'gatherpress--empty-rsvp' ) ) {
if ( ! empty( $responses ) ) {
$updated_class = str_replace(
'gatherpress--is-visible',
'',
$class_attr
);
$updated_class .= ' gatherpress--is-not-visible';
} else {
$updated_class = str_replace(
'gatherpress--is-not-visible',
'',
$class_attr
);
$updated_class .= ' gatherpress--is-visible';
}

$tag->set_attribute( 'class', trim( $updated_class ) );
}
}

$block_content = $tag->get_updated_html();

Check failure on line 121 in includes/core/classes/blocks/class-rsvp-response.php

View workflow job for this annotation

GitHub Actions / PHPStan for WordPress

Unreachable statement - code above always terminates.
}
}

Expand Down
13 changes: 1 addition & 12 deletions src/blocks/rsvp-response-v2/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,12 @@
"icon": "groups",
"example": {},
"description": "Displays a list of members who have confirmed their attendance for an event.",
"attributes": {
"emptyRsvpMessage": {
"type": "string",
"default": "No one is attending this event yet."

}
},
"attributes": {},
"supports": {
"align": [ "wide", "full" ],
"interactivity": true,
"html": false
},
"allowedBlocks": [
"core/group",
"core/comment-author-name",
"core/avatar"
],
"providesContext": {
"commentId": "commentId"
},
Expand Down
Loading

0 comments on commit 4df96fa

Please sign in to comment.