Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Created November 23, 2023 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/f02b261b7d84b6c882dab89eb90dcf7f to your computer and use it in GitHub Desktop.
Save andrasguseo/f02b261b7d84b6c882dab89eb90dcf7f to your computer and use it in GitHub Desktop.
ET > Template override to allow the sorting of RSVP blocks on the front-end based on selected properties
<?php
/**
* A template override to allow the sorting of RSVP blocks on the front-end based on selected properties.
*
* Block: RSVP
*
* This is a template override for the following file:
* event-tickets/src/views/v2/rsvp.php
*
* This file should be placed in the following folder:
* [your-theme]/tribe/tickets/v2/rsvp.php
*
* See more documentation about our Blocks Editor templating system.
*
* @link https://evnt.is/1amp Help article for RSVP & Ticket template files.
*
* @since 4.12.3
*
* @version 5.0.0
*
* @var Tribe__Tickets__Editor__Template $this
* @var WP_Post|int $post_id The post object or ID.
* @var boolean $has_rsvps True if there are RSVPs.
* @var array $active_rsvps An array containing the active RSVPs.
* @var string $block_html_id The unique HTML id for the block.
*
* Plugins required: Event Tickets
* Author: Andras Guseo
* Created: November 23, 2023
*/
// We don't display anything if there is no RSVP.
if ( ! $has_rsvps ) {
return false;
}
// Bail if there are no active RSVP.
if ( empty( $active_rsvps ) ) {
return;
}
/**
* START: RSVP Sorting
*
* Specify the properties you want to sort by and the orders of sorting.
* Recommended properties:
* - ID - The post ID of the RSVP/ticket
* - name - The name/title of the RSVP/ticket,
* - stock - The current available stock of the RSVP/ticket
* - capacity - The full capacity of the RSVP/ticket
* - start_date - The sale start date of the RSVP/ticket
* - start_time - The sale start time of the RSVP/ticket
* - end_date - The sale end date of the RSVP/ticket
* - end_time - The sale end time of the RSVP/ticket
* - sku - The SKU name of the RSVP/ticket
* Sorting orders: ASC or DESC
*
* If you only want to sort by one property, use $property2 = null
*/
$property1 = 'stock';
$order1 = 'ASC';
$property2 = null;
$order2 = 'ASC';
// Use usort to sort the array by the specified properties and orders
usort( $active_rsvps, function ( $a, $b ) use ( $property1, $order1, $property2, $order2 ) {
return compareByProperties( $a, $b, $property1, $order1, $property2, $order2 );
} );
/**
* A custom comparison function for sorting the RSVPs.
*
* @param mixed $a A property of the RSVP to compare.
* @param mixed $b A property of the RSVP to compare.
* @param string $property1 The primary property to arrange RSVPs by.
* @param string $order1 The sorting order for the primary property.
* @param string $property2 The secondary property to arrange RSVPs by.
* @param string $order2 The sorting order for the secondary property.
*
* @return int
*/
function compareByProperties( $a, $b, $property1, $order1, $property2 = null, $order2 = 'ASC' ) {
// Compare objects based on the specified properties and orders
$result1 = ( $order1 == 'ASC' ) ? ( $a->$property1 <=> $b->$property1 ) : ( $b->$property1 <=> $a->$property1 );
if ( $property2 != null && $result1 == 0 ) {
// If the first specified property is the same, compare by the second property and order
$result2 = ( $order2 == 'ASC' ) ? strcmp( $a->$property2, $b->$property2 ) : strcmp( $b->$property2, $a->$property2 );
return $result2;
}
return $result1;
}
// END: RSVP Sorting
?>
<div
id="<?php echo esc_attr( $block_html_id ); ?>"
class="tribe-common event-tickets"
>
<?php foreach ( $active_rsvps as $rsvp ) : ?>
<div
class="tribe-tickets__rsvp-wrapper"
data-rsvp-id="<?php echo esc_attr( $rsvp->ID ); ?>"
>
<?php $this->template( 'v2/components/loader/loader' ); ?>
<?php $this->template( 'v2/rsvp/content', [ 'rsvp' => $rsvp ] ); ?>
</div>
<?php endforeach; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment