Skip to content

Instantly share code, notes, and snippets.

@eteubert
Created February 27, 2012 15:07
Show Gist options
  • Select an option

  • Save eteubert/1924433 to your computer and use it in GitHub Desktop.

Select an option

Save eteubert/1924433 to your computer and use it in GitHub Desktop.
dtpr_get_all_user_capabilities
<?php
/**
* Return array containing all capabilities that user has access to.
* If he is an admin, the list will contain all available capability IDs.
*
* @param $user_id integer
* @return array
*/
function dtpr_get_all_user_capabilities( $user_id ) {
global $wpdb;
$capabilities = dtpr_get_user_capabilities( $user_id );
$all = array();
if ( dtpr_is_admin( $user_id ) ) {
$all_objects = Inpsyde_User_Level::all();
foreach ( $all_objects as $user_level ) {
$all[] = $user_level->id;
}
} else {
// find all sub-capabilities for the given capability
foreach ( $capabilities[ 'capabilities' ] as $organization_unit_id => $user_level_id ) {
$sql = '
SELECT
id
FROM
' . Inpsyde_User_Level::table_name() . '
WHERE
organization_unit_id = ' . $organization_unit_id . '
AND rank >= ( SELECT rank FROM ' . Inpsyde_User_Level::table_name() . ' WHERE id = ' . $user_level_id . ')
';
$ids = $wpdb->get_col( $sql );
$all = array_merge( $all, $ids );
}
}
return $all;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment