Created
October 12, 2018 12:52
-
-
Save hlashbrooke/52dbe85b124cd21f0461bdd295257c91 to your computer and use it in GitHub Desktop.
Filtering items available in media library - taken from v1 of the WooCommerce Product Vendors extension.
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
/** | |
* 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