Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active May 4, 2023 18:17
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 Pebblo/16a7dabb6a6d8f0f3d4370ccda06098b to your computer and use it in GitHub Desktop.
Save Pebblo/16a7dabb6a6d8f0f3d4370ccda06098b to your computer and use it in GitHub Desktop.
Example of how to hide tickets that do not have a capability assigned to them from logged in users, so logged in users will only see tickets that have a 'minimum capability' set on them they the user has on their account. This snippet checks for an event meta value 'hide_no_cap_tickets' from within the event before removing any tickets from view.
<?php //Please do not include the opening PHP tag if you already have one.
function tw_ee_hide_no_cap_tickets_from_logged_in_users( $return_value, $ticket, $max, $min, $required_ticket_sold_out, $ticket_price, $ticket_bundle, $ticket_status, $status_class) {
$event = $ticket->get_related_event();
$hide_no_cap_tickets_when_logged_in = $event->get_extra_meta( 'hide_no_cap_tickets', true );
if( $hide_no_cap_tickets_when_logged_in ) {
$cap_required = $ticket->get_extra_meta( 'ee_ticket_cap_required', true );
if( empty( $cap_required ) && ( is_user_logged_in() && ! current_user_can( 'manage_options') ) ) {
return '';
}
}
return $return_value;
}
add_filter( 'FHEE__ticket_selector_chart_template__do_ticket_inside_row', 'tw_ee_hide_no_cap_tickets_from_logged_in_users', 20, 9);
<?php //Please do not include the opening PHP tag if you already have one.
//This function removes tickets with no capabilties set on the ticket, WITHOUT first checking for an event meta value.
function tw_ee_hide_no_cap_tickets_from_logged_in_users( $return_value, $ticket, $max, $min, $required_ticket_sold_out, $ticket_price, $ticket_bundle, $ticket_status, $status_class) {
//Don't remvoe tickets when adding registrations from the admin.
if ( is_admin() ) {
return $return_value;
}
//Pull the current minimum capability set on the ticket.
$cap_required = $ticket->get_extra_meta( 'ee_ticket_cap_required', true );
//Check for a capability set and if the user is logged in but not an administrator.
if( empty( $cap_required ) && ( is_user_logged_in() && ! current_user_can( 'manage_options') ) ) {
return '';
}
return $return_value;
}
add_filter( 'FHEE__ticket_selector_chart_template__do_ticket_inside_row', 'tw_ee_hide_no_cap_tickets_from_logged_in_users', 20, 9);
<?php //Please do not include the opening PHP tag if you already have one.
//This function removes tickets with no capabilties set on the ticket, WITHOUT first checking for an event meta value.
//It also displays the standard messages EE would normally show if the logged in user is a subscriber.
//This is a bit of a hack as you should pass role names to current_user_can() but for the time being it will work.
function tw_ee_hide_no_cap_tickets_from_logged_in_users( $return_value, $ticket, $max, $min, $required_ticket_sold_out, $ticket_price, $ticket_bundle, $ticket_status, $status_class) {
$cap_required = $ticket->get_extra_meta( 'ee_ticket_cap_required', true );
if( empty( $cap_required ) && ( is_user_logged_in() && ! current_user_can( 'manage_options') ) ) {
if( current_user_can( 'subscriber' ) ){
return $return_value;
} else {
return '';
}
}
return $return_value;
}
add_filter( 'FHEE__ticket_selector_chart_template__do_ticket_inside_row', 'tw_ee_hide_no_cap_tickets_from_logged_in_users', 20, 9);
<?php //Please do not include the opening PHP tag if you already have one.
function tw_ee_hide_tickets_from_logged_in_users_with_no_access( $return_value, $ticket, $max, $min, $required_ticket_sold_out, $ticket_price, $ticket_bundle, $ticket_status, $status_class) {
//Pull the capbilitiy set on the ticket.
$cap_required = $ticket->get_extra_meta( 'ee_ticket_cap_required', true );
// No cap required, return the ticket row as is.
if(empty($cap_required)) {
return $return_value;
}
// Check if the user is logged in and has the capability for the ticket, if not, return an empty row.
if(
is_user_logged_in()
&& ! EE_Registry::instance()->CAP->current_user_can(
$cap_required,
'wp_user_ticket_selector_check'
)
) {
return '';
}
return $return_value;
}
add_filter( 'FHEE__ticket_selector_chart_template__do_ticket_inside_row', 'tw_ee_hide_tickets_from_logged_in_users_with_no_access', 20, 9);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment