Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created April 4, 2017 23:21
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 barryhughes/f17acb877500e4b777ac8c701baff16f to your computer and use it in GitHub Desktop.
Save barryhughes/f17acb877500e4b777ac8c701baff16f to your computer and use it in GitHub Desktop.
Temp workaround: require that a new or existing venue is submitted via the CE frontend submission form
<?php
/**
* Require that a new or existing venue is specified when events are submitted via
* the frontend submission form provided by Community Events.
*
* This is a temporary workaround for bug #76297.
*/
function ce_submissions_require_venue( array $submission ) {
// Unhook self
remove_filter( 'tribe_events_community_sanitize_submission', 'ce_submissions_require_venue' );
// Gather submitted venue data
$venue_ids = (array) Tribe__Utils__Array::get( $submission, array( 'Venue', 'VenueID' ), array() );
$new_venues = (array) Tribe__Utils__Array::get( $submission, array( 'Venue', 'Venue' ), array() );
// If there are signs of valid existing venues being specified, do nothing more
foreach ( $venue_ids as $possible_id ){
if ( absint( $possible_id ) ) {
return $submission;
}
}
// If there are signs of new venues being submitted, do nothing more
foreach ( $new_venues as $venue_name ) {
$venue_name = trim( $venue_name );
if ( ! empty( $venue_name ) ) {
return $submission;
}
}
// Force the venue check to run and fail
add_filter( 'tribe_events_community_required_fields', 'ce_submissions_add_venue_check' );
add_filter( 'tribe_events_community_required_venue_fields', 'ce_submissions_add_failure_field' );
add_filter( 'tribe_community_events_form_errors', 'ce_submissions_tidy_venue_error_message' );
return $submission;
}
function ce_submissions_add_venue_check( array $required_fields ) {
$required_fields[] = 'venue';
return $required_fields;
}
function ce_submissions_add_failure_field( array $required_fields ) {
$required_fields[] = '__unfindable';
return $required_fields;
}
function ce_submissions_tidy_venue_error_message( array $errors ) {
foreach ( $errors as $index => $error_message ) {
$message = $error_message['message'];
if ( false !== strpos( $message, '__unfindable' ) ) {
$errors[ $index ]['message'] = ce_submissions_strip_unfindable_error( $message );
}
}
return $errors;
}
function ce_submissions_strip_unfindable_error( $message ) {
$fragments = explode( '</p>', $message );
foreach ( $fragments as $index => $single_message ) {
if ( false !== strpos( $single_message, '__unfindable' ) ) {
unset( $fragments[ $index ] );
}
}
return join( '</p>', $fragments );
}
add_filter( 'tribe_events_community_sanitize_submission', 'ce_submissions_require_venue' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment