-
-
Save stephenharris/f441d7f5fd1fca9d49e4011848edbba5 to your computer and use it in GitHub Desktop.
Users can only see bookings for their events on the bookings admin screen unless they have permission to 'manage_others_eo_bookings'
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 | |
/** | |
* Plugin Name: Event Organiser Pro - Owner's Bookings Only | |
* Plugin URI: http://www.wp-event-organiser.com | |
* Version: 0.1.0 | |
* Description: Users can only see bookings for their events on the bookings admin screen unless they have permission to 'manage_others_eo_bookings' | |
* | |
*/ | |
add_action( 'pre_get_posts', function ( $query ) { | |
if( is_admin() && 'eo_booking' == $query->get( 'post_type' ) ){ | |
$query->set( 'suppress_filters', false ); | |
} | |
}, 10, 1 ); | |
add_filter( 'posts_clauses', function ( $clauses, $query ) { | |
global $wpdb; | |
$organiser = get_current_user_id(); | |
if( current_user_can( 'manage_others_eo_bookings' ) || !$organiser || 'eo_booking' != $query->get( 'post_type' ) ){ | |
return $clauses; | |
} | |
//Check which screen we're on | |
$screen = get_current_screen(); | |
if( !is_admin() || ( $screen && 'event_page_bookings' != $screen->id ) ){ | |
return $clauses; | |
} | |
$postalias = "{$wpdb->posts}eo"; | |
$postmetaalias = "{$wpdb->postmeta}eo"; | |
$clauses['join'] .= "INNER JOIN {$wpdb->postmeta} AS {$postmetaalias} | |
ON {$wpdb->posts}.ID = {$postmetaalias}.post_id | |
INNER JOIN {$wpdb->posts} AS {$postalias} | |
ON {$postalias}.ID = {$postmetaalias}.meta_value"; | |
$clauses['where'] .= $wpdb->prepare( | |
" AND {$postmetaalias}.meta_key = '_eo_booking_event_id' | |
AND {$postalias}.post_author = %d", | |
$organiser | |
); | |
return $clauses; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment