Created
January 12, 2024 22:55
-
-
Save andrasguseo/20189276ee59c4ec795d470e1b46ac3a to your computer and use it in GitHub Desktop.
TEC > Add custom event statuses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyCustomEventStatuses { | |
/** | |
* Constructor. | |
*/ | |
public function __construct() { | |
add_filter( 'tec_event_statuses', [ $this, 'my_custom_statuses' ], 20, 2 ); | |
add_filter( 'tribe_events_single_event_title_html', [ $this, 'tec_custom_status_on_single_classic' ], 10, 2 ); | |
add_filter( "tribe_json_ld_event_object", [ $this, 'tec_custom_event_schema' ], 99, 3 ); | |
} | |
/** | |
* Add a status label above the event title. | |
* | |
* @param $title | |
* @param $event_id | |
* | |
* @return mixed|string | |
*/ | |
function tec_custom_status_on_single_classic( $title, $event_id ) { | |
$statuses = $this->custom_statuses(); | |
$event_status = get_post_meta( $event_id, '_tribe_events_status', true ); | |
if ( array_key_exists( $event_status, $statuses ) ) { | |
$status = ' | |
<div class="tribe-common-b2 tribe-events-status-single-notice"> | |
<div class="tribe-events-status-single tribe-events-status-single--' . $event_status . '"> | |
<div class="tribe-events-status-single__header"> | |
<div class="tribe-events-status-single__header tribe-events-status-single__header--bold tribe-events-status-single__header--alert-icon"> | |
' . $statuses[ $event_status ][ 'name' ] . ' | |
</div>'; | |
if ( get_post_meta( $event_id, '_tribe_events_status_reason', true ) ) { | |
$status .= ' | |
<div class="tribe-events-status-single__description">' . | |
wp_kses_post( get_post_meta( $event_id, '_tribe_events_status_reason', true ) ) . | |
'</div>'; | |
} | |
$status .= ' | |
</div> | |
</div> | |
</div> | |
'; | |
$title = $status . $title; | |
} | |
return $title; | |
} | |
/** | |
* Add the custom statuses to the dropdown on the edit event page. | |
* | |
* @param $statuses | |
* @param $current_status | |
* | |
* @return mixed | |
*/ | |
public function my_custom_statuses( $statuses, $current_status ) { | |
$custom_statuses = $this->custom_statuses(); | |
foreach ( $custom_statuses as $custom_status ) { | |
$statuses[] = [ | |
'text' => $custom_status[ 'name' ], | |
'id' => $custom_status[ 'value' ], | |
'value' => $custom_status[ 'value' ], | |
'selected' => $custom_status[ 'value' ] === $current_status ? true : false, | |
]; | |
} | |
return $statuses; | |
} | |
/** | |
* Add the custom event status to the schema, or remove it. | |
* | |
* @param $data | |
* @param $args | |
* @param $post | |
* | |
* @return mixed | |
*/ | |
function tec_custom_event_schema( $data, $args, $post ) { | |
$custom_statuses = $this->custom_statuses(); | |
$event_status = get_post_meta( $post->ID, '_tribe_events_status', true ); | |
if ( array_key_exists( $event_status, $custom_statuses ) ) { | |
if ( $custom_statuses[ $event_status ][ 'schema' ] === '' ) { | |
unset( $data->eventStatus ); | |
} else { | |
$data->eventStatus = $custom_statuses[ $event_status ][ 'schema' ]; | |
} | |
} | |
return $data; | |
} | |
/** | |
* Define the custom event statuses. | |
* | |
* @return array[] | |
*/ | |
public function custom_statuses() { | |
$statuses = [ | |
'rescheduled' => [ | |
'name' => 'Rescheduled', | |
'value' => 'rescheduled', | |
'schema' => 'EventRecheduled', | |
], | |
'suspended' => [ | |
'name' => 'Suspended', | |
'value' => 'suspended', | |
'schema' => '', | |
], | |
'my_other_status' => [ | |
'name' => 'My other status', | |
'value' => 'my_other_status', | |
'schema' => '', | |
], | |
]; | |
return $statuses; | |
} | |
} | |
new MyCustomEventStatuses(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment