Created
April 4, 2017 23:21
-
-
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
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 | |
/** | |
* 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