Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filtering items available in media library - taken from v1 of the WooCommerce Product Vendors extension.
/**
* Only show current vendor's media in the media library
* @param array $request Default request arguments
* @return array Modified request arguments
*/
add_filter( 'request', 'pv_restrict_media_library', 10, 1 );
function pv_restrict_media_library( $request = array() ) {
if( ! is_admin() ) {
return $request;
}
$screen = get_current_screen();
if( in_array( $screen->id, array( 'upload', 'product' ) ) ) {
// Check if user has vendor access to the dashboard
if( vendor_access() ) {
$vendor_id = is_vendor();
if( ! $vendor_id ) {
return;
}
$vendor_admins = get_vendor_admins( $vendor_id );
if( ! $vendor_admins ) {
return;
}
$admins = array();
foreach( $vendor_admins as $admin ) {
if( ! $admin->ID ) {
continue;
}
$admins[] = $admin->ID;
}
if( 0 == count( $admins ) ) {
return;
}
$request['author__in'] = $admins;
}
}
return $request;
}
/**
* Only show current vendor's media in the media library modal on the product edit screen
* @param array $query Default query arguments
* @return array Modified query arguments
*/
add_filter( 'ajax_query_attachments_args', 'pv_restrict_media_library_modal', 10, 1 );
function pv_restrict_media_library_modal( $query = array() ) {
if( ! is_admin() ) {
return $query;
}
$screen = get_current_screen();
// Check if user has vendor access to the dashboard
if( vendor_access() ) {
$vendor_id = is_vendor();
if( ! $vendor_id ) {
return;
}
$vendor_admins = get_vendor_admins( $vendor_id );
if( ! $vendor_admins ) {
return;
}
$admins = array();
foreach( $vendor_admins as $admin ) {
if( ! $admin->ID ) {
continue;
}
$admins[] = $admin->ID;
}
if( 0 == count( $admins ) ) {
return;
}
$query['author__in'] = $admins;
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment